|
# -*- coding: utf-8 -*-
import logging
import random
import time
from django_six import CompatibilityBaseCommand, close_old_connections
from paho.mqtt import client as mqtt_client
from api.eqpt_views import mqtt_upload_temperature
logger = logging.getLogger('console')
# defined(PRORON_MQTT_DOMESTIC_SERVER)
# define MQTT_SERVER_TYPE MQTT_SERVER_TYPE_DOMAIN_NAME
# define MQTT_BROKER_URI "china.mqtt.protontek.com" /* Domestic server "47.100.92.19" */
# define MQTT_USERNAME "proton"
# define MQTT_PASSWORD "proton123"
# define MQTT_PORT 1883
broker = 'china.mqtt.protontek.com'
username = 'proton'
password = 'proton123'
port = 1883
topic = 'esp/#'
client_id = f'python-mqtt-{random.randint(0, 1000)}'
# MQTT连接
def connect_mqtt():
def on_connect(client, userdata, flags, rc):
if rc == 0:
print('Connected to MQTT Broker')
else:
print('Failed to connect, return code %d\n', rc)
def on_disconnect(client, userdata, rc):
if rc != 0:
print("Unexpected MQTT disconnection. Will auto-reconnect")
# Set Connecting Client ID
print(f'Connected to MQTT Broker by client_id `{client_id}`')
# https://github.com/eclipse/paho.mqtt.python/issues/573
client = mqtt_client.Client(client_id, clean_session=False)
client.username_pw_set(username, password=password)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.connect(broker, port)
return client
# MQTT发布消息
# from commands.management.commands.mqtt import publish_run
# publish_run()
def publish(client):
msg_count = 0
while True:
time.sleep(1)
msg = f'messages: `{msg_count}`'
result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f'Send `{msg}` to topic `{topic}`')
else:
print(f'Failed to send message to topic {topic}')
msg_count += 1
def publish_run():
client = connect_mqtt()
client.loop_start()
publish(client)
# MQTT订阅消息
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
# Received `{"mac":"A4DA324E7A63","pkt":"215","chg_sta":true,"bat":"100","raw_temp":"4697,4696,4697","sta":"0","alg_temp":"4697,4696,4697","alg_gstr":0,"ble_rssi":-21,"wifi_rssi":-68,"current_time":"2021-08-08 15:22:59"}` from `esp/240AC4D3C1AC` topic
#
# {
# "mac": "A4DA324E7A63", # 体温贴 mac,固定 6 个字节,12 个字符
# "pkt": "215", # 广播包包序为 85,有效包序范围[1, 255]
# "chg_sta": true, # 充电状态,true 充电,false 未充电
# "bat": "100", # 电量剩余 65%,有效电量范围[0, 100]
# "raw_temp": "4697,4696,4697", # 三个原始温度数
# "sta": "0", # 算法返回状态
# "alg_temp": "4697,4696,4697", # 算法返回三个温度
# "alg_gstr": 0, # 算法手臂姿态
# "ble_rssi": -21, # 体温贴相对于底座的信号强度
# "wifi_rssi": -68, # 底座网络信号强度
# "current_time": "2021-08-08 15:22:59" # 底座接收到体温贴信息的实时时间
# }
payload = msg.payload.decode()
print(f'Received `{payload}` from `{msg.topic}` topic')
close_old_connections()
mqtt_upload_temperature(payload)
close_old_connections()
client.subscribe(topic)
client.on_message = on_message
def subscribe_run():
client = connect_mqtt()
subscribe(client)
client.loop_forever()
class Command(CompatibilityBaseCommand):
def handle(self, *args, **options):
logger.info('MQTT client is dealing')
subscribe_run()
|