mirror of
https://github.com/no2chem/wideq.git
synced 2025-05-21 01:20:11 -07:00
Try out monitoring
This commit is contained in:
parent
3f6b4f1bf8
commit
d0a4e3e823
10
example.py
10
example.py
@ -1,5 +1,6 @@
|
||||
import wideq
|
||||
import json
|
||||
import time
|
||||
|
||||
STATE_FILE = 'wideq_state.json'
|
||||
|
||||
@ -101,6 +102,15 @@ def example():
|
||||
|
||||
print_devices(devices)
|
||||
|
||||
# For fun, try monitoring the first device
|
||||
first_device_id = devices[0]['deviceId']
|
||||
with wideq.Monitor(session, first_device_id) as mon:
|
||||
for i in range(4):
|
||||
time.sleep(1)
|
||||
print('Polling...')
|
||||
res = mon.poll()
|
||||
print(res)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
example()
|
||||
|
34
wideq.py
34
wideq.py
@ -201,14 +201,42 @@ class Session(object):
|
||||
})
|
||||
return work_id
|
||||
|
||||
def monitor_get(self, work_ids):
|
||||
def monitor_poll(self, work_ids):
|
||||
"""Get the results of monitoring tasks.
|
||||
|
||||
`work_ids` is a mapping from device IDs to work IDs. Return a
|
||||
mapping from device IDs to status results.
|
||||
mapping from work IDs to status results.
|
||||
"""
|
||||
|
||||
work_list = [{'deviceId': k, 'workId': v}
|
||||
for k, v in work_ids.items()]
|
||||
out = self.post('rti/rtiResult', {'workList': work_list})['workList']
|
||||
return {d['deviceId']: d for d in out}
|
||||
return {d['workId']: d for d in out}
|
||||
|
||||
def monitor_stop(self, work_id):
|
||||
"""Stop monitoring a device."""
|
||||
|
||||
self.post('rti/rtiMon', {
|
||||
'cmd': 'Mon',
|
||||
'cmdOpt': 'Stop',
|
||||
'workId': work_id,
|
||||
})
|
||||
|
||||
|
||||
class Monitor(object):
|
||||
"""A monitoring task for a single device."""
|
||||
|
||||
def __init__(self, session, device_id):
|
||||
self.session = session
|
||||
self.device_id = device_id
|
||||
|
||||
def __enter__(self):
|
||||
self.work_id = self.session.monitor_start(self.device_id)
|
||||
return self
|
||||
|
||||
def poll(self):
|
||||
data = self.session.monitor_poll({self.device_id: self.work_id})
|
||||
return data[self.work_id]
|
||||
|
||||
def __exit__(self, type, value, tb):
|
||||
self.session.monitor_stop(self.work_id)
|
||||
|
Loading…
x
Reference in New Issue
Block a user