Skip to content

Commit a6598cd

Browse files
committed
Add /offline to list offline devices
1 parent 8b0fb84 commit a6598cd

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

server/server.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
/sync - Fetches the device list and local keys from the Tuya Cloud API
2222
/cloudconfig/{apiKey}/{apiSecret}/{apiRegion}/{apiDeviceID}
2323
- Sets the Tuya Cloud API login info
24+
/offline - List of registered devices that are offline
2425
2526
"""
2627

@@ -125,6 +126,18 @@ def appenddevice(newdevice, devices):
125126
devices[newdevice["id"]] = newdevice
126127
return False
127128

129+
def offlineDevices():
130+
# return undiscovered devices
131+
offline={}
132+
for d in tuyadevices:
133+
id = d["id"]
134+
if id not in deviceslist:
135+
offline[id] = {}
136+
offline[id]["name"] = d["name"]
137+
if "mac" in d:
138+
offline[id]["mac"] = d["mac"]
139+
return offline
140+
128141
def formatreturn(value):
129142
if value is None:
130143
result = {"status": "OK"}
@@ -344,7 +357,16 @@ def do_GET(self):
344357
if(id in deviceslist):
345358
message = json.dumps(deviceslist[id])
346359
else:
347-
message = json.dumps({"Error": "Device ID not found.", "id": id})
360+
jout={}
361+
[name, key, mac] = tuyaLookup(id)
362+
if name != "":
363+
jout["name"] = name
364+
jout["mac"] = mac
365+
jout["key"] = key
366+
jout["id"] = id
367+
message = json.dumps(jout)
368+
else:
369+
message = json.dumps({"Error": "Device ID not found.", "id": id})
348370
elif self.path.startswith('/turnoff/'):
349371
id = self.path.split('/turnoff/')[1]
350372
sw = 1
@@ -417,6 +439,8 @@ def do_GET(self):
417439
message = json.dumps(tuyaCloudRefresh())
418440
retrytimer = time.time() + RETRYTIME
419441
retrydevices['*'] = 1
442+
elif self.path == '/offline':
443+
message = json.dumps(offlineDevices())
420444
else:
421445
# Serve static assets from web root first, if found.
422446
fcontent, ftype = get_static(web_root, self.path)

server/web/index.html

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
<div class="title"></div>
1010
<p>Stats</p>
1111
<div class="number"></div>
12-
<p>Devices</p>
12+
<p class="onlinetext">Devices Online</p>
1313
<div class="devices"></div>
14-
14+
<p class="offlinetext">Registered Devices Offline</p>
15+
<div class="offline"></div>
1516
<script>
1617

1718
// Get Version
@@ -30,6 +31,10 @@
3031
$.getJSON(pwurl, function(data) {
3132
var text = `Number of Devices Found: ${data.found}` + "<br>" +
3233
`Total Devices Registered: ${data.registered}`;
34+
var online = `Devices Online: ${data.found}`;
35+
var offline = `Devices Offline: ${data.registered - data.found}`;
36+
$(".onlinetext").html(online)
37+
$(".offlinetext").html(offline)
3338
$(".number").html(text);
3439
});
3540
setTimeout(numdevices, 1000);
@@ -79,10 +84,45 @@
7984
setTimeout(devicelist, 1000);
8085
}
8186

87+
// Device Offline List
88+
function offlinelist() {
89+
var pwurl = window.location.protocol + "//" + window.location.hostname + ":8888/offline";
90+
$.getJSON(pwurl, function(data) {
91+
// store entire array in deviceDB
92+
let deviceDB = [];
93+
//
94+
var output = "";
95+
var id = "";
96+
var name = "";
97+
for (let x in data) {
98+
let device = {
99+
"name": data[x].name,
100+
"id": x,
101+
}
102+
deviceDB.push(device)
103+
}
104+
105+
// print sorted list
106+
let sortedDevices = deviceDB.sort((c1, c2) => (c1.name > c2.name) ? 1 : (c1.name < c2.name) ? -1 : 0);
107+
let rownum = 1;
108+
for (let x in sortedDevices) {
109+
output = output + rownum +
110+
" - <a href='device.html?id=" + sortedDevices[x].id + "'>" +
111+
(sortedDevices[x].name.length > 0 ? sortedDevices[x].name : '[' + sortedDevices[x].id + ']') + "</a> - " +
112+
sortedDevices[x].id + "<br>\n";
113+
rownum++;
114+
}
115+
116+
$(".offline").html(output);
117+
});
118+
setTimeout(offlinelist, 1000);
119+
}
120+
82121
// Display
83122
showversion();
84123
numdevices();
85124
devicelist();
125+
offlinelist();
86126

87127
</script>
88128

0 commit comments

Comments
 (0)