forked from switchgears/extremefeedbacklamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudxfd.py
executable file
·136 lines (104 loc) · 3.58 KB
/
cloudxfd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
import multiprocessing
from flask import Flask, render_template, request, redirect, url_for, flash
import time
import zmq
import socket
import os
CLOUD_XFD_DATA = "/home/pi/extremefeedbacklamp/cloud-xfd.data"
def get_url():
"""Get the url stored in the data file"""
if not os.path.isfile(CLOUD_XFD_DATA):
with open(CLOUD_XFD_DATA, "w") as f:
f.write("")
with open(CLOUD_XFD_DATA, "r") as f:
return f.read()
def put_url(address):
"""Update the url stored in the data file"""
with open(CLOUD_XFD_DATA, "w") as f:
f.write(address)
def http_worker(lock):
"""The Flask application"""
app = Flask("cloud_xfd")
# noinspection PyUnusedLocal
@app.route("/")
def index():
with lock:
url = get_url()
return render_template("index.html", url=url)
# noinspection PyUnusedLocal
@app.route("/update", methods=["POST"])
def update():
address = request.form["url"]
with lock:
put_url(address)
flash("The URL has been successfully updated")
return redirect(url_for('index'))
app.template_folder = "/home/pi/extremefeedbacklamp/templates"
app.secret_key = "notreallyasecret"
app.run('0.0.0.0')
def clean_url(address):
"""Remove unwanted data and provide a default value (127.0.0.1)"""
if address == "":
return '127.0.0.1'
address = address.replace("http://", "")
address = address.replace("https://", "")
address = address.split(":")[0]
return address
def zeromq_worker(lock):
"""The ZeroMQ poller"""
with lock:
address = clean_url(get_url())
context = zmq.Context()
zmq_socket = context.socket(zmq.REQ)
zmq_socket.connect('tcp://' + address + ':61616')
poller = zmq.Poller()
poller.register(zmq_socket, zmq.POLLIN)
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
if os.path.isfile("/sys/class/net/eth0/address"):
with open("/sys/class/net/eth0/address", "r") as f:
mac_address = f.read()
else:
mac_address = "test"
while True:
zmq_socket.send(mac_address, zmq.NOBLOCK)
while True:
with lock:
new_address = clean_url(get_url())
if new_address != address:
url = new_address
zmq_socket.close()
zmq_socket = context.socket(zmq.REQ)
zmq_socket.connect('tcp://' + url + ':61616')
poller = zmq.Poller()
poller.register(zmq_socket, zmq.POLLIN)
zmq_socket.send(mac_address, zmq.NOBLOCK)
messages = dict(poller.poll(1000))
if messages:
break
else:
time.sleep(1)
message = zmq_socket.recv()
if message:
for part in message.split("\a"):
udp_socket.sendto(part, ("0.0.0.0", 39418))
time.sleep(1)
def cloud_xfd():
"""The main function.
Please note that we are using processes instead of functions
because Flask prefers to be running in the main thread"""
lock = multiprocessing.Lock()
http_process = multiprocessing.Process(target=http_worker, args=(lock,))
http_process.daemon = True
http_process.start()
with lock:
print "http process started"
zmq_process = multiprocessing.Process(target=zeromq_worker, args=(lock,))
zmq_process.daemon = True
zmq_process.start()
with lock:
print "zmq thread started"
http_process.join()
zmq_process.join()
if __name__ == "__main__":
cloud_xfd()