def writeSensorDataToDbms(self, sd): du = DataUtil() d = du.toJsonFromSensorData(sd) data = json.loads(d) # print(data) c = redis.Redis(host="localhost", port="6379") c.set(sd.getName(), str(data))
def getSensorDataFromDbms(self): r = redis.Redis(host="localhost", port="6379") actData2 = r.get("Temperature") if (actData2 == None): return None else: actData2 = actData2.decode() du = DataUtil() curVal = du.toSensorDataFromJson(actData2) return curVal
def getActuatorDataFromDbms(self): r = redis.Redis(host="localhost", port="6379") actData2 = r.get("Actuator_Data") if (actData2 == None): return "1" else: actData2 = actData2.decode() du = DataUtil() # curVal=du.toActuatorDataFromJson(actData2) # return curVal j = du.toActuatorDataFromJson(actData2) return j
class Module07Test(unittest.TestCase): sensorD = SensorData() #hasConf=confU.hasConfig() curVal = sensorD.getCurrentValue() avgVal = sensorD.getAverageValue() actD = ActuatorData() du = DataUtil() adl = ActuatorDataListener() j = "" def testSensorData(self): self.assertTrue(isinstance(self.curVal, float), "Float Value") self.assertTrue(self.avgVal >= 0.0 and self.avgVal <= 30.0, "Average Temperature within the range") ''' It checks for the datatype of the getCommand function to be String. ''' def testDataUtil(self): self.assertTrue( isinstance(self.du.toJsonFromSensorData(self.sensorD), str), "String") def testActuatorDataFromJson(self): self.assertTrue( isinstance(self.du.toActuatorDataFromJson(self.j), str), "String") def testActuatorDataListener(self): self.assertTrue( isinstance(self.adl.onActuatorMessage(str(self.curVal)), float), "Type String")
class coapclient: host = "127.0.0.1" port = 5683 path ="coap://127.0.0.1:5683" du=DataUtil() def coapTest(self, s): # print("here") client = HelperClient(server=(self.host, self.port)) # print(s) join_thread client.get(self.path) #client.post(path, "1") # json2=json.loads(s) # print("hello") client.put(self.path,payload=self.du.toSensorDataFromJson(s)) BasicResource() #client.get(path) client.delete(self.path, timeout=2) #client.get(path) # client.post(self.path,payload=self.du.toSensorDataFromJson(s)) #client.get(path) #client.put(path,payload="2") client.stop()
''' Created on Mar 4, 2020 @author: Kratika Maheshwari ''' import paho.mqtt.client as mqtt import json from labs.module05.DataUtil import DataUtil from labs.common.SensorData import SensorData MQTT_Host = "127.0.0.1" MQTT_Port = 1883 MQTT_KeepAlive = 10 du = DataUtil() sd = SensorData() mqttc = mqtt.Client() class MqttClientConnector(): def __init__(self): mqttc.connect(MQTT_Host, MQTT_Port, MQTT_KeepAlive) print("MQTT Session established") def publish_sensor_data(self, mqtt_topic, sd): mqtt_msg = du.toJsonFromSensorData(sd) print(mqtt_msg) mqttc.publish(mqtt_topic, mqtt_msg) def subscribe_actuator(self): mqttc.subscribe("ActuatorData")
class TempSensorAdaptorTask(Thread): ''' Variables are defined and objects of required classes are created with the class scope. ''' sensorData = SensorData() #constructor of sensorData is created. sensorDataM = SensorDataManager( ) #constructor of SensorDataManager is created. smtpCCObject = SmtpClientConnector.SmtpClientConnector( ) ##constructor of SmtpClientConnector is created. confP = ConfigUtil() #constructor of ConfigUtil is created. avgValue = 0.0 count = 0.0 total = 0.0 currentValue = 0.0 maxValue = 0.0 minValue = 0.0 timestamp = None sh = SenseHat() #constructor of SenseHat is created. du = DataUtil() ''' nominal temperature is fetched from configuration file using object of ConfigParser. ''' nominal = confP.getValue("device", "nominalTemp") ''' constructor of class TempSensorAdaptorTask ''' def __init__(self, name): # self.generateTemp() Thread.__init__(self) self.name = name self.temp = pu.getSensorDataFromDbms() self.act = pu.getActuatorDataFromDbms() # self.act=None if (self.temp == None): self.run() # print(self.temp) ''' this run function overrides the run function of Thread and calls the sense hat function to generate the temperature values,addValue fnctn of sensorData is called handleSensorData of class sensorDataManager is called and sensorData object is passed as a parameter. This function will return the actuator data. notification fucn will be called to check the condition of sending the email ''' def run(self): #for i in range(0,10): while True: self.val = self.sh.get_temperature( ) #calling get_temperature function j = {} #curVal=pu.getActuatorDataFromDbms() jj = pu.getActuatorDataFromDbms() if (len(self.act) < 20): pu.writeActuatorDataToDbms() # print(jj) adl = ActuatorDataListener() c = adl.onActuatorMessage(self.temp) if (c != self.temp): self.temp = c self.sensorData.addValue( self.val) #calling addValue function of sensorData # self.act_data=self.sensorDataM.handleSensorData(self.sensorData) self.getSensorData() sdj = self.du.toJsonFromSensorData(self.sensorData) print("**************") print(sdj) st = pu.writeSensorDataToDbms(self.sensorData) time.sleep(2) ''' values of sensor data is fetched to store in local variables. ''' def getSensorData(self): self.avgValue = self.sensorData.getAverageValue() self.count = self.sensorData.getCount() self.total = self.sensorData.getTotal() self.currentValue = self.sensorData.getCurrentValue() self.maxValue = self.sensorData.getMaxValue() self.minValue = self.sensorData.getMinValue() self.timestamp = self.sensorData.getTimestamp() ''' notification regarding email alert and logging information is handled by sendNotification function, it takes 3 arguments namely command which is set to actuator data, current Temperature value and the nominal value ''' def sendNotification(self, command, current, nominal): logging.basicConfig(level=logging.INFO, format='%(message)s') # logging.basicConfig(level=logging.INFO) data = "Temperature:\nTime: " + str( self.timestamp) + "\nCurrent: " + str( self.currentValue) + "\nAverage: " + str( self.avgValue) + "\nSamples: " + str( self.count) + "\nMin: " + str( self.minValue) + "\nMax: " + str(self.maxValue) logging.info(data) print("\n")