Skip to content

Commit 2bf893a

Browse files
committed
Rework Cloud log start/end times, and update documentation
1 parent 79c461c commit 2bf893a

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,15 @@ result = c.sendcommand(id,commands)
392392
print("Results\n:", result)
393393
```
394394
395-
Up to one week of device logs can also be pulled from the Cloud. By default getdevicelog() will pull 1 day of logs or 100 log entries, whichever comes first.
395+
Up to one week of device logs can also be pulled from the Cloud. By default getdevicelog() will pull 1 day of logs or 100 log entries, whichever comes first. The returned timestamps are unixtime*1000, and event_id 7 (data report) will probably be the most useful.
396396
397397
```python
398398
import tinytuya
399399
import json
400400
401401
c = tinytuya.Cloud()
402+
#r = c.getdevicelog( '00112233445566778899', start=-1, end=0, size=100 )
403+
#r = c.getdevicelog( '00112233445566778899', start=1669990000, end=1669990300, size=20 )
402404
r = c.getdevicelog( '00112233445566778899' )
403405
print( json.dumps(r, indent=2) )
404406
```

examples/cloud.py

+16
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,19 @@
6464
result = c.sendcommand(id,commands)
6565
print("Results\n:", result)
6666

67+
# Get device logs
68+
# Note: the returned timestamps are unixtime*1000
69+
# event_id 7 (data report) will probably be the most useful
70+
# More information can be found at https://developer.tuya.com/en/docs/cloud/cbea13f274?id=Kalmcohrembze
71+
72+
# Get device logs from the last day
73+
result = c.getdevicelog(id)
74+
print("Device logs:\n", result)
75+
76+
# Get device logs from 7 days ago through 5 days ago (2 days worth)
77+
#result = c.getdevicelog(id, start=-7, end=-5)
78+
#print("Device logs:\n", result)
79+
80+
# Get device logs for one day ending an hour ago
81+
#result = c.getdevicelog(id, end=time.time() - 3600)
82+
#print("Device logs:\n", result)

tinytuya/Cloud.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
sendcommand(deviceid, commands)
2222
getconnectstatus(deviceid)
2323
getdevicelog(deviceid, start=[now - 1 day], end=[now], evtype="1,2,3,4,5,6,7,8,9,10", size=100, params={})
24+
-> when start or end are negative, they are the number of days before "right now"
25+
i.e. "start=-1" is 1 day ago, "start=-7" is 7 days ago
2426
"""
2527

2628
from .core import *
@@ -78,6 +80,7 @@ def __init__(self, apiRegion=None, apiKey=None, apiSecret=None, apiDeviceID=None
7880
self.token = None
7981
self.error = None
8082
self.new_sign_algorithm = new_sign_algorithm
83+
self.server_time_offset = 0
8184

8285
if (not apiKey) or (not apiSecret):
8386
try:
@@ -245,6 +248,12 @@ def _gettoken(self):
245248
)
246249
return self.error
247250

251+
if 't' in response_dict:
252+
# round it to 2 minutes to try and factor out any processing delays
253+
self.server_time_offset = round( ((response_dict['t'] / 1000.0) - time.time()) / 120 )
254+
self.server_time_offset *= 120
255+
log.warning(self.server_time_offset)
256+
248257
self.token = response_dict['result']['access_token']
249258
return self.token
250259

@@ -476,10 +485,24 @@ def getdevicelog(self, deviceid=None, start=None, end=None, evtype=None, size=10
476485
ERR_PARAMS,
477486
"Missing DeviceID Parameter"
478487
)
488+
489+
# server expects times as unixtime * 1000
479490
if not end:
480-
end = int(time.mktime(time.gmtime()))
491+
end = int((time.time() + self.server_time_offset) * 1000)
492+
elif end < 0:
493+
end = int(((time.time() + self.server_time_offset) + (end * 86400) ) * 1000)
494+
else:
495+
end = Cloud.format_timestamp( end )
481496
if not start:
482-
start = end - 86400
497+
start = end - (86400*1000)
498+
elif start < 0:
499+
start = int(((time.time() + self.server_time_offset) + (start * 86400) ) * 1000)
500+
else:
501+
start = Cloud.format_timestamp( start )
502+
if start > end:
503+
tmp = start
504+
start = end
505+
end = tmp
483506
if not evtype:
484507
# get them all by default
485508
# 1 = device online, 7 = DP report
@@ -499,3 +522,15 @@ def getdevicelog(self, deviceid=None, start=None, end=None, evtype=None, size=10
499522
params['query_type'] = 1
500523

501524
return self.cloudrequest( '/v1.0/devices/%s/logs' % deviceid, query=params)
525+
526+
@staticmethod
527+
def format_timestamp( ts ):
528+
# converts a 10-digit unix timestamp to the 13-digit stamp the servers expect
529+
if type(ts) != int:
530+
if len(str(int(ts))) == 10:
531+
ts = int( ts * 1000 )
532+
else:
533+
ts = int( ts )
534+
elif len(str(ts)) == 10:
535+
ts *= 1000
536+
return ts

0 commit comments

Comments
 (0)