def __init__(self): ''' Constructor which initializes the sensor object and the SMTPCLient object ''' self.sensor = SensorData.SensorData() self.SmtpClient = SmtpClientConnector.MyClass()
class TempSensorAdaptor(Thread): #creating the sensor data object and initial values to use inside Adaptor sensorData = SensorData.SensorData() actuator = ActuatorData.ActuatorData() connector = SmtpClientConnector.SmtpClientConnector() actuatorEmulator = TempActuatorEmulator.TempActuatorEmulator() isPrevTempSet = False lowVal = 0 highVal = 30 nominalTemp = 20 alertDiff = 5 #initiating the thread for the Adaptor def __init__(self): Thread.__init__(self) def run(self): while True: if self.enableAdaptor: #generate temperature information self.curTemp = uniform(float(self.lowVal), float(self.highVal)) self.sensorData.addValue(self.curTemp) print('\n--------------------') print('New sensor readings:') print(' ' + str(self.sensorData)) if self.isPrevTempSet == False: self.prevTemp = self.curTemp self.isPrevTempSet = True else: #checking for alerting difference and sending the message through SMTP if (abs(self.curTemp - self.sensorData.getAvgValue()) >= self.alertDiff): print('\n Current temp exceeds average by > ' + str(self.alertDiff) + '. Triggering alert...') self.connector.publishMessage( 'Exceptional sensor data [test]', self.sensorData) ''' checking to see if the temperature exceeds nominal temperature to set status for actuator and send the message accordingly ''' if (self.curTemp > self.nominalTemp): self.actuator.setCommand(ActuatorData.COMMAND_ON) self.actuator.setStatusCode(ActuatorData.STATUS_ACTIVE) self.actuator.setErrorCode(ActuatorData.ERROR_OK) self.actuator.setStateData('Decrease') self.actuator.setValue(self.curTemp - self.nominalTemp) self.actuatorEmulator.processMessage(self.actuator) elif (self.curTemp < self.nominalTemp): self.actuator.setCommand(ActuatorData.COMMAND_OFF) self.actuator.setStatusCode(ActuatorData.STATUS_ACTIVE) self.actuator.setErrorCode(ActuatorData.ERROR_OK) self.actuator.setStatusCode('Increase') self.actuator.setValue(self.curTemp - self.nominalTemp) self.actuatorEmulator.processMessage(self.actuator) sleep(5)
def __init__(self, loop_param, sleep_param, pUtil: PersistenceUtil.PersistenceUtil, mqttClient: MqttClientConnector.MqttClientConnector): ''' Constructor ''' threading.Thread.__init__(self) self.loop_limit = loop_param self.sleep_time = sleep_param #Creating a SensorData instance and setting it's name self.sensorData = SensorData.SensorData() self.sensorData.setName("Temperature Sensor Data") #SenseHat instance to interact with the senseHAT self.sense = SenseHat() #clearing matrix self.sense.clear() #MQTT client self.mqtt = mqttClient #PersistenceUtil self.pUtil = pUtil
def __init__(self, loop_param, sleep_param, pUtil: PersistenceUtil.PersistenceUtil,coAPClient: CoAPClientConnector.CoAPClientConnector): ''' Constructor ''' #Init asyncio event loop self.loop = asyncio.new_event_loop() #Init the threadx threading.Thread.__init__(self, args=(self.loop,)) self.loop_limit = loop_param self.sleep_time = sleep_param #Creating a SensorData instance and setting it's name self.sensorData = SensorData.SensorData() self.sensorData.setName("Temperature Sensor Data") #SenseHat instance to interact with the senseHAT self.sense = SenseHat() #clearing matrix self.sense.clear() #MQTT client self.coAP = coAPClient #PersistenceUtil self.pUtil = pUtil
class TempSensorEmulator (threading.Thread): sensorData = SensorData.SensorData() connector = SmtpClientConnector.SmtpClientConnector() enableEmulator = False isPrevTempSet = False rateInSec = DEFAULT_RATE_IN_SEC sensorData.setName('Temperature') lowVal = 0 highVal = 30 alertDiff = 5 def __init__(self, rateInSec = DEFAULT_RATE_IN_SEC): super(TempSensorEmulator, self).__init__() if rateInSec > 0: self.rateInSec = rateInSec def run(self): while True: if self.enableEmulator: self.curTemp = uniform(float(self.lowVal), float(self.highVal)) self.sensorData.addValue(self.curTemp) print('\n--------------------') print('New sensor readings:') print(' ' + str(self.sensorData)) if self.isPrevTempSet == False: self.prevTemp = self.curTemp self.isPrevTempSet = True else: if (abs(self.curTemp - self.sensorData.getAvgValue()) >= self.alertDiff): print('\n Current temp exceeds average by > ' + str(self.alertDiff) + '. Triggeringalert...') self.connector.publishMessage('Exceptional sensor data [test]', self.sensorData) sleep(self.rateInSec)
def __init__(self, alertdiff): ''' constructor @param sensorData: sensorData class instance @param connector: instance of smtpClientConnector class @param alertDiff: the threshold value for sending alert message @param enableEmulator:boolean state of the emulator, initialized to False @param timeInterval:time in seconds between each data generation/collection @param lowValue:lowest value of the temperature achieved/expected @param highValue: highest value of the temperature expected/achieved @param curTemp: current value of the temperature ''' threading.Thread.__init__(self) self.enableEmulator = False logging.basicConfig( level=logging.INFO, format=' %(asctime)s %(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S') # self.timeInterval = int(self.config.getProperty(self.config.configConst.CONSTRAINED_DEVICE, self.config.configConst.POLL_CYCLES_KEY)) self.alertDiff = alertdiff # self.nominalTemp = int(self.config.getProperty(self.config.configConst.CONSTRAINED_DEVICE, self.config.configConst.NOMINAL_TEMP)) self.lowValue = 0 self.highValue = 30 self.curTemp = 0 self.prevTemp = 0.0 self.prevTempFlag = False self.SenseHat = SenseHat() self.sensorData = SensorData.SensorData() # self.connector = SMTPClientConnector.SMTPClientConnector() self.mqttClientConnector = mqttClientConnector()
class TempSensorEmulator(Thread): #creating the sensor data object and initial values to use inside Emulator sensorData = SensorData.SensorData() connector = SmtpClientConnector.SmtpClientConnector() isPrevTempSet = False lowVal = 0 highVal = 30 alertDiff = 5 #initiating the thread for the emulator def __init__(self): Thread.__init__(self) def run(self): while True: if self.enableEmulator: self.curTemp = uniform(float(self.lowVal), float(self.highVal)) self.sensorData.addValue(self.curTemp) print('\n--------------------') print('New sensor readings:') print(' ' + str(self.sensorData)) if self.isPrevTempSet == False: self.prevTemp = self.curTemp self.isPrevTempSet = True else: if (abs(self.curTemp - self.sensorData.getAvgValue()) >= self.alertDiff): print('\n Current temp exceeds average by > ' + str(self.alertDiff) + '. Triggering alert...') self.connector.publishMessage( 'Exceptional sensor data [test]', self.sensorData) sleep(5)
def __init__(self): Thread.__init__(self) self.enableEmulator = True # Turn the emulator ON self.sensor = SensorData.SensorData() # Obtain sensor data self.actuator = ActuatorData.ActuatorData() # Obtain actuator data self.alertDiff = 5 # Set time for obtaining values from sensor self.rateInSec = 5 # Import configurations self.config = ConfigUtil.ConfigUtil('../../../config/ConnectedDevicesConfig.props')
def __init__(self, name): Thread.__init__(self) self.enableAdaptor = True self.sensor = SensorData.SensorData(name, 0, 30) self.temp = ConfigUtil.ConfigUtil( '../../../config/ConnectedDevicesConfig.props') self.tempEmulator = TempActuatorEmulator.TempActuatorEmulator()
def __init__(self, name): Thread.__init__(self) self.enableAdaptor = True; self.sensorData = SensorData.SensorData(name, 0, 30); self.actuator = ActuatorData.ActuatorData() self.connector = SmtpClientConnector.SmtpClientConnector() self.tempConf = ConfigUtil.ConfigUtil('../../../config/ConnectedDevicesConfig.props'); self.actuatorEmulator = TempActuatorEmulator.TempActuatorEmulator();
class TempSensorAdaptor(threading.Thread): ''' classdocs ''' actuatorData = None tempActuatorEmulator = None connector = SmtpClientConnector.SmtpClientConnector() sensorData = SensorData.SensorData() alertDiff = 5 def __init__(self, enableEmulator, lowVal, highVal, curTemp, isPrevTempSet): super(TempSensorAdaptor, self).__init__() self.enableEmulator = enableEmulator self.curTemp = curTemp self.lowVal = lowVal self.highVal = highVal self.isPrevTempSet = isPrevTempSet self.config = ConfigUtil.ConfigUtil('') self.config.loadConfig() self.actuatorData = ActuatorData() self.tempActuatorEmulator = TempActuatorEmulator() def run(self): nominalTemp = int(self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.NOMINAL_TEMP_KEY)) while True: if self.enableEmulator: self.curTemp = uniform(float(self.lowVal), float(self.highVal)) #self.curTemp = self.sensorData.addValue(self.curTemp) #print('the sensor readings:') #print(' ' + str(self.sensorData)) if self.isPrevTempSet == False: self.prevTemp = self.curTemp self.isPrevTempSet = True else: if (abs(self.curTemp - self.sensorData.getAvgValue()) >= self.alertDiff): print('\n Current temp exceeds average by > ' + str(self.alertDiff) + '. Triggeringalert...') #self.connector.publishMessage('Exceptional sensor data [test]', str(self.sensorData)) if (abs(self.curTemp - nominalTemp) > self.alertDiff): val = self.curTemp - nominalTemp self.actuatorData.setValue(val) if (val > 0): self.actuatorData.setCommand(1) else: self.actuatorData.setCommand(0) self.tempActuatorEmulator.processMessage(self.actuatorData) sleep(1)
class HumiditySensorAdaptorTask(threading.Thread): ''' creating the required instances ''' rateInSec = DEFAULT_RATE_IN_SEC config = ConfigUtil.ConfigUtil() sensorData = SensorData.SensorData() sensorDataMgr = SensorDataManager.SensorDataManager() def __init__(self): ''' Initializing my constructor and SenseHAT API consequently ''' threading.Thread.__init__(self) self.enableEmulator = False self.senseHat = SenseHat() self.initHumidity() self.humidityData = self.senseHat.get_humidity() def initHumidity(self): logging.info("Fetching data from SenseHat API...") ''' enabling the Emulator ''' def setEmulator(self, boolean): self.enableEmulator = boolean ''' method to display my humidity data ''' def displayHumidityData(self): print('Relative Humidity through SenseHat data: ' + str(self.humidityData)) ''' method to update my humidity data through the SensorData instance ''' def updateHumidityValueSH(self): self.sensorData.addHumidityValueSH(self.humidityData) ''' Thread starts here ''' def run(self): while True: if self.enableEmulator: self.updateHumidityValueSH() self.displayHumidityData() self.sensorDataMgr.handleSensorData( self.sensorData.getHumidityValueSH(), 'humidSH') sleep(self.rateInSec)
def __init__(self): self.mqttClient = mqttClient.Client() #instance of the Client class of paho self.config = ConfigUtil.ConfigUtil('../../../config/data/ConnectedDevicesConfig.props') self.config.loadConfig() self.brokerAddr = self.config.getProperty(ConfigConst.MQTT_CLOUD_SECTION, ConfigConst.CLOUD_MQTT_BROKER) self.port = int(self.config.getProperty(ConfigConst.MQTT_CLOUD_SECTION,ConfigConst.CLOUD_MQTT_PORT)) self.brockerKeepAlive = int(self.config.getProperty(ConfigConst.MQTT_CLOUD_SECTION,ConfigConst.KEEP_ALIVE_KEY)) self.dataUtil = DataUtil.DataUtil() self.sensoData = SensorData.SensorData() #instance of SensorData class print(self.port)
def __init__(self, name): ''' _init_() is called during object initialization. ''' Thread.__init__(self) self.enableEmulator = True self.sensor = SensorData.SensorData(name, 0, 30) self.temp_delay = ConfigUtil.ConfigUtil( '../../../config/ConnectedDevicesConfig.props') '''
def __init__(self, name): ''' This Constructor is initialized in __init__; and setting enableEmulator flag to true. Assigning the sensor minimum temperature value = 0 and maximum value = 30. ''' Thread.__init__(self) self.enableEmulator = True self.sensor = SensorData.SensorData(name, 0, 30) self.temp_delay = ConfigUtil.ConfigUtil( '../../../config/ConnectedDevicesConfig.props')
def __init__(self): super(module02,self).__init__() # Turn Emulator ON self.enableEmulator = True # Obtain Sensor data from file # Initialize object of sensor data self.sensor = SensorData.SensorData() # Define alert parameter and rate of obtaining data self.alertDiff = 5 self.rateInSec = 5
def __init__(self, name): ''' _init_() is called during object initialization. ''' Thread.__init__(self) self.enableEmulator = True # flag to enable emulator function self.sensor = SensorData.SensorData(name, 0, 30) # creating a sensor data object self.temp = ConfigUtil.ConfigUtil( '../../../config/ConnectedDevicesConfig.props') self.temp_emul = TempActuatorEmulator.TempActuatorEmulator() '''
def __init__(self): ''' Constructor ''' #Calling thread constructor threading.Thread.__init__(self) #Creating a SensorData instance and setting it's name self.sensor_data = SensorData.SensorData() self.sensor_data.setName("I2CBus Sensor Data") #SMBus instance self.i2cReader = smbus.SMBus(1) pass
def __init__(self, name): ''' This thread generates a random float variable(Current Value). If the current value is greater than given threshold notification is generated. Creating a sensor data object. This Constructor is initialized in __init__; and setting enableEmulator flag to true. Assigning the sensor minimum temperature value = 0 and maximum value = 30. ''' Thread.__init__(self) self.enableAdaptor = True self.sensor = SensorData.SensorData(name, 0, 30) self.temp = ConfigUtil.ConfigUtil( '../../../config/ConnectedDevicesConfig.props') self.tempEmulator = TempActuatorEmulator.TempActuatorEmulator()
def testHandleSensorData(self): #Creating a temporary sensorData instance sensor = SensorData.SensorData() sensor.addValue(20) self.sensorDataManagerTest.SEND_EMAIL_NOTIFICATION = False #Calling the test handleSensorData function, should return true if worked properly self.assertEqual( True, self.sensorDataManagerTest.handleSensorData(sensor, "testMessage")) #Passing any other generic object will render a False result obj = object() self.assertEqual( False, self.sensorDataManagerTest.handleSensorData(obj, "no")) pass
def __init__(self): ''' Constructor ''' #Creating a SensorData instance and setting it's name self.sensor_data = SensorData.SensorData() self.sensor_data.setName("Temperature Sensor Data") #SenseHat instance to interact with the senseHAT self.sense = SenseHat() #clearing matrix self.sense.clear() #SensorDataManager instance self.sensorDataManager = SensorDataManager.SensorDataManager() pass
def __init__(self, alertdiff): ''' Constructor ''' threading.Thread.__init__(self) logging.basicConfig(level=logging.INFO , format=' %(asctime)s %(levelname)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S' ) self.enableEmulator = False self.sensorData = SensorData.SensorData() self.connector = SMTPClientConnector.SMTPClientConnector() self.timeInterval = int(self.configUtil.getProperty(self.configUtil.configConst.CONSTRAINED_DEVICE, self.configUtil.configConst.POLL_CYCLES_KEY)) self.alertDiff = alertdiff self.lowValue = 0 self.highValue = 30 self.curTemp = 0
def __init__(self, name): ''' Constructor Creates object of SensorData, ConfiUtil and TempActuatorEmulator This class is used to send notification through mail and initiate an action to control the temperature ''' Thread.__init__(self) self.enableEmulator = True # flag to enable emulator function self.sensor = SensorData.SensorData(name, 0, 30) # creating a sensor data object self.temp = ConfigUtil.ConfigUtil( '../../../config/ConnectedDevicesConfig.props') self.temp_emul = TempActuatorEmulator.TempActuatorEmulator() '''
def setUp(self): #JSON strings to test on self.sensorJSON = "{\"currentValue\": 10.0, \"totalCount\": 1, \"avgValue\": 10.0, \"maxValue\": 10.0, \"minValue\": 10.0, \"timestamp\": \"2020-02-23 16:29:41.32\", \"name\": \"Temperature\"}" self.actuatorJSON = "{\"command\": \"Decrease\", \"name\": \"Temperature Sensor Data\", \"value\": \"DOWNARROW\"}" #ActuatorData instance filled with data self.actuatorData = ActuatorData.ActuatorData() self.actuatorData.setName("TestActuator") self.actuatorData.setCommand("TestCommand") self.actuatorData.setValue(0.0) #SensorData instance filled with data self.sensorData = SensorData.SensorData() self.sensorData.setName("TestSensor") self.sensorData.addValue(10) #Datautil Instance self.dataUtil = DataUtil.DataUtil() pass
def setUp(self): #Instance for persistenceUtil self.pUtil = PersistenceUtil.PersistenceUtil() self.pUtil.enableThreads = False #ActuatorData instance filled with data self.actuatorData = ActuatorData.ActuatorData() self.actuatorData.setName("TestActuator") self.actuatorData.setCommand("TestCommand") self.actuatorData.setValue("TestValue") #SensorData instance filled with data self.sensorData = SensorData.SensorData() self.sensorData.setName("TestSensor") self.sensorData.addValue(10) #Creating a variable to avoid running this pipeline in cloud if path.exists("config/ConnectedDevicesConfig.props"): self.pipelineAvoid = False else: self.pipelineAvoid = True
def run(self): count = 10 sensordata = SensorData.SensorData() while count > 0: #generate a random temperature in [0,30] temperature = random.uniform(0.0, 30.0) sensordata.addValue(temperature) # self.sendNotification() #check if the temperature is surpass the threshold if (abs(sensordata.getValue() - sensordata.getAvgValue()) >= self.threshold): logging.info('\n Current temp exceeds average by > ' + str(self.threshold) + '. Triggering alert...') smtpClientConnector = SmtpClientConnector.SmtpClientConnector() smtpClientConnector.publishMessage("Excessive Temp", sensordata) count = count - 1 time.sleep(30)
def __init__(self): ''' this is a default constructor ''' #Initializing my variables and the instances self.dataUtil = DataUtil.DataUtil() self.sensorData = SensorData.SensorData() self.actuatorData = ActuatorData.ActuatorData() self.mqttBrokerAddr = 'mqtt.eclipse.org' self.port = 1883 self.keepAlive = 65 self.connectedFlag = False self.topic = "Raspi/Temperature" self.mqttClient = mqtt.Client("DeviceClient") self.mqttClient.on_connect = self.on_connect self.mqttClient.on_disconnect = self.on_disconnect self.mqttClient.connect(self.mqttBrokerAddr, self.port, self.keepAlive)
def toSensorDataFromJson(self, jsonStr) -> SensorData.SensorData: ''' Convert from JSON to SensorData instance ''' #Logging and writing to file self.writeSensorDataToFile("Recieved sensorData JSON" + str(jsonStr)) #Loading the jsonString jsonData = json.loads(jsonStr) #Creating a sensorData instance sensorData = SensorData.SensorData() #Adding values to the sensorData Instance sensorData.currentValue = jsonData['currentValue'] sensorData.totalCount = jsonData['totalCount'] sensorData.avgValue = jsonData['avgValue'] sensorData.maxValue = jsonData['maxValue'] sensorData.minValue = jsonData['minValue'] sensorData.timestamp = jsonData['timestamp'] sensorData.name = jsonData['name'] return sensorData
def setUp(self): #CoAPClient self.coAPTest = CoAPClientConnector() #SenseHatUpdater self.senseHatUpdater = SenseHatUpdater() #Mqtt self.mqttTest = MqttClientConnector() #DeviceDataManager self.manager = DeviceDataManager() #SerialCommunicator self.ser = SerialCommunicator(baud=152000, looplimit=1) #HeartRateTask self.hr = HeartRateTask(self.coAPTest, looplimit=1) #HeartRateTask self.spo = SpO2Task(self.coAPTest, looplimit=1) #CPUUtil Task self.cpu = Cpu(self.coAPTest, looplimit=1) #MemUtil Task self.mem = Mem(self.coAPTest, looplimit=1) #Getting an asyncio event loop self.loop = asyncio.get_event_loop() #SensorData object self.sensorData = SensorData.SensorData() self.sensorData.addValue(10) self.sensorData.setName("TESTNAME") #ActuatorData object self.actuatorData = ActuatorData.ActuatorData() self.actuatorData.setCommand("TESTCOMMAND") self.actuatorData.setValue("TESTVALUE") self.actuatorData.setName("TESTNAME")
class TempSensorEmulator(threading.Thread): rateInSec = DEFAULT_FATE_IN_SEC connector = SmtpClientConnector.SmtpClientConnector() sensorData = SensorData.SensorData() alertDiff = 5 enableEmulator = False lowVal = 0 highVal = 30 curTemp = 0 isPrevTempSet = True #Constructor def __init__(self): super(TempSensorEmulator, self).__init__() #set enableEmulator def setEnableEmulatorFlag(self, flag): self.enableEmulator = flag def run(self): while True: if self.enableEmulator: self.curTemp = uniform(float(self.lowVal), float( self.highVal)) #get a emulated temperature self.sensorData.addValue( self.curTemp) #add current temperature to SensorData class print('\n-------------------+-') print('New sensor readings:') print(' ' + str(self.sensorData)) if self.isPrevTempSet == False: #skip if this is the first temperature self.prevTemp = self.curTemp self.isPrevTempSet = True elif ( abs(self.curTemp - self.sensorData.getAvgValue()) >= self.alertDiff ): #if the current temperature is larger or less than the average temperature more than the alert value, publish the message print('\n Current temp exceeds average by > ' + str(self.alertDiff) + '. Triggeringalert...') self.connector.publishMessage( 'Exceptional sensor data [test]', str(self.sensorData)) sleep(self.rateInSec)