|
1
|
|
|
from quart import Quart |
|
2
|
|
|
from quart import render_template, make_response, websocket |
|
3
|
|
|
|
|
4
|
|
|
import asyncio |
|
5
|
|
|
from functools import wraps |
|
6
|
|
|
import sys |
|
7
|
|
|
|
|
8
|
|
|
from bricknil.sensor import * |
|
9
|
|
|
from bricknil.sensor.motor import Motor |
|
10
|
|
|
|
|
11
|
|
|
from random import randint |
|
12
|
|
|
app = Quart(__name__) |
|
13
|
|
|
|
|
14
|
|
|
@app.route('/') |
|
15
|
|
|
async def index(): |
|
16
|
|
|
return await render_template('base.html', message='hello world') |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
connected = set() |
|
20
|
|
|
hubs = {} # Per websocket set |
|
21
|
|
|
replay_messages = {} # Dict is mainly for the value |
|
22
|
|
|
|
|
23
|
|
|
def collect_websocket(func): |
|
24
|
|
|
@wraps(func) |
|
25
|
|
|
async def wrapper(*args, **kwargs): |
|
26
|
|
|
global connected |
|
27
|
|
|
connected.add(websocket._get_current_object()) |
|
28
|
|
|
try: |
|
29
|
|
|
return await func(*args, **kwargs) |
|
30
|
|
|
finally: |
|
31
|
|
|
connected.remove(websocket._get_current_object()) |
|
32
|
|
|
return wrapper |
|
33
|
|
|
|
|
34
|
|
|
@app.websocket('/ws') |
|
35
|
|
|
@collect_websocket |
|
36
|
|
|
async def ws(): |
|
37
|
|
|
# Replay all the relevant messages seen before this time: |
|
38
|
|
|
# - ADD_HUB |
|
39
|
|
|
# - ADD_PERIPHERAL |
|
40
|
|
|
# - UPDATE_VALUE (only the last message is stored) |
|
41
|
|
|
for msg, val in replay_messages.items(): |
|
42
|
|
|
print(f'Replaying: {msg} {val}') |
|
43
|
|
|
await websocket.send(f'{msg} {val}') |
|
44
|
|
|
|
|
45
|
|
|
while True: |
|
46
|
|
|
data = await websocket.receive() |
|
47
|
|
|
rn = randint(0,100) |
|
48
|
|
|
msg = f'Got your message: {data}. Here is your treat: {rn}!' |
|
49
|
|
|
await websocket.send(f'echo {msg}') |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
@app.before_serving |
|
53
|
|
|
def startup(): |
|
54
|
|
|
# Start up an socket listener for bricknil messages |
|
55
|
|
|
asyncio.ensure_future(socket_listener()) |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
async def _broadcast(message): |
|
59
|
|
|
if message.startswith("UPDATE_VALUE"): |
|
60
|
|
|
msg_parts = message.split(' ') |
|
61
|
|
|
key = ' '.join(msg_parts[:-1]) |
|
62
|
|
|
val = msg_parts[-1] |
|
63
|
|
|
replay_messages[key] = val |
|
64
|
|
|
else: |
|
65
|
|
|
replay_messages[message] = "" |
|
66
|
|
|
|
|
67
|
|
|
for websock in connected: |
|
68
|
|
|
await websock.send(message) |
|
69
|
|
|
|
|
70
|
|
|
async def broadcast(message): |
|
71
|
|
|
global hubs |
|
72
|
|
|
print(message) |
|
73
|
|
|
message = message.decode('utf-8') |
|
74
|
|
|
print(message.split('|')) |
|
75
|
|
|
hub_name, peripheral_type, peripheral_name, port, msg = message.split('|') |
|
76
|
|
|
if 'Motor' in peripheral_type: |
|
77
|
|
|
peripheral_type = 'Motor' |
|
78
|
|
|
if msg.startswith('flush'): |
|
79
|
|
|
msg = 'RESET {hub_name}' |
|
80
|
|
|
await _broadcast(msg) |
|
81
|
|
|
elif msg.startswith('set') or msg.startswith('value change'): |
|
82
|
|
|
mode_val = msg.split('mode:')[1].strip() |
|
83
|
|
|
print(mode_val) |
|
84
|
|
|
mode, val = [int(m.strip()) for m in mode_val.split('=')] |
|
85
|
|
|
local_peripheral_name = f'{peripheral_name}_{mode}' # append the mode so we have different peripherals for each mode |
|
86
|
|
|
|
|
87
|
|
|
# Get the class name to get the attributes |
|
88
|
|
|
thismodule = sys.modules[__name__] |
|
89
|
|
|
class_ = getattr(thismodule, peripheral_type) |
|
90
|
|
|
# map the capabilities to mode if present |
|
91
|
|
|
if 'capability' in dir(class_): |
|
92
|
|
|
# This is a sensor! |
|
93
|
|
|
mode_str = class_.capability(mode).name |
|
94
|
|
|
if mode_str in ['sense_distance', 'sense_reflectivity']: |
|
95
|
|
|
peripheral_type = 'Distance' |
|
96
|
|
|
else: |
|
97
|
|
|
mode_str = str(mode) # Find some way to set this for a pure output device |
|
98
|
|
|
|
|
99
|
|
|
if not hub_name in hubs: |
|
100
|
|
|
hub = hubs.setdefault(hub_name, {}) |
|
101
|
|
|
msg = f'ADD_HUB {hub_name}' |
|
102
|
|
|
await _broadcast(msg) |
|
103
|
|
|
hub = hubs[hub_name] |
|
104
|
|
|
if local_peripheral_name not in hub: |
|
105
|
|
|
msg = f'ADD_PERIPHERAL {hub_name} {peripheral_type} {peripheral_name} {mode_str}' |
|
106
|
|
|
await _broadcast(msg) |
|
107
|
|
|
hub[local_peripheral_name] = val |
|
108
|
|
|
msg = f'UPDATE_VALUE {hub_name} {peripheral_type} {peripheral_name} {mode_str} {val}' |
|
109
|
|
|
await _broadcast(msg) |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
async def socket_listener(): |
|
113
|
|
|
|
|
114
|
|
|
#await asyncio.sleep(5) |
|
115
|
|
|
#await broadcast(b'test|LED|led|19|set output mode: 0 = 5') |
|
116
|
|
|
#await broadcast(b'test|DuploSpeaker|speakr|19|set output mode: 0 = 1') |
|
117
|
|
|
#await broadcast(b'test|DuploTrainMotor|motor|19|set output mode: 0 = 50') |
|
118
|
|
|
#await broadcast(b'test|LED|led|19|set output mode: 0 = 6') |
|
119
|
|
|
#await broadcast(b'test|VoltageSensor|voltage|19|value change mode: 0 = 3000') |
|
120
|
|
|
#await broadcast(b'test|DuploVisionSensor|vision|19|value change mode: 2 = 30') |
|
121
|
|
|
#await broadcast(b'test|DuploSpeedSensor|speed_sensor|19|value change mode: 1 = 30') |
|
122
|
|
|
#await asyncio.sleep(1) |
|
123
|
|
|
#await broadcast(b'test|DuploTrainMotor|motor|19|set output mode: 0 = 80') |
|
124
|
|
|
#await asyncio.sleep(5) |
|
125
|
|
|
#await broadcast(b'test|*|*|*|flush') |
|
126
|
|
|
#return |
|
127
|
|
|
loop = asyncio.get_running_loop() |
|
128
|
|
|
reader, writer = await asyncio.open_connection('127.0.0.1', 25000, loop=loop) |
|
129
|
|
|
print('Connected') |
|
130
|
|
|
while True: |
|
131
|
|
|
#rn = randint(0,100) |
|
132
|
|
|
#data = f'{rn}' |
|
133
|
|
|
#await asyncio.sleep(1) |
|
134
|
|
|
#data = await reader.read(1000) |
|
135
|
|
|
data = await reader.readline() |
|
136
|
|
|
if not data: |
|
137
|
|
|
break |
|
138
|
|
|
await broadcast(data) |
|
139
|
|
|
print(f'received: {data}') |
|
140
|
|
|
print('Connection closed') |
|
141
|
|
|
|
|
142
|
|
|
#server = await loop.create_server(BricknilProtocol, '127.0.0.1', 25000) |
|
143
|
|
|
#print('starting to serve') |
|
144
|
|
|
#await server.serve_forever() |
|
145
|
|
|
#print('served!') |
|
146
|
|
|
#return 'Success' |
|
147
|
|
|
|
|
148
|
|
|
#while True: |
|
149
|
|
|
#print('hello') |
|
150
|
|
|
#await asyncio.sleep(1) |
|
151
|
|
|
|