-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEQMap.py
295 lines (235 loc) · 7.28 KB
/
EQMap.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""
Earthquake Display Program
For the Raspberry Pi Model 3B and the official 7" display
Concept, Design by: Craig A. Lindley
"""
import time
from datetime import datetime
from DisplayManager import displayManager
from EQEventGatherer import eqGathererEU
from EQEventGatherer import eqGathererUSGS
from EventDB import eventDB
# Data Sourcing
use_eu = True
use_usgs = True
# Colors for display
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
# Acquire new EQ data every 30 seconds
ACQUISITION_TIME_MS = 30000
# Blink every .5 seconds
BLINK_TIME_MS = 500
# Title page display every 15 minutes
TITLEPAGE_DISPLAY_TIME_MS = 900000
# Times in the future for actions to occur
ftForAcquisition = 0
ftForBlink = 0
ftForTitlePageDisplay = 0
# Current quake data
cqIDUK = ""
cqIDUSGS = ""
cqLocation = "loading..."
cqLon = 0.0
cqLat = 0.0
cqMag = 0.0
cqDepth = 0.0
cqAlert = None
cqTsunami = 0
dataToggle = False
blinkToggle = False
# Return system millisecond count
def millis():
return int(round(time.time() * 1000))
# Repaint the map from the events in the DB
def repaintMap():
highestMag, trending, max_location = eventDB.getLargestEvent()
highestMag = str(highestMag)
eventCount = eventDB.numberOfEvents()
# Display fresh map
displayManager.displayMap()
# Display current local time upper left
displayManager.displayCurrentTime()
# Display EQ location in color under map
displayManager.displayEventLong(cqLocation, cqMag, cqDepth)
# Display map Draw data with event count and date
displayManager.displayBottomDataFeed(max_location,eventCount)
# Display EQ depth and last EQ timestamp upper right
isCluster = cqLocation in (str(eventDB.getActiveRegion(preserve=True)))
if isCluster and eventCount > 4:
displayManager.displayDBStats(cqMag, eventCount, highestMag, cqTsunami, cqAlert, cluster=True)
else:
displayManager.displayDBStats(cqMag, eventCount, highestMag, cqTsunami, cqAlert)
# Display all of the EQ events in the DB as circles
count = eventDB.numberOfEvents()
if count > 0:
for i in range(count):
lon, lat, mag, alert, tsunami, location = eventDB.getEvent(i)
# Color depends upon magnitude
color = displayManager.colorFromMag(mag)
displayManager.mapEarthquake(lon, lat, mag, color)
return True
# Display title page and schedule next display event
def displayTitlePage():
global ftForTitlePageDisplay
# Display the title/ wash page
highestMag, trending, max_location = eventDB.getLargestEvent()
highestMag = str(highestMag)
dayTrend = str(eventDB.getDayTrend())
displayManager.displayWashPage(highestMag, str(eventDB.getActiveRegion()), dayTrend, max_location)
# Schedule next title page display
ftForTitlePageDisplay = millis() + TITLEPAGE_DISPLAY_TIME_MS
# getUSGS Function
def getUpdatesUSGS():
global cqIDUSGS,cqLocation,cqLon,cqLat,cqMag,cqDepth,cqTsunami,cqAlert
# internet check test
try:
# Check for new earthquake event
eqGathererUSGS.requestEQEvent()
except:
pass
# Determine if we have seen this event before If so ignore it
if cqIDUSGS != eqGathererUSGS.getEventID():
#if data has no ID dont use it
if eqGathererUSGS.getEventID is None:
return False
else:
# Extract the EQ data
try:
cqLocation = eqGathererUSGS.getLocation()
cqLon = eqGathererUSGS.getLon()
cqLat = eqGathererUSGS.getLat()
cqMag = eqGathererUSGS.getMag()
cqDepth = eqGathererUSGS.getDepth()
cqTsunami = eqGathererUSGS.getTsunami()
cqAlert = eqGathererUSGS.getAlert()
except Exception:
return False
# Add new event to DB if it isnt also from the other source
if not eventDB.checkDupLonLat(cqLon, cqLat):
eventDB.addEvent(cqLon, cqLat, cqMag, cqTsunami, cqAlert, cqLocation)
# Update the current event ID
cqIDUSGS = eqGathererUSGS.getEventID()
# Display the new EQ data
repaintMap()
return cqIDUSGS,cqLocation,cqLon,cqLat,cqMag,cqDepth,cqTsunami,cqAlert
return False
# getEU Function
def getUpdatesEU():
global cqIDUK,cqLocation,cqLon,cqLat,cqMag,cqDepth,cqTsunami,cqAlert
# internet check test
try:
# Check for new earthquake event
eqGathererEU.requestEQEvent()
except:
pass
# Determine if we have seen this event before If so ignore it
if cqIDUK != eqGathererEU.getEventID():
# Extract the EQ data
cqLocation = eqGathererEU.getLocation()
cqLon = eqGathererEU.getLon()
cqLat = eqGathererEU.getLat()
cqMag = eqGathererEU.getMag()
cqDepth = eqGathererEU.getDepth()
cqTsunami = 0
cqAlert = None
# Add new event to DB if it isnt also from the other source
if not eventDB.checkDupLonLat(cqLon, cqLat):
eventDB.addEvent(cqLon, cqLat, cqMag, cqTsunami, cqAlert, cqLocation)
# Update the current event ID
cqIDUK = eqGathererEU.getEventID()
# Display the new EQ data
repaintMap()
# Code execution start
def main():
# Setup for global variable access
global ftForAcquisition
global ftForBlink
global cqIDUK
global cqIDUSGS
global cqLocation
global cqLon
global cqLat
global cqMag
global cqDepth
global cqTsunami
global cqAlert
global blinkToggle
global dataToggle
global largestLOC
ftForAcquisition = 0
ftForBlink = 0
dbCleared = False
# True if display is on; false if off
displayState = False
#exit loop handler
running = True
#loop
try:
while running:
# Get the current time
now = datetime.now()
# Reset the DB at 0:00 so display show EQs per day TODO settings menu
# And we don't loose the EQ events
if now.hour == 0 and dbCleared == False:
eventDB.save()
eventDB.clear()
dbCleared = True
# Now check to see if the display should be off or on TODO settings menu
if now.hour > 6 and now.hour < 22:
# Normal viewing hours have arrived TODO is this working?
# If display is off, turn it on
if displayState == False:
displayManager.backlight(True)
displayState = True
dbCleared = False
# Display the title page
displayTitlePage()
# Force a redisplay of all quake data
repaintMap()
else:
# Normal viewing hours over. Turn the display off
if displayState == True:
# Turn the display off
displayManager.backlight(False)
displayState = False
# Is it time to display the title page ?
if millis() > ftForTitlePageDisplay:
displayTitlePage()
# Force a redisplay of all quake data
repaintMap()
#eventDB.save() #Save Database #DEBUG
# Is it time to acquire new earthquake data ?
if millis() > ftForAcquisition:
# server requests from EU and USGS balanced and dupe checked in function
if dataToggle:
dataToggle = False
if use_usgs:
getUpdatesUSGS()
else:
getUpdatesEU()
else:
dataToggle = True
if use_eu:
getUpdatesEU()
else:
getUpdatesUSGS()
ftForAcquisition = millis() + ACQUISITION_TIME_MS
# Is it time to blink EQ circle?
if millis() > ftForBlink:
if blinkToggle:
color = displayManager.colorFromMag(cqMag)
displayManager.mapEarthquake(cqLon, cqLat, cqMag, color)
blinkToggle = False
else:
displayManager.mapEarthquake(cqLon, cqLat, cqMag, BLACK)
blinkToggle = True
# Update current display time on the off beat
displayManager.displayCurrentTime()
ftForBlink = millis() + BLINK_TIME_MS
except KeyboardInterrupt:
print("closing EQMap")
# Earthquake Map Program Entry Point
if __name__ == '__main__':
main()