Beispiel #1
0
 def runTests(self, resource):
     sensorData = SensorData(resource, 10, 30)
     sensorData.addValue(10)
     dataUtil = DataUtil()
     jsondata = dataUtil.sensorTojson(sensorData)
     self.initClient()
     self.pingServer()
     self.handleGet(resource)
     self.handlePost(resource, jsondata)
     self.handleGet(resource)
     sensorData.addValue(20)
     jsondata = dataUtil.sensorTojson(sensorData)
     self.handlePut(resource, jsondata)
     self.handleGet(resource)
     self.handleDelete(resource)
     self.handleGet(resource)
Beispiel #2
0
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)
Created on Mar 5, 2019
Simple Python script for MQTT Subscriber Client
@author: Shyama Sastha Krishnamoorthy Srinivasan
'''

import logging
from labs.common import ConfigConst
from labs.common.ConfigUtil import ConfigUtil
from labs.common.DataUtil import DataUtil
from labs.module06.MqttClientConnector import MqttClientConnector

#Setting values for Topic and address for MQTT broker
topic = "Temperature Sensor"
config = ConfigUtil('../../../config/ConnectedDevicesConfig.props')
host = config.getProperty(ConfigConst.MQTT_GATEWAY_SECTION,
                          ConfigConst.HOST_KEY)

subscribe = MqttClientConnector(topic)
subscribe.subscribe(host)  # Connecting to MQTT Broker
msg = subscribe.message()  # Subscribing to required topic
logging.debug('\nJSon Received: ')
print("\nReceived Json data: \n" + str(msg))

data = DataUtil()
sensorData = data.jsonTosensor(msg)  # Converting Jsondata to Sensordata
logging.debug('\nJson in "SensorData format": ')
print("\nMessage received in 'SensorData format': \n" + str(sensorData) + "\n")

jsondata = data.sensorTojson(sensorData)  # Converting Jsondata to Sensordata
logging.debug('\nBack to Json: ')
print("\nMessage converted back to Json: \n" + str(jsondata) + "\n")