def setUp(self): #instantiate the classes required self.sensorDataManager = SensorDataManager() self.hI2CSensorAdaptorTask = HI2CSensorAdaptorTask() self.humiditySensorAdaptorTask = HumiditySensorAdaptorTask() self.multiActuatorAdaptor = MultiActuatorAdaptor() self.multiSensorAdaptor = MultiSensorAdaptor() self.sense = sense_hat.SenseHat()
def setUp(self): self.sd = SensorData() self.sd.addValue(11) self.sd.setName("Test") self.sdm = SensorDataManager() self.sdm.actuator.addValue(11) self.actuator = ActuatorData() self.actuator.addValue(2) self.actuator.setName("Test") self.tsa = MultiSensorAdaptor() self.taa = MultiActuatorAdaptor() self.tat = HumiditySensorAdaptorTask() self.tat.sensorData = SensorData() self.shla = SenseHatLedActivator()
def adaptor(self): result= TempSensorAdaptorTask() result.run() # time.sleep(1) # print("helloadaptor") time.sleep(1) humidity = HI2CSensorAdaptorTask() humidity.run() time.sleep(1) library_humidity = HumiditySensorAdaptorTask() library_humidity.run() diff=humidity.getteravg() - library_humidity.getteravg() diff=abs(diff) self.setterdiff(diff) formatstring="Temperature difference from lib and i2c is: "+str(diff) FORMAT = " %(message)s" logging.basicConfig(level=logging.INFO,format=FORMAT) logging.info(formatstring)
class MultiSensorAdaptor : #Default Constructor def __init__(self): self.sreader = HumiditySensorAdaptorTask() self.ireader = I2CSensorAdaptorTask() #Fetch current readings from the sensor def getSensorData(self): i = 0 self.ireader.objectLoader() self.sreader.objectLoader() while(i<5): self.sreader.readSensorValue() sleep(0.0001) self.ireader.displayHumidityData() self.sreader.pushData() self.ireader.pushData() i+=1 sleep(5) return True
def __init__(self): #constructor of TempSensorAdaptor ''' Starting the threads for all the three classes of sensors ''' while True: temp = TempSensorAdaptorTask( "Temp Sensor" ) #object of class TempSensorAdaptorTask is created humidity1 = HumiditySensorAdaptorTask( "Humidity sensor" ) #object of class HumiditySensorAdaptorTask is created humidity2 = HI2CSensorAdaptorTask( "I2C Sensor" ) #object of class HI2CSensorAdaptorTask is created temp.start() #Thread is started temp.join() #Thread is put on wait until it finishes humidity1.start() #Thread is started humidity1.join() #Thread is put on wait until it finishes humidity2.start() #Thread is started humidity2.join() #Thread is put on wait until it finishes
class Module04Test(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 the classes required self.sensorDataManager = SensorDataManager() self.hI2CSensorAdaptorTask = HI2CSensorAdaptorTask() self.humiditySensorAdaptorTask = HumiditySensorAdaptorTask() self.multiActuatorAdaptor = MultiActuatorAdaptor() self.multiSensorAdaptor = MultiSensorAdaptor() self.sense = sense_hat.SenseHat() """ 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 as none to release the resources they're holding self.sensorDataManager = None self.hI2CSensorAdaptorTask = None self.multiActuatorAdaptor = None self.multiSensorAdaptorTask = None self.multiSensorAdaptor = None self.sense = None pass """ This method tests the handleSensorData() of SensorDataManager module. It tests whether the sensor data list passed to the method triggers the correct actuator command. """ def testGetHandleSensorData(self): #log the test logging.info( "\nTesting the handleSensorData() method of SensorDataManager") #initialize sensor data to represent reading from I2C sensorDataI2C = SensorData() #add a humidity value sensorDataI2C.addValue(10) #set the name for i2c sensorDataI2C.setName("i2c humidity") #initialize sensor data to represent reading from SenseHAT API sensorDataAPI = SensorData() #add a humidity value sensorDataAPI.addValue(9) #set the name for i2c sensorDataAPI.setName("sense_hat API humidity") #add them to a list sensorDataTest = [] sensorDataTest.append(sensorDataI2C) sensorDataTest.append(sensorDataAPI) #get the actuatorDataList returned from the sensor data manager actuatorDataList = self.sensorDataManager.handleSensorData( sensorDataTest) #check if actuatorData at both indexes are of type ActuatorData self.assertIsInstance(actuatorDataList[0], ActuatorData) self.assertIsInstance(actuatorDataList[1], ActuatorData) #test the actuatorData value at the first index, which should simply be the first value (10) self.assertEqual(10, actuatorDataList[0].getValue()) #test the actuatorData command at the first index which should be DISPLAY I2C Humidity self.assertEqual(actuatorDataList[0].getCommand(), "DISPLAY I2C Humidity") #test the actuatorData name at the second index which should be I2C Humidity self.assertEqual(actuatorDataList[0].getName(), "I2C Humidity") #test the value at the second index, which should simply be the first value (9) self.assertEqual(9, actuatorDataList[1].getValue()) #test the actuatorData command at the second index which should be DISPLAY I2C Humidity self.assertEqual(actuatorDataList[1].getCommand(), "DISPLAY SENSE HAT API Humidity") #test the actuatorData name at the second index which should be I2C Humidity self.assertEqual(actuatorDataList[1].getName(), "SENSE HAT API Humidity") #initialize a random sensorData object and add it to the list and call the manager again sensorData = SensorData() #set invalid sensor data name sensorData.setName("blah blah") #append to list sensorDataTest.append(sensorData) #get the actuatorDataList returned from the manager should still have a length of 2 because we only added an invalid type actuatorDataList = self.sensorDataManager.handleSensorData( sensorDataTest) #check if length of actuatorDataList is 2 self.assertEqual(len(actuatorDataList), 2) #clear the list and only add invalid sensor data sensorDataTest = [] sensorDataTest.append(sensorData) #get the actuatorDataList returned from the manager should be None actuatorDataList = self.sensorDataManager.handleSensorData( sensorDataTest) #check if it is none self.assertEqual(actuatorDataList, None) """ This tests the sendNotification() method of the SensorDataManager, it simply whether the notification is being sent or not. This has been shown in documentation using screenshot of the email """ def testSendNotification(self): #log the test logging.info( "\nTesting the sendNotification() method of SensorDataManager") #if the config file is loaded: while testing on system if self.sensorDataManager.smtpClientConnector.config.configFileLoaded == True: #returns true, notification is being sent self.assertEqual( True, self.sensorDataManager.sendNotification( "Hello", "This is a test")) pass """ This tests the updateActuator() method of the MultiActuatorAdaptor, it checks whether the actuator is updated (by returning an actuatorData reference) when the trigger is valid and when the trigger is invalid (NOT A VALID TRIGGER) """ 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 """ This tests the getHumidityData() method of the HI2CSensorAdaptorTask, it checks whether the returned value and the sense_hat api value have difference < 1 """ def testGetHumidityDataHI12C(self): #log the test logging.info( "\nTesting the getHumidityData() method of HI2CSensorAdaptorTask") #get the humidity data sensorData = self.hI2CSensorAdaptorTask.getHumidityData() #sleep for little time sleep(1) #get the absolute difference between sensorData from i2c and the sense hat api self.assertTrue( abs(sensorData.getCurrentValue() - self.sense.get_humidity()) <= 1.0, "Value" + str(sensorData.getCurrentValue())) """ This tests the getHumidityData() method of the HI2CSensorAdaptorTask, it checks whether the returned value and the sense_hat api value have difference < 1 """ def testGetHumidityDataAPI(self): #log the test logging.info( "\nTesting the getHumidityData() method of HumiditySensorAdaptorTask" ) #get the humidity data sensorData = self.humiditySensorAdaptorTask.getHumidityData() #sleep for little time sleep(0.5) #get the absolute difference between sensorData from i2c and the sense hat api self.assertTrue( abs(sensorData.getCurrentValue() - self.sense.get_humidity()) <= 1.0) """ Tests the getSensorData() method of both the HI2CSensorAdaptorTask and HumiditySensorAdaptorTask """ def testGetSensorData(self): #get the sensor data reference from the getSensorData method sensorDataHI2C = self.hI2CSensorAdaptorTask.getSensorData() sensorDataHumidity = self.humiditySensorAdaptorTask.getSensorData() #check if instance of SensorData self.assertIsInstance(sensorDataHI2C, SensorData) self.assertIsInstance(sensorDataHumidity, SensorData) """ This tests the whether the difference between the sensor values detected from , it checks whether the i2c bus and sense hat API have a difference of <= 1.0 """ def testComparisonI2CSenseHat(self): #log the test logging.info( "\nTesting the comparison between readings of I2C Bus and API") #get the sensor data references from both sensorDataHumidity = self.humiditySensorAdaptorTask.getSensorData() #sleep for little time sleep(0.5) sensorDataHI2C = self.hI2CSensorAdaptorTask.getSensorData() #check for difference self.assertTrue( abs(sensorDataHumidity.getCurrentValue() - sensorDataHI2C.getCurrentValue()) <= 1.0, "Value more than 1") """ This test checks the run() method of the MultiSensorAdaptor """ def testRun(self): #log the test logging.info("\nTesting the run() method of MultiSensorAdaptor()") #enable the fetcher self.multiSensorAdaptor.hI2CSensorAdaptorTask.enableFetcher = True self.multiSensorAdaptor.humiditySensorAdaptorTask.enableFetcher = True #change numReadings to a small finite value to check self.multiSensorAdaptor.numReadings = 1 #change sleep time (rateInSec) to a small amount self.multiSensorAdaptor.rateInSec = 1 #run when numReadings > 0 and adaptor is enabled self.assertEqual(True, self.multiSensorAdaptor.run()) #change numReadings to 0 self.multiSensorAdaptor.numReadings = 0 #run when numReadings = 0 and fetcher is enabled, should return false because run will not fetch self.assertEqual(False, self.multiSensorAdaptor.run())
import logging from time import sleep from labs.module04.TempSensorAdaptorTask import TempSensorAdaptorTask from labs.module04.HumiditySensorAdaptorTask import HumiditySensorAdaptorTask from labs.module04.HI2CSensorAdaptorTask import HI2CSensorAdaptorTask from _datetime import datetime logging.getLogger().setLevel(logging.INFO) #logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s',level=logging.INFO) logging.info("Starting temperature sensor adaptor daemon thread...") tempsensoradaptor = TempSensorAdaptorTask() tempsensoradaptor.daemon = False tempsensoradaptor.enableTempSensor = False tempsensoradaptor.start() humiditysensoradaptor = HumiditySensorAdaptorTask() humiditysensoradaptor.enableHumidSensor = True humiditysensoradaptor.start() hi2csensoradaptor = HI2CSensorAdaptorTask() hi2csensoradaptor.enableHI2CSensor = True hi2csensoradaptor.start() while (True): sleep(1) sh = humiditysensoradaptor.curHumid i2c = hi2csensoradaptor.RH print(str(datetime.now()) + " SenseHat API Humidity: " + str(sh)) print(str(datetime.now()) + " I2C Direct Humidity: " + str(i2c)) print( str(datetime.now()) + " Difference: " + str((abs(sh - i2c)) / sh * 100) + "%")
def __init__(self): self.sreader = HumiditySensorAdaptorTask() self.ireader = I2CSensorAdaptorTask()
class MultiSensorAdaptor(threading.Thread): ''' classdocs ''' #initialize H12CAdaptorTask, HumiditySensorAdaptorTask and SensorDataManager hI2CSensorAdaptorTask = HI2CSensorAdaptorTask() humiditySensorAdaptorTask = HumiditySensorAdaptorTask() sensorDataManager = SensorDataManager() #the constructor used to set the readings and the frequency of readings if provided, else defaults to 10 and 5 respectively def __init__(self, numReadings=10, rateInSec=4): ''' Constructor ''' threading.Thread.__init__(self) #set the number of readings you want to get self.numReadings = numReadings #set the rate at which you want to get the readings self.rateInSec = rateInSec #method for running the thread def run(self): #log the initial message logging.info("Starting sensor reading daemon threads") logging.info("Initializing I2C bus and enabling I2C addresses") #enable the fetchers for both the tasks self.hI2CSensorAdaptorTask.enableFetcher = True self.humiditySensorAdaptorTask.enableFetcher = True #data is not false doesn't run if 0 readings set: if self.numReadings == 0: return False #this try-except block checks whether a keyboard interrupt exception occurs and clears the sensehat if yes try: #run the loop as many times as indicated in the numReadings variable while self.numReadings > 0: #initialize the sensor data list that will store both sensorData from I2C bus and senseHat API sensorDataHumidity = [] #check if fetcher is enabled for hi2cSensorAdaptorTask if self.hI2CSensorAdaptorTask.enableFetcher: #store the sensorData value from the current reading sensorDataHumidity.append( self.hI2CSensorAdaptorTask.getHumidityData()) #sleep for few seconds between getting these two values sleep(0.05) #check if fetcher is enabled for humiditySensorAdaptorTask if self.humiditySensorAdaptorTask.enableFetcher: #store the sensor data value from the current reading to the list sensorDataHumidity.append( self.humiditySensorAdaptorTask.getHumidityData()) #if there was valid sensorData in sensorDataHumidity if len(sensorDataHumidity) != 0: #handle the sensor data using the manager self.sensorDataManager.handleSensorData(sensorDataHumidity) else: #return false return False #log the difference logData = str(datetime.now()) + ",INFO:Difference: " + str( abs(sensorDataHumidity[0].getCurrentValue() - sensorDataHumidity[1].getCurrentValue())) logging.info(logData) #decrement the number of readings self.numReadings -= 1 #sleep for few seconds sleep(self.rateInSec) #if keyboard interrupt occurs except (KeyboardInterrupt): #clear the sensorData self.sensorDataManager.multiActuatorAdaptor.sense.clear() return True
class Module04Test(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.sd = SensorData() self.sd.addValue(11) self.sd.setName("Test") self.sdm = SensorDataManager() self.sdm.actuator.addValue(11) self.actuator = ActuatorData() self.actuator.addValue(2) self.actuator.setName("Test") self.tsa = MultiSensorAdaptor() self.taa = MultiActuatorAdaptor() self.tat = HumiditySensorAdaptorTask() self.tat.sensorData = SensorData() self.shla = SenseHatLedActivator() """ 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. """ '@Test' def testSendNotification(self): self.assertTrue(self.sdm.sendNotification(), "Notification Unsucessful") pass '@Test' def testhadleSensorData(self): self.assertTrue(self.sdm.hadleSensorData(self.sd), "Sensor handle data method not working") '@Test' def testgetSensorData(self): self.assertTrue(self.tsa.getSensorData(), "Issue in temperature adaptor") '@Test' def testreadSensorValueMin(self): self.assertGreaterEqual(self.tat.readSensorValue(), 0.0, 'sensor value coming less than 0') self.assertGreaterEqual(100, self.tat.readSensorValue(), 'sensor value coming more than 100') '@Test' def testupdateActuator(self): self.assertTrue(self.taa.updateActuator(self.actuator), "Actuator update failed") '@Test' def testupdateLed(self): self.assertTrue(self.shla.updateLed("Test Message"), "Led update failed") '@Test' def testreadSensorValuePushNotification(self): self.tat.sensorData = SensorData() self.tat.sensorData.addValue(12) self.tat.sensorData.setName("Test") self.tat.sdm = SensorDataManager() self.assertTrue(self.tat.pushData(), "Message not getting pushed to Sensor Data Manager")