diff --git a/server/rtty.py b/server/rtty.py index ff80510..eaa7cf3 100755 --- a/server/rtty.py +++ b/server/rtty.py @@ -13,16 +13,20 @@ from aiohttp import web, WSMsgType class Devices: devs = {} + def add(self, ws, mac): self.devs[mac] = {'ws': ws, 'active': 3} + def active(self, mac): self.devs[mac]['active'] = 3 + def flush(self): for mac in list(self.devs): self.devs[mac]['active'] -= 1 if self.devs[mac]['active'] == 0: self.devs[mac]['ws'].close() del self.devs[mac] + def login(self, ws, mac): sid = md5((mac + str(random.uniform(1, 100))).encode('utf8')).hexdigest() dev = self.devs.get(mac) @@ -34,14 +38,17 @@ class Devices: dev['ws'].send_str(json.dumps({'type': 'login', 'mac': mac, 'sid': sid})) syslog.syslog('new logged to ' + mac) return sid + def logout(self, mac, sid): dev = self.devs.get(mac) del dev[sid] dev['ws'].send_str(json.dumps({'type': 'logout', 'mac': mac, 'sid': sid})) + def send_data2user(self, msg): mac = msg['mac'] sid = msg['sid'] self.devs[mac][sid].send_str(json.dumps(msg)) + def send_data2device(self, msg): mac = msg['mac'] sid = msg['sid'] @@ -124,4 +131,4 @@ def keepalive(loop): loop = asyncio.get_event_loop() loop.call_later(5, keepalive, loop) -web.run_app(app, port = port) \ No newline at end of file +web.run_app(app, port = port)