-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.py
More file actions
executable file
·201 lines (155 loc) · 5.58 KB
/
Copy pathcode.py
File metadata and controls
executable file
·201 lines (155 loc) · 5.58 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# SPDX-FileCopyrightText: 2020 Brent Rubell for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import os
import ipaddress
import ssl
import wifi
import socketpool
import adafruit_requests
import time
import alarm
import displayio
import terminalio
import adafruit_imageload
from adafruit_magtag.magtag import MagTag
from adafruit_display_text import label
# URLs to fetch from
shortnames = ["wmata", "dcfd"]
status = []
JSON_STATUS_URL = "https://api.openmhz.com/status/"
magtag = MagTag()
# Make the display context
main_group = displayio.Group()
# create the label
updating_label = label.Label(
font=terminalio.FONT, text="Time Is:\n{}".format(time.monotonic()), scale=1, color=0x000000
)
# set label position on the display
updating_label.anchor_point = (0, 0)
updating_label.anchored_position = (20, 20)
# add label to group that is showing on display
main_group.append(updating_label)
def alert(message):
alert_group = displayio.Group()
alert_label = label.Label(
font=terminalio.FONT, text=message, scale=2, color=0x000000
)
alert_label.anchor_point = (0, 0)
alert_label.anchored_position = (10, 30)
alert_group.append(alert_label)
magtag.splash.append(alert_group)
magtag.display.refresh()
while True:
magtag.peripherals.neopixel_disable = False
magtag.peripherals.neopixels.fill((255, 0, 0))
magtag.peripherals.play_tone(1047, 0.25)
time.sleep(0.1)
magtag.peripherals.neopixel_disable = True
time.sleep(1)
def update_status():
global status
status = []
for shortname in shortnames:
try:
response = requests.get(JSON_STATUS_URL + shortname)
if response.status_code == 200:
status.append(response.json())
else:
message = f"OpenMHz maybe down!\nCode: {response.status_code}"
print(message)
alert(message)
except Exception as e:
message = f"OpenMHz maybe down!\nError: {e}"
print(message)
alert(message)
request_time = status[0]["requestTime"]
update_group = displayio.Group()
update_label = label.Label(
font=terminalio.FONT, text=f'updated: { request_time}', scale=1, color=0x000000
)
update_label.anchor_point = (0, 0)
update_label.anchored_position = (10, 110)
update_group.append(update_label)
magtag.splash.append(update_group)
def check_active():
inactive = []
for i, system in enumerate(status):
if system["active"] == False:
inactive.append(i)
if len(inactive) > 0:
output = ""
for i in inactive:
output = output + f'{shortnames[i]} inactive \n \n'
alert(output)
def print_status():
output = ""
check_active()
system_offset = 0
gauge_icon_bmp, gauge_icon_pal = adafruit_imageload.load("/bmp/noun-gauge-small.bmp")
headphone_icon_bmp, headphone_icon_pal = adafruit_imageload.load("/bmp/noun-headphone-small.bmp")
for i, shortname in enumerate(shortnames):
system = status[i]
system_labels = displayio.Group()
name_label = label.Label(
font=terminalio.FONT, text=shortname, scale=2, color=0x000000
)
# set label position on the display
name_label.anchor_point = (0, 0)
name_label.anchored_position = (10, system_offset)
system_labels.append(name_label)
call_avg = "{:.1f}".format(system["callAvg"])
listeners = str(system["clientCount"])
headphone_icon = displayio.TileGrid(
headphone_icon_bmp,
pixel_shader=headphone_icon_pal,
width=1,
height=1,
tile_width=24,
tile_height=24,
)
headphone_icon.x = 130
headphone_icon.y = system_offset
system_labels.append(headphone_icon)
listener_label = label.Label(
font=terminalio.FONT, text=f'{listeners}', scale=2, color=0x000000
)
listener_label.anchor_point = (0, 0)
listener_label.anchored_position = (160, system_offset)
system_labels.append(listener_label)
gauge_icon = displayio.TileGrid(
gauge_icon_bmp,
pixel_shader=gauge_icon_pal,
width=1,
height=1,
tile_width=24,
tile_height=24,
)
gauge_icon.x = 210
gauge_icon.y = system_offset
system_labels.append(gauge_icon)
call_label = label.Label(
font=terminalio.FONT, text=f'{call_avg} ', scale=2, color=0x000000
)
call_label.anchor_point = (0, 0)
call_label.anchored_position = (240, system_offset)
system_labels.append(call_label)
system_offset += 30
magtag.splash.append(system_labels)
print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}")
print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}")
wifi.radio.connect(os.getenv("CIRCUITPY_WIFI_SSID"), os.getenv("CIRCUITPY_WIFI_PASSWORD"))
print(f"Connected to {os.getenv('CIRCUITPY_WIFI_SSID')}")
print(f"My IP address: {wifi.radio.ipv4_address}")
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
print("Done")
update_status()
print_status()
magtag.display.refresh()
# Create a an alarm that will trigger 20 seconds from now.
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 600)
# Exit the program, and then deep sleep until the alarm wakes us.
alarm.exit_and_deep_sleep_until_alarms(time_alarm)
# Does not return, so we never get here.
magtag.exit_and_deep_sleep(60)