-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_factory.py
102 lines (77 loc) · 2.89 KB
/
sensor_factory.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
#!/usr/bin/env python
#Factroy for creating Sensor Singletons
#Author: Kevin Murphy
#Date : 5 - Dec - 14
import os
import ctypes
import socket
import subprocess
import constants as CONSTS
#Import Sensors
from py_sensors.thermistor import Thermistor
from py_sensors.mq7 import MQ7
from py_sensors.mq2 import MQ2
from py_sensors.motion_detector import MotionDetector
class SensorFactory(object):
#Constants
DEBUG = True
LOGTAG = "SensorFactory"
LIB_PATH = "./sensors/libs/lib_SensorManager.so"
__sensorLib = None
__alertManager = None
#Singletons
__mq7 = None
__thermistor = None
__motionDetector = None
__mq2 = None
#Sensor Holder
__sensors = []
def __init__(self, alertManager):
if self.DEBUG:
print self.LOGTAG , " :: Created..."
if socket.gethostname() == CONSTS.RASP_PI:
if self.DEBUG:
print self.LOGTAG, " :: Running on Raspberry Pi"
subprocess.call("gpio load spi", shell=True)
self.__sensorLib = ctypes.cdll.LoadLibrary(self.LIB_PATH)
self.__alertManager = alertManager
self.createAllSensors()
def createAllSensors(self):
self.getInstance_MQ7()
self.getInstance_MQ2()
self.getInstance_Thermistor()
self.getInstance_MotionDetector()
def getSensors(self):
return self.__sensors
def getInstance_MQ7(self):
if self.__mq7 is None:
if self.__sensorLib is None:
self.__mq7 = MQ7(lib=None, alertManager=None)
else:
self.__mq7 = MQ7(lib=self.__sensorLib, alertManager=self.__alertManager)
self.__sensors.append(self.__mq7)
return self.__mq7
def getInstance_MQ2(self):
if self.__mq2 is None:
if self.__sensorLib is None:
self.__mq2 = MQ2(lib=None, alertManager=None)
else:
self.__mq2 = MQ2(lib=self.__sensorLib, alertManager=self.__alertManager)
self.__sensors.append(self.__mq2)
return self.__mq2
def getInstance_Thermistor(self):
if self.__thermistor is None:
if self.__sensorLib is None:
self.__thermistor = Thermistor(lib=None, alertManager=None)
else:
self.__thermistor = Thermistor(lib=self.__sensorLib, alertManager=self.__alertManager)
self.__sensors.append(self.__thermistor)
return self.__thermistor
def getInstance_MotionDetector(self):
if self.__motionDetector is None:
if self.__sensorLib is None:
self.__motionDetector = MotionDetector(lib=None, alertManager=None)
else:
self.__motionDetector = MotionDetector(lib=self.__sensorLib, alertManager=self.__alertManager)
self.__sensors.append(self.__motionDetector)
return self.__motionDetector