-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
67 lines (54 loc) · 1.7 KB
/
application.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
import time
import os
import sys
from flask import Flask, render_template, url_for
from flask_socketio import SocketIO, send, disconnect, emit
import time
import multiprocessing
import serial
import serial.threaded
import serialworker
SERIAL_PORT = '/dev/ttyUSB0'
SERIAL_BAUDRATE = 9600
app = Flask(__name__)
socketio = SocketIO(app, async_mode='threading')
clients = []
ser = None
input_queue = multiprocessing.Queue()
output_queue = multiprocessing.Queue()
@app.route('/')
def root():
return render_template('index.html')
@socketio.on('message', namespace='/webapp')
def handle_message(message):
print('received message: ' + message)
#input_queue.put(message)
ser.write(message.encode('utf-8'))
@socketio.on('connect', namespace='/webapp')
def test_connect():
emit("message", "hello client")
print('new connection')
@socketio.on('disconnect', namespace='/webapp')
def test_disconnect():
print('Client disconnected', request.sid)
@socketio.on_error(namespace='/webapp')
def chat_error_handler(e):
print('An error has occurred: ' + str(e))
if __name__ == '__main__':
print("running")
#sp = serialworker.SerialProcess(input_queue, output_queue)
#sp.daemon = True
#sp.start()
# connect to serial port
ser = serial.serial_for_url(SERIAL_PORT, do_not_open=True)
ser.baudrate = SERIAL_BAUDRATE
try:
ser.open()
except serial.SerialException as e:
sys.stderr.write('Could not open serial port {}: {}\n'.format(ser.name, e))
sys.exit(1)
ser_to_net = serialworker.SerialToNet()
ser_to_net.socket = socketio
serial_worker = serial.threaded.ReaderThread(ser, ser_to_net)
serial_worker.start()
socketio.run(app, debug=True)