创建项目及AI生成基本代码
This commit is contained in:
6
internal/simulation/__init__.py
Normal file
6
internal/simulation/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
# 模拟模块
|
||||
# 用于在没有实际设备的情况下模拟场内子系统设备
|
||||
|
||||
from .device_interface import SimulatedDevice, DeviceManager, AreaController, NormalDevice
|
||||
|
||||
__all__ = ['SimulatedDevice', 'DeviceManager', 'AreaController', 'NormalDevice']
|
||||
480
internal/simulation/device_interface.py
Normal file
480
internal/simulation/device_interface.py
Normal file
@@ -0,0 +1,480 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
设备模拟接口模块,用于模拟场内子系统设备
|
||||
支持四级结构:平台->中继->区域主控->普通设备
|
||||
"""
|
||||
|
||||
import random
|
||||
import time
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DeviceType(Enum):
|
||||
"""设备类型枚举"""
|
||||
AREA_CONTROLLER = "area_controller" # 区域主控
|
||||
NORMAL_DEVICE = "normal_device" # 普通设备
|
||||
|
||||
|
||||
class SimulatedDevice:
|
||||
"""模拟设备基类"""
|
||||
|
||||
def __init__(self, device_id, device_type, status="stopped"):
|
||||
"""
|
||||
初始化模拟设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
device_type (DeviceType): 设备类型
|
||||
status (str): 初始状态
|
||||
"""
|
||||
self.device_id = device_id
|
||||
self.device_type = device_type
|
||||
self.status = status
|
||||
self.created_at = time.time()
|
||||
logger.info(f"创建模拟设备: ID={device_id}, 类型={device_type.value}, 状态={status}")
|
||||
|
||||
|
||||
class AreaController(SimulatedDevice):
|
||||
"""区域主控设备"""
|
||||
|
||||
def __init__(self, device_id, lora_address, status="stopped"):
|
||||
"""
|
||||
初始化区域主控设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
lora_address (str): LoRa地址
|
||||
status (str): 初始状态
|
||||
"""
|
||||
super().__init__(device_id, DeviceType.AREA_CONTROLLER, status)
|
||||
self.lora_address = lora_address
|
||||
self.devices = {} # 管理的普通设备
|
||||
logger.info(f"创建区域主控: ID={device_id}, LoRa地址={lora_address}")
|
||||
|
||||
def add_device(self, device):
|
||||
"""
|
||||
添加普通设备到区域主控
|
||||
|
||||
Args:
|
||||
device (NormalDevice): 普通设备实例
|
||||
"""
|
||||
self.devices[device.device_id] = device
|
||||
logger.info(f"区域主控 {self.device_id} 添加设备: {device.device_id}")
|
||||
|
||||
def remove_device(self, device_id):
|
||||
"""
|
||||
从区域主控移除普通设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
"""
|
||||
if device_id in self.devices:
|
||||
del self.devices[device_id]
|
||||
logger.info(f"区域主控 {self.device_id} 移除设备: {device_id}")
|
||||
|
||||
def get_device(self, device_id):
|
||||
"""
|
||||
获取区域主控下的普通设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
|
||||
Returns:
|
||||
NormalDevice: 普通设备实例,如果不存在则返回None
|
||||
"""
|
||||
return self.devices.get(device_id)
|
||||
|
||||
def get_all_devices(self):
|
||||
"""
|
||||
获取区域主控下的所有普通设备
|
||||
|
||||
Returns:
|
||||
list: 所有普通设备实例的列表
|
||||
"""
|
||||
return list(self.devices.values())
|
||||
|
||||
|
||||
class NormalDevice(SimulatedDevice):
|
||||
"""普通设备"""
|
||||
|
||||
def __init__(self, device_id, device_type, rs485_bus, rs485_address, controller_id, status="stopped"):
|
||||
"""
|
||||
初始化普通设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
device_type (str): 设备类型(如风机、水帘等)
|
||||
rs485_bus (str): 485总线号
|
||||
rs485_address (str): 485总线地址
|
||||
controller_id (str): 所属区域主控ID
|
||||
status (str): 初始状态
|
||||
"""
|
||||
super().__init__(device_id, DeviceType.NORMAL_DEVICE, status)
|
||||
self.device_type = device_type
|
||||
self.rs485_bus = rs485_bus
|
||||
self.rs485_address = rs485_address
|
||||
self.controller_id = controller_id
|
||||
logger.info(f"创建普通设备: ID={device_id}, 类型={device_type}, "
|
||||
f"485总线={rs485_bus}, 485地址={rs485_address}, 所属主控={controller_id}")
|
||||
|
||||
|
||||
class DeviceManager:
|
||||
"""设备管理器"""
|
||||
|
||||
def __init__(self):
|
||||
"""初始化设备管理器"""
|
||||
self.controllers = {} # 区域主控设备
|
||||
self.devices = {} # 所有普通设备
|
||||
logger.info("初始化设备管理器")
|
||||
|
||||
def add_controller(self, controller):
|
||||
"""
|
||||
添加区域主控设备
|
||||
|
||||
Args:
|
||||
controller (AreaController): 区域主控设备实例
|
||||
"""
|
||||
self.controllers[controller.device_id] = controller
|
||||
logger.info(f"添加区域主控到管理器: {controller.device_id}")
|
||||
|
||||
def remove_controller(self, controller_id):
|
||||
"""
|
||||
移除区域主控设备
|
||||
|
||||
Args:
|
||||
controller_id (str): 区域主控设备ID
|
||||
"""
|
||||
if controller_id in self.controllers:
|
||||
# 同时移除该主控下的所有普通设备
|
||||
controller = self.controllers[controller_id]
|
||||
for device_id in list(controller.devices.keys()):
|
||||
if device_id in self.devices:
|
||||
del self.devices[device_id]
|
||||
del self.controllers[controller_id]
|
||||
logger.info(f"从管理器移除区域主控: {controller_id}")
|
||||
|
||||
def get_controller(self, controller_id):
|
||||
"""
|
||||
获取区域主控设备
|
||||
|
||||
Args:
|
||||
controller_id (str): 区域主控设备ID
|
||||
|
||||
Returns:
|
||||
AreaController: 区域主控设备实例,如果不存在则返回None
|
||||
"""
|
||||
controller = self.controllers.get(controller_id)
|
||||
if controller:
|
||||
logger.debug(f"获取区域主控: {controller_id}")
|
||||
else:
|
||||
logger.warning(f"尝试获取不存在的区域主控: {controller_id}")
|
||||
return controller
|
||||
|
||||
def get_all_controllers(self):
|
||||
"""
|
||||
获取所有区域主控设备
|
||||
|
||||
Returns:
|
||||
list: 所有区域主控设备实例的列表
|
||||
"""
|
||||
logger.info(f"获取所有区域主控,共 {len(self.controllers)} 个")
|
||||
return list(self.controllers.values())
|
||||
|
||||
def add_device(self, device):
|
||||
"""
|
||||
添加普通设备
|
||||
|
||||
Args:
|
||||
device (NormalDevice): 普通设备实例
|
||||
"""
|
||||
self.devices[device.device_id] = device
|
||||
logger.info(f"添加普通设备到管理器: {device.device_id}")
|
||||
|
||||
def remove_device(self, device_id):
|
||||
"""
|
||||
移除普通设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
"""
|
||||
if device_id in self.devices:
|
||||
del self.devices[device_id]
|
||||
logger.info(f"从管理器移除普通设备: {device_id}")
|
||||
|
||||
def get_device(self, device_id):
|
||||
"""
|
||||
获取普通设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
|
||||
Returns:
|
||||
NormalDevice: 普通设备实例,如果不存在则返回None
|
||||
"""
|
||||
device = self.devices.get(device_id)
|
||||
if device:
|
||||
logger.debug(f"获取普通设备: {device_id}")
|
||||
else:
|
||||
logger.warning(f"尝试获取不存在的普通设备: {device_id}")
|
||||
return device
|
||||
|
||||
def get_all_devices(self):
|
||||
"""
|
||||
获取所有普通设备
|
||||
|
||||
Returns:
|
||||
list: 所有普通设备实例的列表
|
||||
"""
|
||||
logger.info(f"获取所有普通设备,共 {len(self.devices)} 个")
|
||||
return list(self.devices.values())
|
||||
|
||||
def control_device(self, device_id, action):
|
||||
"""
|
||||
控制指定设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
action (str): 控制动作
|
||||
|
||||
Returns:
|
||||
dict: 控制结果
|
||||
"""
|
||||
logger.info(f"控制设备: ID={device_id}, 动作={action}")
|
||||
|
||||
# 先尝试查找普通设备
|
||||
device = self.get_device(device_id)
|
||||
if device:
|
||||
return self._control_normal_device(device, action)
|
||||
|
||||
# 再尝试查找区域主控设备
|
||||
controller = self.get_controller(device_id)
|
||||
if controller:
|
||||
return self._control_area_controller(controller, action)
|
||||
|
||||
result = {
|
||||
"device_id": device_id,
|
||||
"status": "failed",
|
||||
"message": f"设备 {device_id} 不存在"
|
||||
}
|
||||
logger.error(f"控制设备失败: 设备 {device_id} 不存在")
|
||||
return result
|
||||
|
||||
def _control_normal_device(self, device, action):
|
||||
"""
|
||||
控制普通设备
|
||||
|
||||
Args:
|
||||
device (NormalDevice): 普通设备实例
|
||||
action (str): 控制动作
|
||||
|
||||
Returns:
|
||||
dict: 控制结果
|
||||
"""
|
||||
logger.info(f"控制普通设备 {device.device_id}: 动作={action}")
|
||||
|
||||
if action == "on":
|
||||
device.status = "running"
|
||||
result = {
|
||||
"device_id": device.device_id,
|
||||
"status": "success",
|
||||
"message": f"设备 {device.device_id} 已开启"
|
||||
}
|
||||
logger.info(f"普通设备 {device.device_id} 开启成功")
|
||||
return result
|
||||
elif action == "off":
|
||||
device.status = "stopped"
|
||||
result = {
|
||||
"device_id": device.device_id,
|
||||
"status": "success",
|
||||
"message": f"设备 {device.device_id} 已关闭"
|
||||
}
|
||||
logger.info(f"普通设备 {device.device_id} 关闭成功")
|
||||
return result
|
||||
else:
|
||||
result = {
|
||||
"device_id": device.device_id,
|
||||
"status": "failed",
|
||||
"message": f"不支持的操作: {action}"
|
||||
}
|
||||
logger.warning(f"普通设备 {device.device_id} 不支持的操作: {action}")
|
||||
return result
|
||||
|
||||
def _control_area_controller(self, controller, action):
|
||||
"""
|
||||
控制区域主控设备
|
||||
|
||||
Args:
|
||||
controller (AreaController): 区域主控设备实例
|
||||
action (str): 控制动作
|
||||
|
||||
Returns:
|
||||
dict: 控制结果
|
||||
"""
|
||||
logger.info(f"控制区域主控 {controller.device_id}: 动作={action}")
|
||||
|
||||
if action == "on":
|
||||
controller.status = "running"
|
||||
result = {
|
||||
"device_id": controller.device_id,
|
||||
"status": "success",
|
||||
"message": f"区域主控 {controller.device_id} 已开启"
|
||||
}
|
||||
logger.info(f"区域主控 {controller.device_id} 开启成功")
|
||||
return result
|
||||
elif action == "off":
|
||||
controller.status = "stopped"
|
||||
result = {
|
||||
"device_id": controller.device_id,
|
||||
"status": "success",
|
||||
"message": f"区域主控 {controller.device_id} 已关闭"
|
||||
}
|
||||
logger.info(f"区域主控 {controller.device_id} 关闭成功")
|
||||
return result
|
||||
else:
|
||||
result = {
|
||||
"device_id": controller.device_id,
|
||||
"status": "failed",
|
||||
"message": f"不支持的操作: {action}"
|
||||
}
|
||||
logger.warning(f"区域主控 {controller.device_id} 不支持的操作: {action}")
|
||||
return result
|
||||
|
||||
def query_device_status(self, device_id):
|
||||
"""
|
||||
查询指定设备状态
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
|
||||
Returns:
|
||||
dict: 设备状态信息
|
||||
"""
|
||||
logger.info(f"查询设备状态: ID={device_id}")
|
||||
|
||||
# 先尝试查找普通设备
|
||||
device = self.get_device(device_id)
|
||||
if device:
|
||||
return self._query_normal_device_status(device)
|
||||
|
||||
# 再尝试查找区域主控设备
|
||||
controller = self.get_controller(device_id)
|
||||
if controller:
|
||||
return self._query_area_controller_status(controller)
|
||||
|
||||
result = {
|
||||
"device_id": device_id,
|
||||
"status": "failed",
|
||||
"message": f"设备 {device_id} 不存在"
|
||||
}
|
||||
logger.error(f"查询设备状态失败: 设备 {device_id} 不存在")
|
||||
return result
|
||||
|
||||
def _query_normal_device_status(self, device):
|
||||
"""
|
||||
查询普通设备状态
|
||||
|
||||
Args:
|
||||
device (NormalDevice): 普通设备实例
|
||||
|
||||
Returns:
|
||||
dict: 设备状态信息
|
||||
"""
|
||||
logger.info(f"查询普通设备 {device.device_id} 状态")
|
||||
|
||||
# 模拟一些随机的设备数据
|
||||
if device.status == "running":
|
||||
power = random.randint(200, 240)
|
||||
current = random.uniform(4.0, 6.0)
|
||||
result = {
|
||||
"device_id": device.device_id,
|
||||
"device_type": device.device_type,
|
||||
"status": device.status,
|
||||
"rs485_bus": device.rs485_bus,
|
||||
"rs485_address": device.rs485_address,
|
||||
"controller_id": device.controller_id,
|
||||
"power": power,
|
||||
"current": round(current, 2)
|
||||
}
|
||||
logger.info(f"普通设备 {device.device_id} 状态: 运行中, 功率={power}V, 电流={round(current, 2)}A")
|
||||
return result
|
||||
else:
|
||||
result = {
|
||||
"device_id": device.device_id,
|
||||
"device_type": device.device_type,
|
||||
"status": device.status,
|
||||
"rs485_bus": device.rs485_bus,
|
||||
"rs485_address": device.rs485_address,
|
||||
"controller_id": device.controller_id,
|
||||
"power": 0,
|
||||
"current": 0.0
|
||||
}
|
||||
logger.info(f"普通设备 {device.device_id} 状态: 已停止")
|
||||
return result
|
||||
|
||||
def _query_area_controller_status(self, controller):
|
||||
"""
|
||||
查询区域主控状态
|
||||
|
||||
Args:
|
||||
controller (AreaController): 区域主控设备实例
|
||||
|
||||
Returns:
|
||||
dict: 设备状态信息
|
||||
"""
|
||||
logger.info(f"查询区域主控 {controller.device_id} 状态")
|
||||
|
||||
result = {
|
||||
"device_id": controller.device_id,
|
||||
"device_type": "area_controller",
|
||||
"status": controller.status,
|
||||
"lora_address": controller.lora_address,
|
||||
"managed_devices": len(controller.devices)
|
||||
}
|
||||
logger.info(f"区域主控 {controller.device_id} 状态: {controller.status}, 管理设备数: {len(controller.devices)}")
|
||||
return result
|
||||
|
||||
def query_all_device_status(self):
|
||||
"""
|
||||
查询所有设备状态
|
||||
|
||||
Returns:
|
||||
list: 所有设备状态信息列表
|
||||
"""
|
||||
logger.info("查询所有设备状态")
|
||||
statuses = []
|
||||
|
||||
# 添加区域主控状态
|
||||
for controller in self.controllers.values():
|
||||
status_info = {
|
||||
"device_id": controller.device_id,
|
||||
"device_type": "area_controller",
|
||||
"status": controller.status,
|
||||
"lora_address": controller.lora_address,
|
||||
"managed_devices": len(controller.devices)
|
||||
}
|
||||
statuses.append(status_info)
|
||||
logger.debug(f"区域主控 {controller.device_id} 状态: {controller.status}")
|
||||
|
||||
# 添加普通设备状态
|
||||
for device in self.devices.values():
|
||||
status_info = {
|
||||
"device_id": device.device_id,
|
||||
"device_type": device.device_type,
|
||||
"status": device.status,
|
||||
"rs485_bus": device.rs485_bus,
|
||||
"rs485_address": device.rs485_address,
|
||||
"controller_id": device.controller_id
|
||||
}
|
||||
statuses.append(status_info)
|
||||
logger.debug(f"普通设备 {device.device_id} 状态: {device.status}")
|
||||
|
||||
logger.info(f"查询所有设备状态完成,共 {len(statuses)} 个设备")
|
||||
return statuses
|
||||
227
internal/simulation/devices.py
Normal file
227
internal/simulation/devices.py
Normal file
@@ -0,0 +1,227 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
模拟设备模块,用于创建和管理模拟设备
|
||||
"""
|
||||
|
||||
import random
|
||||
import time
|
||||
import logging
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SimulatedDevice:
|
||||
"""模拟设备类"""
|
||||
|
||||
def __init__(self, device_id, device_type, status="stopped"):
|
||||
"""
|
||||
初始化模拟设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
device_type (str): 设备类型
|
||||
status (str): 初始状态
|
||||
"""
|
||||
self.device_id = device_id
|
||||
self.device_type = device_type
|
||||
self.status = status
|
||||
self.created_at = time.time()
|
||||
logger.info(f"创建模拟设备: ID={device_id}, 类型={device_type}, 状态={status}")
|
||||
|
||||
def control(self, action):
|
||||
"""
|
||||
控制设备
|
||||
|
||||
Args:
|
||||
action (str): 控制动作 ('on' 或 'off')
|
||||
|
||||
Returns:
|
||||
dict: 控制结果
|
||||
"""
|
||||
logger.info(f"控制设备 {self.device_id}: 动作={action}")
|
||||
|
||||
if action == "on":
|
||||
self.status = "running"
|
||||
result = {
|
||||
"device_id": self.device_id,
|
||||
"status": "success",
|
||||
"message": f"设备 {self.device_id} 已开启"
|
||||
}
|
||||
logger.info(f"设备 {self.device_id} 开启成功")
|
||||
return result
|
||||
elif action == "off":
|
||||
self.status = "stopped"
|
||||
result = {
|
||||
"device_id": self.device_id,
|
||||
"status": "success",
|
||||
"message": f"设备 {self.device_id} 已关闭"
|
||||
}
|
||||
logger.info(f"设备 {self.device_id} 关闭成功")
|
||||
return result
|
||||
else:
|
||||
result = {
|
||||
"device_id": self.device_id,
|
||||
"status": "failed",
|
||||
"message": f"不支持的操作: {action}"
|
||||
}
|
||||
logger.warning(f"设备 {self.device_id} 不支持的操作: {action}")
|
||||
return result
|
||||
|
||||
def get_status(self):
|
||||
"""
|
||||
获取设备状态
|
||||
|
||||
Returns:
|
||||
dict: 设备状态信息
|
||||
"""
|
||||
logger.info(f"查询设备 {self.device_id} 状态")
|
||||
|
||||
# 模拟一些随机的设备数据
|
||||
if self.status == "running":
|
||||
power = random.randint(200, 240)
|
||||
current = random.uniform(4.0, 6.0)
|
||||
result = {
|
||||
"device_id": self.device_id,
|
||||
"status": self.status,
|
||||
"power": power,
|
||||
"current": round(current, 2)
|
||||
}
|
||||
logger.info(f"设备 {self.device_id} 状态: 运行中, 功率={power}V, 电流={round(current, 2)}A")
|
||||
return result
|
||||
else:
|
||||
result = {
|
||||
"device_id": self.device_id,
|
||||
"status": self.status,
|
||||
"power": 0,
|
||||
"current": 0.0
|
||||
}
|
||||
logger.info(f"设备 {self.device_id} 状态: 已停止")
|
||||
return result
|
||||
|
||||
|
||||
class DeviceManager:
|
||||
"""设备管理器"""
|
||||
|
||||
def __init__(self):
|
||||
"""初始化设备管理器"""
|
||||
self.devices = {}
|
||||
logger.info("初始化设备管理器")
|
||||
|
||||
def add_device(self, device):
|
||||
"""
|
||||
添加设备
|
||||
|
||||
Args:
|
||||
device (SimulatedDevice): 模拟设备实例
|
||||
"""
|
||||
self.devices[device.device_id] = device
|
||||
logger.info(f"添加设备到管理器: {device.device_id}")
|
||||
|
||||
def remove_device(self, device_id):
|
||||
"""
|
||||
移除设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
"""
|
||||
if device_id in self.devices:
|
||||
del self.devices[device_id]
|
||||
logger.info(f"从管理器移除设备: {device_id}")
|
||||
|
||||
def get_device(self, device_id):
|
||||
"""
|
||||
获取设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
|
||||
Returns:
|
||||
SimulatedDevice: 设备实例,如果不存在则返回None
|
||||
"""
|
||||
device = self.devices.get(device_id)
|
||||
if device:
|
||||
logger.debug(f"获取设备: {device_id}")
|
||||
else:
|
||||
logger.warning(f"尝试获取不存在的设备: {device_id}")
|
||||
return device
|
||||
|
||||
def get_all_devices(self):
|
||||
"""
|
||||
获取所有设备
|
||||
|
||||
Returns:
|
||||
list: 所有设备实例的列表
|
||||
"""
|
||||
logger.info(f"获取所有设备,共 {len(self.devices)} 个设备")
|
||||
return list(self.devices.values())
|
||||
|
||||
def control_device(self, device_id, action):
|
||||
"""
|
||||
控制指定设备
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
action (str): 控制动作
|
||||
|
||||
Returns:
|
||||
dict: 控制结果
|
||||
"""
|
||||
logger.info(f"控制设备: ID={device_id}, 动作={action}")
|
||||
device = self.get_device(device_id)
|
||||
if device:
|
||||
return device.control(action)
|
||||
else:
|
||||
result = {
|
||||
"device_id": device_id,
|
||||
"status": "failed",
|
||||
"message": f"设备 {device_id} 不存在"
|
||||
}
|
||||
logger.error(f"控制设备失败: 设备 {device_id} 不存在")
|
||||
return result
|
||||
|
||||
def query_device_status(self, device_id):
|
||||
"""
|
||||
查询指定设备状态
|
||||
|
||||
Args:
|
||||
device_id (str): 设备ID
|
||||
|
||||
Returns:
|
||||
dict: 设备状态信息
|
||||
"""
|
||||
logger.info(f"查询设备状态: ID={device_id}")
|
||||
device = self.get_device(device_id)
|
||||
if device:
|
||||
return device.get_status()
|
||||
else:
|
||||
result = {
|
||||
"device_id": device_id,
|
||||
"status": "failed",
|
||||
"message": f"设备 {device_id} 不存在"
|
||||
}
|
||||
logger.error(f"查询设备状态失败: 设备 {device_id} 不存在")
|
||||
return result
|
||||
|
||||
def query_all_device_status(self):
|
||||
"""
|
||||
查询所有设备状态
|
||||
|
||||
Returns:
|
||||
list: 所有设备状态信息列表
|
||||
"""
|
||||
logger.info("查询所有设备状态")
|
||||
statuses = []
|
||||
for device in self.devices.values():
|
||||
status_info = {
|
||||
"device_id": device.device_id,
|
||||
"device_type": device.device_type,
|
||||
"status": device.status
|
||||
}
|
||||
statuses.append(status_info)
|
||||
logger.debug(f"设备 {device.device_id} 状态: {device.status}")
|
||||
logger.info(f"查询所有设备状态完成,共 {len(statuses)} 个设备")
|
||||
return statuses
|
||||
187
internal/simulation/websocket.py
Normal file
187
internal/simulation/websocket.py
Normal file
@@ -0,0 +1,187 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
模拟WebSocket通信模块,用于模拟与猪场主控的WebSocket通信
|
||||
"""
|
||||
|
||||
import json
|
||||
import time
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from .devices import DeviceManager, SimulatedDevice
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SimulatedWebSocketServer:
|
||||
"""模拟WebSocket服务器"""
|
||||
|
||||
def __init__(self, config):
|
||||
"""
|
||||
初始化模拟WebSocket服务器
|
||||
|
||||
Args:
|
||||
config: 配置对象
|
||||
"""
|
||||
self.config = config
|
||||
self.device_manager = DeviceManager()
|
||||
self._initialize_devices()
|
||||
logger.info("模拟WebSocket服务器初始化完成")
|
||||
|
||||
def _initialize_devices(self):
|
||||
"""初始化模拟设备"""
|
||||
logger.info("开始初始化模拟设备")
|
||||
# 根据配置创建模拟设备
|
||||
for device_config in self.config.simulation_devices:
|
||||
device = SimulatedDevice(
|
||||
device_id=device_config['id'],
|
||||
device_type=device_config['type'],
|
||||
status=device_config.get('status', 'stopped')
|
||||
)
|
||||
self.device_manager.add_device(device)
|
||||
logger.info(f"模拟设备初始化完成,共创建 {len(self.config.simulation_devices)} 个设备")
|
||||
|
||||
def handle_message(self, message_str):
|
||||
"""
|
||||
处理接收到的消息
|
||||
|
||||
Args:
|
||||
message_str (str): JSON格式的消息字符串
|
||||
|
||||
Returns:
|
||||
dict: 响应消息
|
||||
"""
|
||||
logger.info(f"收到消息: {message_str}")
|
||||
|
||||
try:
|
||||
message = json.loads(message_str)
|
||||
msg_type = message.get('type')
|
||||
logger.info(f"消息类型: {msg_type}")
|
||||
|
||||
if msg_type == 'heartbeat':
|
||||
return self._handle_heartbeat(message)
|
||||
elif msg_type == 'command':
|
||||
command = message.get('command')
|
||||
logger.info(f"命令类型: {command}")
|
||||
if command == 'control_device':
|
||||
return self._handle_control_device(message)
|
||||
elif command == 'query_device_status':
|
||||
return self._handle_query_device_status(message)
|
||||
elif command == 'query_all_device_status':
|
||||
return self._handle_query_all_device_status(message)
|
||||
|
||||
# 未知消息类型
|
||||
result = {
|
||||
"type": "response",
|
||||
"status": "failed",
|
||||
"message": f"未知消息类型: {msg_type}",
|
||||
"timestamp": datetime.utcnow().isoformat() + 'Z'
|
||||
}
|
||||
logger.warning(f"未知消息类型: {msg_type}")
|
||||
return result
|
||||
except json.JSONDecodeError:
|
||||
result = {
|
||||
"type": "response",
|
||||
"status": "failed",
|
||||
"message": "无效的JSON格式",
|
||||
"timestamp": datetime.utcnow().isoformat() + 'Z'
|
||||
}
|
||||
logger.error("无效的JSON格式")
|
||||
return result
|
||||
|
||||
def _handle_heartbeat(self, message):
|
||||
"""
|
||||
处理心跳消息
|
||||
|
||||
Args:
|
||||
message (dict): 心跳消息
|
||||
|
||||
Returns:
|
||||
dict: 心跳响应
|
||||
"""
|
||||
logger.info("处理心跳消息")
|
||||
result = {
|
||||
"type": "response",
|
||||
"command": "heartbeat",
|
||||
"status": "success",
|
||||
"message": "心跳响应",
|
||||
"timestamp": datetime.utcnow().isoformat() + 'Z'
|
||||
}
|
||||
logger.info("心跳响应已发送")
|
||||
return result
|
||||
|
||||
def _handle_control_device(self, message):
|
||||
"""
|
||||
处理设备控制命令
|
||||
|
||||
Args:
|
||||
message (dict): 控制命令消息
|
||||
|
||||
Returns:
|
||||
dict: 控制响应
|
||||
"""
|
||||
data = message.get('data', {})
|
||||
device_id = data.get('device_id')
|
||||
action = data.get('action')
|
||||
|
||||
logger.info(f"处理设备控制命令: 设备ID={device_id}, 动作={action}")
|
||||
result = self.device_manager.control_device(device_id, action)
|
||||
|
||||
response = {
|
||||
"type": "response",
|
||||
"command": "control_device",
|
||||
"data": result,
|
||||
"timestamp": datetime.utcnow().isoformat() + 'Z'
|
||||
}
|
||||
logger.info(f"设备控制命令处理完成: {response}")
|
||||
return response
|
||||
|
||||
def _handle_query_device_status(self, message):
|
||||
"""
|
||||
处理查询设备状态命令
|
||||
|
||||
Args:
|
||||
message (dict): 查询命令消息
|
||||
|
||||
Returns:
|
||||
dict: 状态响应
|
||||
"""
|
||||
data = message.get('data', {})
|
||||
device_id = data.get('device_id')
|
||||
|
||||
logger.info(f"处理查询设备状态命令: 设备ID={device_id}")
|
||||
result = self.device_manager.query_device_status(device_id)
|
||||
|
||||
response = {
|
||||
"type": "response",
|
||||
"command": "query_device_status",
|
||||
"data": result,
|
||||
"timestamp": datetime.utcnow().isoformat() + 'Z'
|
||||
}
|
||||
logger.info(f"设备状态查询完成: {response}")
|
||||
return response
|
||||
|
||||
def _handle_query_all_device_status(self, message):
|
||||
"""
|
||||
处理查询所有设备状态命令
|
||||
|
||||
Args:
|
||||
message (dict): 查询命令消息
|
||||
|
||||
Returns:
|
||||
dict: 状态响应
|
||||
"""
|
||||
logger.info("处理查询所有设备状态命令")
|
||||
result = self.device_manager.query_all_device_status()
|
||||
|
||||
response = {
|
||||
"type": "response",
|
||||
"command": "query_all_device_status",
|
||||
"data": result,
|
||||
"timestamp": datetime.utcnow().isoformat() + 'Z'
|
||||
}
|
||||
logger.info(f"所有设备状态查询完成,共 {len(result)} 个设备")
|
||||
return response
|
||||
Reference in New Issue
Block a user