Beispiel #1
0
    def _onGetResponse(self, response):

        logging.info('[COAP_CALLBACK] GET response received.')

        if response:
            logging.info('Token: ' + str(response.token))
            logging.info(str(response.location_path))
            logging.info(str(response.payload))

            #
            # NOTE: This next section is optional if you want to send a callback to the self.dataMsgListener instance
            #

            # TODO: get the URI and convert to ResourceNameEnum

            resource = ResourceNameEnum.CDA_ACTUATOR_CMD_RESOURCE
            msg = str(response.payload)
            if response.payload:
                ad = DataUtil.jsonToActuatorData(self, msg)
                if ad.getActuatorType() == 4:
                    resource = ResourceNameEnum.CDA_ACTUATOR_CMD_RESOURCE
                else:
                    resource = ResourceNameEnum.CDA_CLOUD_ACTUATOR_CMD_RESOURCE
            if self.dataMsgListener and response.payload:
                self.dataMsgListener.handleIncomingMessage(resource, msg)
    def _handleIncomingDataAnalysis(self, msg: str):
        """
		Call this from handleIncomeMessage() to determine if there's
		any action to take on the message. Steps to take:
		1) Validate msg: Most will be ActuatorData, but you may pass other info as well.
		2) Convert msg: Use DataUtil to convert if appropriate.
		3) Act on msg: Determine what - if any - action is required, and execute.
		"""
        logging("_handleIncomingDataAnalysis method is called...")
        self.actuatorAdapterManager.sendActuatorCommand(
            DataUtil.jsonToActuatorData(self, msg))
        pass
Beispiel #3
0
    def handleIncomingMessage(self, resourceEnum: ResourceNameEnum,
                              msg: str) -> bool:
        """
		handle the IncomingMessage
		
		@return bool
		"""
        logging.info(
            "[CDA_CALLBACK]----->>>The handleIncomingMessage method is being called"
        )
        # Use the DataUtil class to convert the msg content (which should be JSON) to an ActuatorData instance
        ad = DataUtil.jsonToActuatorData(self, msg)
        self._handleIncomingDataAnalysis(msg)
