Commit 2deb9c27 authored by 方开's avatar 方开 🍔
Browse files

增加对气象站的解析和接收的命令的错误处理

parent c722ab20
......@@ -13,6 +13,14 @@ from common.filePath.filePath import logging
monkey.patch_all()
# 将十六进制数转成二进制有符号数据负数取补码,正数不变,最终计算其十进制的数值
def hex2Int(data, times=1, bits=16):
data = (bin(int(data, 16))[2:]).zfill(bits)
value = int(data[1:], 2) - int(data[0]) * (1 << (bits-1))
result = value / times
return result
# 根据仓房类型不同对温度、空气温湿度、虫害、气体、通风控制数据作解析,水份类型数据直接全部返回
def checkData(data: bytes, houseType: str, measureType: str, pointnum):
# 收到的所有数据用小写表示便于处理
......@@ -89,21 +97,22 @@ def checkData(data: bytes, houseType: str, measureType: str, pointnum):
# 通风控制数据
# 返回数据的最后两个字节代表1-16路的开关状态
elif flag == "a3" and measureType == "wind":
windData = data[18:]
windData = (bin(int(data[-4:], 16))[2:]).zfill(16)
windData = windData[6:12]
logging.info(f"通风状态数据为{windData}")
return [windData]
# 气象站
elif measureType == "weather":
weatherData = data[12:]
# 分别解析相应的数据
airpeedData = int(weatherData[:4], 16) / 10
rainFallData = int(weatherData[4:8], 16) / 5
airPressureData = int(weatherData[8:12], 16) / 10
sumRainfallData = int(weatherData[12:16], 16) / 5
temperatureData = int(weatherData[16:20], 16) / 10
humidityData = int(weatherData[20:24], 16) / 10
snowData = int(weatherData[24:28], 16) * 1
windDirectionData = int(weatherData[28:32], 16) * 1
airpeedData = hex2Int(weatherData[:4], times=10)
rainFallData = hex2Int(weatherData[4:8], times=5)
airPressureData = hex2Int(weatherData[8:12], times=10)
sumRainfallData = hex2Int(weatherData[12:16], times=5)
temperatureData = hex2Int(weatherData[16:20], times=10)
humidityData = hex2Int(weatherData[20:24], times=10)
snowData = hex2Int(weatherData[24:28], times=1)
windDirectionData = hex2Int(weatherData[28:32], times=1)
# 换算成实际数据列表返回
weatherDatas = [airpeedData, rainFallData, airPressureData, sumRainfallData, temperatureData, humidityData, snowData,
......@@ -145,16 +154,6 @@ def checkData(data: bytes, houseType: str, measureType: str, pointnum):
# 分别取出温度和湿度数据
tempData = data[14:18]
humiData = data[18:22]
# 温湿度换算成实际值
# if tempData[0] == "8":
# tempValue = int(tempData[1:4], 16) * 0.0625
# else:
# tempValue = None
# if humiData[0] == "8":
# humiValue = int(humiData[1:4], 16) / 10
# else:
# humiValue = None
tempValue = int(tempData[1:4], 16) * 0.0625
humiValue = int(humiData[1:4], 16) / 10
realTempHumi = [tempValue, humiValue]
......@@ -355,44 +354,53 @@ def handleCommand(command: str):
if measureCmd:
# 对于测试指令中的每种类型进行测试
alldict = {}
for tp in measureCmd:
# 气体虫害测试超时为60秒
if tp in ["air", "bug"]:
timeout = 50
# 水分为20秒
elif tp == "water":
timeout = 50
# 其余类型为3秒
else:
timeout = 2
data = measureit(houseId, tp, timeout)
# 对于水分信息,需要获取到其类型
if tp == "water":
gc = GetCmds(houseId)
subid = gc.getSubIdByType("water")
cerealsSpecies = gc.getSubInfo(
subid[0]).get("cerealsSpecies")
alldict["water"] = [
{"species": cerealsSpecies, "data": data}]
elif tp in ["temp", "temphumi", "bug", "air", "weather"]:
alldict[tp] = [{"species": tp, "data": data}]
try:
for tp in measureCmd:
# 气体虫害测试超时为60秒
if tp in ["air", "bug"]:
timeout = 50
# 水分为20秒
elif tp == "water":
timeout = 50
# 其余类型为3秒
else:
timeout = 2
data = measureit(houseId, tp, timeout)
# 对于水分信息,需要获取到其类型
if tp == "water":
gc = GetCmds(houseId)
subid = gc.getSubIdByType("water")
cerealsSpecies = gc.getSubInfo(
subid[0]).get("cerealsSpecies")
alldict["water"] = [
{"species": cerealsSpecies, "data": data}]
elif tp in ["temp", "temphumi", "bug", "air", "weather"]:
alldict[tp] = [{"species": tp, "data": data}]
else:
pass
logging.info("已测试完所有指令数据")
dicts["measured"] = alldict
sendData = json.dumps(dicts)
return sendData
else:
pass
logging.info("已测试完所有指令数据")
dicts["measured"] = alldict
sendData = json.dumps(dicts)
return sendData
except Exception as f:
logging.info(f"error:{f}")
sendData = None
# 如果是控制命令
elif controlCmd != None:
logging.info(controlCmd)
controlList = []
for tp in controlCmd:
data = controlit(houseId, tp, flag=controlCmd[tp], timeout=3)
controlList.append({"species": tp, "data": data})
dicts["controled"] = controlList
sendData = json.dumps(dicts)
try:
for tp in controlCmd:
data = controlit(
houseId, tp, flag=controlCmd[tp], timeout=3)
controlList.append({"species": tp, "data": data})
dicts["controled"] = controlList
sendData = json.dumps(dicts)
except Exception as g:
logging.info(f"指令error:{g}")
sendData = None
else:
sendData = None
else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment