def on_message(self, client, userdata, msg):
     if (msg.topic == 'GtoD'):
         data = msg.payload.decode()
         dataUtil.logAndWrite('Received actuation data from Gateway: ' +
                              data)
         actuate.actuate(
             data
         )  # Calls the actuator method on each received actuation data
 def run(self):
     while True:
         cpu = psutil.cpu_percent()  # Get CPU usage
         mem = psutil.virtual_memory().percent  # Get RAM usage
         
         # Logging the values 
         cpulog = "CPU Utilization=" + str(cpu)
         memlog = "Memory Utilization=" + str(mem)
         dataUtil.logAndWrite(cpulog)
         dataUtil.logAndWrite(memlog)
         
         self.now = datetime.now()
         self.nowtime = str(self.now.strftime("%Y-%m-%dT%H:%M:%S"))
         
         sysJson = '{"device":{"cpu": '+ str(cpu) +',"mem": '+ str(mem) +', "timestamp":'+self.nowtime+' }}'
         #self.pub.publish("DtoG", sysJson)
         
         sleep(6)
 def addValue(self, data):
     self.currentValue = data
     self.totalValue += data
     self.sampleCount += 1
     if (self.maximumValue < data):
         self.maximumValue = data  # Sets maximum temperature value
     if (self.minimumValue > data):
         self.minimumValue = data  # Sets minimum temperature value
     self.getAverageValue()
     self.now = datetime.now()
     self.nowtime = str(self.now.strftime("%Y-%m-%dT%H:%M:%S"))
     self.log = "\n" + self.getName() + ":\n Time   : " + str(
         self.getTimeStamp()) + "\n Current: " + str(
             data) + "\n Average: " + str(
                 self.getAverageValue()) + "\n Samples: " + str(
                     self.getCount()) + "\n Min    : " + str(
                         self.getMinValue()) + "\n Max    : " + str(
                             self.getMaxValue()) + "\n"
     dataUtil.logAndWrite(self.log)  # Prints log to console
Пример #4
0
    def run(self):
        while True:
            # Get values
            self.getTemperature()
            self.getHumidity()
            self.getPressure()

            # Convert to JSON
            temperature = DataUtil.toDictSensor(self, tSensorData)
            humidity = DataUtil.toDictSensor(self, hSensorData)
            pressure = DataUtil.toDictSensor(self, pSensorData)

            # Create JSON object
            jsonData = DataUtil.tofinalJSON(self, temperature, humidity,
                                            pressure)

            # Publish JSON object to Gateway Device
            mqttpub.publish("DtoG", jsonData)

            dataUtil.logAndWrite('Published sensor data to Gateway: ' +
                                 jsonData)
            sleep(10)
    def senseDisplay(self, message):
        sensehat.clear()  # Clear the display
        sensehat.set_rotation(self.rotateDeg)  # set rotation
        try:
            dataUtil.logAndWrite('Message displayed on SenseHat LED: ')
            sensehat.show_letter(message)  # show message on LED
            dataUtil.logAndWrite('Actuation Successful..')

        except:
            dataUtil.logAndWrite(
                "Could not connect to the display!"
            )  # Print error if cannot connect to the actuator
        sleep(self.rate)
        sensehat.clear()
        sleep(self.rate)
 def on_connect(self, client, userdata, flags, rc):
     dataUtil.logAndWrite(
         "Subscribe to Gateway: Connected with result code " + str(rc))