-
Notifications
You must be signed in to change notification settings - Fork 19
/
controller.py
179 lines (154 loc) · 4.43 KB
/
controller.py
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
# Free for personal use. Prohibited from commercial use without consent.
import RPi.GPIO as GPIO
import time
import urllib
import re
from threading import Thread
def get_metar(airport):
try:
stream = urllib.urlopen('http://www.aviationweather.gov/metar/data?ids='+airport+'&format=raw&hours=0&taf=off&layout=off&date=0');
for line in stream:
if '<!-- Data starts here -->' in line:
return re.sub('<[^<]+?>', '', stream.readline())
return 'INVALID'
except Exception, e:
print str(e);
return 'INVALID'
finally:
stream.close();
def get_vis(metar):
components = metar.split(' ');
for component in components:
if 'SM' in component:
return re.sub('SM', '', component)
return 'INVALID'
def get_vis_category(vis):
if vis == 'INVALID':
return 'INVALID'
if '/' in vis:
return 'LIFR'
vis_int = int(vis)
if vis < 3:
return 'IFR'
if vis <= 5:
return 'MVFR'
return 'VFR'
def get_ceiling(metar):
components = metar.split(' ' );
minimum_ceiling = 10000
for component in components:
if 'BKN' in component or 'OVC' in component:
ceiling = int(filter(str.isdigit,component)) * 100
if(ceiling < minimum_ceiling):
minimum_ceiling = ceiling
return minimum_ceiling
def get_ceiling_category(ceiling):
if ceiling < 500:
return 'LIFR'
if ceiling < 1000:
return 'IFR'
if ceiling < 3000:
return 'MVFR'
return 'VFR'
def get_category(metar):
vis = get_vis_category(get_vis(metar))
ceiling = get_ceiling_category(get_ceiling(metar))
if(vis == 'INVALID' or ceiling == 'INVALID'):
return 'INVALID'
if(vis == 'LIFR' or ceiling == 'LIFR'):
return 'LIFR'
if(vis == 'IFR' or ceiling == 'IFR'):
return 'IFR'
if(vis == 'MVFR' or ceiling == 'MVFR'):
return 'MVFR'
return 'VFR'
airport_pins = {'KRNT':(3,5,7),
'KSEA':(11,13,15),
'KPLU':(19,21,23),
'KOLM':(29,31,33),
'KTIW':(32,35,37),
'KPWT':(36,38,40),
'KSHN':(8,10,12)}
overrides = {'KOLM':'VFR',
'KTIW':'MVFR',
'KPWT':'INVALID'}
colors = {'RED':(GPIO.HIGH, GPIO.LOW, GPIO.LOW),
'GREEN':(GPIO.LOW, GPIO.HIGH, GPIO.LOW),
'BLUE':(GPIO.LOW, GPIO.LOW, GPIO.HIGH),
'LOW':(GPIO.LOW, GPIO.LOW, GPIO.LOW)}
airport_should_flash = {}
airport_color = {}
for airport in airport_pins:
airport_should_flash[airport] = True
airport_color[airport] = 'BLUE'
def set_airport_display(airport, category):
if category == 'VFR':
airport_should_flash[airport] = False
airport_color[airport] = 'GREEN'
elif category == 'MVFR':
airport_should_flash[airport] = False
airport_color[airport] = 'BLUE'
elif category == 'IFR':
airport_should_flash[airport] = False
airport_color[airport] = 'RED'
elif category == 'LIFR':
airport_should_flash[airport] = True
airport_color[airport] = 'RED'
else:
airport_should_flash[airport] = True
airport_color[airport] = 'BLUE'
def refresh_airport_displays():
for airport in airport_pins:
print "Retrieving METAR for "+airport
metar = get_metar(airport)
print "METAR for "+airport+" = "+metar
category = get_category(metar)
if airport in overrides:
category = overrides[airport]
print "Category for "+airport+" = "+category
set_airport_display(airport, category)
def render_airport_displays(airport_flasher):
for airport in airport_pins:
if airport_should_flash[airport] and airport_flasher:
setLed(airport_pins[airport], 'LOW')
else:
setLed(airport_pins[airport], airport_color[airport])
#VFR - Green
#MVFR - Blue
#IFR - Red
#LIFR - Flashing red
#Error - Flashing blue
def setLed(pins, color):
GPIO.output(pins, colors[color])
def all_airports(color):
for airport in airport_pins:
print str(airport_pins[airport])
GPIO.setup(airport_pins[airport], GPIO.OUT)
setLed(airport_pins[airport], color)
def render_thread():
print "Starting rendering thread"
while(True):
print "render"
render_airport_displays(True)
time.sleep(1)
render_airport_displays(False)
time.sleep(1)
def refresh_thread():
print "Starting refresh thread"
while(True):
print "Refreshing categories"
refresh_airport_displays()
time.sleep(60)
GPIO.setmode(GPIO.BOARD)
# Test LEDS on startup
all_airports('GREEN')
time.sleep(2)
all_airports('LOW')
time.sleep(2)
thread1 = Thread(target = render_thread)
thread2 = Thread(target = refresh_thread)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
GPIO.cleanup()