pd9427
This commit is contained in:
@ -97,7 +97,7 @@ class InverterController:
|
||||
self.inverter.write_register(0x2000, 1) # 1=正转运行
|
||||
print("启动变频器")
|
||||
elif action == 'stop':
|
||||
self.inverter.write_register(0x2000, 5) # 6=减速停机,5自由停机
|
||||
self.inverter.write_register(0x2000, 6) # 6=减速停机,5自由停机
|
||||
print("停止变频器")
|
||||
_ret=True
|
||||
else:
|
||||
|
||||
224
hardware/metric_device.py
Normal file
224
hardware/metric_device.py
Normal file
@ -0,0 +1,224 @@
|
||||
# metric_device.py
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
import statistics
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Optional, Dict, Any, List
|
||||
from dataclasses import dataclass, asdict
|
||||
|
||||
@dataclass
|
||||
class DeviceReading:
|
||||
"""标准化设备读数"""
|
||||
weight: Optional[int] # 重量(None 表示 OL/ER)
|
||||
raw_data: bytes # 原始字节
|
||||
status: str # 'ST', 'US', 'OL', 'ER'
|
||||
timestamp: float # 本地接收时间(秒,time.time())
|
||||
rtt_ms: float # 网络往返延迟(毫秒)
|
||||
is_valid: bool # 是否含有效重量
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return {
|
||||
**asdict(self),
|
||||
"timestamp_iso": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.timestamp)),
|
||||
"weight_kg": self.weight / 1000.0 if self.weight is not None else None,
|
||||
}
|
||||
|
||||
|
||||
class MetricDevice(ABC):
|
||||
"""抽象计量设备基类 —— 支持 get() + RTT 统计"""
|
||||
def __init__(self, ip: str, port: int, name: str = ""):
|
||||
self.ip = ip
|
||||
self.port = port
|
||||
self.name = name or f"{ip}:{port}"
|
||||
self._latest_reading: Optional[DeviceReading] = None
|
||||
self._lock = threading.Lock()
|
||||
|
||||
# RTT 统计(滑动窗口)
|
||||
self._rtt_window: List[float] = []
|
||||
self._rtt_max_len = 100 # 保留最近 100 次 RTT
|
||||
self._rtt_stats = {
|
||||
"min_ms": float('inf'),
|
||||
"max_ms": 0.0,
|
||||
"avg_ms": 0.0,
|
||||
"count": 0,
|
||||
}
|
||||
|
||||
def _update_rtt(self, rtt_ms: float):
|
||||
"""更新 RTT 统计"""
|
||||
self._rtt_window.append(rtt_ms)
|
||||
if len(self._rtt_window) > self._rtt_max_len:
|
||||
self._rtt_window.pop(0)
|
||||
|
||||
rtt_list = self._rtt_window
|
||||
self._rtt_stats.update({
|
||||
"min_ms": min(rtt_list) if rtt_list else 0,
|
||||
"max_ms": max(rtt_list) if rtt_list else 0,
|
||||
"avg_ms": statistics.mean(rtt_list) if rtt_list else 0,
|
||||
"count": len(rtt_list),
|
||||
})
|
||||
|
||||
def get_rtt_stats(self) -> Dict[str, float]:
|
||||
"""获取 RTT 统计信息"""
|
||||
with self._lock:
|
||||
return self._rtt_stats.copy()
|
||||
|
||||
def get(self) -> Optional[Dict[str, Any]]:
|
||||
""" 统一 get() 接口:返回最新读数(含 RTT)"""
|
||||
with self._lock:
|
||||
if self._latest_reading is None:
|
||||
return None
|
||||
return self._latest_reading.to_dict()
|
||||
|
||||
@abstractmethod
|
||||
def _connect_and_run(self):
|
||||
"""子类实现连接与数据接收循环"""
|
||||
pass
|
||||
|
||||
def start(self):
|
||||
"""启动后台线程"""
|
||||
thread = threading.Thread(target=self._connect_and_run, daemon=True, name=f"Dev-{self.name}")
|
||||
thread.start()
|
||||
|
||||
def _update_reading(self, reading: DeviceReading):
|
||||
"""线程安全更新最新读数"""
|
||||
with self._lock:
|
||||
self._latest_reading = reading
|
||||
|
||||
|
||||
class TCPScaleDevice(MetricDevice):
|
||||
"""TCP ASCII 协议称重设备"""
|
||||
def __init__(self, ip: str, port: int, name: str = ""):
|
||||
super().__init__(ip, port, name)
|
||||
self._buffer = b""
|
||||
self._socket: Optional[socket.socket] = None
|
||||
self._running = True
|
||||
self._valid_weight = None # 有效重量
|
||||
|
||||
def _parse_line(self, line: bytes) -> Optional[DeviceReading]:
|
||||
try:
|
||||
clean = line.strip()
|
||||
if not clean:
|
||||
return None
|
||||
parts = clean.split(b',')
|
||||
if len(parts) < 3:
|
||||
return DeviceReading(
|
||||
weight=None, raw_data=clean, status="??",
|
||||
timestamp=time.time(), rtt_ms=0.0, is_valid=False
|
||||
)
|
||||
|
||||
status = parts[0].decode('ascii', errors='replace').upper()
|
||||
mode = parts[1].decode('ascii', errors='replace').upper()
|
||||
weight_str = parts[2].decode('ascii', errors='replace')
|
||||
|
||||
weight = None
|
||||
is_valid = False
|
||||
|
||||
if weight_str.replace('+', '').replace('-', '').isdigit():
|
||||
try:
|
||||
weight = int(weight_str)
|
||||
is_valid = True
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
return DeviceReading(
|
||||
weight=weight,
|
||||
raw_data=clean,
|
||||
status=status,
|
||||
timestamp=time.time(),
|
||||
rtt_ms=0.0, # 稍后更新
|
||||
is_valid=is_valid,
|
||||
)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def _extract_lines(self, data: bytes) -> List[bytes]:
|
||||
"""自适应行分割"""
|
||||
self._buffer += data
|
||||
lines = []
|
||||
if b'\r\n' in self._buffer or b'\n' in self._buffer:
|
||||
norm = self._buffer.replace(b'\r\n', b'\n')
|
||||
parts = norm.split(b'\n')
|
||||
*complete, self._buffer = parts
|
||||
lines = [line for line in complete if line]
|
||||
else:
|
||||
# 无换行符:整包作为单行
|
||||
if self._buffer:
|
||||
lines = [self._buffer]
|
||||
self._buffer = b""
|
||||
return lines
|
||||
|
||||
def _connect_and_run(self):
|
||||
while self._running:
|
||||
try:
|
||||
# 重连逻辑
|
||||
if self._socket is None:
|
||||
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self._socket.settimeout(5.0)
|
||||
self._socket.connect((self.ip, self.port))
|
||||
|
||||
# 接收数据
|
||||
t0 = time.perf_counter()
|
||||
data = self._socket.recv(1024)
|
||||
recv_rtt = (time.perf_counter() - t0) * 1000
|
||||
|
||||
if not data:
|
||||
break
|
||||
|
||||
# 更新 RTT
|
||||
self._update_rtt(recv_rtt * 2)
|
||||
|
||||
# 解析
|
||||
lines = self._extract_lines(data)
|
||||
for line in lines:
|
||||
reading = self._parse_line(line)
|
||||
if reading:
|
||||
reading.rtt_ms = recv_rtt * 2 # 近似 RTT
|
||||
self._update_reading(reading)
|
||||
if reading.is_valid: # 重量有效
|
||||
self._update_valid_weight(reading.weight)
|
||||
|
||||
except socket.timeout:
|
||||
continue
|
||||
except (OSError, socket.error) as e:
|
||||
print(f"[{self.name}] Error: {e}")
|
||||
if self._socket:
|
||||
self._socket.close()
|
||||
self._socket = None
|
||||
time.sleep(2)
|
||||
except Exception as e:
|
||||
print(f"[{self.name}] Fatal: {e}")
|
||||
break
|
||||
|
||||
if self._socket:
|
||||
self._socket.close()
|
||||
|
||||
def _update_valid_weight(self, weight: int):
|
||||
with self._lock:
|
||||
self._valid_weight = weight
|
||||
|
||||
def get_valid_weight(self) -> Optional[int]:
|
||||
with self._lock:
|
||||
return self._valid_weight
|
||||
|
||||
|
||||
# ===== 设计模式:设备工厂 + 管理器 =====
|
||||
class DeviceManager:
|
||||
"""设备工厂 + 单例管理"""
|
||||
_instances: Dict[str, MetricDevice] = {}
|
||||
_lock = threading.Lock()
|
||||
|
||||
@classmethod
|
||||
def get_device(cls, ip: str, port: int, name: str = "") -> MetricDevice:
|
||||
""" 工厂方法:按 (ip, port) 单例返回设备"""
|
||||
key = f"{ip}:{port}"
|
||||
with cls._lock:
|
||||
if key not in cls._instances:
|
||||
device = TCPScaleDevice(ip, port, name)
|
||||
device.start()
|
||||
cls._instances[key] = device
|
||||
return cls._instances[key]
|
||||
|
||||
@classmethod
|
||||
def get_all_devices(cls) -> List[MetricDevice]:
|
||||
return list(cls._instances.values())
|
||||
@ -305,13 +305,13 @@ class RelayController:
|
||||
|
||||
def control_upper_to_jbl(self):
|
||||
"""控制上料斗到搅拌楼"""
|
||||
# self.control(self.UPPER_TO_ZD, 'close')
|
||||
# self.control(self.UPPER_TO_JBL, 'open')
|
||||
self.control(self.UPPER_TO_ZD, 'close')
|
||||
self.control(self.UPPER_TO_JBL, 'open')
|
||||
|
||||
def control_upper_to_zd(self):
|
||||
"""控制上料斗到料斗"""
|
||||
# self.control(self.UPPER_TO_JBL, 'close')
|
||||
# self.control(self.UPPER_TO_ZD, 'open')
|
||||
self.control(self.UPPER_TO_JBL, 'close')
|
||||
self.control(self.UPPER_TO_ZD, 'open')
|
||||
|
||||
def close_all(self):
|
||||
"""关闭所有继电器"""
|
||||
@ -324,3 +324,5 @@ class RelayController:
|
||||
self.control(self.DOOR_LOWER_CLOSE, 'close')
|
||||
self.control(self.DOOR_UPPER_OPEN, 'close')
|
||||
self.control(self.DOOR_UPPER_CLOSE, 'close')
|
||||
|
||||
|
||||
|
||||
@ -1,200 +0,0 @@
|
||||
# hardware/transmitter.py
|
||||
import socket
|
||||
import threading
|
||||
from config.ini_manager import ini_manager
|
||||
from config.settings import app_set_config
|
||||
import time
|
||||
|
||||
class TransmitterController:
|
||||
def __init__(self):
|
||||
self.upper_ip = ini_manager.upper_transmitter_ip
|
||||
self.upper_port = ini_manager.upper_transmitter_port
|
||||
self.lower_ip = ini_manager.lower_transmitter_ip
|
||||
self.lower_port = ini_manager.lower_transmitter_port
|
||||
|
||||
# 存储最新重量值
|
||||
self.latest_weights = {1: None, 2: None}
|
||||
# 存储连接状态
|
||||
self.connection_status = {1: False, 2: False}
|
||||
# 线程控制
|
||||
self.running = True
|
||||
self.threads = {}
|
||||
# 连接配置
|
||||
self.TIMEOUT = 5 # 连接超时时间
|
||||
self.BUFFER_SIZE = 1024
|
||||
|
||||
# 启动后台接收线程
|
||||
self._start_receiver_threads()
|
||||
|
||||
def _start_receiver_threads(self):
|
||||
"""启动后台接收线程"""
|
||||
for transmitter_id in [1, 2]:
|
||||
if (transmitter_id == 1 and self.upper_ip and self.upper_port) or \
|
||||
(transmitter_id == 2 and self.lower_ip and self.lower_port):
|
||||
thread = threading.Thread(
|
||||
target=self._continuous_receiver,
|
||||
args=(transmitter_id,),
|
||||
daemon=True,
|
||||
name=f'transmitter_receiver_{transmitter_id}'
|
||||
)
|
||||
thread.start()
|
||||
self.threads[transmitter_id] = thread
|
||||
print(f"启动变送器 {transmitter_id} 后台接收线程")
|
||||
|
||||
def _continuous_receiver(self, transmitter_id):
|
||||
"""后台持续接收数据的线程函数"""
|
||||
while self.running:
|
||||
IP = None
|
||||
PORT = None
|
||||
|
||||
if transmitter_id == 1:
|
||||
IP = self.upper_ip
|
||||
PORT = self.upper_port
|
||||
elif transmitter_id == 2:
|
||||
IP = self.lower_ip
|
||||
PORT = self.lower_port
|
||||
|
||||
if not IP or not PORT:
|
||||
time.sleep(5)
|
||||
continue
|
||||
|
||||
sock = None
|
||||
try:
|
||||
# 创建连接
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(self.TIMEOUT)
|
||||
sock.connect((IP, PORT))
|
||||
self.connection_status[transmitter_id] = True
|
||||
print(f"变送器 {transmitter_id} 连接成功: {IP}:{PORT}")
|
||||
|
||||
# 持续接收数据
|
||||
while self.running:
|
||||
try:
|
||||
data = sock.recv(self.BUFFER_SIZE)
|
||||
if data:
|
||||
# 提取有效数据包
|
||||
packet = self.get_latest_valid_packet(data)
|
||||
if packet:
|
||||
# 解析重量
|
||||
weight = self.parse_weight(packet)
|
||||
if weight is not None:
|
||||
self.latest_weights[transmitter_id] = weight
|
||||
# 可选:打印接收到的重量
|
||||
# print(f"变送器 {transmitter_id} 重量: {weight}")
|
||||
else:
|
||||
# 连接关闭
|
||||
print(f"变送器 {transmitter_id} 连接关闭")
|
||||
break
|
||||
except socket.timeout:
|
||||
# 超时是正常的,继续接收
|
||||
continue
|
||||
except Exception as e:
|
||||
print(f"接收数据异常: {e}")
|
||||
break
|
||||
|
||||
except ConnectionRefusedError:
|
||||
print(f"变送器 {transmitter_id} 连接失败:{IP}:{PORT} 拒绝连接")
|
||||
except Exception as e:
|
||||
print(f"变送器 {transmitter_id} 异常:{e}")
|
||||
finally:
|
||||
self.connection_status[transmitter_id] = False
|
||||
if sock:
|
||||
try:
|
||||
sock.close()
|
||||
except:
|
||||
pass
|
||||
# 重试间隔
|
||||
time.sleep(3)
|
||||
|
||||
# 直接读取 变送器返回的数据(从缓存中获取)
|
||||
def read_data_sub(self, transmitter_id):
|
||||
|
||||
"""
|
||||
Args: transmitter_id 为1 表示上料斗, 为2 表示下料斗
|
||||
return: 读取成功返回重量 weight: int, 失败返回 None
|
||||
"""
|
||||
# 直接返回缓存的最新重量值
|
||||
return self.latest_weights.get(transmitter_id)
|
||||
|
||||
def get_connection_status(self, transmitter_id):
|
||||
"""
|
||||
获取变送器连接状态
|
||||
Args: transmitter_id 为1 表示上料斗, 为2 表示下料斗
|
||||
return: 连接状态 bool
|
||||
"""
|
||||
return self.connection_status.get(transmitter_id, False)
|
||||
|
||||
def stop(self):
|
||||
"""停止后台线程"""
|
||||
self.running = False
|
||||
# 等待线程结束
|
||||
for thread in self.threads.values():
|
||||
if thread.is_alive():
|
||||
thread.join(timeout=2)
|
||||
print("变送器后台接收线程已停止")
|
||||
|
||||
def get_latest_valid_packet(self, raw_data):
|
||||
"""
|
||||
解决TCP粘包:
|
||||
从原始数据中,筛选所有有效包,返回最新的一个有效包
|
||||
有效包标准: 1. 能UTF-8解码 2. 按逗号拆分≥3个字段 3. 第三个字段含数字(重量)
|
||||
"""
|
||||
DELIMITER = b'\r\n'
|
||||
# 1. 按分隔符拆分,过滤空包
|
||||
packets = [p for p in raw_data.split(DELIMITER) if p]
|
||||
if not packets:
|
||||
return None
|
||||
|
||||
valid_packets = []
|
||||
for packet in packets:
|
||||
try:
|
||||
# 过滤无效ASCII字符(只保留可见字符)
|
||||
valid_chars = [c for c in packet if 32 <= c <= 126]
|
||||
filtered_packet = bytes(valid_chars)
|
||||
# 2. 验证解码
|
||||
data_str = filtered_packet.decode('utf-8').strip()
|
||||
# 3. 验证字段数量
|
||||
parts = data_str.split(',')
|
||||
if len(parts) < 3:
|
||||
continue
|
||||
# 4. 验证重量字段含数字
|
||||
weight_part = parts[2].strip()
|
||||
if not any(char.isdigit() for char in weight_part):
|
||||
continue
|
||||
# 满足所有条件,加入有效包列表
|
||||
valid_packets.append(packet)
|
||||
except (UnicodeDecodeError, IndexError):
|
||||
# 解码失败或字段异常,跳过该包
|
||||
continue
|
||||
|
||||
# 返回最后一个有效包(最新),无有效包则返回None
|
||||
return valid_packets[-1] if valid_packets else None
|
||||
|
||||
def parse_weight(self, packet_data):
|
||||
"""解析重量函数:提取重量数值(如从 b'ST,NT,+0000175\r\n' 中提取 175)"""
|
||||
try:
|
||||
data_str = packet_data.decode('utf-8').strip()
|
||||
parts = data_str.split(',')
|
||||
# 确保有完整的数据包,三个字段
|
||||
if len(parts) < 3:
|
||||
print(f"parse_weight: 包格式错误(字段不足):{data_str}")
|
||||
return None
|
||||
|
||||
weight_part = parts[2].strip()
|
||||
return int(''.join(filter(str.isdigit, weight_part)))
|
||||
except (IndexError, ValueError, UnicodeDecodeError) as e:
|
||||
# print(f"数据解析失败:{e},原始数据包:{packet_data}")
|
||||
return None
|
||||
|
||||
def read_data(self,transmitter_id):
|
||||
"""获取重量函数:根据变送器ID获取当前重量,三次"""
|
||||
max_try_times=5
|
||||
try_times=0
|
||||
while try_times<max_try_times:
|
||||
weight=self.read_data_sub(transmitter_id)
|
||||
if weight is not None:
|
||||
return weight
|
||||
try_times+=1
|
||||
print(f'-----获取重量异常-------------- transmitter_id: {transmitter_id}')
|
||||
print(f'-----获取重量异常-------------- transmitter_id: {transmitter_id}')
|
||||
return None
|
||||
32
hardware/transmitter_device.py
Normal file
32
hardware/transmitter_device.py
Normal file
@ -0,0 +1,32 @@
|
||||
# hardware/transmitter.py
|
||||
from pymodbus.exceptions import ModbusException
|
||||
import socket
|
||||
from config.ini_manager import ini_manager
|
||||
from config.settings import app_set_config
|
||||
import time
|
||||
from metric_device import DeviceManager
|
||||
from enum import Enum, unique
|
||||
|
||||
@unique
|
||||
class HopperType(Enum):
|
||||
UPPER = 1 # 上料斗
|
||||
LOWER = 2 # 下料斗
|
||||
|
||||
class TransmitterController:
|
||||
def __init__(self, relay_controller=None):
|
||||
self.relay_controller = relay_controller
|
||||
UPPER_HOPPER_IP = ini_manager.upper_transmitter_ip
|
||||
UPPER_HOPPER_PORT = ini_manager.upper_transmitter_port
|
||||
LOWER_HOPPER_IP = ini_manager.lower_transmitter_ip
|
||||
LOWER_HOPPER_PORT = ini_manager.lower_transmitter_port
|
||||
self.upper_scale = DeviceManager.get_device(UPPER_HOPPER_IP, UPPER_HOPPER_PORT, "upper_scale") # 上料斗
|
||||
self.lower_scale = DeviceManager.get_device(LOWER_HOPPER_IP, LOWER_HOPPER_PORT, "lower_scale") # 下料斗
|
||||
|
||||
def read_data(self, transmitter_id):
|
||||
"""获取重量函数:
|
||||
Args: transmitter_id 为指定的上/下料斗的id
|
||||
"""
|
||||
if transmitter_id == HopperType.UPPER:
|
||||
return self.upper_scale.get_valid_weight()
|
||||
elif transmitter_id == HopperType.LOWER:
|
||||
return self.lower_scale.get_valid_weight()
|
||||
Reference in New Issue
Block a user