123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import pymysql
- import paho.mqtt.client as mqtt
- import json
- import threading
- from time import sleep as sl
- from datetime import datetime as dt
- from concurrent.futures import ThreadPoolExecutor
- class MQTT(object):
-
-
-
-
-
-
-
-
-
-
-
-
-
- def __init__(self):
- self.res = 0
-
- def on_connect(client, userdata, flags, rc):
-
-
-
-
-
-
-
-
-
- client.subscribe('AISKY/Coffee/MK-G/b8:27:eb:b4:59:3e/Log')
-
- def on_message(client, userdata, msg):
- msg = msg
- payload = json.loads(msg.payload.decode('utf-8'))
- self.res = payload
- print('self.res = payload :', payload)
-
-
-
- self.client = mqtt.Client()
-
- self.command = {'tank-number':'', 'command':'coffee_mqtt_restart', 'value':''}
- def thread_job(self):
-
- self.client.on_connect = on_connect
-
- self.client.on_message = on_message
-
- self.client.username_pw_set("aisky-client", "aiskyc")
-
-
- self.client.connect("60.250.156.234", 1883, 60)
-
-
-
- self.client.loop_forever()
- mqtt_subscribe_thread = threading.Thread(target=thread_job, args=(self,))
- mqtt_subscribe_thread.daemon = True
- mqtt_subscribe_thread.start()
-
- sl(1)
-
-
- def mqttPublish(self, topic, command):
- self.client.publish(topic, json.dumps(command))
- print('json.dumps(command):', json.dumps(command))
-
- return True
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if __name__ == '__main__':
- mqtt = MQTT()
- try:
-
- mqtt.mqttPublish('AISKY/Coffee/MK-G/b8:27:eb:b4:59:3e', mqtt.command)
- print('mqttPublish_test')
- while True:
- pass
- except KeyboardInterrupt:
- mqtt.pool.shutdown(wait=True)
- mqtt.cursor.close()
- mqtt.db.close()
|