def on_message(self, client, userdata, msg): if (msg.topic == 'DtoG'): data = msg.payload.decode() awsConnector.awsPublish("data/gateway/sensorData", data) log = 'Received data from device -' + data logging.info(log) dataUtil.writeToFile(log)
def awsPublish(self, topic, message): # Connect to AWS IoT endpoint and publish a message self.myMQTTClient.connect() self.myMQTTClient.publish(topic, message, 0) logging.info("[INFO] Published!") dataUtil.writeToFile("[INFO] Published!") self.myMQTTClient.disconnect()
def publish(self, topic, json): client = mqtt.Client() # Initializing the MQTT object client.connect("127.0.0.1", 1883, 10) # Setting the connection parameters client.publish(topic, json, 0) # Publishing the message log = 'Published actuation -' + json + '- under topic -' + topic + '- to device' logging.info(log) dataUtil.writeToFile(log) client.disconnect() # disconnect
def run(self): # Connect to AWS IoT endpoint and publish a message self.myMQTTClient.connect() self.myMQTTClient.subscribe("data/cloud/actuationData", 0, self.on_message) #self.myMQTTClient.onMessage = self.on_message logging.info("[INFO] Subscribed!") dataUtil.writeToFile("[INFO] Subscibed!") while True: time.sleep(1)
def sendEmail(self, body): # Set config parameters to variables subject = "IoT Device Notification" configParams = ConfUtil.getValue('smtp.cloud') host = configParams.get("host") port = configParams.get("port") sender = configParams.get("fromaddr") receiver = configParams.get("toaddr") authToken = configParams.get("authtoken") # Send mail server = smtplib.SMTP(host, port) # connect to smtp server server.starttls() # Start TTLS service server.login(sender, authToken) # Login to mailserver message = 'Subject: {}\n\n{}'.format(subject, body) # set message contents server.sendmail(sender, receiver, message) # Send an email logging.info('Email notification sent..') dataUtil.writeToFile('Email notification sent')
def run(self): while True: cpu = psutil.cpu_percent() # Get CPU usage mem = psutil.virtual_memory().percent # Get RAM usage logger = logging.getLogger('log') logging.basicConfig(level=logging.INFO, format='%(asctime)s:%(levelname)s:%(message)s') # Logging the values cpulog = "CPU Utilization=" + str(cpu) memlog = "Memory Utilization=" + str(mem) logger.info(cpulog) logger.info(memlog) dataUtil.writeToFile(cpulog) dataUtil.writeToFile(memlog) self.now = datetime.now() self.nowtime = str(self.now.strftime("%Y-%m-%dT%H:%M:%S")) sysJson = '{"gateway":{"cpu": ' + str(cpu) + ',"mem": ' + str( mem) + ', "timestamp":' + self.nowtime + '}}' #awsConnector.awsPublish("data/gateway/sensorData", sysJson) sleep(6)
def actuationFalse(self, log): logging.info(log) dataUtil.writeToFile(log)
def actuationTrue(self, log): #smtp.sendEmail(log) logging.info(log) dataUtil.writeToFile(log)
def logAndWrite(self, data): logging.info(data) dataUtil.writeToFile(data)
def on_connect(self, client, userdata, flags, rc): dataUtil.writeToFile( "Subscribe to Device: Connected with result code " + str(rc)) logging.info("Subscribe to Device: Connected with result code " + str(rc))
def on_message(self, client, userdata, msg): data = msg.payload.decode() logging.info(data) dataUtil.writeToFile(data) actLogic.checkActuation(data)