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 = 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 handleActuatorCommandResponse(self, data: ActuatorData) -> bool: logging.info("handleActuatorCommandResponse called") #pass data to handler super().handleActuatorCommandResponse(data) #translate data to json and set updstream du = DataUtil() self._handleUpstreamTransmission(ResourceNameEnum.CDA_ACTUATOR_RESPONSE_RESOURCE.value, du.actuatorDataToJson(data))
def handleSensorMessage(self, data: SensorData) -> bool: logging.info("handleSensorMessage called") super().handleSensorMessage(data) # translate sensor data to json and handle it du = DataUtil() self._handleUpstreamTransmission(ResourceNameEnum.CDA_SENSOR_MSG_RESOURCE.value, du.sensorDataToJson(data)) self._handleSensorDataAnalysis(data)
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 onActuatorCommandMessage(self, client, userdata, msg): logging.info('[Callback] Actuator command message received. Topic: %s.', msg.topic) if self.dataMsgListener: try: actuatorData = DataUtil().jsonToActuatorData(msg.payload) self.dataMsgListener.handleActuatorCommandMessage(actuatorData) except: logging.exception("Failed to convert incoming actuation command payload to ActuatorData: ")
def onActuatorCommandMessage(self, client, userdata, msg): ##logging.info('[MQTT_CALLBACK] Actuator command message received. Topic: %s.', msg.topic) jsonData = str(msg.payload, encoding="utf-8") if self.dataMsgListener: try: actuatorData = DataUtil().jsonToActuatorData(jsonData) self.dataMsgListener.handleActuatorCommandMessage(actuatorData) except: logging.exception( "Failed to convert incoming actuation command payload to ActuatorData: " )
def _execTestPost(self, maxTestRuns: int, useCon: bool): sensorData = SensorData() payload = DataUtil().sensorDataToJson(sensorData) startTime = time.time_ns() for seqNo in range(0, maxTestRuns): self.coapClient.sendPostRequest(resource=ResourceNameEnum.CDA_SENSOR_MSG_RESOURCE, enableCON=useCon, payload=payload) endTime = time.time_ns() elapsedMillis = (endTime - startTime) / self.NS_IN_MILLIS print("POST message - useCON = " + str(useCon) + " [" + str(maxTestRuns) + "]: " + str(elapsedMillis) + " ms")
def setUpClass(self): logging.basicConfig(format = '%(asctime)s:%(module)s:%(levelname)s:%(message)s', level = logging.DEBUG) logging.info("Running DataIntegrationTest test cases...") encodeToUtf8 = False self.dataUtil = DataUtil(encodeToUtf8) self.cdaDataPath = ConfigUtil().getProperty(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEST_CDA_DATA_PATH_KEY) self.gdaDataPath = ConfigUtil().getProperty(ConfigConst.CONSTRAINED_DEVICE, ConfigConst.TEST_GDA_DATA_PATH_KEY) if not os.path.exists(self.cdaDataPath): logging.info("================================================") logging.info("DataIntegrationTest - path needs to be created: " + self.cdaDataPath) os.makedirs(self.cdaDataPath, exist_ok = True)
def setUpClass(self): logging.basicConfig(format = '%(asctime)s:%(module)s:%(levelname)s:%(message)s', level = logging.DEBUG) logging.info("Testing DataUtil class...") encodeToUtf8 = False self.dataUtil = DataUtil(encodeToUtf8) self.adName = "FooBar ActuatorData" self.sdName = "FooBar SensorData" self.spdName = "FooBar SystemPerformanceData" self.sampleSpdJson = "{\"name\": \"" + self.spdName + "\", \"timeStamp\":\"2019.01.16 21:32:34.123123\",\"cpuUtil\":0.0,\"diskUtil\":0.0,\"memUtil\":0.0}" self.sampleAdJson = "{\"name\": \"" + self.adName + "\", \"timeStamp\": \"2019-01-20 15:38:35.123123\", \"hasError\": false, \"command\": 0, \"statusCode\": 0, \"stateData\": null, \"curValue\": 0.0}" self.sampleSdJson = "{\"name\": \"" + self.sdName + "\", \"timeStamp\":\"2019.01.16 21:32:34.123123\",\"curValue\":0.0}"
def __init__(self): self.dataUtil = DataUtil() self.configUtil = ConfigUtil() self.host = self.configUtil.getProperty( ConfigConst.DATA_GATEWAY_SERVICE, ConfigConst.HOST_KEY) self.port = self.configUtil.getInteger( ConfigConst.DATA_GATEWAY_SERVICE, ConfigConst.PORT_KEY) self.enableCrypt = self.configUtil.getBoolean( ConfigConst.DATA_GATEWAY_SERVICE, ConfigConst.ENABLE_CRYPT_KEY) if self.enableCrypt: self.credFilePath = self.configUtil.getProperty( ConfigConst.DATA_GATEWAY_SERVICE, ConfigConst.CRED_FILE_KEY) # TODO: init redis with encryption logging.info("Redis client setting: host = {0}, port = {1}.".format( self.host, self.port)) # Init with no connection established self.curConnection: redis.client.Redis = None
def setUpClass(self): logging.basicConfig( format='%(asctime)s:%(module)s:%(levelname)s:%(message)s', level=logging.DEBUG) logging.info("Testing DataUtil class...") encodeToUtf8 = False self.dataUtil = DataUtil(encodeToUtf8) self.adName = "FooBar ActuatorData" self.sdName = "FooBar SensorData" self.spdName = "FooBar SystemPerformanceData" self.sampleSpdJson = "{\"name\": \"" + self.spdName + "\", \"timeStamp\":\"2019-01-16T21:32:34.123123\",\"cpuUtil\":0.0,\"diskUtil\":0.0,\"memUtil\":0.0}" self.sampleAdJson = "{\"name\": \"" + self.adName + "\", \"timeStamp\": \"2019-01-20T15:38:35.123123\", \"hasError\": false, \"command\": 0, \"statusCode\": 0, \"stateData\": null, \"curValue\": 0.0}" self.sampleSdJson = "{\"name\": \"" + self.sdName + "\", \"timeStamp\":\"2019-01-16T21:32:34.123123\",\"curValue\":0.0}" self.sampleAd2Json = "{\"actuatorType\":0,\"command\":1,\"value\":12.5,\"isResponse\":false,\"stateData\":\"\",\"name\":\"DataUtilTestName\",\"timeStamp\":\"2021-01-03T02:21:25.163507800Z\",\"statusCode\":1,\"typeID\":0,\"locationID\":\"gatewaydevice001\",\"latitude\":0.0,\"longitude\":0.0,\"elevation\":0.0,\"timeStampMillis\":1609640485163}"
def _execTestPublish(self, maxTestRuns: int, qos: int): self.assertTrue(self.mqttClient.connectClient()) sensorData = SensorData() payload = DataUtil().sensorDataToJson(sensorData) startTime = time.time_ns() for seqNo in range(0, maxTestRuns): self.mqttClient.publishMessage( resource=ResourceNameEnum.CDA_SENSOR_MSG_RESOURCE, msg=payload, qos=qos) endTime = time.time_ns() elapsedMillis = (endTime - startTime) / self.NS_IN_MILLIS self.assertTrue(self.mqttClient.disconnectClient()) logging.info("Publish message - QoS " + str(qos) + " [" + str(maxTestRuns) + "]: " + str(elapsedMillis) + " ms")
def _execTestPublish(self, maxTestRuns: int, qos: int): self.assertTrue(self.mqttClient.connectClient()) sensorData = SensorData() payload = DataUtil().sensorDataToJson(sensorData) payloadLen = len(payload) startTime = time.time_ns() for seqNo in range(0, maxTestRuns): self.mqttClient.publishMessage( resource=ResourceNameEnum.CDA_SENSOR_MSG_RESOURCE, msg=payload, qos=qos) endTime = time.time_ns() elapsedMillis = (endTime - startTime) / self.NS_IN_MILLIS self.assertTrue(self.mqttClient.disconnectClient()) logging.info( \ "\n\tTesting Publish: QoS = %r | msgs = %r | payload size = %r | start = %r | end = %r | elapsed = %r", \ qos, maxTestRuns, payloadLen, startTime / 1000, endTime / 1000, elapsedMillis / 1000)
def handleIncomingMessage(self, resourceEnum: ResourceNameEnum, msg: str) -> bool: logging.info("handleIncommingMessage called") #translate json to object and pass it to analysis du = DataUtil() du.jsonToActuatorData(msg) self._handleIncomingDataAnalysis(msg)
def handleSystemPerformanceMessage(self, data: SystemPerformanceData) -> bool: logging.info("handleSystemPerformanceMessage called") #translate sys perf data to json and set upstream du = DataUtil() self._handleUpstreamTransmission(ResourceNameEnum.CDA_SYSTEM_PERF_MSG_RESOURC.value, du.systemPerformanceDataToJson(data))