class DataIntegrationTest(unittest.TestCase):
	"""
	This test case class contains very basic integration tests for
	DataUtil and data container classes for use between the CDA and
	GDA to verify JSON compatibility. It should not be considered complete,
	but serve as a starting point for the student implementing
	additional functionality within their Programming the IoT
	environment.
	"""
	
	@classmethod
	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 setUp(self):
		logging.info("================================================")
		logging.info("DataUtil test execution...")
		logging.info("================================================")
		
		pass

	def tearDown(self):
		pass
	
	#@unittest.skip("Ignore for now.")
	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')
		
	#@unittest.skip("Ignore for now.")
	def testWriteSensorDataToCdaDataPath(self):
		logging.info("\n\n----- [SensorData to JSON to file] -----")
		
		dataObj  = SensorData()
		dataStr  = self.dataUtil.sensorDataToJson(dataObj)
		fileName = self.cdaDataPath + '/SensorData.dat'

		logging.info("Sample SensorData JSON (validated): " + str(dataStr))
		logging.info("Writing SensorData JSON to CDA data path: " + fileName)
		
		fileRef = Path(fileName)
		fileRef.write_text(dataStr, encoding = 'utf-8')

	#@unittest.skip("Ignore for now.")
	def testWriteSystemPerformanceDataToCdaDataPath(self):
		logging.info("\n\n----- [SystemPerformanceData to JSON to file] -----")
		
		dataObj  = SystemPerformanceData()
		dataStr  = self.dataUtil.sensorDataToJson(dataObj)
		fileName = self.cdaDataPath + '/SystemPerformanceData.dat'

		logging.info("Sample SystemPerformanceData JSON (validated): " + str(dataStr))
		logging.info("Writing SystemPerformanceData JSON to CDA data path: " + fileName)
		
		fileRef = Path(fileName)
		fileRef.write_text(dataStr, encoding = 'utf-8')

	#@unittest.skip("Ignore for now.")
	def testReadActuatorDataFromGdaDataPath(self):
		logging.info("\n\n----- [ActuatorData JSON from file to object] -----")
		
		fileName = self.gdaDataPath + '/ActuatorData.dat'
		fileRef  = Path(fileName)
		dataStr  = fileRef.read_text(encoding = 'utf-8')

		dataObj  = self.dataUtil.jsonToActuatorData(dataStr)

		logging.info("ActuatorData JSON from GDA: " + dataStr)
		logging.info("ActuatorData object: " + str(dataObj))

	#@unittest.skip("Ignore for now.")
	def testReadSensorDataFromGdaDataPath(self):
		logging.info("\n\n----- [SensorData JSON from file to object] -----")
		
		fileName = self.gdaDataPath + '/SensorData.dat'
		fileRef  = Path(fileName)
		dataStr  = fileRef.read_text(encoding = 'utf-8')

		dataObj  = self.dataUtil.jsonToSensorData(dataStr)

		logging.info("SensorData JSON from GDA: " + dataStr)
		logging.info("SensorData object: " + str(dataObj))

	#@unittest.skip("Ignore for now.")
	def testReadSystemPerformanceDataFromGdaDataPath(self):
		logging.info("\n\n----- [SystemPerformanceData JSON from file to object] -----")
		
		fileName = self.gdaDataPath + '/SystemPerformanceData.dat'
		fileRef  = Path(fileName)
		dataStr  = fileRef.read_text(encoding = 'utf-8')

		dataObj  = self.dataUtil.jsonToSystemPerformanceData(dataStr)

		logging.info("SystemPerformanceData JSON from GDA: " + dataStr)
		logging.info("SystemPerformanceData object: " + str(dataObj))
