加入空调控制

This commit is contained in:
幸运锦鲤 2024-09-07 20:46:39 +08:00
parent c702c85468
commit e81fd65ae9
5 changed files with 130 additions and 75 deletions

View File

@ -27,6 +27,7 @@ class Config:
ACTION_water_heater = '08f9e07db383' ACTION_water_heater = '08f9e07db383'
ACTION_wen_xiang = '5ccf7fd6178a' ACTION_wen_xiang = '5ccf7fd6178a'
ACTION_light = '840d8e9989dc' ACTION_light = '840d8e9989dc'
ACTION_kongtiao = 'kongtiao'
# 程序变量 # 程序变量
RUN_FLAG = 1 RUN_FLAG = 1

View File

@ -28,6 +28,12 @@
打开CC AF 打开CC AF
关闭DD BF 关闭DD BF
空调:
打开FF 11
关闭FF 22
遥控打开空调并开启制冷FF 66口令打开制冷对应红外第三组
遥控关闭空调FF 88 (口令:关闭制冷;对应红外第四组)
485 红外2 485 红外2
第一组68 08 00 FF 12 00 11 16 第一组68 08 00 FF 12 00 11 16
第二组68 08 00 FF 12 01 12 16 第二组68 08 00 FF 12 01 12 16

33
main.py
View File

@ -60,6 +60,11 @@ run = bytearray([0xAA, 0x55, 0x05, 0x55, 0xAA])
# 校验整除 # 校验整除
check_code = bytearray([0xAA, 0x55, 0x04, 0x55, 0xAA]) check_code = bytearray([0xAA, 0x55, 0x04, 0x55, 0xAA])
# 打开制冷(第三组)
refrigeration_on = bytearray([0x68, 0x08, 0x00, 0xFF, 0x12, 0x02, 0x13, 0x16])
# 关闭制冷(第四组)
refrigeration_off = bytearray([0x68, 0x08, 0x00, 0xFF, 0x12, 0x03, 0x14, 0x16])
control = master_control.open_serial(Config.CONTROL_OPEN_SERIAL, 9600, 0.5) control = master_control.open_serial(Config.CONTROL_OPEN_SERIAL, 9600, 0.5)
@ -119,7 +124,9 @@ def run_start_main(mqtt_client):
elif (ser_cmd == 'd388') or (client.callback_result == 'd388'): elif (ser_cmd == 'd388') or (client.callback_result == 'd388'):
master_control.infrared_sensor(control, infrared_control_off) master_control.infrared_sensor(control, infrared_control_off)
glc.info('已关闭', gd)
client.callback_result = 'xxx' client.callback_result = 'xxx'
# WIFI插座 # WIFI插座
elif (ser_cmd == 'bba1') or (client.callback_result == 'bba1'): elif (ser_cmd == 'bba1') or (client.callback_result == 'bba1'):
action_cmd.action(client, Config.ACTION_socket, 'a1') action_cmd.action(client, Config.ACTION_socket, 'a1')
@ -144,12 +151,35 @@ def run_start_main(mqtt_client):
action_cmd.action(client, Config.ACTION_wen_xiang, 'a1') action_cmd.action(client, Config.ACTION_wen_xiang, 'a1')
glc.info('电蚊香已打开', gd) glc.info('电蚊香已打开', gd)
client.callback_result = 'xxx' client.callback_result = 'xxx'
# 关闭热水器 # 关闭电蚊香
elif (ser_cmd == 'bbdf') or (client.callback_result == 'bbdf'): elif (ser_cmd == 'bbdf') or (client.callback_result == 'bbdf'):
action_cmd.action(client, Config.ACTION_wen_xiang, 'b1') action_cmd.action(client, Config.ACTION_wen_xiang, 'b1')
glc.info('电蚊香已关闭', gd) glc.info('电蚊香已关闭', gd)
client.callback_result = 'xxx' client.callback_result = 'xxx'
# 打开空调
elif (ser_cmd == 'ff11') or (client.callback_result == 'ff11'):
action_cmd.action(client, Config.ACTION_kongtiao, 'a1')
glc.info('空调已打开', gd)
client.callback_result = 'xxx'
# 关闭空调
elif (ser_cmd == 'ff22') or (client.callback_result == 'ff22'):
action_cmd.action(client, Config.ACTION_kongtiao, 'b1')
glc.info('空调已关闭', gd)
client.callback_result = 'xxx'
# 打开制冷
elif (ser_cmd == 'ff66') or (client.callback_result == 'ff66'):
su = master_control.infrared_sensor(control, refrigeration_on)
if su == 1:
glc.info('制冷模式已打开', gd)
client.callback_result = 'xxx'
# 关闭制冷
elif (ser_cmd == 'ff88') or (client.callback_result == 'ff88'):
su = master_control.infrared_sensor(control, refrigeration_off)
if su == 1:
glc.info('制冷模式已关闭', gd)
client.callback_result = 'xxx'
# 校验语音模块串口是否运行异常 # 校验语音模块串口是否运行异常
elif (ser_cmd == 'ffffffffff') or (client.callback_result == 'ffffffffff'): elif (ser_cmd == 'ffffffffff') or (client.callback_result == 'ffffffffff'):
master_control.run_flage(voice, check_code, ser_cmd) master_control.run_flage(voice, check_code, ser_cmd)
@ -188,6 +218,7 @@ if __name__ == '__main__':
client.subscribe(Config.ACTION_socket) client.subscribe(Config.ACTION_socket)
client.subscribe(Config.ACTION_water_heater) client.subscribe(Config.ACTION_water_heater)
client.subscribe(Config.ACTION_wen_xiang) client.subscribe(Config.ACTION_wen_xiang)
client.subscribe(Config.ACTION_kongtiao)
client.light_subscribe(Config.ACTION_light) client.light_subscribe(Config.ACTION_light)
action_cmd.action(client, Config.ACTION_light, 'qc') action_cmd.action(client, Config.ACTION_light, 'qc')

