Simple_home_control_system/main.py
2024-09-07 20:46:39 +08:00

234 lines
9.5 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
import threading
import time
import os
from src import master_control
from src import voice_processing
from src import SaveLogger
from src import python_mqtt
from config import Config
from src import local_status
from src import data_up
from src import action_cmd
from src.glogcenter import glc
from src import light_control
gd = glc.GlcData()
gd.system = 'smart_home'
gd.traceId = os.path.basename(__file__)
__version__ = Config.VERSION
__description__ = Config.DESCRIPTION
file_path = os.path.abspath(__file__)
__last_modified__ = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(file_path)))
__changelog__ = Config.CHANGELOG
broker = Config.BROKER
port = Config.PORT
topic = Config.TOPIC
client_id = Config.MQTT_CLIENT_ID
log_file_path = os.path.join(os.getcwd(), 'logs/Program_run_state.log')
save_log = SaveLogger(os.path.basename(__file__), log_file=log_file_path)
log_file_path1 = os.path.join(os.getcwd(), 'logs/Error.log')
save_log_error = SaveLogger('main.py1', log_file=log_file_path1)
log_file_path2 = os.path.join(os.getcwd(), 'logs/local_server_status.log')
save_log_info = SaveLogger('main.py2', log_file=log_file_path2)
# 继电器
Channel_one_open = bytearray([0x05, 0x06, 0x00, 0x00, 0x00, 0x01])
Channel_one_close = bytearray([0x05, 0x06, 0x00, 0x00, 0x00, 0x00])
Channel_two_open = bytearray([0x05, 0x06, 0x00, 0x01, 0x00, 0x01])
Channel_two_close = bytearray([0x05, 0x06, 0x00, 0x01, 0x00, 0x00])
# 红外发射传感器
infrared_control_on = bytearray([0x68, 0x08, 0x00, 0xFF, 0x12, 0x00, 0x11, 0x16])
infrared_control_off = bytearray([0x68, 0x08, 0x00, 0xFF, 0x12, 0x01, 0x12, 0x16])
# 测试数据
test_three = bytearray([0x68, 0x08, 0x00, 0xFF, 0x12, 0x02, 0x13, 0x16])
test_four = bytearray([0x68, 0x08, 0x00, 0xFF, 0x12, 0x03, 0x14, 0x16])
# 温湿度
temperature = bytearray([0x07, 0x04, 0x00, 0x00, 0x00, 0x02])
# 设备温度
humiture = bytearray([0x01, 0x03, 0x00, 0x00, 0x00, 0x01])
# 程序启动语音播报
run = bytearray([0xAA, 0x55, 0x05, 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)
def run_start_sensor(mqtt_client):
while True:
ser = master_control.humiture(control, temperature, mqtt_client)
ser2 = master_control.equipment_temperature(control, humiture, mqtt_client)
def run_local_status(mqtt_client):
while True:
return_data = local_status.status_main()
save_log_info.info(return_data)
data_up.mqtt_up(mqtt_client, Config.LOCAL_STATUS_UP, return_data)
time.sleep(10)
def run_start_main(mqtt_client):
voice = master_control.open_serial(Config.VOICE_OPEN_SERIAL, 115200, 0.3)
while True:
ser_cmd = voice_processing.get_voice_data(voice)
if Config.RUN_FLAG == 1:
master_control.run_flage(voice, run, ser_cmd)
# 开灯(大灯)
if (ser_cmd == 'a487') or (client.callback_result == 'a487'):
light_control.light_func(mqtt_client, 'headlamp', ser_cmd)
# master_control.infrared_sensor(control, infrared_control_on)
save_log.info('已开灯')
glc.info('已开灯', gd)
client.callback_result = 'xxx'
elif (ser_cmd == 'a488') or (client.callback_result == 'a488'):
light_control.light_func(mqtt_client, 'headlamp', ser_cmd)
# master_control.infrared_sensor(control, infrared_control_off)
save_log.info('已关灯')
glc.info('已关灯', gd)
client.callback_result = 'xxx'
# 暖色
elif (ser_cmd == 'd366') or (client.callback_result == 'd366'):
light_control.light_func(mqtt_client, 'warmcolors', ser_cmd)
client.callback_result = 'xxx'
# 开灯(小灯)
elif (ser_cmd == 'a455') or (client.callback_result == 'a455'):
light_control.light_func(mqtt_client, 'lamplet', ser_cmd)
save_log.info('已打闭小灯')
glc.info('已打闭小灯', gd)
client.callback_result = 'xxx'
elif (ser_cmd == 'a456') or (client.callback_result == 'a456'):
light_control.light_func(mqtt_client, 'lamplet', ser_cmd)
save_log.info('已关闭小灯')
glc.info('已关闭小灯', gd)
client.callback_result = 'xxx'
elif (ser_cmd == 'd388') or (client.callback_result == 'd388'):
master_control.infrared_sensor(control, infrared_control_off)
glc.info('已关闭', gd)
client.callback_result = 'xxx'
# WIFI插座
elif (ser_cmd == 'bba1') or (client.callback_result == 'bba1'):
action_cmd.action(client, Config.ACTION_socket, 'a1')
glc.info('床头插座已打开', gd)
client.callback_result = 'xxx'
elif (ser_cmd == 'bbb1') or (client.callback_result == 'bbb1'):
action_cmd.action(client, Config.ACTION_socket, 'b1')
glc.info('床头插座已关闭', gd)
client.callback_result = 'xxx'
# 打开热水器
elif (ser_cmd == 'ccaf') or (client.callback_result == 'ccaf'):
action_cmd.action(client, Config.ACTION_water_heater, 'a1')
glc.info('热水器已打开', gd)
client.callback_result = 'xxx'
# 关闭热水器
elif (ser_cmd == 'ddbf') or (client.callback_result == 'ddbf'):
action_cmd.action(client, Config.ACTION_water_heater, 'b1')
glc.info('热水器已关闭', gd)
client.callback_result = 'xxx'
# 打开电蚊香
elif (ser_cmd == 'aadf') or (client.callback_result == 'aadf'):
action_cmd.action(client, Config.ACTION_wen_xiang, 'a1')
glc.info('电蚊香已打开', gd)
client.callback_result = 'xxx'
# 关闭电蚊香
elif (ser_cmd == 'bbdf') or (client.callback_result == 'bbdf'):
action_cmd.action(client, Config.ACTION_wen_xiang, 'b1')
glc.info('电蚊香已关闭', gd)
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'):
master_control.run_flage(voice, check_code, ser_cmd)
save_log.info('语音模块校验成功!')
glc.info('语音模块校验成功!', gd)
time.sleep(1)
if __name__ == '__main__':
save_log.info("智能家居系统即将启动......")
save_log.info(f"项目版本:{__version__}")
save_log.info(f"上次修改时间::{__last_modified__}")
save_log.info(f"修改说明:{__changelog__}")
glc.info("智能家居系统即将启动......", gd)
glc.info(f"上次修改时间::{__last_modified__}", gd)
glc.info(f"修改说明:{__changelog__}", gd)
client = python_mqtt.MQTTClient(broker, port, topic, client_id)
try:
client.connect()
client.loop_start()
except Exception as e:
save_log_error.error(f'MQTTClient Error: {e}')
glc.error(f'MQTTClient Error: {e}', gd)
sys.exit(1)
run_start_sensor_thread = threading.Thread(target=run_start_sensor, args=(client,))
run_start_sensor_thread.start()
run_local_status_thread = threading.Thread(target=run_local_status, args=(client,))
run_local_status_thread.start()
run_start_main_thread = threading.Thread(target=run_start_main, args=(client,))
run_start_main_thread.start()
client.subscribe(Config.VOICE_CMD_SUB)
client.subscribe(Config.ACTION_socket)
client.subscribe(Config.ACTION_water_heater)
client.subscribe(Config.ACTION_wen_xiang)
client.subscribe(Config.ACTION_kongtiao)
client.light_subscribe(Config.ACTION_light)
action_cmd.action(client, Config.ACTION_light, 'qc')
while True:
time.sleep(20)
if not run_start_main_thread.is_alive(): # 检查线程是否还在运行
save_log_error.error("Thread died, restarting")
glc.error("Thread died, restarting", gd)
run_start_main_thread = threading.Thread(target=run_start_main, args=(client,))
run_start_main_thread.start()
save_log.info('The voice module thread is running properly...')
glc.info('The voice module thread is running properly...', gd)