修改水流量传感器支持,增加2代传感器
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import math
|
||||
import time
|
||||
import json
|
||||
import logging
|
||||
@@ -166,6 +167,73 @@ class SensorService:
|
||||
logger.info(f"成功发布服务状态到 {SERVICE_STATUS_TOPIC}: {service_message}")
|
||||
self.last_service_status_time = current_time
|
||||
|
||||
# float get_temperature(float Rt)
|
||||
# {
|
||||
# const float Rp = 50000.0; ///< 10K NTC
|
||||
# const float T2 = (273.15+25.0); ///< 10K NTC
|
||||
# const float Bx = 3950.0; ///< 10K NTC
|
||||
# const float Ka = 273.15; ///< 10K NTC
|
||||
|
||||
# float Temp = 0.0f;
|
||||
# Temp = Rt / Rp;
|
||||
# Temp = log(Temp);
|
||||
# Temp /=Bx;
|
||||
# Temp += (1 / T2);
|
||||
# Temp = 1 / Temp;
|
||||
# Temp -= Ka;
|
||||
# return Temp;
|
||||
# }
|
||||
|
||||
def wsu_get_temperature(self, Rt):
|
||||
"""计算WSU温度"""
|
||||
Rp = 50000.0
|
||||
T2 = (273.15+25.0)
|
||||
Bx = 3950.0
|
||||
Ka = 273.15
|
||||
|
||||
Temp = 0.0
|
||||
Temp = Rt / Rp
|
||||
Temp = math.log10(Temp)
|
||||
Temp /=Bx
|
||||
Temp += (1 / T2)
|
||||
Temp = 1 / Temp
|
||||
Temp -= Ka
|
||||
return Temp
|
||||
|
||||
def wsu_get_flow_rate_g1(self, freq):
|
||||
"""计算WSU流量"""
|
||||
if freq == 0:
|
||||
return 0.0
|
||||
return 50 * 1000.0 * 60 * 10 / 660 / freq
|
||||
|
||||
def wsu_get_flow_rate_g2(self, freq):
|
||||
"""计算WSU流量"""
|
||||
if freq == 0:
|
||||
return 0.0
|
||||
return 50 * 1000.0 * 60 * 10 / 660 / freq
|
||||
|
||||
def handle_wsu_data(self, device, regs):
|
||||
"""处理WSU数据"""
|
||||
|
||||
if not regs or len(regs) < 2:
|
||||
return 0, 0
|
||||
|
||||
generation = device.get("generation", "0")
|
||||
flow_rate = regs[0]
|
||||
water_temp = regs[1]
|
||||
|
||||
if generation == "0" and regs[1] < 500:
|
||||
flow_rate = flow_rate / 1000.0
|
||||
water_temp = water_temp / 10.0
|
||||
elif generation == "1" and regs[1] >= 500:
|
||||
flow_rate = self.wsu_get_flow_rate_g1(flow_rate)
|
||||
water_temp = self.wsu_get_temperature(water_temp * 2)
|
||||
elif generation == "2":
|
||||
flow_rate = self.wsu_get_flow_rate_g2(flow_rate)
|
||||
water_temp = self.wsu_get_temperature(water_temp * 2)
|
||||
|
||||
return flow_rate, water_temp
|
||||
|
||||
def read_and_publish_devices_data(self):
|
||||
"""读取并发布所有设备数据"""
|
||||
# 确保Modbus连接正常
|
||||
@@ -190,12 +258,15 @@ class SensorService:
|
||||
device["error_count"] = device.get("error_count", 10) + 1
|
||||
else:
|
||||
device["error_count"] = 0
|
||||
|
||||
flow_rate, water_temp = self.handle_wsu_data(device, regs)
|
||||
|
||||
device.update({
|
||||
"status" : {
|
||||
"online": device.get("error_count", 10) < 3,
|
||||
"flow_rate": regs[0] / 1000.0 if regs and len(regs) > 0 else None,
|
||||
"water_temp": regs[1] / 10.0 if regs and len(regs) > 1 else None,
|
||||
}
|
||||
"status" : {
|
||||
"online": device.get("error_count", 10) < 3,
|
||||
"flow_rate": flow_rate,
|
||||
"water_temp": water_temp
|
||||
}
|
||||
})
|
||||
elif device.get('type', '') == 'OSU':
|
||||
# 读取输入寄存器
|
||||
|
||||
Reference in New Issue
Block a user