def toSensorDataFromJson(self, jsonData): sdDict = json.loads(jsonData) sd = SensorData() sd.name = sdDict['name'] sd.timeStamp = sdDict['timeStamp'] sd.avgValue = sdDict['avgValue'] sd.minValue = sdDict['minValue'] sd.maxValue = sdDict['maxValue'] sd.curValue = sdDict['curValue'] sd.totValue = sdDict['totValue'] sd.sampleCount = sdDict['sampleCount'] return sd
def jsonTosensor(self,jsonData): sensedataDict = json.loads(jsonData) print(" decode [pre] --> " + str(sensedataDict)) sensedata = SensorData('Temperature',0,30) sensedata.name = sensedataDict['name'] sensedata.timeStamp = sensedataDict['timeStamp'] sensedata.avgVal = sensedataDict['avgVal'] sensedata.minVal = sensedataDict['minVal'] sensedata.maxVal = sensedataDict['maxVal'] sensedata.curVal = sensedataDict['curVal'] sensedata.totValue = sensedataDict['totValue'] sensedata.sampleCount = sensedataDict['sampleCount'] print(" decode [post] --> " + str(sensedata)) return sensedata
def toSensorDataFromJson(self,jsonData): sdDict = json.loads(jsonData) print(" decode [pre] --> " + str(sdDict)) sd = SensorData() sd.name = sdDict['name'] sd.timeStamp = sdDict['timeStamp'] sd.avgValue = sdDict['avgValue'] sd.minValue = sdDict['minValue'] sd.maxValue = sdDict['maxValue'] sd.curValue = sdDict['curValue'] sd.totValue = sdDict['totValue'] sd.sampleCount = sdDict['sampleCount'] print(" decode [post] --> " + str(sd)) return sd
from labs.common.SensorData import SensorData from labs.common.DataUtil import DataUtil from labs.module06.MqttClientConnector import MqttClientConnector topic = "Temperature Sensor" config = ConfigUtil('../../../config/ConnectedDevicesConfig.props') host = config.getProperty(ConfigConst.MQTT_GATEWAY_SECTION, ConfigConst.HOST_KEY) #Creating Sensor Data sensorData = SensorData(topic, 10, 30) sensorData.curVal = uniform(float(sensorData.getMinValue()), float(sensorData.getMaxValue())) sensorData.addValue(sensorData.curVal) sensorData.diffVal = sensorData.curVal - sensorData.avgVal sensorData.timestamp = datetime.now().replace(microsecond=0) sensorData.sampleCount = 1 logging.info('\nSensorData for sending: ') print("\n" + str(sensorData)) #Converting SensorData to json format data = DataUtil() jsondata = data.sensorTojson(sensorData) logging.info('\nSensorData after Json conversion: ') print("\nSensorData in Json Format for publishing: \n" + str(jsondata) + "\n") pubclient = MqttClientConnector() #Function call to publish the Json to the MQTT broker through MQTT ClientConnector pubclient.publish(host, topic, jsondata)