Fix ID conflicts

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
This commit is contained in:
Jianhui Zhao
2018-01-15 15:42:39 +08:00
parent 8e470cc1ea
commit 17df030b62
2 changed files with 17 additions and 1 deletions
+7 -1
View File
@@ -15,7 +15,10 @@ class Devices:
devs = {}
def add(self, ws, did):
if self.devs.get(did):
return False
self.devs[did] = {'ws': ws, 'active': 3}
return True
def active(self, did):
self.devs[did]['active'] = 3
@@ -62,7 +65,10 @@ async def websocket_handler_device(request):
await ws.prepare(request)
did = request.query['did']
devices.add(ws, did)
if not devices.add(ws, did):
# Fix me
ws.send_str(json.dumps({'type': 'add', 'err': 'ID conflicts'}))
return ws
syslog.syslog('New device:' + did)
+10
View File
@@ -40,6 +40,7 @@ enum {
RTTYD_TYPE,
RTTYD_DID,
RTTYD_SID,
RTTYD_ERR,
RTTYD_DATA
};
@@ -56,6 +57,10 @@ static const struct blobmsg_policy pol[] = {
.name = "sid",
.type = BLOBMSG_TYPE_STRING,
},
[RTTYD_ERR] = {
.name = "err",
.type = BLOBMSG_TYPE_STRING,
},
[RTTYD_DATA] = {
.name = "data",
.type = BLOBMSG_TYPE_STRING,
@@ -240,6 +245,11 @@ static void uwsc_onmessage(struct uwsc_client *cl, char *msg, uint64_t len, enum
}
} else if (!strcmp(type, "pong")) {
active = 3;
} else if (!strcmp(type, "add")) {
if (tb[RTTYD_ERR]) {
uwsc_log_err("add failed: %s", blobmsg_get_string(tb[RTTYD_ERR]));
uloop_end();
}
}
}