class MultiActuatorAdaptorTask(object): def __init__(self): self.ActData = ActData() self.dUtil = DataUtil() #Method for checking ActutatorData initialization def updateActuator(self, act_type, data): if act_type == "ActFromGD": if (data == "1"): self.stateVal = "ON" elif (data == "0"): self.stateVal = "OFF" self.ActData.setName("LED ACTUATOR") self.ActData.setCommand(data) self.ActData.setValue(data) self.ActData.setStateData(self.stateVal) self.ActData.updateData(self.ActData) print("Actuator Name: " + self.ActData.getName()) # print("COMMAND: " + self.ActData.getCommand()) jsonData = self.dUtil.toJsonFromActuatorData(self.ActData) logging.info("Actuator JSON: " + jsonData) return True else: print("Failed to send Actuation Signal") return False
def testUpdateActuator(self): #create an invalid actuator trigger actuatorData = ActuatorData() actuatorData.setCommand("NOT A VALID TRIGGER") #add a valid value actuatorData.setValue(self.arrowBlueDec) #updateActuator should return a false self.assertEqual(False, self.tempActuatorAdaptor.updateActuator(actuatorData)) #create a valid actuator trigger actuatorData = ActuatorData() actuatorData.setCommand("INCREASE TEMP") actuatorData.setValue(self.arrowRedInc) #updateActuator should return a True self.assertEqual(True, self.tempActuatorAdaptor.updateActuator(actuatorData)) #sending a none should throw an exception, where when caught, returns a false self.assertEqual(False, self.tempActuatorAdaptor.updateActuator(None)) pass
class TempActuatorEmulator(): def __init__(self): self.thisActuatorData = ActuatorData() self.thisActuatorData.setValue(20) self.thisActuatorData.setCommand('IDEAL') def processMessage(self, actuatorData): if(self.thisActuatorData!=actuatorData): if (actuatorData.getCommand() == 'INCREASE'): print('Increasing temperature') GPIO.set_rotation(actuatorData.getValue()) #GPIO stands for general purpose input output #we set rotation and get values elif (actuatorData.getCommand() == 'DECREASE'): print('Decreasing temperature') GPIO.set_rotation(actuatorData.getValue()) else: print('maintaining the ideal temperature') GPIO.set_rotation(actuatorData.getValue()) self.thisActuatorData = actuatorData
def testActuatorDataToJson(self): #instantiate two actuatorData instances actuatorData1 = ActuatorData() actuatorData2 = ActuatorData() #ceate two actuatorData instances and check if their jsonStr are equal actuatorData1.setName("Temp Actuator") actuatorData2.setName("Temp Actuator") actuatorData1.setCommand("INCREASE") actuatorData2.setCommand("INCREASE") actuatorData1.setValue("UP") actuatorData2.setValue("UP") #get their JSON strings jsonStr1 = self.dataUtil.toJsonFromActuatorData(actuatorData1) jsonStr2 = self.dataUtil.toJsonFromActuatorData(actuatorData2) #check for equality self.assertEqual(jsonStr1, jsonStr2) #pass a non ActuatorData type to write jsonStr1 = self.dataUtil.toJsonFromActuatorData("Hello") #get the actuatorData back from the string passed actuatorData = self.dataUtil.toActuatorDataFromJson(jsonStr1) #the values should be "Not Set: self.assertEqual("Not Set", actuatorData.getCommand()) self.assertEqual("Not Set", actuatorData.getName()) self.assertEqual("Not Set", actuatorData.getValue()) pass
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)
def testJsonToActuatorData(self): #Create actuatorData actuatorData = ActuatorData() #Set some command, name and value actuatorData.setName("Temp Actuator") actuatorData.setCommand("INCREASE") actuatorData.setValue("UP") #convert to JSON jsonStr = self.dataUtil.toJsonFromActuatorData(actuatorData) #convert back to ActuatorData actuatorDataTest = self.dataUtil.toActuatorDataFromJson(jsonStr) #test if their variables are equal self.assertEqual("Temp Actuator", actuatorDataTest.getName()) self.assertEqual("INCREASE", actuatorDataTest.getCommand()) self.assertEqual("UP", actuatorDataTest.getValue()) """ /* * Set the variable to a double value, check if the conversion to JsonStr and back * to ActuatorData value remains of double type */ """ #Set some command, name and double value actuatorData.setName("Temp Actuator") actuatorData.setCommand("INCREASE") actuatorData.setValue(6.0) #convert to JSON jsonStr = self.dataUtil.toJsonFromActuatorData(actuatorData) #convert back to ActuatorData actuatorDataTest = self.dataUtil.toActuatorDataFromJson(jsonStr) #test if their variables are equal self.assertEqual("Temp Actuator", actuatorDataTest.getName()) self.assertEqual("INCREASE", actuatorDataTest.getCommand()) self.assertEqual(6.0, actuatorDataTest.getValue())
class TempActuatorEmulator(): def __init__(self): self.thisActuatorData = ActuatorData() self.thisActuatorData.setValue(20) self.thisActuatorData.setCommand('IDEAL') ''' Processes the data from adaptor sets the GPIO lights to rotate corresponding to the temperature value from the adaptor ''' def processMessage(self, actuatorData): # led = SenseHatLedActivator() # led.setEnableLedFlag(True) # led.start() if (self.thisActuatorData != actuatorData): #If the command is to increase, display the same and command the GPIO to rotate #according to the temperature value if (actuatorData.getCommand() == 'INCREASE'): print('Increasing temperature') GPIO.set_rotation(actuatorData.getValue()) # led.run() #If the command is to decrease, display the same and command the GPIO to rotate #according to the temperature value elif (actuatorData.getCommand() == 'DECREASE'): print('Decreasing temperature') GPIO.set_rotation(actuatorData.getValue()) #If no temperature change display that the temperature is at the ideal state else: print('maintaining the ideal temperature') GPIO.set_rotation(actuatorData.getValue()) self.thisActuatorData = actuatorData
def testUpdateActuator(self): #log the test logging.info( "\nTesting the updateActuator() method of MultiActuatorAdaptor") #create an invalid actuator trigger actuatorData = ActuatorData() actuatorData.setCommand("NOT A VALID TRIGGER") #add a valid value actuatorData.setValue(90) #add it to list of actuatorDataTest actuatorDataTest = [] actuatorDataTest.append(actuatorData) #updateActuator should return a false self.assertEqual( False, self.multiActuatorAdaptor.updateActuator(actuatorDataTest)) #create a valid actuator trigger actuatorData = ActuatorData() actuatorData.setCommand("DISPLAY SENSE HAT API Humidity") actuatorData.setValue(12) actuatorDataTest = [] actuatorDataTest.append(actuatorData) #updateActuator should return a True self.assertEqual( True, self.multiActuatorAdaptor.updateActuator(actuatorDataTest)) #sending a none should throw an exception, where when caught, returns a false self.assertEqual(False, self.multiActuatorAdaptor.updateActuator(None)) pass
class TempSensorAdaptor(threading.Thread): enableAdaptor = False actuatorData = None tempActuatorEmulator = None connector = SmtpClientConnector.SmtpClientConnector() sensorData = SensorData.SensorData() alertDiff = 5 rateInSec = DEFAULT_FATE_IN_SEC enableEmulator = False lowVal = 0 highVal = 30 curTemp = 0 isPrevTempSet = True #Constructor def __init__(self): super(TempSensorAdaptor, self).__init__() self.config = ConfigUtil.ConfigUtil('') self.config.loadConfig() self.actuatorData = ActuatorData() self.tempActuatorEmulator = TempActuatorEmulator() #set enableEmulator def setEnableAdaptorFlag(self, flag): self.enableAdaptor = flag def run(self): nominalTemp = int( self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.NOMINAL_TEMP_KEY) ) #get the value of nominal temperature from ConfigConst class 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 else: if ( 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)) if ( abs(self.curTemp - nominalTemp) > self.alertDiff ): #if the current temperature is larger or less than the nominal temperature more than the alert level, run the actuator to adjust the temperature print('\n+++++++++++++++++++') print('to the actuator:') val = self.curTemp - nominalTemp self.actuatorData.setValue(val) if (val > 0): print('val > 0') self.actuatorData.setCommand(1) else: print('val < 0') self.actuatorData.setCommand(0) self.tempActuatorEmulator.processMessage( self.actuatorData) sleep(self.rateInSec)
def handleSensorData(self, sensorDatas): #initialize the list for actuatorData actuatorDataList = [] #iterate through the sensorDatas list for sensorData in sensorDatas: #if the reading is directly from i2c bus if sensorData.getName() == "i2c humidity": #get the current sensor value sensorVal = sensorData.getCurrentValue() #instantiate ActuatorData actuatorData = ActuatorData() #the actuator should display the current humidity actuatorData.setCommand("DISPLAY I2C Humidity") #set the name of the actuator actuatorData.setName("I2C Humidity") #set the value to the current humidity value actuatorData.setValue(sensorVal) #send email notification consisting of the reading self.sendNotification(sensorData.loggingData, "Humidity Reading from I2C Bus") #append the actuator data to the list actuatorDataList.append(actuatorData) #if the reading is directly from sensehat api elif sensorData.getName() == "sense_hat API humidity": #get the current sensor value sensorVal = sensorData.getCurrentValue() #instantiate ActuatorData actuatorData = ActuatorData() #the actuator should display the current humidity actuatorData.setCommand("DISPLAY SENSE HAT API Humidity") #set the name of the actuator actuatorData.setName("SENSE HAT API Humidity") #set the value to the current humidity value actuatorData.setValue(sensorVal) #send email notification consisting of the reading self.sendNotification(sensorData.loggingData, "Humidity Reading from SenseHAT API") #append the actuator data to the list actuatorDataList.append(actuatorData) #if no valid sensor data reading if len(actuatorDataList) == 0: #return none return None #send the list to the updateActuator method of the multiActuatorAdaptor self.multiActuatorAdaptor.updateActuator(actuatorDataList) #return the list of actuatorData return actuatorDataList
class ActuatorDataTest(unittest.TestCase): """ Use this to setup your tests. This is where you may want to load configuration information (if needed), initialize class-scoped variables, create class-scoped instances of complex objects, initialize any requisite connections, etc. """ def setUp(self): #instantiate ActuatorData self.actuatorData = ActuatorData() pass """ Use this to tear down any allocated resources after your tests are complete. This is where you may want to release connections, zero out any long-term data, etc. """ def tearDown(self): #set the reference to the variables to none to release any resources self.actuatorData = None pass """ Tests the getCommand() method of ActuatorData module. Checks whether the command is updating appropriately, and doesn't break if the command is set to None """ def testGetCommand(self): #test the command when nothing has been set self.assertEqual('Not set', self.actuatorData.getCommand(), "Command was not set") #test the command when set to 'INCREASE TEMP' self.actuatorData.setCommand('INCREASE TEMP') self.assertEqual('INCREASE TEMP', self.actuatorData.getCommand(), "Command was set to INCREASE TEMP") #test the command is set to 'Not Set' when set to None self.actuatorData.setCommand(None) self.assertEqual('Not set', self.actuatorData.getCommand(), "Command was not set") pass """ Tests the setCommand() method of ActuatorData module. Checks whether the command is updating appropriately, and doesn't break if the name is set to None """ def testSetCommand(self): #test the command when set to None self.actuatorData.setCommand(None) self.assertEqual('Not set', self.actuatorData.getCommand(), "Command was not set") #test the command when set to 'INCREASE TEMP' self.actuatorData.setCommand('INCREASE TEMP') self.assertEqual('INCREASE TEMP', self.actuatorData.getCommand(), "Command was set to INCREASE TEMP") pass """ Tests the getValue() method of ActuatorData module. Checks whether the value is updating appropriately, and doesn't break if the name is set to None """ def testGetValue(self): #test the name when nothing has been set self.assertEqual('Not set', self.actuatorData.getValue(), "Name was not set") #test the name when set to None self.actuatorData.setValue(None) self.assertEqual('Not set', self.actuatorData.getValue(), "Name was not set") #test the name when set to redArrowInc self.actuatorData.setValue('My Pixel') self.assertEqual('My Pixel', self.actuatorData.getValue(), "Name was set to My Pixel") pass """ Tests the setValue() method of ActuatorData module. Checks whether the value is updating appropriately, and doesn't break if the name is set to None """ def testSetValue(self): #test the name when nothing has been set self.assertEqual('Not set', self.actuatorData.getValue(), "Name was not set") #test the name when set to None self.actuatorData.setValue(None) self.assertEqual('Not set', self.actuatorData.getValue(), "Name was not set") #test the name when set to redArrowInc self.actuatorData.setValue('My Pixel') self.assertEqual('My Pixel', self.actuatorData.getValue(), "Name was set to My Pixel") pass """ Tests the getName() method of ActuatorData module. Checks whether the name is updating appropriately, and doesn't break if the name is set to None """ def testGetName(self): #test the name when nothing has been set self.assertEqual('Not set', self.actuatorData.getName(), "Name was not set") #test the name when set to None self.actuatorData.setName(None) self.assertEqual('Not set', self.actuatorData.getName(), "Name was not set") #test the name when set to 'Temperature' self.actuatorData.setName('Temperature') self.assertEqual('Temperature', self.actuatorData.getName(), "Name was set to Temperature") """ Tests the setName() method of ActuatorData module. Checks whether the name is updating appropriately, and doesn't break if the name is set to None """ def testSetName(self): #test the name when set to None self.actuatorData.setName(None) self.assertEqual('Not set', self.actuatorData.getName(), "Name was not set") #test the command when set to 'INCREASE TEMP' self.actuatorData.setName('Temperature') self.assertEqual('Temperature', self.actuatorData.getName(), "Name was set to Temperature") pass