def toSensorfromJson(self, jsonData): sdDict = json.loads(jsonData) sd = SensorData('Temperature', 0, 30) sd.name = sdDict['name'] sd.timeStamp = sdDict['timeStamp'] sd.avgVal = sdDict['avgVal'] sd.minVal = sdDict['minVal'] sd.maxVal = sdDict['maxVal'] sd.curVal = sdDict['curVal'] 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
from labs.common import ConfigConst from labs.common.ConfigUtil import ConfigUtil 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()