Beispiel #5
0
class DataUtilTest(unittest.TestCase):
    """
	This test case class contains very basic unit tests for
	DataUtil. It should not be considered complete,
	but serve as a starting point for the student implementing
	additional functionality within their Programming the IoT
	environment.
	"""
    @classmethod
    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 setUp(self):
        logging.info("================================================")
        logging.info("DataUtil test execution...")
        logging.info("================================================")

        pass

    def tearDown(self):
        pass

    # @unittest.skip("Ignore for now.")
    def testActuatorDataConversionsFromJson(self):
        logging.info("\n\n----- [ActuatorData Conversions from JSON] -----")

        adObj1 = self.dataUtil.jsonToActuatorData(self.sampleAdJson)
        adObj1Str = self.dataUtil.actuatorDataToJson(adObj1)
        adObj2 = self.dataUtil.jsonToActuatorData(adObj1Str)

        logging.info("Sample JSON: " + str(self.sampleAdJson))
        logging.info("JSON to ActuatorData: " + str(adObj1))
        logging.info("ActuatorData back to JSON: " + str(adObj1Str))

        self.assertEqual(self.adName, adObj1.getName())
        self.assertEqual(self.adName, adObj2.getName())
        self.assertEqual(adObj1.getTimeStamp(), adObj2.getTimeStamp())

    # @unittest.skip("Ignore for now.")
    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)

    # @unittest.skip("Ignore for now.")
    def testSensorDataConversionsFromJson(self):
        logging.info("\n\n----- [SensorData Conversions from JSON] -----")

        sdObj1 = self.dataUtil.jsonToSensorData(self.sampleSdJson)
        sdObj1Str = self.dataUtil.sensorDataToJson(sdObj1)
        sdObj2 = self.dataUtil.jsonToSensorData(sdObj1Str)

        logging.info("Sample JSON: " + str(self.sampleSdJson))
        logging.info("JSON to SensorData: " + str(sdObj1))
        logging.info("SensorData back to JSON: " + str(sdObj1Str))

        self.assertEqual(self.sdName, sdObj1.getName())
        self.assertEqual(self.sdName, sdObj2.getName())
        self.assertEqual(sdObj1.getTimeStamp(), sdObj2.getTimeStamp())

    # @unittest.skip("Ignore for now.")
    def testSensorDataConversionsFromObject(self):
        logging.info("\n\n----- [JSON Conversions from SensorData] -----")

        sdName = "Foobar2 Sensor"
        sdObj1 = SensorData()
        sdObj1.setName(sdName)

        sdObj1Str = self.dataUtil.sensorDataToJson(sdObj1)
        sdObj2 = self.dataUtil.jsonToSensorData(sdObj1Str)
        sdObj2Str = self.dataUtil.sensorDataToJson(sdObj2)

        logging.info("Sample SensorData: " + str(sdObj1))
        logging.info("SensorData to JSON: " + str(sdObj1Str))
        logging.info("JSON back to SensorData: " + str(sdObj2))
        logging.info("SensorData back to JSON: " + str(sdObj2Str))

        self.assertEqual(sdName, sdObj1.getName())
        self.assertEqual(sdObj1.getName(), sdObj2.getName())
        self.assertEqual(sdObj1.getTimeStamp(), sdObj2.getTimeStamp())
        self.assertEqual(sdObj1Str, sdObj2Str)

    # @unittest.skip("Ignore for now.")
    def testSystemPerformanceConversionsFromJson(self):
        logging.info(
            "\n\n----- [SystemPerformanceData Conversions from JSON] -----")

        spdObj1 = self.dataUtil.jsonToSystemPerformanceData(self.sampleSpdJson)
        spdObj1Str = self.dataUtil.systemPerformanceDataToJson(spdObj1)
        spdObj2 = self.dataUtil.jsonToSystemPerformanceData(spdObj1Str)

        logging.info("Sample JSON: " + str(self.sampleSpdJson))
        logging.info("JSON to SystemPerformanceData: " + str(spdObj1))
        logging.info("SystemPerformanceData back to JSON: " + str(spdObj1Str))

        self.assertEqual(self.spdName, spdObj1.getName())
        self.assertEqual(self.spdName, spdObj2.getName())
        self.assertEqual(spdObj1.getTimeStamp(), spdObj2.getTimeStamp())

    # @unittest.skip("Ignore for now.")
    def testSystemPerformanceDataConversionsFromObject(self):
        logging.info(
            "\n\n----- [JSON Conversions from SystemPerformanceData] -----")

        spdName = "Foobar2 SystemPerformanceData"
        spdObj1 = SystemPerformanceData()
        spdObj1.setName(spdName)

        spdObj1Str = self.dataUtil.systemPerformanceDataToJson(spdObj1)
        spdObj2 = self.dataUtil.jsonToSystemPerformanceData(spdObj1Str)
        spdObj2Str = self.dataUtil.systemPerformanceDataToJson(spdObj2)

        logging.info("Sample SystemPerformanceData: " + str(spdObj1))
        logging.info("SystemPerformanceData to JSON: " + str(spdObj1Str))
        logging.info("JSON back to SystemPerformanceData: " + str(spdObj2))
        logging.info("SystemPerformanceData back to JSON: " + str(spdObj2Str))

        self.assertEqual(spdName, spdObj1.getName())
        self.assertEqual(spdObj1.getName(), spdObj2.getName())
        self.assertEqual(spdObj1.getTimeStamp(), spdObj2.getTimeStamp())
        self.assertEqual(spdObj1Str, spdObj2Str)
Beispiel #6
0
 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 handleIncomingMessage(self, resourceEnum: ResourceNameEnum,
                           msg: str) -> bool:
     logging("handleIncomingMessage method is called...")
     self._handleIncomingDataAnalysis(DataUtil.jsonToActuatorData(
         self, msg))
     pass