class MultiActuatorAdaptorTask(object):
    def __init__(self):
        self.ActData = ActData()
        self.dUtil = DataUtil()

    #Method for checking ActutatorData initialization
    def updateActuator(self, act_type, data):

        if act_type == "ActFromGD":

            if (data == "1"):
                self.stateVal = "ON"
            elif (data == "0"):
                self.stateVal = "OFF"

            self.ActData.setName("LED ACTUATOR")
            self.ActData.setCommand(data)
            self.ActData.setValue(data)
            self.ActData.setStateData(self.stateVal)
            self.ActData.updateData(self.ActData)
            print("Actuator Name: " + self.ActData.getName())
            #             print("COMMAND: " + self.ActData.getCommand())
            jsonData = self.dUtil.toJsonFromActuatorData(self.ActData)
            logging.info("Actuator JSON: " + jsonData)
            return True
        else:
            print("Failed to send Actuation Signal")
            return False
Пример #2
0
class TempActuatorEmulator():
    actuatorData = None
    senseHatLedActivator = None
    simpleLedActivator = None

    def __init__(self):
        '''
        Constructor
        '''
        self.actuatorData = ActuatorData()
        self.senseHatLedActivator = SenseHatLedActivator()
        self.simpleLedActivator = SimpleLedActivator()

    def processMessage(self, ActuatorData):
        self.actuatorData.updateData(ActuatorData)
        self.simpleLedActivator.setEnableLedFlag(True)
        if self.actuatorData.getCommand() == 0:
            message = "Temperature need to raise " + str(
                abs(self.actuatorData.getValue()))
        if self.actuatorData.getCommand() == 1:
            message = "Temperature need to lower " + str(
                self.actuatorData.getValue())
        self.senseHatLedActivator.setEnableLedFlag(True)
        self.senseHatLedActivator.setDisplayMessage(message)
        self.senseHatLedActivator.run()
Пример #3
0
class TempActuatorEmulator():
    actuatorData = None
    senseHatLedActivator = None
    simpleLedActivator = None

    #Constructor
    def __init__(self):
        self.actuatorData = ActuatorData()
        self.senseHatLedActivator = SenseHatLedActivator()
        self.simpleLedActivator = SimpleLedActivator()

    #generate a message and run the activator
    def processMessage(self, ActuatorData):
        #print('processMessage...')
        self.actuatorData.updateData(ActuatorData)
        self.simpleLedActivator.setEnableLedFlag(True)
        if self.actuatorData.getCommand() == 0:
            #print('create msg-------')
            msg = "Temperature is " + str(
                abs(self.actuatorData.getValue())
            ) + " degree lower than nominal temperature, open the cool function"
        if self.actuatorData.getCommand() == 1:
            msg = "Temperature is " + str(
                self.actuatorData.getValue()
            ) + " degree higher than nominal temperature, open the heat function"
        self.senseHatLedActivator.setEnableLedFlag(True)
        self.senseHatLedActivator.setDisplayMessage(msg)
        #print('before run')
        self.senseHatLedActivator.run()
	def testUpdateValue(self):
		actuatorData = ActuatorData()
		testData = ActuatorData()
		testData.val = 10
		actuatorData.updateData(testData)
		assert actuatorData.val==10
		
		pass
class TempActuatorEmulator(object):
    '''
    classdocs
    '''
    diff = 0.0
    flag = None

    def __init__(self):
        '''
        Constructor
        '''
        self._adata = ActuatorData()

    '''
    After getting the differential from two degrees, start to tell the user how the temperature is going by showing message on senseHat.
    '''

    def displayMSG(self):
        print("Start to show message...")
        self.senseHatLedActivator = SenseHatLedActivator()
        self.senseHatLedActivator.setEnableLedFlag(True)
        if self.flag == True:
            self.senseHatLedActivator.setDisplayMessage("Turn down " +
                                                        str(self.diff)[:4])
        elif self.flag == False:
            self.senseHatLedActivator.setDisplayMessage("Turn up " +
                                                        str(self.diff)[:4])
        self.senseHatLedActivator.run()

    '''
    Compare the new value and the previous value of the ActuatorData.
    If different, calculate the differential value.
    After showing the message on senseHat, store the new value into ActuatorData.
    '''

    def processMessage(self, adata):
        if self._adata.get_val == 0:
            self._adata = adata
        else:
            old_val = self._adata.get_val()
            new_val = adata.get_val()
            if old_val != new_val:
                if float(old_val) > float(new_val):
                    # If -, notice user to decrease the temperature
                    self.diff = abs(float(old_val) - float(new_val))
                    self.flag = True
                else:
                    # If +, notice user to increase the temperature
                    self.diff = abs(float(old_val) - float(new_val))
                    self.flag = False
                self.displayMSG()
                self._adata.updateData(adata.get_command(),
                                       adata.get_statusCode(),
                                       adata.get_errCode(),
                                       adata.get_stateData(), new_val)
                self._adata.updateTimeStamp()
                print("New ActuatorData instance created.")
                print(self._adata.get_timeStamp())
