-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (51 loc) · 1.47 KB
/
main.py
File metadata and controls
66 lines (51 loc) · 1.47 KB
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
from flask import Flask, render_template
from flask_socketio import SocketIO, send, emit
import numpy as np
import json
import time
import random
import threading
# Required for server-side emit() to work
import eventlet
eventlet.monkey_patch()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'dreamchaser'
socketio = SocketIO(app)
@app.route("/")
def index():
title = "Px-Dash"
return render_template("index.html", title=title)
def produce_chart_data():
t = 0;
while True:
# Sleep for random duration to prove async working
time.sleep(0.5)
if t > 100:
t = 0
else:
t+=1
# Get some data from source and emit to clients when recieved
px1_data = get_some_data(t)
px2_data = get_some_data(t+25)
socketio.emit('px-1A', px1_data)
socketio.emit('px-1B', px2_data)
print("Emit data")
def get_some_data(t):
T = 50
data = np.sin(2*np.pi*t/T)
return data
def turn_on_box():
while True:
time.sleep(2)
if True:
socketio.emit('box-status-data', 1)
else:
socketio.emit('box-status-data', 0)
if __name__ == '__main__':
t = threading.Thread(target=produce_chart_data)
t.start()
box_thread = threading.Thread(target=turn_on_box)
box_thread.start()
PORT = json.load(open('config.json'))["PORT"]
print("Running on localhost:"+str(PORT))
socketio.run(app, host='0.0.0.0', port=PORT,debug=True)