Exemplo n.º 1
0
class DeviceDataManager:
    #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()
#Creating multiThreaded environment

    def run(self):
        t1 = Thread(target=self.sensorAdaptor.getSensorData)
        #logging.info("Running t1")
        t2 = Thread(target=self.actuatorOP.runListener)
        t1.start()
        t2.start()

        #self.sensorAdaptor.getSensorData()

    #Method for evaluating the sensor values and create decision for actation
    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 handleSensorData(self, SensorData):
        pu = PersistenceUtil()
        pu.writeSensorToDataDbms(SensorData)
class DataUtilTest(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.du = DataUtil()
        self.sd = SensorData()
        self.sd.addValue(15)
        self.sd.setName("Test")
        self.ad = ActuatorData()
        self.ad.addValue(44)
        self.ad.setName("Test")
        self.ad.setCommand("Test")
        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

    """
	Place your comments describing the test here.
	"""

    def testSomething(self):
        pass

    '@Test'

    def testCheckJsonData(self):
        jstring = self.du.toJsonFromSensorData(self.sd)
        #print(jstring)
        sd1 = self.du.toSensorDataFromJson(jstring)

        #print(str(self.sd.getCurrentValue()))
        #print(str(sd1.getCurrentValue()))
        self.assertTrue(self.sd.getCount() == sd1.getCount(),
                        "count does not match")
        self.assertTrue(self.sd.getCurrentValue() == sd1.getCurrentValue(),
                        "current does not match")
        pass

    '@Test'

    def testCheckActuatorData(self):
        jstring = self.du.toJsonFromActuatorData(self.ad)

    '@Test'

    def testtoActuatorDataFromJsonFile(self):
        self.assertTrue(self.du.toActuatorDataFromJsonFile(),
                        "File to actuator failed")
        pass

    '@Test'

    def testwriteActuatorDataToFile(self):
        self.assertTrue(self.du.writeActuatorDataToFile(self.ad),
                        "File to actuator failed")
        pass

    '@Test'

    def testwriteSensorDataToFile(self):
        self.assertTrue(self.du.toActuatorDataFromJsonFile(),
                        "File to actuator failed")
        pass
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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 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")