def testStartAndStopManagerWithMqtt(self): """ NOTE: Be sure to enable CoAP by setting the following flag to True within PiotConfig.props enableMqttClient = True enableCoapClient = False """ ddMgr = DeviceDataManager() ddMgr.startManager() mqttClient = MqttClientConnector() mqttClient.connectClient() ad = ActuatorData() ad.setCommand(1) adJson = DataUtil().actuatorDataToJson(ad) mqttClient.publishMessage(ResourceNameEnum.CDA_ACTUATOR_CMD_RESOURCE, msg=adJson, qos=1) sleep(10) mqttClient.disconnectClient() ddMgr.stopManager()
def testActuatorCmdPubSub(self): qos = 1 # NOTE: delay can be anything you'd like - the sleep() calls are simply to slow things down a bit for observation delay = self.cfg.getInteger(ConfigConst.MQTT_GATEWAY_SERVICE, ConfigConst.KEEP_ALIVE_KEY, ConfigConst.DEFAULT_KEEP_ALIVE) actuatorData = ActuatorData() payload = DataUtil().actuatorDataToJson(actuatorData) listener = DefaultDataMessageListener() self.mcc.setDataMessageListener(listener) self.mcc.connectClient() sleep(5) self.mcc.publishMessage( resource=ResourceNameEnum.CDA_ACTUATOR_CMD_RESOURCE, msg=payload, qos=qos) sleep(delay) self.mcc.disconnectClient()
def testUpdateActuator(self): ad = ActuatorData(actuatorType=ActuatorData.HVAC_ACTUATOR_TYPE) ad.setCommand(ActuatorData.COMMAND_ON) ad.setValue(self.DEFAULT_VAL_A) self.assertTrue(self.hSimTask.updateActuator(ad)) adr = self.hSimTask.getLatestActuatorResponse() self.assertIsNotNone(adr) self.assertEqual(adr.getValue(), self.DEFAULT_VAL_A) logging.info("ActuatorData: " + str(adr)) ad.setValue(self.DEFAULT_VAL_B) self.assertTrue(self.hSimTask.updateActuator(ad)) adr = self.hSimTask.getLatestActuatorResponse() self.assertIsNotNone(adr) self.assertEqual(adr.getValue(), self.DEFAULT_VAL_B) logging.info("ActuatorData: " + str(adr)) ad.setCommand(ActuatorData.COMMAND_OFF) self.assertTrue(self.hSimTask.updateActuator(ad)) adr = self.hSimTask.getLatestActuatorResponse() self.assertIsNotNone(adr) self.assertEqual(adr.getCommand(), ActuatorData.COMMAND_OFF) logging.info("ActuatorData: " + str(adr))
def testDefaultValues(self): ad = ActuatorData() self.assertEquals(ad.getCommand(), ConfigConst.DEFAULT_COMMAND) self.assertEquals(ad.getStatusCode(), ConfigConst.DEFAULT_STATUS) logging.info("Actuator data as string: " + str(ad))
def testUpdateActuator(self): ad = ActuatorData(typeID=ConfigConst.HUMIDIFIER_ACTUATOR_TYPE) ad.setCommand(ConfigConst.COMMAND_ON) ad.setValue(self.DEFAULT_VAL_A) adr = self.hSimTask.updateActuator(ad) self.assertIsNotNone(adr) self.assertEquals(adr.getValue(), self.DEFAULT_VAL_A) logging.info("ActuatorData: " + str(adr)) ad.setValue(self.DEFAULT_VAL_B) adr = self.hSimTask.updateActuator(ad) self.assertIsNotNone(adr) self.assertEquals(adr.getValue(), self.DEFAULT_VAL_B) logging.info("ActuatorData: " + str(adr)) ad.setCommand(ConfigConst.COMMAND_OFF) adr = self.hSimTask.updateActuator(ad) self.assertIsNotNone(adr) self.assertEquals(adr.getCommand(), ConfigConst.COMMAND_OFF) logging.info("ActuatorData: " + str(adr))
def testActuatorCmdPubSub(self): qos = 0 delay = self.cfg.getInteger(ConfigConst.MQTT_GATEWAY_SERVICE, ConfigConst.KEEP_ALIVE_KEY, ConfigConst.DEFAULT_KEEP_ALIVE) actuatorData = ActuatorData() actuatorData.setCommand(7) self.mcc.setDataMessageListener(DefaultDataMessageListener()) payload = DataUtil().actuatorDataToJson(actuatorData) self.mcc.connectClient() sleep(5) self.mcc.publishMessage( resource=ResourceNameEnum.CDA_ACTUATOR_CMD_RESOURCE, msg=payload, qos=qos) sleep(delay + 5) self.mcc.disconnectClient()
def testLedDisplayEmulation(self): ad = ActuatorData(actuatorType=ConfigConst.LED_DISPLAY_ACTUATOR_TYPE) ad.setCommand(ConfigConst.COMMAND_ON) ad.setStateData("What's up?") self.actuatorAdapterMgr.sendActuatorCommand(ad) ad.setCommand(ConfigConst.COMMAND_OFF) self.actuatorAdapterMgr.sendActuatorCommand(ad)
def testHvacEmulation(self): ad = ActuatorData(actuatorType=ConfigConst.HVAC_ACTUATOR_TYPE) ad.setValue(22.5) ad.setCommand(ConfigConst.COMMAND_ON) self.actuatorAdapterMgr.sendActuatorCommand(ad) ad.setCommand(ConfigConst.COMMAND_OFF) self.actuatorAdapterMgr.sendActuatorCommand(ad)
def testHumidifierEmulation(self): ad = ActuatorData(actuatorType=ConfigConst.HUMIDIFIER_ACTUATOR_TYPE) ad.setValue(50.0) ad.setCommand(ConfigConst.COMMAND_ON) self.actuatorAdapterMgr.sendActuatorCommand(ad) ad.setCommand(ConfigConst.COMMAND_OFF) self.actuatorAdapterMgr.sendActuatorCommand(ad)
def jsonToActuatorData(self, jsonData) -> ActuatorData: """ Convert json string to ActuatorData Instance :param jsonData: Given json string :return: Decoded ActuatorData Instance """ adDict: dict = json.loads(jsonData) ad = ActuatorData(d=adDict) return ad
def _createTestActuatorData(self): ad = ActuatorData() ad.setName(self.DEFAULT_NAME) ad.setCommand(ActuatorData.COMMAND_ON) ad.setStateData(self.DEFAULT_STATE_DATA) ad.setValue(self.DEFAULT_VALUE) return ad
def testFullUpdate(self): ad = ActuatorData() ad2 = self._createTestActuatorData() ad.updateData(ad2) self.assertEquals(ad.getCommand(), ActuatorData.COMMAND_ON) self.assertEquals(ad.getStateData(), self.DEFAULT_STATE_DATA) self.assertEquals(ad.getValue(), self.DEFAULT_VALUE)
def testLEDSimulation(self): ad = ActuatorData(actuatorType=ActuatorData.LED_DISPLAY_ACTUATOR_TYPE) ad.setStateData(stateData="Test stateData") ad.setCommand(ActuatorData.COMMAND_ON) self.actuatorAdapterMgr.sendActuatorCommand(ad) ad.setCommand(ActuatorData.COMMAND_OFF) self.actuatorAdapterMgr.sendActuatorCommand(ad)
def testActuatorDataCallback(self): ddMgr = DeviceDataManager() actuatorData = ActuatorData(typeID=ConfigConst.HVAC_ACTUATOR_TYPE) actuatorData.setCommand(ConfigConst.COMMAND_ON) actuatorData.setStateData("This is a test.") ddMgr.handleActuatorCommandMessage(actuatorData) sleep(10)
def _handleSensorDataAnalysis(self, data: SensorData): """ Call this from handleSensorMessage() to determine if there's any action to take on the message. Steps to take: 1) Check config: Is there a rule or flag that requires immediate processing of data? 2) Act on data: If # 1 is true, determine what - if any - action is required, and execute. """ logging.info( "[CDA_CALLBACK]----->>>The _handleSensorDataAnalysis method is being called" ) """ """ if self.enableHandleTempChangeOnDevice and data.getSensorType( ) == SensorData.TEMP_SENSOR_TYPE: ad = ActuatorData(actuatorType=ActuatorData.HVAC_ACTUATOR_TYPE) value = data.getValue() if value >= self.triggerHvacTempFloor and value <= self.triggerHvacTempCeiling: ad.setCommand(ActuatorData.COMMAND_OFF) else: ad.setCommand(ActuatorData.COMMAND_ON) self.actuatorAdapterManager.sendActuatorCommand(ad) """ """ if self.enableHandleSoilHumidityChangeOnDevice and data.getSensorType( ) == SensorData.SOIL_HUMIDITY_SENSOR_TYPE: ad = ActuatorData( actuatorType=ActuatorData.SPRINKLER_ACTUATOR_TYPE) value = data.getValue() if value >= self.triggerWaterDeviceHumiCeiling: ad.setCommand(ActuatorData.COMMAND_OFF) self.actuatorAdapterManager.sendActuatorCommand(ad) elif value <= self.triggerWaterDeviceHumiFloor: ad.setCommand(ActuatorData.COMMAND_ON) self.actuatorAdapterManager.sendActuatorCommand(ad) self.coapClient.sendGetRequest( ResourceNameEnum.CDA_ACTUATOR_CMD_RESOURCE, False, 5) else: self.coapClient.sendGetRequest( ResourceNameEnum.CDA_CLOUD_ACTUATOR_CMD_RESOURCE, False, 5)
def _createTestActuatorData(self): ad = ActuatorData() ad.setName(self.DEFAULT_NAME) ad.setCommand(ConfigConst.COMMAND_ON) ad.setStateData(self.DEFAULT_STATE_DATA) ad.setValue(self.DEFAULT_VALUE) logging.info("Actuator data as string: " + str(ad)) return ad
def __init__(self, actuatorType: int = ActuatorData.DEFAULT_ACTUATOR_TYPE, simpleName: str = "Actuator", actuatorName=ConfigConst.NOT_SET): """ Initialization of class. Create an instance of BaseActuatorSimTask """ self.actuatorType = actuatorType self.simpleName = simpleName self.latestAd = ActuatorData(name=actuatorName)
def testStartAndStopManagerWithMqttAndCoap(self): ddMgr = DeviceDataManager(enableMqtt=False, enableCoap=False) actuatorData = ActuatorData( actuatorType=ActuatorData.HVAC_ACTUATOR_TYPE) actuatorData.setCommand(ActuatorData.COMMAND_ON) actuatorData.setStateData("This is a test.") ddMgr.handleActuatorCommandMessage(actuatorData) sleep(10)
def testWriteActuatorDataToCdaDataPath(self): logging.info("\n\n----- [ActuatorData to JSON to file] -----") dataObj = ActuatorData() dataStr = self.dataUtil.actuatorDataToJson(dataObj) fileName = self.cdaDataPath + '/ActuatorData.dat' logging.info("Sample ActuatorData JSON (validated): " + str(dataStr)) logging.info("Writing ActuatorData JSON to CDA data path: " + fileName) fileRef = Path(fileName) fileRef.write_text(dataStr, encoding = 'utf-8')
def __init__(self, actuatorType: int = ActuatorData.DEFAULT_ACTUATOR_TYPE, simpleName: str = ConfigConst.NOT_SET): """ Init BaseActuatorSimTask with default values :param actuatorType: Type of actuator :param simpleName: Name of actuator """ self.actuatorType = actuatorType self.simpleName = simpleName self.latestActuatorData = ActuatorData(self.actuatorType, self.simpleName) pass
def _handleSensorDataAnalysis(self, data: SensorData): """ Call this from handleSensorMessage() to determine if there's any action to take on the message. Steps to take: 1) Check config: Is there a rule or flag that requires immediate processing of data? 2) Act on data: If # 1 is true, determine what - if any - action is required, and execute. """ logging("_handleSensorDataAnalysis method is called...") if (self.enableHandleTempChangeOnDevice == True): self.actuatorData = ActuatorData() self.actuatorData.actuator_type = ActuatorData.HVAC_ACTUATOR_TYPE self.actuatorData.COMMAND_ON self.actuatorAdapterManager.sendActuatorCommand(self.actuatorData) pass
def jsonToActuatorData(self, jsonData): # process data print(str(jsonData)) jsonData = str(jsonData).replace("\'", "\"").replace( 'False', 'false').replace('True', 'true') # load json data adDict = json.loads(jsonData) # create object and put json data in it ad = ActuatorData() # vars() get the properties dict of the object mvDict = vars(ad) # iter properties and fill it for key in adDict: if key in mvDict: setattr(ad, key, adDict[key]) return ad
def _handleSensorDataAnalysis(self, data: SensorData): """ Call this from handleSensorMessage() to determine if there's any action to take on the message. Steps to take: 1) Check config: Is there a rule or flag that requires immediate processing of data? 2) Act on data: If # 1 is true, determine what - if any - action is required, and execute. """ logging.info("_handleSensorDataAnalysis called,msg:"+str(data)) if self.enableHandleTempChangeOnDevice is True: hvac = ActuatorData(ActuatorData.HVAC_ACTUATOR_TYPE) if data.getValue() < self.triggerHvacTempCeiling and data.getValue() > self.triggerHvacTempFloor: # start hvac when in trigger range hvac.setCommand(ActuatorData.COMMAND_ON) else: # stop hvac when not in range hvac.setCommand(ActuatorData.COMMAND_OFF) # send command self.aam.sendActuatorCommand(hvac)
def testActuatorDataConversionsFromJsonUsingDecimal(self): logging.info("\n\n----- [ActuatorData Conversions from JSON] -----") adData = ActuatorData() adData.setCommand(1) adData.setStateData("This is a test.") jsonData1 = self.dataUtil.actuatorDataToJson(adData, True) logging.info("\n\nActuatorData to JSON:\n\n" + str(jsonData1)) adData2 = self.dataUtil.jsonToActuatorData(jsonData1) logging.info("\n\nJSON to ActuatorData:\n\n" + str(adData2)) jsonData2 = self.dataUtil.actuatorDataToJson(adData2, True) logging.info("\n\nActuatorData back to JSON:\n\n" + str(jsonData2))
def testUpdateEmulator(self): ad = ActuatorData(actuatorType = ActuatorData.HVAC_ACTUATOR_TYPE) ad.setCommand(ActuatorData.COMMAND_ON) ad.setValue(22.0) self.assertTrue(self.hvSimTask.updateActuator(ad)) adr = self.hvSimTask.getLatestActuatorResponse() if adr: self.assertEqual(adr.getCommand(), ActuatorData.COMMAND_ON) self.assertEqual(adr.getStatusCode(), 0) logging.info("ActuatorData: " + str(adr)) # wait 5 seconds sleep(5) else: logging.warning("ActuatorData is None.") ad.setValue(20.0) self.assertTrue(self.hvSimTask.updateActuator(ad)) adr = self.hvSimTask.getLatestActuatorResponse() if adr: self.assertEqual(adr.getCommand(), ActuatorData.COMMAND_ON) self.assertEqual(adr.getStatusCode(), 0) logging.info("ActuatorData: " + str(adr)) # wait 5 seconds else: logging.warning("ActuatorData is None.") ad.setCommand(ActuatorData.COMMAND_OFF) self.assertTrue(self.hvSimTask.updateActuator(ad)) adr = self.hvSimTask.getLatestActuatorResponse() if adr: self.assertEqual(adr.getCommand(), ActuatorData.COMMAND_OFF) self.assertEqual(adr.getStatusCode(), 0) logging.info("ActuatorData: " + str(adr)) else: logging.warning("ActuatorData is None.")
def testActuatorDataConversionsFromObject(self): logging.info("\n\n----- [JSON Conversions from ActuatorData] -----") adName = "FooBar2 Actuator" adObj1 = ActuatorData() adObj1.setName(adName) adObj1Str = self.dataUtil.actuatorDataToJson(adObj1) adObj2 = self.dataUtil.jsonToActuatorData(adObj1Str) adObj2Str = self.dataUtil.actuatorDataToJson(adObj2) logging.info("Sample ActuatorData: " + str(adObj1)) logging.info("ActuatorData to JSON: " + str(adObj1Str)) logging.info("JSON back to ActuatorData: " + str(adObj2)) logging.info("ActuatorData back to JSON: " + str(adObj2Str)) self.assertEqual(adName, adObj1.getName()) self.assertEqual(adObj1.getName(), adObj2.getName()) self.assertEqual(adObj1.getTimeStamp(), adObj2.getTimeStamp()) self.assertEqual(adObj1Str, adObj2Str)
def testUpdateEmulator(self): ad = ActuatorData(typeID=ConfigConst.HUMIDIFIER_ACTUATOR_TYPE) ad.setCommand(ConfigConst.COMMAND_ON) ad.setValue(50.0) adr = self.huSimTask.updateActuator(ad) if adr: self.assertEqual(adr.getCommand(), ConfigConst.COMMAND_ON) self.assertEqual(adr.getStatusCode(), 0) logging.info("ActuatorData: " + str(adr)) # wait 5 seconds sleep(5) else: logging.warning("ActuatorData is None.") ad.setValue(35.0) adr = self.huSimTask.updateActuator(ad) if adr: self.assertEqual(adr.getCommand(), ConfigConst.COMMAND_ON) self.assertEqual(adr.getStatusCode(), 0) logging.info("ActuatorData: " + str(adr)) # wait 5 seconds else: logging.warning("ActuatorData is None.") ad.setCommand(ConfigConst.COMMAND_OFF) adr = self.huSimTask.updateActuator(ad) if adr: self.assertEqual(adr.getCommand(), ConfigConst.COMMAND_OFF) self.assertEqual(adr.getStatusCode(), 0) logging.info("ActuatorData: " + str(adr)) else: logging.warning("ActuatorData is None.")
def testUpdateEmulator(self): ad = ActuatorData(actuatorType=ConfigConst.LED_DISPLAY_ACTUATOR_TYPE) ad.setCommand(ConfigConst.COMMAND_ON) ad.setStateData(self.HELLO_WORLD_A) adr = self.lddSimTask.updateActuator(ad) if adr: self.assertEqual(adr.getCommand(), ConfigConst.COMMAND_ON) self.assertEqual(adr.getStatusCode(), 0) logging.info("ActuatorData: " + str(adr)) # wait 5 seconds sleep(5) else: logging.warning("ActuatorData is None.") ad.setStateData(self.HELLO_WORLD_B) adr = self.lddSimTask.updateActuator(ad) if adr: self.assertEqual(adr.getCommand(), ConfigConst.COMMAND_ON) self.assertEqual(adr.getStatusCode(), 0) logging.info("ActuatorData: " + str(adr)) # wait 5 seconds else: logging.warning("ActuatorData is None.") ad.setCommand(ConfigConst.COMMAND_OFF) adr = self.lddSimTask.updateActuator(ad) if adr: self.assertEqual(adr.getCommand(), ConfigConst.COMMAND_OFF) self.assertEqual(adr.getStatusCode(), 0) logging.info("ActuatorData: " + str(adr)) else: logging.warning("ActuatorData is None.")
def jsonToActuatorData(self, jsonData): """ Convert JSON to an ActuatorData instance @return ActuatorData """ jsonData = jsonData.replace("\'", "\"").replace('False', 'false').replace( 'True', 'true') logging.info('Convert JSON to an ActuatorData instance: ' + jsonData) ## Load the dictionary data for the JSON string adDict = json.loads(jsonData) ##Create an instance of ActuatorData, extract the variables, then map the JSON dict into the new object via an iterative lookup of each key / value pair. ad = ActuatorData() mvDict = vars(ad) for key in adDict: if key in mvDict: setattr(ad, key, adDict[key]) return ad
def __init__(self, actuatorType: int = ActuatorData.DEFAULT_ACTUATOR_TYPE, simpleName: str = "Actuator"): self.actuatorType = actuatorType self.simpleName = simpleName self.actuatorData = ActuatorData(name=simpleName)