View File

@ -1,74 +1,74 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*-coding:utf-8 -*- # -*-coding:utf-8 -*-
""" """
# File : python_mqtt.py # File : python_mqtt.py
# Time 2024/5/2 下午 4:17 # Time 2024/5/2 下午 4:17
# Modify sityliu # Modify sityliu
# Author :蓝陌 # Author :蓝陌
# version python 3.8 # version python 3.8
# Description # Description
""" """
from paho.mqtt import client as mqtt_client from paho.mqtt import client as mqtt_client
import os import os
import time import time
from src import SaveLogger from src import SaveLogger
from src.glogcenter import glc from src.glogcenter import glc
from config import Config from config import Config
gd = glc.GlcData() gd = glc.GlcData()
gd.system = 'smart_home' gd.system = 'smart_home'
gd.traceId = os.path.basename(__file__) gd.traceId = os.path.basename(__file__)
log_file_path = os.path.join(os.getcwd(), 'logs/mqtt_status.log') log_file_path = os.path.join(os.getcwd(), 'logs/mqtt_status.log')
save_log = SaveLogger(os.path.basename(__file__), log_file=log_file_path) save_log = SaveLogger(os.path.basename(__file__), log_file=log_file_path)
class MQTTClient: class MQTTClient:
def __init__(self, broker, port, topic, client_id): def __init__(self, broker, port, topic, client_id):
self.broker = broker self.broker = broker
self.port = port self.port = port
self.topic = topic self.topic = topic
self.client_id = client_id self.client_id = client_id
self.client = None self.client = None
self.callback_result = 'xxx' # 用于存储回调函数的返回值 self.callback_result = 'xxx' # 用于存储回调函数的返回值
def connect(self): def connect(self):
def on_connect(def_client, userdata, flags, rc): def on_connect(def_client, userdata, flags, rc):
if rc == 0: if rc == 0:
save_log.info("Connected to MQTT Broker!") save_log.info("Connected to MQTT Broker!")
glc.info("Connected to MQTT Broker!", gd) glc.info("Connected to MQTT Broker!", gd)
else: else:
save_log.info("Failed to connect, return code:" + rc) save_log.info("Failed to connect, return code:" + rc)
glc.error("Failed to connect, return code:" + rc, gd) glc.error("Failed to connect, return code:" + rc, gd)
self.client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1, self.client_id) self.client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1, self.client_id)
self.client.username_pw_set('admin', password='admin') self.client.username_pw_set('admin', password='admin')
self.client.on_connect = on_connect self.client.on_connect = on_connect
self.client.connect(self.broker, self.port) self.client.connect(self.broker, self.port)
def loop_start(self): def loop_start(self):
self.client.loop_start() self.client.loop_start()
def publish(self, topic, sed_msg): def publish(self, topic, sed_msg):
self.client.publish(topic, sed_msg) self.client.publish(topic, sed_msg)
# print(f"Send{sed_msg} to topic{topic}") # print(f"Send{sed_msg} to topic{topic}")
def subscribe(self, topic): def subscribe(self, topic):
def on_message(client, userdata, message): def on_message(client, userdata, message):
data_cmd = str(message.payload.decode("utf-8")) data_cmd = str(message.payload.decode("utf-8"))
save_log.info(f"MQTT Received message: {data_cmd}") save_log.info(f"MQTT Received message: {data_cmd}")
glc.info(f"MQTT Received message: {data_cmd}", gd) glc.info(f"MQTT Received message: {data_cmd}", gd)
self.callback_result = data_cmd # 将回调函数的返回值存储在类的成员变量中 self.callback_result = data_cmd # 将回调函数的返回值存储在类的成员变量中
self.client.subscribe(topic) self.client.subscribe(topic)
self.client.on_message = on_message self.client.on_message = on_message
def light_subscribe(self, topic): def light_subscribe(self, topic):
def on_message2(client, userdata, message): def on_message2(client, userdata, message):
data_cmd = str(message.payload.decode("utf-8")) data_cmd = str(message.payload.decode("utf-8"))
save_log.info(f"light_subscribe: {data_cmd}") save_log.info(f"light_subscribe: {data_cmd}")
glc.info(f"light_subscribe: {data_cmd}", gd) glc.info(f"light_subscribe: {data_cmd}", gd)
Config.LIGHT_STATUS = data_cmd # 将回调函数的返回值存储在类的成员变量中 Config.LIGHT_STATUS = data_cmd # 将回调函数的返回值存储在类的成员变量中
self.client.subscribe(topic) self.client.subscribe(topic)
self.client.on_message = on_message2 self.client.on_message = on_message2

