#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Time    :2026/1/28 16:12
# @Author  : Divine
# @Site    :
# @File    :main.py
# @Software: PyCharm
#
# import requests
# from threading import Thread
#
# device_id = settings.DEVICE_ID
#
#
# class Node:
#     # 注册节点的 URL
#     register_node_url = settings.REGISTER_NODE_URL
#
#     def __init__(self):
#         # 注册节点
#         self.register_node()
#         # 创建消费者
#         self.consumer = MQConsumer()
#
#     def register_node(self):
#         """注册节点"""
#         print("--------------开始注册执行设备-----------------")
#         # 发送请求，注册设备
#         response = requests.post(self.register_node_url, json=settings.DEVICE)
#         if response.status_code == 201:
#             print(response.text)
#             db = db_client.DB()
#             username = db.get_user_name_by_user_id(device_id)
#             print(f'执行用户【{username}】的设备id:【{device_id}】已经上线')
#         else:
#             print(f'用户设备{device_id}注册失败！错误信息：{response.text}')
#             # 退出程序
#             sys.exit(0)
#         # print('设备注册结果：', response.text)
#
#     def start(self):
#         """启动节点"""
#         # 发布节点状态
#         redis_pub.publish_status(device_id, '在线')
#         # 发布节点屏幕画面
#         Thread(target=redis_pub.publish_screen, args=(device_id,)).start()
#         # 启动消费者
#         self.consumer.main()
#
#     def stop(self):
#         """关闭节点"""
#         # 关闭 RabbitMQ 连接
#         self.consumer.stop()
#         # 设置节点状态为已停止
#         redis_pub.publish_status(device_id, '离线')
#         # 清理设备执行日志
#         redis_pub.clear_history(device_id)
#         # 关闭 Redis 连接
#         redis_pub.close()
#         # 修改数据库中节点状态为离线
#         db = db_client.DB()
#         db.execute(sql="UPDATE device SET status = '离线' WHERE id = %s", args=device_id)
#         print("-------------节点关闭，清理资源完毕-----------------")
#         # 退出程序
#         sys.exit(0)
#
#     def handle_exit(self, signum, frame):
#         """处理节点退出信号并执行清理"""
#         print(f"收到退出信号{signum}，开始清理...")
#         # 停止节点
#         self.stop()
#
#     def get_user_name(self):
#         """获取用户名"""
#         db = db_client.DB()
#         username = db.get_user_name().get('username')
#         db.close()
#         return username
#
#
# def main():
#     node = Node()
#     # 绑定信号处理函数，确保节点能退出
#     signal.signal(signal.SIGTERM, node.handle_exit)
#     signal.signal(signal.SIGINT, node.handle_exit)
#     # 启动节点并开始任务监听
#     node.start()
#
#
#
# if __name__ == '__main__':
#     main()

