def __init__(self):
     self.actuator = ActuatorData()
     self.sensorAdaptor = TempSensorAdaptor()
     self.config = ConfigUtil()
     self.config.loadConfig("../../../config/ConnectedDevicesConfig.props")
     self.threshold = float(self.config.getValue("device", "nominalTemp"))
     self.actuatorOP = TempActuatorAdaptor()
Пример #2
0
    def setUp(self):

        #instantiate the classes required
        self.sensorDataManager = SensorDataManager()
        self.tempActuatorAdaptor = TempActuatorAdaptor()
        self.tempSensorAdaptor = TempSensorAdaptor()
        self.tempSensorAdaptorTask = TempSensorAdaptorTask()

        pass
    def setUp(self):
        self.sd = SensorData()
        self.sd.addValue(11)
        self.sdm = SensorDataManager()
        self.actuator = ActuatorData()
        self.actuator.setCommand("Raise Temp")
        self.sdm.threshold = 20
        self.tsa = TempSensorAdaptor()
        self.taa = TempActuatorAdaptor()
        self.tat = TempSensorAdaptorTask()
        self.shla = SenseHatLedActivator()

        pass
Пример #4
0
 def setUp(self):
     #self.adaptor= TempSensorAdaptorTask.getteravg()
     self.datamanager_avg = SensorDataManager().getteravg()
     self.datamanager_current = SensorDataManager().gettercurrent()
     self.datamanager_count = SensorDataManager().gettercount()
     self.adaptorTask_sensor = TempSensorAdaptorTask()
     self.sensorAdaptor = TempSensorAdaptor()
def main():

    temp_sensor_object = TempSensorAdaptor()  # Thread 1 - Temperature Sensing
    alarm_trigger = SensorDataManager(
    )  # Thread 2 - Notification generator & actuation function class
    alarm_trigger.start()

    while True:
        pass
'''
@author: Mengfan Shi
'''
from labs.module03.TempActuatorEmulator import TempActuatorEmulator
from labs.module03.TempSensorAdaptor import TempSensorAdaptor
from labs.common.SensorData import SensorData
'''
run every 0.5 seconds, the alertdiff is set to 2
'''
test = TempSensorAdaptor(2, 0.5)
Пример #7
0
'''
Created on Feb 6, 2020

@author: pallaksingh
'''
#import libraries and modules
from labs.module03.TempSensorAdaptor import TempSensorAdaptor

if __name__ == '__main__':
    """
    Module 3: Using SenseHat to read temperature and trigger an event
    """

    #instantiate the temp sensor adaptor
    tempSensorAdaptor = TempSensorAdaptor()

    #call the run function of the adaptor that creates and starts the sensor task thread
    tempSensorAdaptor.run()
Пример #8
0
'''
Created on Oct 12, 2018

@author: l0t0y
'''

from labs.module03.TempSensorAdaptor import TempSensorAdaptor
from asyncio.tasks import sleep

TSA = TempSensorAdaptor()
TSA.daemon = True
print("Daemon Thread Starting...")
TSA.start()

while (True):
    sleep(5)
    pass
Пример #9
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.
	"""
    #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):

        #instantiate the classes required
        self.sensorDataManager = SensorDataManager()
        self.tempActuatorAdaptor = TempActuatorAdaptor()
        self.tempSensorAdaptor = TempSensorAdaptor()
        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):

        #set the reference to the variables as none to release the resources they're holding
        self.sensorDataManager = None
        self.tempActuatorAdaptor = None
        self.tempSensorAdaptor = None
        self.tempSensorAdaptorTask = None
        pass

    """
	This method tests the handleSensorData() of SensorDataManager module. It tests whether the sensor data passed to the method triggers the correct
	actuator command.
	"""

    def testGetHandleSensorData(self):

        #initialize sensor data
        sensorData = SensorData()

        #when current value is greater than nominal temp
        sensorData.addValue(90)

        #get the actuatorData returned from the sensor data manager
        actuatorData = self.sensorDataManager.handleSensorData(sensorData)

        #check if actuatorData is of type ActuatorData
        self.assertIsInstance(actuatorData, ActuatorData)

        #test the value, which should be "arrowBlueDec"
        self.assertEqual(self.arrowBlueDec, actuatorData.getValue())

        #actuatorData command should be decrease temp
        self.assertEqual(actuatorData.getCommand(), "DECREASE TEMP")

        #when current value is less than nominal temp
        sensorData.addValue(10)

        #get the actuatorData returned from the sensor data manager
        actuatorData = self.sensorDataManager.handleSensorData(sensorData)

        #check if actuatorData is of type ActuatorData
        self.assertIsInstance(actuatorData, ActuatorData)

        #actuatorData command should be increase temp
        self.assertEqual(actuatorData.getCommand(), "INCREASE TEMP")

        #when current value is equal to the nominal temp
        sensorData.addValue(20)

        #get the actuatorData returned from the sensor data manager
        actuatorData = self.sensorDataManager.handleSensorData(sensorData)

        #actuatorData is none sensor data equal to nominal temp
        self.assertEqual(actuatorData, None)

        pass

    """
	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):

        #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 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.tempActuatorAdaptor.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.tempActuatorAdaptor.updateActuator(actuatorData))

        #sending a none should throw an exception, where when caught, returns a false
        self.assertEqual(False, self.tempActuatorAdaptor.updateActuator(None))

        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 testTempSensorAdaptor(self):

        #get the reference to the tempSensorEmulatorTask
        tempSensTask = self.tempSensorAdaptor.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.tempSensorAdaptor.run())
