227 lines
6.6 KiB
Python
227 lines
6.6 KiB
Python
|
|
#!/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
|