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)
from random import uniform from datetime import datetime #importing datetime to perform date and time operations topic = "Temperature Sensor" config = ConfigUtil('../../../config/ConnectedDevicesConfig.props') host = config.getProperty(ConfigConst.ConfigConst.MQTT_CLOUD_SECTION, ConfigConst.ConfigConst.HOST_KEY) ''' Creating Sensor Data ''' sensor = SensorData(topic, 15, 30) sensor.curVal = uniform(float(sensor.getMinValue()), float(sensor.getMaxValue())) sensor.addValue(sensor.curVal) sensor.diffVal = sensor.curVal - sensor.avgVal sensor.timestamp = datetime.now() logging.info('SensorData to be sent:') print("Sensor Value before converting to Json: " + str(sensor)) ''' Converting SensorData to json format ''' data = DataUtil() json_data = data.toJsonfromSensor(sensor) logging.info('SensorData converted into Json:') print("SensorData in Json Format before publishing" + str(json_data) + "\n") pub_client = MqttClientConnector() ''' Function is used to publish the Json to the MQTT broker through MQTT ClientConnector @param topic:Topic of message