-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheadlessMonitor.py
More file actions
151 lines (128 loc) · 4.77 KB
/
Copy pathheadlessMonitor.py
File metadata and controls
151 lines (128 loc) · 4.77 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
"""Command-line monitoring interface for Linux.
Uses gpsd to acquire GPS data. gpsd has good documentation, so be sure to
check that. Watch out, though -- if you use gpsd to interact with a GPS unit
and gpsd speaks its proprietary binary protocol, it will have the unit switch
to use that, and when you plug it back in to a Windows machine you will not be
able to read location data. You need to set it back to NMEA first using the
gpsd tools.
Here we simply record new data every SAMPLE_TIME seconds until killed. If GPS
lock is lost, the data is simply dropped.
"""
import emorpho, time, psycopg2
from numpy import isnan, mean, sqrt, square
from util.wifi import WiFi as wifi
from util.gpsd import GPSD as gps
import os, signal, sys, json, datetime
from geopy import distance
SAMPLE_TIME = 2 # seconds
wifi = wifi()
TZ = ""
sensor = "0"
version = "0.0"
gpsBase = (0.0, 0.0)
gpsRange = 100
def loadConfig():
json_config = open('/home/debian/radmonitor-master/radmonitor.config')
config = json.load(json_config)
json_config.close()
for setting in config["radsettings"]:
setattr(e, setting, config["radsettings"][setting])
SAMPLE_TIME = config["sampleTime"]
e.clearStats()
e.startTimedHistogram(SAMPLE_TIME)
return config
def haltMonitor(signal, frame):
wifi.stop()
gps.stop()
print "Exiting headlessMonitor cleanly"
sys.exit(0)
signal.signal(signal.SIGINT, haltMonitor)
def connectToRad():
e.scan()
while e.open(0) == False:
print "Could not connect to Rad Sensor"
os.system("echo heartbeat > /sys/class/leds/beaglebone\:green\:usr2/trigger")
time.sleep(5)
e.scan()
os.system("echo none > /sys/class/leds/beaglebone\:green\:usr2/trigger")
conn = psycopg2.connect(database = 'radiation', user = 'radiation',
password = 'radiation', host = 'localhost')
cur = conn.cursor()
wifi.start()
e = emorpho.eMorpho()
connectToRad()
config = loadConfig()
gpsBase = (config["baseCoords"]["latitude"], config["baseCoords"]["longitude"])
gpsRange = config["baseCoords"]["range"]
sensor = config["sensor"]
version = config["version"]
TZ = config["timezone"]
AdjustTime = config["gpsTime"]
gps = gps(TZ, AdjustTime)
gps.start()
# def getFix(session):
# """Use gpsd session to get a TPV report. Return None if no fix is currently
# available. Return gpsd's dict otherwise."""
# for report in session:
# if report["class"] == "TPV":
# if report["mode"] == 3:
# return report
# else:
# return None
state = gpsState = True
gpsTimeout = 0
sessionCounter = 0
while True:
time.sleep(SAMPLE_TIME)
sessionCounter = sessionCounter + 1
if sessionCounter > 1000:
loadConfig()
sessionCounter = 0
# location = gps.getFix()
location = gps.getLocation()
if location.longitude == 0.0 or isnan(location.longitude):
print "Error: No GPS location"
gpsTimeout = gpsTimeout + 1
if gpsState:
os.system("echo default-on > /sys/class/leds/beaglebone\:green\:usr2/trigger")
gpsState = False
else:
os.system("echo none > /sys/class/leds/beaglebone\:green\:usr2/trigger")
gpsState = True
if gpsTimeout >= 5:
print "Restarting GPS"
gpsTimeout = 0
gps.retry()
continue
else:
baseDistance = distance.vincenty(gpsBase, (location.latitude, location.longitude)).meters
if baseDistance <= gpsRange:
if wifi.running == False:
print "In range of router"
wifi.start()
hist = e.readHistogram()
if hist == False:
print "Error: Couldn't read the histogram"
connectToRad()
continue
stats = e.readStats()
e.startTimedHistogram(SAMPLE_TIME)
# TODO: There's some logic in monitor.py to handle GPS dropouts by
# recording HDOP of -1. We should replicate that here.a
cur.execute("INSERT INTO histograms (location, hdop, time, sampletime, temp, cps, histogram, altitude, sensor, version) " +
"VALUES (ST_GeomFromText('POINT(%s %s)', 4326), %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(location.longitude, location.latitude, location.gpsError, location.timestamp, SAMPLE_TIME,
e.getTemperature(),
stats["cps"],
hist,
location.altitude,
sensor,
version))
conn.commit()
print "Added Entry ", location.timestamp, stats["cps"], location.latitude, location.longitude, location.gpsError, distance.vincenty(gpsBase, (location.latitude, location.longitude)).meters
if state:
os.system("echo default-on > /sys/class/leds/beaglebone\:green\:usr1/trigger")
state = False
else:
os.system("echo none > /sys/class/leds/beaglebone\:green\:usr1/trigger")
state = True