def on_message(self, client, userdata, msg): logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG) logging.info("Received Json: " + msg.topic + " " + str(msg.payload)) # Convert the received json data to sensordata instance self.sensorData = SensorData self.sensorData = DataUtil.toSensorDataFromJson( self, str(msg.payload)[1:].lstrip('\'').rstrip('\'')) # Convert the instance to json again data = DataUtil.toJsonFromSensorData(self, self.sensorData) # Log new JSON string. logging.info("The new JSON string: " + data)
class DataUtilTest(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.du = DataUtil() self.sd = SensorData() self.sd.addValue(15) self.sd.setName("Test") self.ad = ActuatorData() self.ad.addValue(44) self.ad.setName("Test") self.ad.setCommand("Test") 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. """ def testSomething(self): pass '@Test' def testCheckJsonData(self): jstring = self.du.toJsonFromSensorData(self.sd) #print(jstring) sd1 = self.du.toSensorDataFromJson(jstring) #print(str(self.sd.getCurrentValue())) #print(str(sd1.getCurrentValue())) self.assertTrue(self.sd.getCount() == sd1.getCount(), "count does not match") self.assertTrue(self.sd.getCurrentValue() == sd1.getCurrentValue(), "current does not match") pass '@Test' def testCheckActuatorData(self): jstring = self.du.toJsonFromActuatorData(self.ad) '@Test' def testtoActuatorDataFromJsonFile(self): self.assertTrue(self.du.toActuatorDataFromJsonFile(), "File to actuator failed") pass '@Test' def testwriteActuatorDataToFile(self): self.assertTrue(self.du.writeActuatorDataToFile(self.ad), "File to actuator failed") pass '@Test' def testwriteSensorDataToFile(self): self.assertTrue(self.du.toActuatorDataFromJsonFile(), "File to actuator failed") pass
def on_disconnect(self, client, userdata, rc): logging.info("disconnecting reason " + str(rc)) def on_message(self, client, userdata, msg): #log the jsondata that the subsriber received logging.info("the second json:\n" + str(msg.payload.decode("utf-8"))) logging.info("message topic=" + str(msg.topic)) logging.info("message qos=" + str(msg.qos)) logging.info("message retain flag=" + str(msg.retain)) self.json = str(msg.payload.decode("utf-8")) #logging.info("the second json:\n" + self.json) if __name__ == '__main__': logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.INFO) mqttSubClientTestApp = MqttSubClientTestApp() mqttSubClientTestApp.client_loop() #if(mqttSubClientTestApp.json!=None): #convert the jsondata to sensedata object dataUtil = DataUtil() #print("json\n"+str(mqttSubClientTestApp.json)) sensordata = dataUtil.toSensorDataFromJson(mqttSubClientTestApp.json) logging.info("sensordata converted from json:" + str(sensordata.getAvgValue())) #convert the sensedata object to jsondata again and log the third jsondata finaljson = dataUtil.toJsonFromSensorData(sensordata) logging.info("the third json:\n" + finaljson)
class DataUtilTest(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): #instantiate DataUtil self.dataUtil = DataUtil() 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 to null to release any resources self.dataUtil = None pass """ /** * This method tests the toJsonFromActuatorData() of DataUtil class * Checks if two actuatorData instances with same values produce the same * JSONStr */ """ def testActuatorDataToJson(self): #instantiate two actuatorData instances actuatorData1 = ActuatorData() actuatorData2 = ActuatorData() #ceate two actuatorData instances and check if their jsonStr are equal actuatorData1.setName("Temp Actuator") actuatorData2.setName("Temp Actuator") actuatorData1.setCommand("INCREASE") actuatorData2.setCommand("INCREASE") actuatorData1.setValue("UP") actuatorData2.setValue("UP") #get their JSON strings jsonStr1 = self.dataUtil.toJsonFromActuatorData(actuatorData1) jsonStr2 = self.dataUtil.toJsonFromActuatorData(actuatorData2) #check for equality self.assertEqual(jsonStr1, jsonStr2) #pass a non ActuatorData type to write jsonStr1 = self.dataUtil.toJsonFromActuatorData("Hello") #get the actuatorData back from the string passed actuatorData = self.dataUtil.toActuatorDataFromJson(jsonStr1) #the values should be "Not Set: self.assertEqual("Not Set", actuatorData.getCommand()) self.assertEqual("Not Set", actuatorData.getName()) self.assertEqual("Not Set", actuatorData.getValue()) pass """ /** * This method tests the toJsonFromSensorData() of DataUtil class * Checks if two sensorData instances with same values produce the same * JSONStr */ """ def testSensorDataToJson(self): #Create two sensorData instances sensorData1 = SensorData() sensorData2 = SensorData() #Create two sensor Data instances and check if their jsonStr are equal #First sensorData sensorData1.setName("Temperature Sensor") sensorData1.addValue(9) #change timestamp to bogus value because they will be different sensorData1.timeStamp = "None" #Second sensorData sensorData2.setName("Temperature Sensor") sensorData2.addValue(9) #change timestamp to bogus value because they will be different sensorData2.timeStamp = "None" #Get their JSON strings jsonStr1 = self.dataUtil.toJsonFromSensorData(sensorData1) jsonStr2 = self.dataUtil.toJsonFromSensorData(sensorData2) #Check for equality self.assertEqual(jsonStr1, jsonStr2) pass """ /** * This method tests writeSensorDataToFile() of DataUtil class. */ """ def testwriteSensorDataToFile(self): #create sensorData object sensorData = SensorData() #add a value sensorData.addValue(9) #write to file and assert True self.assertEqual(True, self.dataUtil.writeSensorDataToFile(sensorData)) pass """ /** * This method tests writeActuatorDataToFile() of DataUtil class. */ """ def testwriteActuatorDataToFile(self): #create sensorData object actuatorData = ActuatorData() #add a value actuatorData.setName("Testinng") #write to file and assert True self.assertEqual(True, self.dataUtil.writeActuatorDataToFile(actuatorData)) pass """ /** * This method tests the toActuatorDataFromJson() of DataUtil class * Checks if the actuatorData instances created from JSONString are equivalent in values * and if they have the same data type */ """ def testJsonToActuatorData(self): #Create actuatorData actuatorData = ActuatorData() #Set some command, name and value actuatorData.setName("Temp Actuator") actuatorData.setCommand("INCREASE") actuatorData.setValue("UP") #convert to JSON jsonStr = self.dataUtil.toJsonFromActuatorData(actuatorData) #convert back to ActuatorData actuatorDataTest = self.dataUtil.toActuatorDataFromJson(jsonStr) #test if their variables are equal self.assertEqual("Temp Actuator", actuatorDataTest.getName()) self.assertEqual("INCREASE", actuatorDataTest.getCommand()) self.assertEqual("UP", actuatorDataTest.getValue()) """ /* * Set the variable to a double value, check if the conversion to JsonStr and back * to ActuatorData value remains of double type */ """ #Set some command, name and double value actuatorData.setName("Temp Actuator") actuatorData.setCommand("INCREASE") actuatorData.setValue(6.0) #convert to JSON jsonStr = self.dataUtil.toJsonFromActuatorData(actuatorData) #convert back to ActuatorData actuatorDataTest = self.dataUtil.toActuatorDataFromJson(jsonStr) #test if their variables are equal self.assertEqual("Temp Actuator", actuatorDataTest.getName()) self.assertEqual("INCREASE", actuatorDataTest.getCommand()) self.assertEqual(6.0, actuatorDataTest.getValue()) """ /** * This method tests the toSensorDataFromJson() of DataUtil class * Checks if the sensorData instances created from JSONString are equivalent in values * and if they have the same data type */ """ def testJsonToSensorData(self): #Create sensorData sensorData = SensorData() #Add some value and set a name sensorData.addValue(9.0) sensorData.setName("Temperature Sensor") #convert to JSON jsonStr = self.dataUtil.toJsonFromSensorData(sensorData) #convert back to ActuatorData sensorDataTest = self.dataUtil.toSensorDataFromJson(jsonStr) #test if their variables are equal self.assertEqual("Temperature Sensor", sensorDataTest.getName()) self.assertEqual(9.0, sensorDataTest.getCurrentValue(), 0)