-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.py
89 lines (80 loc) · 2.84 KB
/
server.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
from flask_socketio import SocketIO, emit
from flask import Flask
from flask_cors import CORS
from random import random
from threading import Thread, Event
from time import sleep
from gevent.pywsgi import WSGIServer
from geventwebsocket.handler import WebSocketHandler
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
CORS(app)
# Server functionality for receiving and storing data from elsewhere, not related to the websocket
#Data Generator Thread
thread = Thread()
thread_stop_event = Event()
class DataThread(Thread):
def __init__(self):
self.delay = 0.5
super(DataThread, self).__init__()
def dataGenerator(self):
print("Initialising")
try:
while not thread_stop_event.isSet():
socketio.emit('responseMessage', {'temperature': round(random()*10, 3)})
sleep(self.delay)
except KeyboardInterrupt:
# kill()
print("Keyboard Interrupt")
def run(self):
self.dataGenerator()
# Handle the webapp connecting to the websocket
@socketio.on('connect')
def test_connect():
print('someone connected to websocket')
emit('responseMessage', {'data': 'Connected! ayy'})
# need visibility of the global thread object
global thread
if not thread.isAlive():
print("Starting Thread")
thread = DataThread()
thread.start()
# Handle the webapp connecting to the websocket, including namespace for testing
@socketio.on('connect', namespace='/devices')
def test_connect2():
print('someone connected to websocket!')
emit('responseMessage', {'data': 'Connected devices! ayy'})
# Handle the webapp sending a message to the websocket
@socketio.on('message')
def handle_message(message):
# print('someone sent to the websocket', message)
print('Data', message["data"])
print('Status', message["status"])
global thread
global thread_stop_event
if (message["status"]=="Off"):
if thread.isAlive():
thread_stop_event.set()
else:
print("Thread not alive")
elif (message["status"]=="On"):
if not thread.isAlive():
thread_stop_event.clear()
print("Starting Thread")
thread = DataThread()
thread.start()
else:
print("Unknown command")
# Handle the webapp sending a message to the websocket, including namespace for testing
@socketio.on('message', namespace='/devices')
def handle_message2():
print('someone sent to the websocket!')
@socketio.on_error_default # handles all namespaces without an explicit error handler
def default_error_handler(e):
print('An error occured:')
print(e)
if __name__ == '__main__':
# socketio.run(app, debug=False, host='0.0.0.0')
http_server = WSGIServer(('',5000), app, handler_class=WebSocketHandler)
http_server.serve_forever()