def run(self):
        while True:
            if self.enableEmulator:
                self.sensor.curVal = uniform(float(self.sensor.getMinValue()),
                                             float(self.sensor.getMaxValue()))
                self.sensor.addValue(self.sensor.curVal)
                self.sensor.diffVal = self.sensor.curVal - self.sensor.avgVal
                print(self.sensor)
                if self.sensor.curVal >= (self.sensor.getAvgValue() + 2):
                    data = DataUtil()
                    self.sensor.timestamp = datetime.now()
                    json_data = data.toJsonfromSensor(self.sensor)
                    SensorData.SensorData.breach_values.append(self.sensor)
                    print(SensorData.SensorData.breach_values)
                    print(
                        "warning!!Temperature exceeded the average temperature by %.2f degrees"
                        % (self.sensor.diffVal))
                    sen_not = SmtpClientConnector.SmtpClientConnector()
                    sen_not.publishMessage("Temperature Notification",
                                           json_data)

                sleep(3)
@author: GANESHRAM KANAKASABAI
'''

from labs.common import ConfigConst
from labs.common.ConfigUtil import ConfigUtil
from labs.module06.MqttClientConnector import MqttClientConnector
from labs.common.DataUtil import DataUtil
import logging
'''
Setting values for Topic and address for MQTT broker
'''
topic = "Temperature Sensor"
config = ConfigUtil('../../../config/ConnectedDevicesConfig.props')
host = config.getProperty(ConfigConst.ConfigConst.MQTT_CLOUD_SECTION,
                          ConfigConst.ConfigConst.HOST_KEY)

subscribe = MqttClientConnector(topic)
subscribe.subscribe(host)  # Connecting to MQTT Broker
msg = subscribe.message()  # Subscribing to specefied Topic
logging.debug('JSon Data Received:')
print("Json Data Received:\n " + str(msg) + "\n")

data = DataUtil()
sensor = data.toSensorfromJson(msg)  # Converting Json data to Sensor Data
logging.debug('Json data converted into SensorData')
print("Received message in SensorData format" + str(sensor))

json = data.toJsonfromSensor(sensor)
logging.debug('SensorData converted into Json Data: ')
print('SensorData converted to Json Data again: \n' + str(json))
Exemplo n.º 3
0
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
@param json_data: Json Payload
@param host: address of MQTT broker 
'''
pub_client.publish(topic, json_data, host)