def handleActuatorData(self,SensorData): self.actuator.max_value = SensorData.max_value self.actuator.min_value = SensorData.min_value self.actuator.readings_number = SensorData.readings_number self.actuator.value = SensorData.getCurrentValue() self.actuator.avgTemp = (SensorData.total_value / SensorData.readings_number) self.actuator.total_value = SensorData.total_value self.actuator.setName(SensorData.getName()) self.actuatorOP.updateActuator(self.actuator) return True
def handleActuatorData(self, SensorData): self.actuator.max_value = SensorData.max_value self.actuator.min_value = SensorData.min_value self.actuator.readings_number = SensorData.readings_number self.actuator.value = SensorData.getCurrentValue() self.actuator.avgTemp = (SensorData.total_value / SensorData.readings_number) self.actuator.total_value = SensorData.total_value self.actuator.setName(SensorData.getName()) self.actuatorOP.updateActuator(self.actuator) #print("Value check - " + str(self.actuator.value)) #self.sendNotification() return True
def toJsonFromSensorData(self, sensorData: SensorData.SensorData) -> str: ''' Convert from SensorData instance to JSON ''' #Converting the sensorData to a dictionary jsonData = { "currentValue": sensorData.getCurrentValue(), "totalCount": sensorData.getCount(), "avgValue": sensorData.getAverageValue(), "maxValue": sensorData.getMinValue(), "minValue": sensorData.getMaxValue(), "timestamp": sensorData.timestamp, "name": sensorData.getName() } #dumping the json data and returning it jsonStr = json.dumps(jsonData) #Logging and writing to file self.writeSensorDataToFile("Creating JSON for sensorData" + jsonStr) return jsonStr
class Module03Test(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): self.config = ConfigUtil() self.config.loadConfig('../../../config/ConnectedDevicesConfig.props') self.tempsensor = TempSensorAdaptorTask() self.sensordata = SensorData() self.actuatordata = ActuatorData() self.sensordata.addValue(10) self.sensordata.addValue(15) self.sensordata.addValue(20) self.sensordata.addValue(25) self.sensordata.setName('Temperature') self.actuatordata.setCommand('Increasing') self.actuatordata.setName('SenseHat') self.sdmanager = SensorDataManager() """ 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): pass """ Place your comments describing the test here. """ def testloadConfig(self): self.assertTrue( self.config.loadConfig( '../../../config/ConnectedDevicesConfig.props')) def testhasConfigData(self): self.assertTrue(self.config.hasConfigData()) def testgetValue(self): self.assertEqual(self.config.getValue("smtp.cloud", "port"), '465') def testgetSensorData(self): assert self.tempsensor.getTemperature( ) > 0 and self.tempsensor.getTemperature() < 30 def testgetAverageValue(self): assert self.sensordata.getAverageValue( ) > 0 and self.sensordata.getAverageValue() < 30 def testgetCount(self): self.assertEqual(self.sensordata.getCount(), 4) def testgetCurrentValue(self): assert self.sensordata.getCurrentValue( ) > 0 and self.sensordata.getCurrentValue() < 30 def testMinValue(self): assert self.sensordata.getMinValue( ) >= 0 and self.sensordata.getMinValue() < 30 def testMaxValue(self): assert self.sensordata.getMaxValue( ) > 0 and self.sensordata.getMaxValue() < 30 def testName(self): self.assertEqual(self.sensordata.getName(), 'Temperature') def testgetCommand(self): self.assertEqual(self.actuatordata.getCommand(), 'Increasing') def testName2(self): self.assertEqual(self.actuatordata.getName(), 'SenseHat') def testhandleSenseData(self): assert self.sdmanager.handleSensorData(self.sensordata) is not None
class SensorDataTest(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 sensorData self.sensorData = SensorData() 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.sensorData = None """ This test tests the addValue() method of SensorData module. It checks whether the values are being correctly stored by getting the current value, the average value and the count """ def testAddValue(self): logging.info('Running testAddValue()') #add any valid value self.sensorData.addValue(120) #the current value should represent the recently added value self.assertEqual(120, self.sensorData.getCurrentValue()) #as it is the first test, the count should return 1 self.assertEqual(1, self.sensorData.getCount()) #add any valid value self.sensorData.addValue(140) #average value should return (120 + 140)/2 = 260/2 = 130 self.assertEqual(130, self.sensorData.getAverageValue()) #add an invalid non-numeric value self.sensorData.addValue('blahblahblah') #current value should still remain 140 self.assertEqual(140, self.sensorData.getCurrentValue()) pass """ This test tests the getAverageValue() method of SensorData module. It checks whether the average is being accurately calculated by the method """ def testGetAverageValue(self): #add some valid values self.sensorData.addValue(60) self.sensorData.addValue(90) self.sensorData.addValue(180) #get the average average = self.sensorData.getAverageValue() #check if average value = total value / count self.assertEqual( average, self.sensorData.totalValue / self.sensorData.getCount()) #add invalid values and check if average remains the same self.sensorData.addValue(None) #check if average value = total value / count self.assertEqual( average, self.sensorData.totalValue / self.sensorData.getCount()) """ This test tests the getCount() method of SensorData module. It checks whether the count is incremented properly """ def testGetCount(self): #add some valid values self.sensorData.addValue(30) self.sensorData.addValue(39) self.sensorData.addValue(300) #check if count is 3 self.assertEqual(3, self.sensorData.getCount()) #add an invalid value self.sensorData.addValue('baah') #check if count is still 3 self.assertEqual(3, self.sensorData.getCount()) """ This test tests the getCurrentValue() method of SensorData module. It checks whether the current value is updated properly """ def testGetCurrentValue(self): #add some valid value self.sensorData.addValue(30) #check if current value is 30 self.assertEqual(30, self.sensorData.getCurrentValue()) #add an invalid value self.sensorData.addValue('baah') #check if count is still 30 self.assertEqual(30, self.sensorData.getCurrentValue()) #add another valid value self.sensorData.addValue('60') #check if current value is 30 self.assertEqual(60, self.sensorData.getCurrentValue()) """ This test tests the getMaxValue() method of SensorData module. It checks whether the max value is updated properly """ def getMaxValue(self): #when there are no values it should return none self.assertEqual(None, self.sensorData.getMaxValue()) #add a valid value self.sensorData.addValue('60') #check if max value updates to 60 self.assertEqual(60, self.sensorData.getMaxValue()) #add a large valid value self.sensorData.addValue('90') #check if max value updates to 60 self.assertEqual(90, self.sensorData.getMaxValue()) """ This test tests the getMinValue() method of SensorData module. It checks whether the min value is updated properly """ def getMinValue(self): #when there are no values it should return none self.assertEqual(None, self.sensorData.getMinValue()) #add a valid value self.sensorData.addValue('60') #check if max value updates to 60 self.assertEqual(60, self.sensorData.getMinValue()) #add a small valid value self.sensorData.addValue('-30') #check if min value updates to -30 self.assertEqual(-30, self.sensorData.getMinValue()) """ This test tests the getName() method of SensorData module. It checks whether the the name is being returned properly """ def testGetName(self): #when name is not set, it has been initialized to 'Not Set', check if 'Not Set' self.assertEqual('Not Set', self.sensorData.getName()) #change name self.sensorData.setName("Pallak's Sensor") #check whether the name is now "Pallak's Sensor" self.assertEqual("Pallak's Sensor", self.sensorData.getName()) #check for none self.sensorData.setName(None) #it should return 'Not Set' self.assertEqual('Not Set', self.sensorData.getName()) """ This test tests the setName() method of SensorData module. It checks whether the the name is being updated properly """ def testSetName(self): #change name self.sensorData.setName("Temperature Sensor") #check whether the name is now "Temperature Sensor" self.assertEqual("Temperature Sensor", self.sensorData.getName()) #change to none self.sensorData.setName(None) #check whether the name is now be 'Not Set' self.assertEqual("Not Set", self.sensorData.getName())