class HumidityActuatorEmulator(object):
    '''
    Compare processed humidity value and threshold, set actions on actuator(SenseHat Ledscreen)
    '''
    flag = None

    def __init__(self):

        self.ad = ActuatorData()
        self.sh = SenseHat()

    def displayMsg(self):
        logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s',
                            level=logging.DEBUG)
        logging.info("Start to show message...\n")
        self.led = SenseHatLedActivator()
        self.led.setEnableLedFlag(True)
        if self.flag == 1:
            self.led.setDisplayMessage("Decrease!")
        elif self.flag == 2:
            self.led.setDisplayMessage("Increase!")
        elif self.flag == 3:
            self.led.setDisplayMessage("Keep!")
        self.led.run()

    def process(self, data, threshold):
        if self.ad.get_val() == 0:
            self.ad = data
        else:
            val = data.get_val()
            if val != threshold:
                if val > threshold:
                    self.flag = 1
                elif val < 20:
                    self.flag = 2
                else:
                    self.flag = 3

                self.displayMsg()
                self.ad.updateData(data.get_command(), data.get_statusCode(),
                                   data.get_errCode(), data.get_statusCode,
                                   val)
                self.ad.updateTimeStamp()
                logging.info("ActuatorData created!\n")
class TempSensorAdaptor(object):
    '''
    classdocs
    '''
    rateInSec = 10.0
    alertDiff = 10.0
    curDegree = 0.0
    nominalTemp = 10.0
    sense=None

    '''
    Initial all the variable I need
    '''
    def __init__(self, a, r):
        '''
        Constructor
        '''
        self.sense = SenseHat()
        self.tempActuatorEmulator = TempActuatorEmulator()
        self.alertDiff = a
        self.rateInSec = r
        self.sensorData = SensorData
        self.connector = SmtpClientConnector()
        self.config = ConfigUtil('../../../config/ConnectedDevicesConfig.props')
        self.config.loadConfig()
        self.nominalTemp = self.config.getProperty(ConfigConst.CONSTRAINED_DEVICE, 'nominalTemp')
#         self.nominalTemp = self.config.getProperty()
        _thread.start_new_thread(self.run())
    
    '''
    Connect to senseHat and obtain the environment temperature.
    '''
    def readTempFromSH(self):
        self.sense.clear()
        self.curDegree = self.sense.get_temperature()
    
    '''
    Store the temperature into SenseData.
    '''
    def addTempToSensorData(self):
        self.sensorData.addValue(self.sensorData, self.curDegree)
        now = datetime.datetime.now()
        print(str(now) + "\t" + str(self.curDegree))
        print(str(now) + "\t" + str(self.sensorData.getAvgValue(self.sensorData)))
        print("\n")
    
    '''
    Determine whether or not the particular temperature is higher or lower than the average temperature and then alert the user by sending email.
    '''
    def alert(self):
        if (abs(self.sensorData.getValue(self.sensorData) - self.sensorData.getAvgValue(self.sensorData)) >= self.alertDiff):
            # Start to send email
            logging.info('\n Current temp exceeds average by > ' + str(self.alertDiff) + '. Triggering alert...')
            print("Starting sending email...")
            output = self.sensorData.__str__(self.sensorData)
            self.connector.publishMessage("Excessive Temp", output)
    
    '''
    Determine whether or not the new temperature value is higher or lower than the nominal temperature which is set in ConnectedDevicesConfig
    '''
    def adjust(self):
        if self.sensorData.getValue(self.sensorData) < float(self.nominalTemp) or self.sensorData.getValue(self.sensorData) > float(self.nominalTemp):
            self.actuatorData = ActuatorData()
            self.actuatorData.updateData(1, 1, 0, "adjust", self.sensorData.getValue(self.sensorData))
            self.tempActuatorEmulator.processMessage(self.actuatorData)
    
    '''
    Start a new thread to do the task.
    '''
    def run(self):
        while True:
            self.readTempFromSH()
            self.addTempToSensorData()
            self.alert()
            self.adjust()
            sleep(self.rateInSec)