-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssync.py
More file actions
130 lines (107 loc) · 3.47 KB
/
ssync.py
File metadata and controls
130 lines (107 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
import requests
"""
# import and init
import SensorSync from ssync
s = SensorSync('192.168.2.49')
# or
# s = SensorSync()
# s.ip = '192.168.2.49'
# set and reset sensors
# valid sensor numbers: 1..8
OK, _ = s.activate(1)
OK, _ = s.reset(1)
# optionally, check the returned html when activating/resetting:
OK, _ = s.activate(1, verify=True)
OK, _ = s.reset(1, verify=True)
# request status
OK, status_html = s.status()
# status_html looks like this:
<html>
Version 2.3
IP=192.168.2.49
MAC=DE.AD.BE.EF.FE.ED
SENSORS=0
</html>
OK, _ = s.write_ip('192.168.2.49')
OK, _ = s.write_mac('CA.FE.EE.CA.FE.ED')
OK, _ = s.reboot()
"""
class SensorSync:
APIS = {
'activate': 'http://{ip}?activate={sensor}',
'reset': 'http://{ip}?reset={sensor}',
'reboot': 'http://{ip}?reboot=true',
'status': 'http://{ip}?status=true',
'write_ip': 'http://{ip}?ip={new_ip}',
'write_mac': 'http://{ip}?mac={new_mac}',
}
def __init__(self, ip=None):
self.ip = ip
def _url_for(self, api, **kwargs):
url = SensorSync.APIS[api]
d = {'ip': self.ip}
d.update(kwargs)
return url.format(**d)
def _request_url(self, url):
assert self.ip is not None
r = requests.get(url)
return r.status_code, r.text
def activate(self, sensor, verify = False):
url = self._url_for('activate', sensor=sensor)
s, c = self._request_url(url)
OK = s == 200
if verify:
expected = '<html>\r\nOK sensor {sensor} activated\r\n</html>\r\n'.format(sensor=sensor)
OK = OK and c == expected
return OK, c
def reset(self, sensor, verify = False):
url = self._url_for('reset', sensor=sensor)
s, c = self._request_url(url)
OK = s == 200
if verify:
expected = '<html>\r\nOK sensor {sensor} reset\r\n</html>\r\n'.format(sensor=sensor)
OK = OK and c == expected
return OK, c
def reboot(self, verify):
url = self._url_for('reboot')
s, c = self._request_url(url)
OK = s == 200
if verify:
expected = '<html>\r\nOK\r\n</html>\r\n'
OK = OK and c == expected
return OK, c
def status(self):
url = self._url_for('status')
return self._request_url(url)
def write_ip(self, new_ip, verify = False):
url = self._url_for('write_ip', new_ip=new_ip)
s, c = self._request_url(url)
OK = s == 200
if verify:
expected = '<html>\r\nOK\r\n</html>\r\n'
OK = OK and c == expected
return OK, c
def write_mac(self, new_mac, verify = False):
url = self._url_for('write_mac', new_mac=new_mac)
s, c = self._request_url(url)
OK = s == 200
if verify:
expected = '<html>\r\nOK\r\n</html>\r\n'
OK = OK and c == expected
return OK, c
def t(s, t):
if not s:
print('*************', end='')
print(repr(t))
print(f'status: {s}\n{t}')
print('---')
if __name__ == '__main__':
# test against device in default config mode
s = SensorSync('192.168.2.49')
t(*s.activate(1, verify=True))
t(*s.reset(1, verify=True))
t(*s.status())
t(*s.write_ip('192.168.2.49', verify=True))
t(*s.write_mac('CA.FE.EE.CA.FE.ED', verify=True))
t(*s.reboot(verify=True))