Пример #10
0
'''
Created on Jan 24, 2019
@author: GANESHRAM KANAKASABAI
'''

from time import sleep  # importing sleep function to set delay
from labs.module03.TempSensorAdaptor import TempSensorAdaptor  # importing TempSensorEmulator Class to measure temperature
'''
 Creating a new object to measure temperature and monitor
'''
sensor_data = TempSensorAdaptor("Temperature Data")
'''
When the program quits,any daemon threads are killed automatically
'''
sensor_data.daemon = True
sensor_data.start()
#starting the activity of thread

while True:
    sleep(1)
    # The sleep() suspends the execution for the fixed number of seconds.
    pass
Пример #11
0
 def setUp(self):
     adaptor_object = TempSensorAdaptor()  # Thread object 1
     self.temp_sensor_object = adaptor_object.getSensorobj()
     manager_object = SensorDataManager()  # Thread object 2
     manager_object.start()  # Starting thread
     self.message_object = smtpconnect()  # SMTP class object
Пример #12
0
'''
Created on Sep 27, 2019

@author: cytang
'''
from labs.module03.TempSensorAdaptor import TempSensorAdaptor
import logging

logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s',
                    level=logging.INFO)
logging.info("Module03 app start")
temp = TempSensorAdaptor()
#temp.daemon=True
temp.start()
#temp.run()
Пример #13
0
 def runapplication(self):
     TempSensorAdaptor().adaptor()
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.sd = SensorData()
        self.sd.addValue(11)
        self.sdm = SensorDataManager()
        self.actuator = ActuatorData()
        self.actuator.setCommand("Raise Temp")
        self.sdm.threshold = 20
        self.tsa = TempSensorAdaptor()
        self.taa = TempActuatorAdaptor()
        self.tat = TempSensorAdaptorTask()
        self.shla = SenseHatLedActivator()

        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.
	"""
    '@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')

    '@Test'

    def testreadSensorValueMax(self):
        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")
Пример #15
0
 def __init__(self):
     '''
     Object of class TempSensorAdaptor is created.
     '''
     tempSense = TempSensorAdaptor()
class SensorDataManager:
    #Default Constructor
    def __init__(self):
        self.actuator = ActuatorData()
        self.sensorAdaptor = TempSensorAdaptor()
        self.config = ConfigUtil()
        self.config.loadConfig("../../../config/ConnectedDevicesConfig.props")
        self.threshold = float(self.config.getValue("device", "nominalTemp"))
        self.actuatorOP = TempActuatorAdaptor()

    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
        if (self.threshold > self.actuator.value):
            self.actuator.setCommand("Raise Temp")
        elif (self.threshold < self.actuator.value):
            self.actuator.setCommand("Decrease Temp")
        elif (self.threshold == self.actuator.value):
            self.actuator.setCommand("Temp Stable")
        self.actuatorOP.updateActuator(self.actuator)
        self.sendNotification()
        return True

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

        try:
            if (self.actuator.getCommand() == "Raise Temp"):
                #logging.info('Current Temperature lower than threshold: ' + str(self.threshold) + ' Lowering Temp')
                logging.info(
                    "Current Temperature lower than threshold: \n \nTemperature: \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 = SmtpClientConnector()
                #Creating mail body text
                data = "Current Temperature lower than threshold: \n \nTemperature: \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("Temperature Alert Python", data)
                logging.info('\nMail sent')

            elif (self.actuator.getCommand() == "Decrease Temp"):
                logging.info(
                    "Current Temperature higher than threshold: \n \nTemperature: \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 = SmtpClientConnector()
                #Creating mail body text
                data = "Current Temperature higher than threshold: \n \nTemperature: \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("Temperature Alert Python", data)
                logging.info('\nMail sent')
            return True
        except Exception as e:
            logging.info(e)
            #If the mailing fails, the method returns false
            return False