def setUp(self): #initialize the variables self.multiActuatorAdaptor = MultiActuatorAdaptor() self.multiSensorAdaptor = MultiSensorAdaptor() self.tempSensorAdaptorTask = TempSensorAdaptorTask() pass
def __init__(self): #constructor of TempSensorAdaptor temp=TempSensorAdaptorTask("Thread") #object of class TempSensorAdaptorTask is created ''' Starting the thread ''' temp.start() #temp.join()
class Module05Test(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.tempTask = TempSensorAdaptorTask() self.tempSensorData = SensorData() self.dataUtil = DataUtil() """ 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 testtoJsonFromSensorData(self): self.tempSensorData.addValue(self.tempTask.getTemperature()) self.jsonData = self.dataUtil.toJsonFromSensorData(self.tempSensorData) print(type(self.jsonData)) self.assertTrue(type(self.jsonData) == str) pass
class MultiSensorAdaptor(threading.Thread): ''' * Constructor function which sets MultiSensorAdaptor as a daemon thread ''' def __init__(self): threading.Thread.__init__(self) MultiSensorAdaptor.setDaemon(self, True) logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s', level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S') logging.info("started logging") ''' * Runnable function which starts two seperate threads for polling humidity data via SenseHat api & i2c bus ''' def run(self): self.temp_sensor_object = TempSensorAdaptorTask(20) self.temp_sensor_object.start() # Starting Threaded Class Object time.sleep(100) ''' * Standard getter function for temp_sensor_object Output: temp_sensor_object(SensorData) ''' def getSensorobj(self): return self.temp_sensor_object ''' * Standard getter function for humi_sensori2c_object Output: humi_sensori2c_object(SensorData) ''' def geti2cobject(self): return self.humi_sensori2c_object ''' * Standard getter function for humi_sensorAPI_object Output: humi_sensorAPI_object(SensorData) ''' def getAPIobject(self): return self.humi_sensorAPI_object
class MultiSensorAdaptor(): ''' classdocs ''' #initialize TempSensorAdaptorTask tempSensorAdaptorTask = TempSensorAdaptorTask() def __init__(self): ''' Constructor ''' #start the Dbms listener from PersistenceUtil self.pUtil = PersistenceUtil() self.pUtil.registerActuatorDataDbmsListener() #method for creating and running the thread def run(self): #log the initial message logging.info("Starting getTemperature thread()") #try the running thread try: #enable the temperature fetcher self.tempSensorAdaptorTask.enableFetcher = True #create the thread that calls the getTemperature() method of the tempSensorAdaptorTask threadTempSensorAdaptor = threading.Thread( target=self.tempSensorAdaptorTask.getTemperature()) #set the temp sensor adaptor daemon to true (enable main thread to exit when done) threadTempSensorAdaptor.daemon = True #if found error except: #return false return False #return true for running successfully return True
class MultiSensorAdaptor(threading.Thread): ''' classdocs ''' rateInSec = 10 enableTempSensor = False enableHumidSensor = False tempSensorData = SensorData() humidSensorData = SensorData() hi2cSensorData = SensorData() manager = SensorDataManager() tempsensor = TempSensorAdaptorTask() # humiditysensor = HumiditySensorAdaptorTask() # hi2csensor = HI2CSensorAdaptorTask() persistenceutil = PersistenceUtil() def __init__(self, rateInSec=10): threading.Thread.__init__(self) #self.sensorData = SensorData() self.rateInSec = rateInSec #self.time = self.sensorData.timeStamp def run(self): while True: if self.enableTempSensor: self.tempSensorData.setName('Temp') self.tempSensorData.addValue(self.tempsensor.getTemperature()) #print(self.sensorData.curValue) #self.manager.handleSensorData(self.sensorData) self.persistenceutil.writeSensorDataToRedis( self.tempSensorData) sleep(self.rateInSec) if self.enableHumidSensor: self.humidSensorData.setName("Humid") self.humidSensorData.addValue( self.humiditysensor.getHumidity()) #print(str(datetime.now()) + "SenseHat API Humidity " + str(self.curHumid)) #print(str(datetime.now()) + "I2C Direct Humidity: " + str(self.i2cHumid.getHumidityData())) #print(self.sensorData.curValue) #self.manager.handleSensorData(self.sensorData) self.persistenceutil.writeSensorDataToRedis( self.humidSensorData) sleep(self.rateInSec) if self.enableHI2CSensor: # self.displayAccelerometerData() # self.displayMagnetometerData() # self.displayPressureData() # self.displayHumidityData() # self.getHumidityData() # print(str(datetime.now()) + "I2C Direct Humidity: " + str(self.RH)) self.hi2cSensorData.setName("I2C_Humid") self.hi2cSensorData.addValue(self.hi2csensor.getHumidityData()) #self.manager.handleSensorData(self.sensorData) self.persistenceutil.writeSensorDataToRedis( self.hi2cSensorData) sleep(self.rateInSec)
''' Created on Sep 27, 2019 @author: cytang ''' import logging from labs.module05.TempSensorAdaptorTask import TempSensorAdaptorTask logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.INFO) logging.info("Module05 app start") temp = TempSensorAdaptorTask() #temp.daemon=True temp.start()
class Module05Test(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. """ #initialize the rgb values for the led colors #no color e = [0, 0, 0] #red r = [255, 0, 0] #blue b = [0, 0, 255] #initialize the red arrow when increasing the temperature on sense hat led matrix arrowRedInc = [ e,e,e,r,r,e,e,e, e,e,r,r,r,r,e,e, e,r,e,r,r,e,r,e, r,e,e,r,r,e,e,r, e,e,e,r,r,e,e,e, e,e,e,r,r,e,e,e, e,e,e,r,r,e,e,e, e,e,e,r,r,e,e,e ] #initialize the blue arrow when decreasing the temperature on sense hat led matrix arrowBlueDec = [ e,e,e,b,b,e,e,e, e,e,e,b,b,e,e,e, e,e,e,b,b,e,e,e, e,e,e,b,b,e,e,e, b,e,e,b,b,e,e,b, e,b,e,b,b,e,b,e, e,e,b,b,b,b,e,e, e,e,e,b,b,e,e,e ] def setUp(self): #initialize the variables self.multiActuatorAdaptor = MultiActuatorAdaptor() self.multiSensorAdaptor = MultiSensorAdaptor() self.tempSensorAdaptorTask = TempSensorAdaptorTask() 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): pass """ This tests the getTemperature() method of the TempSensorAdaptorTask, it checks whether the fetcher runs when enabled, disabled, number of readings to get has been set to 0. """ def testGetTemperature(self): #enable the fetcher self.tempSensorAdaptorTask.enableFetcher = True #change numReadings to a small finite value to check self.tempSensorAdaptorTask.numReadings = 1 #change sleep time (rateInSec) to a small amount self.tempSensorAdaptorTask.rateInSec = 1 #run when numReadings > 0 and adaptor is enabled self.assertEqual(True, self.tempSensorAdaptorTask.getTemperature()) #change numReadings to 0 self.tempSensorAdaptorTask.numReadings = 0 #run when numReadings = 0 and emulator is enabled, should return false because generator didn't run self.assertEqual(False, self.tempSensorAdaptorTask.getTemperature()) #disable the emulator self.tempSensorAdaptorTask.enableFetcher = False #change readings to > 0 self.tempSensorAdaptorTask.numReadings = 1 #run when numReadings > 0 and emulator is disabled, should return false because generator didn't run self.assertEqual(False, self.tempSensorAdaptorTask.getTemperature()) """ This tests the run() method of the TempSensorAdaptor, it checks whether it runs successfully. """ def testMultiSensorAdaptor(self): #get the reference to the tempSensorEmulatorTask tempSensTask = self.multiSensorAdaptor.tempSensorAdaptorTask #change numReadings to a small finite value to check tempSensTask.numReadings = 1 #change sleep time (rateInSec) to a small amount tempSensTask.rateInSec = 1 #enable the tempEmulatorTask's emulator tempSensTask.enableFetcher = True #run the run function of tempEmulatorAdaptor and get the value of success of the adaptor self.assertEqual(True, self.multiSensorAdaptor.run()) """ This tests the updateActuator() method of the TempActuatorAdaptor, it checks whether the actuator is updated (by returning an actuatorData reference) when the trigger is valid (INCREASE TEMP) and when the trigger is invalid (NOT A VALID TRIGGER) """ 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.multiActuatorAdaptor.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.multiActuatorAdaptor.updateActuator(actuatorData)) #sending a none should throw an exception, where when caught, returns a false self.assertEqual(False, self.multiActuatorAdaptor.updateActuator(None)) pass
def setUp(self): self.tempTask = TempSensorAdaptorTask() self.tempSensorData = SensorData() self.dataUtil = DataUtil()
def run(self): self.temp_sensor_object = TempSensorAdaptorTask(20) self.temp_sensor_object.start() # Starting Threaded Class Object time.sleep(100)