Example #1
0
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.handler= SensorHandlerApp
        
        self.difference = MultiSensorAdaptor()
        

    """
    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):
        self.handler=None
        self.difference=None
        

    """
    Place your comments describing the test here.
    """
    def testSomething(self):
        #self.handler.x.runapplication()
        self.difference.adaptor()    
        diff=self.difference.getterdiff()
        self.assertTrue(diff<2.0, "the difference from i2c and lib is small")
 def __init__(self):
     self.actuator = ActuatorData()
     self.sensorAdaptor = MultiSensorAdaptor()
     self.config = ConfigUtil()
     self.config.loadConfig("../../../config/ConnectedDevicesConfig.props")
     self.threshold = float(self.config.getValue("device", "nominalTemp"))
     self.actuatorOP = MultiActuatorAdaptor.getInstance()
Example #3
0
    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()
Example #4
0
 def setUp(self):
     self.msa = MultiSensorAdaptor()
     self.msa.start()
     time.sleep(1)
     self.manager_object = SensorDataManager()
     self.manager_object.start()
     time.sleep(10)
     self.x = self.manager_object.get_maaadaptor().getapi_actobj()
     self.y = self.manager_object.get_maaadaptor().getapi_actobj()
Example #5
0
class Module04Test(unittest.TestCase):
	
	'''
	* Setting up threads required for testing
	'''

	def setUp(self):
		self.msa = MultiSensorAdaptor()  # Sensor Thread
		self.msa.start()
		self.sdm = SensorDataManager()  # Actuator Thread
		self.sdm.start()
		time.sleep(3)
	
	'''
	* testing sensordata object for type & value range
	'''

	def test_I2Csensordata(self):
		self.assertTrue(isinstance(self.msa.geti2cobject().getSensorData(), float), "Not a float")
		self.assertTrue(self.msa.geti2cobject().getSensorData() >= 0 and self.msa.geti2cobject().getSensorData() <= 100, "Not a float")
	
	'''
	* testing sensordata object for type & value range
	'''

	def test_APIsensordata(self):	
		self.assertTrue(isinstance(self.msa.getAPIobject().getSensorData(), float), "Not a float")
		self.assertTrue(self.msa.getAPIobject().getSensorData() >= 0 and self.msa.getAPIobject().getSensorData() <= 100, "Not a float")
	
	'''
	* comparing api sensor value & i2c sensor value, for checkings computed difference is within range
	'''

	def test_compare_data(self):	
		self.assertTrue(abs(self.msa.geti2cobject().getSensorData() - self.msa.getAPIobject().getSensorData()) <= 1, "Not Matching")

	'''
	* testing whether sensordata is handled properly for actuation
	'''

	def test_sensor_data_handler(self):
		y = self.sdm.handle_sensordata(i2c_data_object)
		z = self.sdm.handle_sensordata(humidity_data_object)
		self.assertTrue(isinstance(y, bool), "Not a Boolean")
		self.assertTrue(isinstance(z, bool), "Not a Boolean")

	'''
	* testing whether actuator data is updating status of actuator  
	'''

	def test_update_actuator(self):
		time.sleep(10)
		x = self.sdm.get_maaadaptor()
		self.assertTrue(isinstance(x.update_Actuator(x.getapi_actobj()), bool), "Not Boolean")
		self.assertTrue(isinstance(x.update_Actuator(x.geti2c_actobj()), bool), "Not Boolean")
class SensorDataManager:
    #Default Constructor
    def __init__(self):
        self.actuator = ActuatorData()
        self.sensorAdaptor = MultiSensorAdaptor()
        self.config = ConfigUtil()
        self.config.loadConfig("../../../config/ConnectedDevicesConfig.props")
        self.threshold = float(self.config.getValue("device", "nominalTemp"))
        self.actuatorOP = MultiActuatorAdaptor.getInstance()

    def run(self):

        self.sensorAdaptor.getSensorData()

    #Method for evaluating the sensor values and create decision for actation
    def hadleSensorData(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

    #Triggering mail based on the command value. The mail contains the relevant information.
    def sendNotification(self):

        try:
            logging.info(self.actuator.getName() + "Temperature: \nTime: " +
                         str(self.actuator.timestamp) + " Value :" +
                         str(self.actuator.getValue()))
            mail = SmtpClientConnector()
            #Creating mail body text
            data = "Reading from:" + self.actuator.getName(
            ) + "\nTime: " + str(
                self.actuator.timestamp) + "\ncurrent : " + str(
                    self.actuator.getValue()) + "\nAverage :" + str(
                        self.actuator.getAverageValue()) + "\nSamples :" + str(
                            self.actuator.readings_number) + "\nMin: " + str(
                                self.actuator.min_value) + "\nMax :" + str(
                                    self.actuator.max_value)
            mail.publishMessage(
                "Humidity Alert Python -" + self.actuator.getName(), data)
            logging.info('\nMail sent')
            return True
        except Exception as e:
            logging.info(e)
            print(e)
            #If the mailing fails, the method returns false
            return False
Example #7
0
def main():
    '''
    * Thread for sensing values
    '''
    msa = MultiSensorAdaptor()  # Thread 1
    msa.start()  # Starting Thread
    '''
    * Thread for performing actuation based on values sensed
    '''
    manager_object = SensorDataManager()  # Thread 2
    manager_object.start()  # Starting Thread
Example #8
0
class ActuatorDataTest(unittest.TestCase):
    '''
	* Setting up the required classes for test, 
	1.)MutiSensorSensorAdaptor - Senses relative humidity value from environment via two different threads
	2.)SensorDataManager - Triggers actuation based on values from MultiSensorAdaptorTest 
	'''
    def setUp(self):
        self.msa = MultiSensorAdaptor()
        self.msa.start()
        time.sleep(1)
        self.manager_object = SensorDataManager()
        self.manager_object.start()
        time.sleep(10)
        self.x = self.manager_object.get_maaadaptor().getapi_actobj()
        self.y = self.manager_object.get_maaadaptor().getapi_actobj()

    '''
	Gets the input of sensor name given by user & checks whether its string or not 
	'''

    def testSensorName(self):
        time.sleep(2)
        self.assertTrue(
            isinstance(self.x.getName(), str),
            "Not a String")  # Function to check sensor name is string or not
        self.assertTrue(
            isinstance(self.y.getName(), str),
            "Not a String")  # Function to check sensor name is string or not

    '''
	Gets the status_of_actuator from ActuatorData class & checks whether its string or not 
	'''

    def testActuatorStatus(self):
        time.sleep(2)
        self.assertTrue(isinstance(self.x.get_current_actuator_status(),
                                   str), "Not a String"
                        )  # Function to check actuator_status is string or not
        self.assertTrue(isinstance(self.y.get_current_actuator_status(), str),
                        "Not a String")

    '''
	Gets input command  from ActuatorData class & checks whether its string or not 
	'''

    def testCommand(self):
        time.sleep(2)
        self.assertTrue(
            isinstance(self.x.get_command(), str),
            "Not a String")  # Function to check command is string or not
        self.assertTrue(isinstance(self.y.get_command(), str), "Not a String")
 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()
Example #10
0
 def setUp(self):
     msa = MultiSensorAdaptor()
     msa.start()
     manager_object = SensorDataManager()
     manager_object.start()
     time.sleep(10)
     self.api_sensor_object = msa.getAPIobject().getApiSensorDataObject()
     self.i2c_sensor_object = msa.geti2cobject().getI2Csensordataobject()
Example #11
0
	def setUp(self):
		self.msa = MultiSensorAdaptor()  # Sensor Thread
		self.msa.start()
		self.sdm = SensorDataManager()  # Actuator Thread
		self.sdm.start()
		time.sleep(3)
Example #12
0
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())
Example #13
0
'''
Created on Feb 14, 2020

@author: pallaksingh
'''
#import libraries and modules
from labs.module04.MultiSensorAdaptor   import MultiSensorAdaptor
import logging

if __name__ == '__main__':
    
    #instantiate the temp sensor adaptor
    multiSensorAdaptor = MultiSensorAdaptor()
    
    #call the run function of the adaptor runs the sensor threads
    multiSensorAdaptor.start()
    
    
    
Example #14
0
 def __init__(self):         #constructor initializtion
     '''
     object of MultiSensorAdaptor is created
     '''
     multiSensor=MultiSensorAdaptor()
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")
 def runapplication(self):
     MultiSensorAdaptor().adaptor()
     
     #TempSensorAdaptor().adaptor()
Example #17
0
 def setUp(self):
     self.handler= SensorHandlerApp
     
     self.difference = MultiSensorAdaptor()