def runTests(self, resourceName): #generated a random temperture data and created a sensordata object sensordata = SensorData() temperature = random.uniform(0.0, 30.0) sensordata.addValue(temperature) #created DataUtil instance and converted the sensordata to json data datautil = DataUtil() jsondata = datautil.toJsonFromSensorData(sensordata) print("the first json:\n" + jsondata) #post the jsondata to the server response = self.client.post(resourceName, jsondata, None, 10) logging.info(response.pretty_print()) #get data from the server response = self.client.get(resourceName) logging.info(response.pretty_print()) #put jsondata to the server response = self.client.put(resourceName, jsondata, None, 10) logging.info(response.pretty_print()) #delete resources response = self.client.delete(resourceName, None, 10) logging.info(response.pretty_print()) #stop the client self.client.stop()
def setUp(self): self.du = DataUtil() self.sd = SensorData() self.sd.addValue(15) self.sd.setName("Test") self.ad = ActuatorData() self.ad.addValue(44) self.ad.setName("Test") self.ad.setCommand("Test") pass
def __init__(self): logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG) self.connector = MqttClientConnector() self.datautil = DataUtil() self.broker_address = "broker.hivemq.com" self.port = 1883 self.client_id = "Charmi" self.password = None self.mul = MultiSensorAdaptorTask()
def on_message(self, client, userdata, msg): logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG) logging.info("Received Json: " + msg.topic + " " + str(msg.payload)) # Convert the received json data to sensordata instance self.sensorData = SensorData self.sensorData = DataUtil.toSensorDataFromJson( self, str(msg.payload)[1:].lstrip('\'').rstrip('\'')) # Convert the instance to json again data = DataUtil.toJsonFromSensorData(self, self.sensorData) # Log new JSON string. logging.info("The new JSON string: " + data)
class PersistenceUtil(object): ''' classdocs ''' datautil = DataUtil() def __init__(self): ''' Constructor ''' self.r = redis.Redis(host='localhost', port=6379) def writeSensorDataToRedis(self, SensorData): jsondata = self.datautil.toJsonFromSensorData(SensorData) #self.r.lpush("SensorData", jsondata) logging.info("Writing jsonSensorData to Redis...") logging.info("SensorData: " + jsondata) self.r.publish("SensorData", jsondata) def writeActuatorDataToRedis(self, ActuatorData): jsondata = self.datautil.toJsonFromActuatorData(ActuatorData) #self.r.lpush("ActuatorData", jsondata) self.r.publish("ActuatorData", jsondata) def getSensorData(self): sensorData = self.r.get("SensorData") return sensorData
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)
class Module05Test(unittest.TestCase): """ Use this to setup your tests. This is where you may want to load configuration information (if needed), initialize class-scoped variables, create class-scoped instances of complex objects, initialize any requisite connections, etc. """ def setUp(self): self.tempTask = TempSensorAdaptorTask() self.tempSensorData = SensorData() self.dataUtil = DataUtil() """ Use this to tear down any allocated resources after your tests are complete. This is where you may want to release connections, zero out any long-term data, etc. """ def tearDown(self): pass """ Place your comments describing the test here. """ def testtoJsonFromSensorData(self): self.tempSensorData.addValue(self.tempTask.getTemperature()) self.jsonData = self.dataUtil.toJsonFromSensorData(self.tempSensorData) print(type(self.jsonData)) self.assertTrue(type(self.jsonData) == str) pass
class MultiActuatorAdaptorTask(object): def __init__(self): self.ActData = ActData() self.dUtil = DataUtil() #Method for checking ActutatorData initialization def updateActuator(self, act_type, data): if act_type == "ActFromGD": if (data == "1"): self.stateVal = "ON" elif (data == "0"): self.stateVal = "OFF" self.ActData.setName("LED ACTUATOR") self.ActData.setCommand(data) self.ActData.setValue(data) self.ActData.setStateData(self.stateVal) self.ActData.updateData(self.ActData) print("Actuator Name: " + self.ActData.getName()) # print("COMMAND: " + self.ActData.getCommand()) jsonData = self.dUtil.toJsonFromActuatorData(self.ActData) logging.info("Actuator JSON: " + jsonData) return True else: print("Failed to send Actuation Signal") return False
class publisher(): def __init__(self): logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG) self.connector = MqttClientConnector() self.datautil = DataUtil() self.broker_address = "broker.hivemq.com" self.port = 1883 self.client_id = "Charmi" self.password = None self.mul = MultiSensorAdaptorTask() '''connecting with mqtt client and defining callbacks on class MqttClientConnector and publushing data after checking topic''' def publish(self): logging.info("Running publisher") client = mqtt.Client("Charmi") client.username_pw_set(self.client_id, self.password) client.connect(self.broker_address, self.port) logging.info("Connected !!!") client.loop_start() while True: #comment this for PING control Packets sleep(2) data = self.datautil.toJsonFromSensorData(self.mul.run()) print(data) client.on_connect = self.connector.on_connect( client, self.mul.run()) client.publish("test", data, 2, True)
def dataPublish(self): logging.info( "------------------Starting sensor reader---------------\n") while (True): self.pdsd.setLdr(self.ard.getLdrValues()) logging.info("LDR data received from arduino " + str(self.pdsd.getLdr())) self.pdsd.setSoilMoisture(self.ard.getSoilMoistureValues()) logging.info("Soil Moisture data received from arduino " + str(self.pdsd.getSoilMoisture())) self.pdsd.setConstrainMemoryUtil( SystemPerformanceAdaptor.getSystemMemoryUtil()) logging.info("Constrain Device System Memory Util " + self.pdsd.getConstrainMemoryUtil()) self.pdsd.setConstrainCpuUtil( SystemPerformanceAdaptor.getSystemCpuUtil()) logging.info("Constrain Device System CPU Util " + self.pdsd.getConstrainCpuUtil()) self.pdsd.setHumidity(self.ard.getHumidity()) logging.info("Humidity data received " + str(self.pdsd.getHumidity())) self.pdsd.setTemperature(self.ard.getTemperature()) logging.info("Temperature data received " + str(self.pdsd.getTemperature())) json = DataUtil.toJsonFromPlantDeviceSensorData(self.pdsd) logging.info("Sending data to gateway\n" + json) self.mqttP.publishMqtt(json) logging.info("Data sent to broker") sleep(120)
class PersistenceUtil(object): #Default Constructor def __init__(self): self.dataUtil = DataUtil() self.redis_connection = redis.Redis(host='localhost', port=6379) self.actuatorDataListener = ActuatorDataListener() self.sensorDataListener = SensorDataListener() ''' Write the actutorData to the DBMS ''' def readActuatorDataToDbms(self): redis_subscribe = self.redis_connection().pubsub() actuatorData_subscribe = redis_subscribe.subscribe("ActuatorData") redis_message = redis_subscribe.getMessage() self.registerActuatorDatatoDbmsListener() return ''' Method to write SensorData to DBMS''' def writeSensorDataToDbms(self, sensorData): json_sensorData = self.dataUtil.toJsonFromSensorData(sensorData) self.redis_connection.pubsub() self.redis_connection.publish("SensorData", json_sensorData) self.registerSensorDataToDbmsListener() return def registerSensorDataToDbmsListener(self): self.sensorDataListener.OnMessage() def registerActuatorDataToDbmsListener(self): self.actuatorDataListener.OnMessage()
class TempSensorAdaptorTask(threading.Thread): rateInSec = 10 curTemp = 0 sensorData = SensorData() sense = SenseHat() manager = SensorDataManager() dataUtil = DataUtil() ''' Read the Temperature from the SenseHAT ''' def __init__(self, rateInSec=10): threading.Thread.__init__(self) self.rateInSec = rateInSec self.time = self.sensorData.timeStamp def getTemperature(self): self.curTemp = self.sense.get_temperature() return self.curTemp def run(self): while True: self.sensorData.setName('Temp') self.sensorData.addValue(self.getTemperature()) #print(self.sensorData.curValue) self.manager.handleSensorData(self.sensorData) self.dataUtil.toJsonFromSensorData(self.sensorData) sleep(self.rateInSec)
def writeActuatorToDataDbms(self,ActuatorData): dbData = DataUtil.toJsonFromActuatorData(self, ActuatorData) r = redis.StrictRedis(host=self.host, port=self.port, password=self.auth, decode_responses=True) r.set(ActuatorData.getTimeStamp()+"- Actuator",dbData) r.publish("Actuator", dbData) #logging.info("Sending data to DB") r.close() return True
def __init__(self, host): self.sensorData = SensorData self.sensorData.addValue(self.sensorData, 23.34) self.data = DataUtil.toJsonFromSensorData(self, self.sensorData) logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG) logging.info("Json to send: " + self.data) host, port, self.path = parse_uri(host) self.client = HelperClient(server=(host, port))
def run(self): i=0 logging.info("Connecting to broker") self.mqtt.connect() logging.info("Connecting to broker") while(i<2): logging.info("Publishing data using QoS1") message = DataUtil.toJsonFromSensorData(self.sensorAdaptor.getSensorData()) self.mqtt.publishSensorData(message,1) i+=1 sleep(5) while(i<4): logging.info("Publishing data using QoS2") message = DataUtil.toJsonFromSensorData(self.sensorAdaptor.getSensorData()) self.mqtt.publishSensorData(message,2) i+=1 sleep(10) self.mqtt.clientClose() logging.info("Finished Publishing") return True
def __init__(self): self.temp = TempSensorAdaptorTask() self.humidity = HumiditySensorAdaptorTask() self.pressure = PressureSensor() self.soilmoisture = SoilMoistureSensor() self.Luminance = LuminanceSensor() self.gas = GasSensor() self.phvalue = Phmeter() self.datautil = DataUtil() self.all = AllSensorData() self.cpuUtil = SystemCpuUtilTask() self.send = SmtpClientConnector()
def run(self): i = 0 logging.info("Connecting to Server") while (i < 1): logging.info("Publishing data") message = DataUtil.toJsonFromSensorData( self.sensorAdaptor.getSensorData()) self.ccc.send(message) i += 1 sleep(5) logging.info("Finished Publishing") return True
class SensorDataListener(threading.Thread): ''' classdocs ''' #initialize DataUtil dUtil = DataUtil() def __init__(self, r_sensor): ''' Constructor ''' threading.Thread.__init__(self) #set the sensor reference to one by PersistenceUtil self.r_sensor = r_sensor #create instance of pubsub self.r_pubsub = r_sensor.pubsub() #subscribe to keyspace event notifications self.r_pubsub.psubscribe('__keyspace@0__:*') #SensorData callback function def onMessage(self, sensorData): #DO SOMETHING pass def run(self): #listen for new data on the subscribed keyspace notification events for event in self.r_pubsub.listen(): #ignore the first message if type(event['data']) != int: #see if the event is a set event if event['data'].decode() == 'set': #get the channel which consists of the key event_data = event['channel'].decode() #split it to only get the value after the colon channel = event_data.split(':') sensorDataJson = self.r_sensor.get(channel[1]) #call DataUtil to convert it to sensorData sensorData = self.dUtil.toSensorDataFromJson( sensorDataJson) #send to onMessage self.onMessage(sensorData)
def run(self): sensordata = SensorData.SensorData() while True: sense = SenseHat() #generate new temperature data temperature = sense.get_temperature() sensordata.addValue(temperature) #add into log logging.info( "\n---------------------------------\nNew sensor readings:\n" + "name=" + sensordata.name + ",timeStamp=" + sensordata.timeStamp + ",minValue=" + str(sensordata.minValue) + ",aveValue=" + str(sensordata.avgValue) + ",maxValue=" + str(sensordata.maxValue) + ",curValue=" + str(sensordata.curValue) + ",totValue=" + str(sensordata.totValue) + ",sampleCount=" + str(sensordata.sampleCount)) #convert to json format from python object datautil = DataUtil() jsondata = datautil.toJsonFromSensorData(sensordata) if (abs(sensordata.curValue - sensordata.avgValue) > 2): logging.info( "\nCurrent temp exceeds average by >2. Converting data...\n" + "JSON data:\n" + jsondata) #smtp module smtpClientConnector = SmtpClientConnector.SmtpClientConnector() smtpClientConnector.publishMessage("Temperature", jsondata) # logging.info("send email successfully") #write json dta to filesystem of = open("tempData.json", "w+") of.write(jsondata) of.close() # logging.info("write data as JSON file to filesystem successfully") time.sleep(10)
class MqttClientConnector(object): ''' classdocs ''' tempSensor = TempSensorAdaptorTask() sensorData = SensorData() dataUtil = DataUtil() client = mqtt.Client() def __init__(self): ''' Constructor ''' ''' Callback function ''' def _on_connect(self, client, userdata, flags, rc): print("Connected with result code " + str(rc)) def _on_disconnect(self, client, userdata, rc): print("Dissconnected with result code: " + str(rc)) def _on_message(self, client, userdata, msg): print(msg.topic + "" + str(msg.payload)) def getSensorData(self): temp = self.tempSensor.getTemperature() self.sensorData.addValue(temp) jsonData = self.dataUtil.toJsonFromSensorData(self.sensorData) return jsonData ''' Connect to remote mqtt broker:mqtt.eclipse.org publish msg ''' def run(self): self.client.on_connect = self._on_connect self.client.on_message = self._on_message self.client.on_disconnect = self._on_disconnect self.client.connect("mqtt.eclipse.org", 1883, 60) jsonData = self.getSensorData() logging.info("SensorData befor publishing: " + jsonData) self.client.loop_start() self.client.publish("kai_test1", jsonData, 2, True) self.client.loop_stop() self.client.disconnect()
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)
def registerActuatorDataDbmsListener(self,ActuatorDataListener): db = redis.StrictRedis(host=self.host, port=self.port, password=self.auth, decode_responses=True) dbs = db.pubsub() dbs.subscribe("Actuator") #logging.info("Waiting for message") for new_message in dbs.listen(): adj = db.get(new_message['data']) #logging.info("Received new message" + str(new_message['data'])) if(new_message['data']!=1): logging.info("New Actuator Reading Received with ID \n" + str(new_message['data'])) #logging.info("New Actuator Data Received - " + adj ) sleep(.5) jdata = db.get(new_message['data']) logging.info("data - >"+jdata) ad = DataUtil.toActuatorDataFromJson(self, jdata) ActuatorDataListener.onActuatorMessage(ad) return True
class ActuatorDataListener(threading.Thread): redis_server = redis.StrictRedis(host='127.0.0.1', port=6379, db=0) # Initializing Redis Server util = DataUtil() actuation_counter = 1 ''' * Class constructor invoking thread ''' def __init__(self): threading.Thread.__init__(self) ''' * Boolean function which returns true whenever a new ActuatorData instance is recorded in database * Output: True/False (Boolean) ''' def on_Actuator_Message(self): if (self.redis_server.exists(str(self.actuation_counter))): print("Above threshold ........LED actuation begins") self.x = self.redis_server.get(str(self.actuation_counter)) self.y = self.util.jsonToActuatorData(self.x) self.actuation_counter = self.actuation_counter + 1 return True ''' * Runnable thread function ''' def run(self): while (True): self.on_Actuator_Message() time.sleep(6.5) def get_alo_object(self): return self.y def get_x_jsonactdata(self): return self.x
class ActuatorDataListener(object): ''' classdocs ''' #persistenceUtil = PersistenceUtil() dataUtil = DataUtil() multiActuatorAdaptor = MultiActuatorAdaptor() actuatorData = None def __init__(self): ''' Constructor ''' self.r = redis.Redis(host='localhost', port=6379) def listener(self): pubsub = self.r.pubsub() pubsub.subscribe('ActuatorData') #print("Starting message loop") while (True): message = pubsub.get_message() if message: #sensorDataJson = self.persistenceUtil.getSensorData() #print(message) #print(message['data']) if (type(message['data']) != int): logging.info( "Listening on Redis, jsonActuatorData retrieved from Redis..." ) logging.info("ActuatorData: " + str(message['data'])) print("---------------------------") self.actuatorData = self.dataUtil.toActuatorDataFromJson( message['data']) #print(self.actuatorData.Command) self.multiActuatorAdaptor.updateActuator(self.actuatorData)
from labs.common.SensorData import SensorData from labs.module08.TempSensorAdaptorTask import TempSensorAdaptorTask from labs.common.DataUtil import DataUtil import logging from time import sleep if __name__ == '__main__': resource = 'temp' #payload = "for test" logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.DEBUG) print('------->') logging.info("Connecting to Coap Server...") coapClientConnector = CoapClientConnector() tempSensorData = SensorData() temp = TempSensorAdaptorTask() dataUtil = DataUtil() while (True): tempSensorData.addValue(temp.getTemperature()) payload = dataUtil.toJsonFromSensorData(tempSensorData) print("Json Before issuing the request...") logging.info(payload) print('------->') coapClientConnector.postRequest(resource, payload) # coapClientConnector.putRequest(resource, payload) coapClientConnector.getRequest(resource) # coapClientConnector.deleteRequest(resource) sleep(10)
''' Created on Feb 29, 2020 @author: Naveen Rajendran ''' import paho.mqtt.client as mqtt import json from labs.common.DataUtil import DataUtil # Define Variables MQTT_HOST = "127.0.0.1" MQTT_PORT = 1883 MQTT_KEEPALIVE_INTERVAL = 40 json_tool = DataUtil() mqttc = mqtt.Client() ''' * This class is for the purpose of establishing connection between MQTT client & Broker ''' class MqttClientConnector(): ''' * Constructor function establishes MQTT connection using predefined port no & IP address given by user ''' def __init__(self): mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL) print("MQTT SESSION ESTABLISHED") mqttc.loop_start() ''' * Publish SensorData object in json format to GatewayHandlerApp using MQTT
@author: Naveen Rajendran ''' import logging import paho.mqtt.client as mqttClient import time import ssl from labs.common.DataUtil import DataUtil from labs.common.ConfigUtil import ConfigUtil from labs.module09.ArduinoDataReceiver import SensorData_Object from labs.module09.ArduinoDataReceiver import DeviceData_Object import threading from labs.module09.SensorDataManager import logging from labs.module09.ActuatorAdaptor import ActuatorAdaptor mqtt_client = mqttClient.Client() convert_json = DataUtil() act_obj = ActuatorAdaptor() connected_flag = False publish_flag = False subscribe_flag = False """ Class which connects gateway device with UbidotsCloudService using MQTT/TLS protocol. Performs Pub/Sub functions whenever a SensorData is received """ class UbidotsCloudConnector(threading.Thread): def __init__(self): """ Class constructor which initializes all required parameters for connecting ubidots cloud service """
''' Created on Feb 20, 2020 @author: Naveen Rajendran ''' from labs.common.DataUtil import DataUtil import redis import logging from labs.common.ActuatorData import ActuatorData from labs.common.SensorData import SensorData util = DataUtil() """ * This class is used for writing Actuator/Sensor JSON data in Redis DataBase """ class PersistenceUtil(): """ * PersistenceUtil constructor function check for Actuator/SensorData instance & converts into JSON string using DataUtil class functions * Input: SensorData (Object) or ActuatorData (Object) """ def __init__(self, input_obj): if (isinstance(input_obj, SensorData)): self.sdo = input_obj self.sd = util.sensordatatojson(self.sdo) self.writeSensorDatatoDbms(self.sd) elif (isinstance(input_obj, ActuatorData)): self.ado = input_obj self.ad = util.actuatordatatojson(self.ado)
def __init__(self): self.ActData = ActData() self.dUtil = DataUtil()
''' Creating the Sensor Data ''' sensor = SensorData(topic,0,30) sensor.curValue = uniform(float(sensor.getMinValue()), float(sensor.getMaxValue())); sensor.addValue(sensor.curValue); sensor.diffValue = sensor.curValue - sensor.avgVal; sensor.timestamp = datetime.now(); logging.info('SensorData to be sent:') print("Sensor data before converting to Json: " + str(sensor)); ''' Converting the Sensor Data to the JSON format ''' data = DataUtil() jsonData = data.SensorDataToJson(sensor) logging.info('SensorData converted into Json:') print("SensorData in Json Format before publishing" + "\n" + str(jsonData) + "\n") pub_client = MqttClientConnector(); ''' This Function is used to publish the JSON data to the MQTT broker through the MQTT ClientConnector class @param topic: Topic of the message @param jsonData: It is the JSON Payload @param host: It is the address of the MQTT broker ''' pub_client.publish(topic,jsonData,host)