-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggle-leds.py
More file actions
123 lines (109 loc) · 3.76 KB
/
Copy pathtoggle-leds.py
File metadata and controls
123 lines (109 loc) · 3.76 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
import datetime
import requests
import dateutil.parser
from pytz import utc
class StationLeds:
def __init__(self):
self.base_endpoint = 'http://localhost:3000'
self.gps_endpoint = '{}/gps'.format(self.base_endpoint)
self.led_endpoint = '{}/led'.format(self.base_endpoint)
self.leds = ['a', 'b', 'gps']
def get(self, url):
try:
res = requests.get(url)
return res.json()
except Exception as err:
print('error getting url', url)
return
def post(self, url, payload):
try:
kwargs = {}
if payload is not None:
kwargs['json'] = payload
print('posting payload', payload, 'to', url)
res = requests.post(url, **kwargs)
return res.json()
except Exception as err:
print('error posting payload to endpoint', url, payload)
return
def solidLed(self, which_led, state):
if which_led not in self.leds:
print('invalid led', which_led)
return
endpoint = self.led_endpoint
if which_led != 'gps':
endpoint = '{}/{}'.format(self.led_endpoint, 'diag')
endpoint = '{}/{}'.format(endpoint, which_led)
payload = {
'state': state
}
self.post(endpoint, payload=payload)
def blinkLed(self, which_led, rate):
if which_led not in self.leds:
print('invalid led', which_led)
return
endpoint = self.led_endpoint
if which_led != 'gps':
endpoint = '{}/{}'.format(self.led_endpoint, 'diag')
endpoint = '{}/{}'.format(endpoint, which_led)
payload = {
'state': 'blink',
'blink_ms': rate
}
self.post(endpoint, payload=payload)
def toggleGpsLed(self):
try:
res = requests.get(self.gps_endpoint)
except Exception as err:
print("error toggling GPS led")
print(err)
return
if res.status_code == 200:
response = res.json()
info = response['gps']
mode = info.get('mode')
print('toggling GPS LED', mode)
if mode == 3:
self.solidLed(which_led='gps', state='on')
elif mode == 2:
self.blinkLed(which_led='gps', rate=500)
elif mode == 1:
self.blinkLed(which_led='gps', rate=200)
else:
self.solidLed(which_led='gps', state='off')
def checkInternetStatus(self, ping_count):
url = '{}/internet/status?ping_count={}'.format(
self.base_endpoint,
ping_count
)
try:
res = requests.get(url)
except Exception as err:
logging.error(err)
return False
if (res.status_code == 200):
response = res.json()
if response['success'] == ping_count:
return True
return False
def toggleModemLight(self):
if self.checkInternetStatus(ping_count=3) is True:
self.solidLed(which_led='b', state='on')
else:
self.solidLed(which_led='b', state='off')
def checkRadioServer(self):
now = datetime.datetime.utcnow().replace(tzinfo=utc)
url = '{}/radio/stats'.format(self.base_endpoint)
res = self.get(url)
stat_time = dateutil.parser.parse(res['now'])
delta = (now-stat_time).seconds
print(delta)
if delta > 30:
self.blinkLed(which_led='a', rate=100)
else:
self.solidLed(which_led='a', state='on')
if __name__ == '__main__':
leds = StationLeds()
#leds.toggleGpsLed()
#leds.toggleModemLight()
leds.checkRadioServer()