forked from theyosh/TerrariumPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrariumSensor.py
451 lines (357 loc) · 16.9 KB
/
terrariumSensor.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# -*- coding: utf-8 -*-
import terrariumLogging
logger = terrariumLogging.logging.getLogger(__name__)
import datetime
import time
import os.path
import glob
import re
import ow
from hashlib import md5
from terrariumUtils import terrariumUtils
from terrariumAnalogSensor import terrariumSKUSEN0161Sensor
from terrariumGPIOSensor import terrariumYTXXSensorDigital, terrariumDHT11Sensor, terrariumDHT22Sensor, terrariumAM2302Sensor, terrariumHCSR04Sensor
from terrariumI2CSensor import terrariumSHT2XSensor, terrariumHTU21DSensor, terrariumSi7021Sensor, terrariumBME280Sensor, terrariumChirpSensor
class terrariumRemoteSensor(object):
hardwaretype = 'remote'
def __init__(self,url):
self.__url = None
self.__value = None
if terrariumUtils.parse_url(url) is not False:
self.__url = url
def __get_raw_data(self):
if self.__url is not None:
self.__value = terrariumUtils.get_remote_data(self.__url)
def __enter__(self):
"""used to enable python's with statement support"""
return self
def __exit__(self, type, value, traceback):
"""with support"""
self.__url = None
self.__value = None
def get_current(self):
self.__get_raw_data()
return None if not terrariumUtils.is_float(self.__value) else float(self.__value)
def get_temperature(self):
return self.get_current()
def get_humidity(self):
return self.get_current()
def get_moisture(self):
return self.get_current()
def get_conductivity(self):
return self.get_current()
def get_distance(self):
return self.get_current()
def get_distance(self):
return self.get_current()
def get_ph(self):
return self.get_current()
class terrarium1WSensor(object):
hardwaretype = 'w1'
W1_BASE_PATH = '/sys/bus/w1/devices/'
W1_TEMP_REGEX = re.compile(r'(?P<type>t|f)=(?P<value>[0-9\-]+)',re.IGNORECASE)
def __init__(self,path):
self.__path = None
self.__value = None
path = os.path.join(terrarium1WSensor.W1_BASE_PATH,path,'w1_slave')
if os.path.isfile(path):
self.__path = path
def __get_raw_data(self):
if self.__path is not None and os.path.isfile(self.__path):
with open(self.__path, 'r') as w1data:
data = w1data.read()
w1data = terrarium1WSensor.W1_TEMP_REGEX.search(data)
if w1data:
# Found data
self.__value = float(w1data.group('value')) / 1000.0
def __enter__(self):
"""used to enable python's with statement support"""
return self
def __exit__(self, type, value, traceback):
"""with support"""
self.__path = None
self.__value = None
def get_current(self):
self.__get_raw_data()
return None if not terrariumUtils.is_float(self.__value) else float(self.__value)
def get_temperature(self):
return self.get_current()
@staticmethod
def scan():
# Scanning w1 system bus
for address in glob.iglob(terrarium1WSensor.W1_BASE_PATH + '[1-9][0-9]-*'):
if not os.path.isfile(address + '/w1_slave'):
break
data = ''
with open(address + '/w1_slave', 'r') as w1data:
data = w1data.read()
w1data = terrarium1WSensor.W1_TEMP_REGEX.search(data)
if w1data:
# Found valid data
yield (os.path.basename(address),'temperature' if 't' == w1data.group('type') else 'humidity')
class terrariumOWFSSensor(object):
hardwaretype = 'owfs'
def __init__(self,sensor):
self.__sensor = sensor
#self.__sensor.useCache(True)
self.__temperature = None
self.__humidity = None
def __get_raw_data(self):
if 'temperature' in self.__sensor.entryList():
self.__temperature = self.__sensor.temperature
if 'humidity' in self.__sensor.entryList():
self.__humidity = self.__sensor.humidity
def __enter__(self):
"""used to enable python's with statement support"""
return self
def __exit__(self, type, value, traceback):
"""with support"""
self.__sensor = None
self.__temperature = None
self.__humidity = None
def get_temperature(self):
self.__get_raw_data()
return None if not terrariumUtils.is_float(self.__temperature) else float(self.__temperature)
def get_humidity(self):
self.__get_raw_data()
return None if not terrariumUtils.is_float(self.__humidity) else float(self.__humidity)
@staticmethod
def scan(port):
if port > 0:
try:
ow.init(str(port));
sensorsList = ow.Sensor('/').sensorList()
for sensor in sensorsList:
if 'temperature' in sensor.entryList():
yield(sensor,'temperature')
if 'humidity' in sensor.entryList():
yield(sensor,'humidity')
except ow.exNoController:
logger.debug('OWFS file system is not actve / installed on this device!')
pass
class terrariumSensor(object):
UPDATE_TIMEOUT = 30
ERROR_TIMEOUT = 10
VALID_SENSOR_TYPES = ['temperature','humidity','moisture','conductivity','distance','ph','light']
VALID_HARDWARE_TYPES = []
# Append OWFS to the list of valid sensors
VALID_HARDWARE_TYPES.append(terrariumOWFSSensor.hardwaretype)
# Append remote sensor(s) to the list of valid sensors
VALID_HARDWARE_TYPES.append(terrariumRemoteSensor.hardwaretype)
# Append 1-wire sensor(s) to the list of valid sensors
VALID_HARDWARE_TYPES.append(terrarium1WSensor.hardwaretype)
# Append DHT sensor(s) to the list of valid sensors
VALID_HARDWARE_TYPES.append(terrariumDHT11Sensor.hardwaretype)
VALID_HARDWARE_TYPES.append(terrariumDHT22Sensor.hardwaretype)
VALID_HARDWARE_TYPES.append(terrariumAM2302Sensor.hardwaretype)
# Append I2C sensor(s) to the list of valid sensors
VALID_HARDWARE_TYPES.append(terrariumSHT2XSensor.hardwaretype)
VALID_HARDWARE_TYPES.append(terrariumHTU21DSensor.hardwaretype)
VALID_HARDWARE_TYPES.append(terrariumSi7021Sensor.hardwaretype)
VALID_HARDWARE_TYPES.append(terrariumBME280Sensor.hardwaretype)
VALID_HARDWARE_TYPES.append(terrariumChirpSensor.hardwaretype)
# Append YTXX sensor(s) to the list of valid sensors
VALID_HARDWARE_TYPES.append(terrariumYTXXSensorDigital.hardwaretype)
# Append hc-sr04 sensor(s) to the list of valid sensors
VALID_HARDWARE_TYPES.append(terrariumHCSR04Sensor.hardwaretype)
# Appand analog sensor(s) to the list of valid sensors
VALID_HARDWARE_TYPES.append(terrariumSKUSEN0161Sensor.hardwaretype)
def __init__(self, id, hardware_type, sensor_type, sensor, name = '', callback_indicator = None):
self.id = id
self.set_hardware_type(hardware_type)
self.set_address(sensor)
self.set_name(name)
self.set_type(sensor_type,callback_indicator)
self.set_alarm_min(0)
self.set_alarm_max(0)
self.set_limit_min(0)
# For hc-sr04 set at 10 meters else just '100' value
self.set_limit_max(100000 if terrariumHCSR04Sensor.hardwaretype == self.get_hardware_type() else 100)
if self.id is None:
self.id = md5(b'' + self.get_address().replace('-','').upper() + self.get_type()).hexdigest()
self.current = float(0)
self.last_update = datetime.datetime.fromtimestamp(0)
logger.info('Loaded %s %s sensor \'%s\' on location %s.' % (self.get_hardware_type(),self.get_type(),self.get_name(),self.get_address()))
self.update()
@staticmethod
def scan(port,unit_indicator):
starttime = time.time()
logger.debug('Start scanning for temperature/humidity sensors')
sensor_list = []
# Scanning OWFS sensors
if port > 0:
for (owfssensor,owfstype) in terrariumOWFSSensor.scan(port):
sensor_list.append(terrariumSensor(None,
'owfs',
owfstype,
owfssensor,
callback_indicator=unit_indicator))
# Scanning w1 system bus
for (w1sensor,w1type) in terrarium1WSensor.scan():
sensor_list.append(terrariumSensor(None,
'w1',
w1type,
w1sensor,
callback_indicator=unit_indicator))
logger.info('Found %d temperature/humidity sensors in %.5f seconds' % (len(sensor_list),time.time() - starttime))
return sensor_list
def update(self, force = False):
now = datetime.datetime.now()
if now - self.last_update > datetime.timedelta(seconds=terrariumSensor.UPDATE_TIMEOUT) or force:
logger.debug('Updating %s %s sensor \'%s\'' % (self.get_hardware_type(),self.get_type(), self.get_name()))
old_current = self.get_current()
current = None
try:
starttime = time.time()
hardwaresensor = None
address = [self.get_address(),None,None] if ',' not in self.get_address() else self.get_address().split(',')
if len(address) == 2:
address.append(None)
if terrariumRemoteSensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumRemoteSensor(address[0])
elif terrarium1WSensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrarium1WSensor(address[0])
elif terrariumOWFSSensor.hardwaretype == self.get_hardware_type():
# Dirty hack for OWFS sensors.... ;)
hardwaresensor = terrariumOWFSSensor(self.__sensor)
elif terrariumSHT2XSensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumSHT2XSensor(address[0],address[1])
elif terrariumHTU21DSensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumHTU21DSensor(address[0],address[1])
elif terrariumSi7021Sensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumSi7021Sensor(address[0],address[1])
elif terrariumBME280Sensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumBME280Sensor(address[0],address[1])
elif terrariumChirpSensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumChirpSensor(address[0],address[1])
elif terrariumYTXXSensorDigital.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumYTXXSensorDigital(address[0],address[1])
elif terrariumDHT11Sensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumDHT11Sensor(address[0],address[1])
elif terrariumDHT22Sensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumDHT22Sensor(address[0],address[1])
elif terrariumAM2302Sensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumAM2302Sensor(address[0],address[1])
elif terrariumSKUSEN0161Sensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumSKUSEN0161Sensor(address[0],address[1])
elif terrariumHCSR04Sensor.hardwaretype == self.get_hardware_type():
hardwaresensor = terrariumHCSR04Sensor(address[0],address[1],address[2])
if hardwaresensor is not None:
with hardwaresensor as sensor:
if 'temperature' == self.get_type():
current = sensor.get_temperature()
elif 'humidity' == self.get_type():
current = sensor.get_humidity()
elif 'moisture' == self.get_type():
current = sensor.get_moisture()
elif 'conductivity' == self.get_type():
current = sensor.get_conductivity()
elif 'distance' == self.get_type():
current = sensor.get_distance()
elif 'ph' == self.get_type():
current = sensor.get_ph()
elif 'light' == self.get_type():
current = sensor.get_light()
del hardwaresensor
if current is None or not (self.get_limit_min() <= current <= self.get_limit_max()):
# Invalid current value.... log and ingore
logger.warn('Measured value %s%s from %s sensor \'%s\' is outside valid range %.2f%s - %.2f%s in %.5f seconds.' % (current,
self.get_indicator(),
self.get_type(),
self.get_name(),
self.get_limit_min(),
self.get_indicator(),
self.get_limit_max(),
self.get_indicator(),
time.time()-starttime))
else:
self.current = current
self.last_update = now
logger.info('Updated %s sensor \'%s\' from %.2f%s to %.2f%s in %.5f seconds' % (self.get_type(),
self.get_name(),
old_current,
self.get_indicator(),
self.get_current(),
self.get_indicator(),
time.time()-starttime))
except Exception, ex:
print ex
logger.exception('Error updating %s %s sensor \'%s\' with error:' % (self.get_hardware_type(),
self.get_type(),
self.get_name()))
logger.exception(ex)
def get_data(self):
data = {'id' : self.get_id(),
'hardwaretype' : self.get_hardware_type(),
'address' : self.get_address(),
'type' : self.get_type(),
'indicator' : self.get_indicator(),
'name' : self.get_name(),
'current' : self.get_current(),
'alarm_min' : self.get_alarm_min(),
'alarm_max' : self.get_alarm_max(),
'limit_min' : self.get_limit_min(),
'limit_max' : self.get_limit_max(),
'alarm' : self.get_alarm(),
'error' : not self.is_active()
}
return data
def get_id(self):
return self.id
def get_hardware_type(self):
return self.hardwaretype
def set_hardware_type(self,type):
if type in terrariumSensor.VALID_HARDWARE_TYPES:
self.hardwaretype = type
def set_type(self,type,indicator):
if type in terrariumSensor.VALID_SENSOR_TYPES:
self.type = type
self.__indicator = indicator
def get_type(self):
return self.type
def get_indicator(self):
# Use a callback from terrariumEngine for 'realtime' updates
return self.__indicator(self.get_type())
def get_address(self):
return self.sensor_address
def set_address(self,address):
if isinstance(address, basestring):
self.sensor_address = address
elif terrariumOWFSSensor.hardwaretype == self.get_hardware_type() and not isinstance(address, basestring):
# OW Sensor object
self.__sensor = address
self.sensor_address = self.__sensor.address
def set_name(self,name):
self.name = str(name)
def get_name(self):
return self.name
def get_alarm_min(self):
return self.alarm_min
def set_alarm_min(self,limit):
self.alarm_min = float(limit)
def get_alarm_max(self):
return self.alarm_max
def set_alarm_max(self,limit):
self.alarm_max = float(limit)
def get_limit_min(self):
return self.limit_min
def set_limit_min(self,limit):
self.limit_min = float(limit)
def get_limit_max(self):
return self.limit_max
def set_limit_max(self,limit):
self.limit_max = float(limit)
def get_current(self, force = False):
current = self.current
indicator = self.get_indicator().lower()
if 'f' == indicator:
current = terrariumUtils.to_fahrenheit(self.current)
elif 'inch' == indicator:
current = terrariumUtils.to_inches(self.current)
return float(current)
def get_alarm(self):
return not self.get_alarm_min() <= self.get_current() <= self.get_alarm_max()
def is_active(self):
return datetime.datetime.now() - self.last_update < datetime.timedelta(minutes=terrariumSensor.ERROR_TIMEOUT)
def stop(self):
logger.info('Cleaned up sensor %s at location %s' % (self.get_name(), self.get_address()))