Skip to content

Commit

Permalink
optimize code format
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
Jianhui Zhao committed Jan 12, 2018
1 parent 0e88eee commit f94ca85
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion server/rtty.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@

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)
Expand All @@ -34,14 +38,17 @@ def login(self, ws, mac):
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']
Expand Down Expand Up @@ -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)

0 comments on commit f94ca85

Please sign in to comment.