mirror of
https://github.com/zhaojh329/rtty.git
synced 2026-02-27 09:53:17 +08:00
optimize code format
Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
+8
-1
@@ -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)
|
||||
web.run_app(app, port = port)
|
||||
|
||||
Reference in New Issue
Block a user