View File

@ -66,6 +66,23 @@ def get_voice_data(ser_data):
elif com_input.hex() == 'bbdf': elif com_input.hex() == 'bbdf':
save_log.info(f'关闭电蚊香指令处理完返回,command:{com_input.hex()}') save_log.info(f'关闭电蚊香指令处理完返回,command:{com_input.hex()}')
glc.info(f'关闭电蚊香指令处理完返回,command:{com_input.hex()}',gd) glc.info(f'关闭电蚊香指令处理完返回,command:{com_input.hex()}',gd)
# 打开空调
elif com_input.hex() == 'ff11':
save_log.info(f'打开空调指令处理完返回,command:{com_input.hex()}')
glc.info(f'打开空调指令处理完返回,command:{com_input.hex()}', gd)
# 关闭电蚊香
elif com_input.hex() == 'ff22':
save_log.info(f'关闭空调指令处理完返回,command:{com_input.hex()}')
glc.info(f'关闭空调指令处理完返回,command:{com_input.hex()}', gd)
# 打开制冷
elif com_input.hex() == 'ff66':
save_log.info(f'打开制冷指令处理完返回,command:{com_input.hex()}')
glc.info(f'打开制冷指令处理完返回,command:{com_input.hex()}', gd)
# 关闭制冷
elif com_input.hex() == 'ff88':
save_log.info(f'关闭制冷指令处理完返回,command:{com_input.hex()}')
glc.info(f'关闭制冷指令处理完返回,command:{com_input.hex()}', gd)
elif com_input.hex() in ('', 'aa15', 'aa19', 'aa12', 'ffffffffff'): elif com_input.hex() in ('', 'aa15', 'aa19', 'aa12', 'ffffffffff'):
pass pass
else: else: