-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_to_server.py
273 lines (217 loc) · 8.93 KB
/
data_to_server.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
# Python 3
# TODO: Report time in queue instead of the current date/time
import collections
import datetime
import DHT22 # Temperature sensor
import icetemp
import os
import pickle
import pigpio # Raspberry pi interface software
import socket
import sys
import time
# Debug mode (print log messages)
DEBUG = False
# Good enough for 1 full week of storage, writing a message every minute
max_queue_length = 10080
# There is no callback once we request a reading from the sensor, so we wait 0.5 seconds
trigger_wait_time = 0.5
# Raspberry Pi pin configuration for sensor
sensor_gpio = 4
power_pin = 8
led_pin = 16
# Path to the directory this file is in
dir_path = os.path.dirname(os.path.realpath(__file__))
# Name of the file that will store a cache/backup of the current queue
cache_file_name = dir_path + "/queue_cache"
def log(message) :
if DEBUG :
print(message)
def queueReading(queue, sensor, source_name, tunit, air, ice, humid) :
log("queueReading")
air_temp = -1
ice_temp = -1
humidity = -1
# Request the temperature from the sensor and wait until it is available
if sensor :
sensor.trigger()
time.sleep(trigger_wait_time)
now = datetime.datetime.now()
if air :
air_temp = sensor.temperature()
if tunit.upper() == "F" :
air_temp = air_temp * 1.8 + 32
air_temp_str = "{0:.1f}".format(air_temp)
air_temperature_message = {"message": ("insert:{table:'airtemps', values:{source:'%s',value:%s,unit:'%s',$ago:%%date%%}};;" % (source_name, air_temp_str, tunit)), "date": now}
log("Queueing message: %s" % air_temperature_message)
queue.append(air_temperature_message)
if ice :
ice_temp = icetemp.read_temp()
if tunit.upper() == "F" :
ice_temp = ice_temp * 1.8 + 32
ice_temp_str = "{0:.1f}".format(ice_temp)
ice_temperature_message = {"message": ("insert:{table:'icetemps', values:{source:'%s',value:%s,unit:'%s',$ago:%%date%%}};;" % (source_name, ice_temp_str, tunit)), "date": now}
log("Queueing message: %s" % ice_temperature_message)
queue.append(ice_temperature_message)
if humid :
humidity = sensor.humidity()
humidity_str = "{0:.1f}".format(humidity)
humidity_message = {"message": ("insert:{table:'humidities', values:{source:'%s',value:%s,unit:'%%',$ago:%%date%%}};;" % (source_name, humidity_str)), "date": now}
log("Queueing message: %s" % humidity_message)
queue.append(humidity_message)
# Try to drain the queue by writing messages to the connection
# If there is no active, connection, attempt to establish one
# Return the established connection, if made
# If there is an error establishing a connection or sending the message, return None
def tryDrainQueue(connection, queue, host, port) :
log("tryDrainQueue (host=%s, port=%s, queuesize: %s)" % (host, str(port), str(len(queue))))
if not connection :
connection = getTcpConnection(host, port)
if not connection :
log("Can't establish connection.")
return None
while len(queue) > 0 :
now = datetime.datetime.now()
item = queue.popleft()
message = item["message"]
when = item["date"]
message = message.replace("%date%", str((now - when).total_seconds()))
try :
connection.send(bytes(message, 'UTF-8'))
except Exception as e :
# Error sending message, so put it back in the queue.
# We'll try again on the next go-around
log("Error sending message: " + str(e))
queue.appendleft(item)
return None
return connection
def writeQueueCacheToDisk(queue) :
log("writeQueueCacheToDisk")
# Only write if the queue is not empty
if (len(queue) == 0) :
return
cache_file = open(cache_file_name, "wb")
pickle.dump(queue, cache_file)
cache_file.close()
def readQueueCacheFromDisk() :
log("readQueueCacheFromDisk (cache_file_name=%s)" % cache_file_name)
cache_file = None
result = None
# Try to open the cache file for reading
try :
cache_file = open(cache_file_name, "r")
except :
log("Error opening cache file")
# If the file was opened, try to load it with pickle
if cache_file :
try :
result = pickle.load(cache_file)
except :
log("Error with pickle.load")
# If the file was opened, attempt to close it
try :
if cache_file :
cache_file.close()
except :
pass
# If we weren't able to load, get a default deque
if not result :
result = getNewDeque()
# Delete the cache file whether or not we were able to load it and return the queue
deleteCacheFile()
return result
def deleteCacheFile() :
log("deleteCacheFile (cache_file_name=%s)" % cache_file_name)
try :
os.remove(cache_file_name)
except :
pass
def getNewDeque() :
log("getNewDeque")
return collections.deque(list(), max_queue_length)
def getTcpConnection(host, port) :
log("getTcpConnection (host=%s, port=%s)" % (host, str(port)))
try :
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2-second timeout to make the connection
connection.settimeout(2)
connection.connect((host, port))
return connection
except Exception as e :
log("Error initializing TCP connection: %s" % str(e))
return None
def checkArgs(args) :
log("checkArgs")
if len(args) < 4 :
print ("""Usage: %s source_name server_host server_port [ice] [air] [humid] [interval=update_interval_in_seconds] [tunit=F|C] [debug]""" % args[0])
sys.exit(-1)
def parseArgs(args) :
log("parseArgs")
source_name = args[1]
server_host = args[2]
server_port = int(args[3])
ice = False
air = False
humid = False
# Set the update interval based on a default value or command line argument
contains_interval = filter(lambda a: ("interval=" in a), args)
interval_arg = next(contains_interval, "interval=60")
interval = max(4, float(interval_arg[9:]))
# Set the temperature unit based on a default value or command line argument
tunit = "F"
contains_unit = filter(lambda a: ("tunit=" in a), args)
unit_arg = next(contains_unit, "tunit=F")
tunit = unit_arg[6:]
debug = False
if "debug" in args :
debug = True
if "ice" in args :
ice = True
if "air" in args :
air = True
if "humid" in args :
humid = True
return source_name, server_host, server_port, interval, tunit, ice, air, humid, debug
if __name__ == "__main__" :
"""
source_name: the name of the source of this reading. E.g.: "EAST" or "NORTH"
server_host: hostname/ip of the TCP server listening for data readings. E.g.: 192.168.0.100
server_port: port of the TCP server is listening on for data readings. E.g. 3002
interval: specify an update interval in seconds. E.g. interval=600 (default = 300, minval = 4)
tunit: specify a unit for the temperature reading. C or F is accepted. E.g. tunit=C (default = F)
debug: if specified, log debug messages
"""
checkArgs(sys.argv)
source_name, server_host, server_port, interval, tunit, ice, air, humid, debug = parseArgs(sys.argv)
# Set debug mode
DEBUG = debug
log("Args parsed. source_name: %s, server_host: %s, server_port: %s, interval: %s, tunit: %s, air: %s, ice: %s, humid: %s, debug: %s" % (source_name, server_host, str(server_port), str(interval), tunit, str(air), str(ice), str(humid), str(debug)))
# Initialize the GPIO interface and temperature sensor
pi = pigpio.pi()
sensor = None
if air or humid :
sensor = DHT22.sensor(pi, sensor_gpio, LED=led_pin, power=power_pin)
# The queue is used to accumulate readings. It is drained when they are
# sent to the server. If the server is unavailable, the queue will
# accumulate messages.
queue = readQueueCacheFromDisk()
# Variable to store the TCP connection
connection = None
# Set the time this script began running
start_time = time.time()
log("Start time: %s" % str(start_time))
iteration = 0
while True :
log("Iteration: %s" % str(iteration))
queueReading(queue, sensor, source_name, tunit, air, ice, humid)
connection = tryDrainQueue(connection, queue, server_host, server_port)
writeQueueCacheToDisk(queue)
# Ensure this function runs exactly every <interval>, regardless of
# the execution time of the function.
time_passed_since_start = time.time() - start_time
sleep_interval = interval - (time_passed_since_start % interval)
# Note: if execution of this loop takes longer than <interval>, we
# effectively skip execution of the next loop
log("Next iteration in %s seconds." % str(sleep_interval))
time.sleep(sleep_interval)
iteration = iteration + 1