Commit dca9ded5 authored by 方开's avatar 方开 🍔
Browse files

增加对每个通道测温点数信息的生成和温度数据的截取

parent 30bd7917
......@@ -85,18 +85,18 @@ def getConfig(configData: dict):
subId, cmds = genSubMachineCmd(
subMachineData, "01")
pointNum = subMachineData.get("pointNum")
# 空气温湿度通道号为23 代表外温外湿,需要设置全局指令
for cmd in cmds:
if cmd[-2:] == "23":
# 将外温外湿指令从该仓删除作为全仓指令
cmds.remove(cmd)
d3 = {"extertemphumi":{"ip": ip, "port": port, "cmd": cmd}}
d3 = {"extertemphumi": {
"ip": ip, "port": port, "cmd": cmd}}
# 对于外温外湿需要设置成全局指令,为了区别单独放在一个文件里
with open(specialPath, "w", encoding="utf-8") as f:
json.dump(d3, f, ensure_ascii=False, indent=4)
json.dump(
d3, f, ensure_ascii=False, indent=4)
d2["pointNum"] = pointNum
humiList.append(subId)
allList.append(subId)
......@@ -129,7 +129,7 @@ def getConfig(configData: dict):
allList.append(subId)
elif subType == "wind":
addrs = format(int(subAddr, 16) + 200, "02x")
addrs = format(int(subAddr, 16), "02x")
cmds = [{
"status": [f"0301{addrs}000000010121"],
"on": [
......@@ -203,8 +203,7 @@ def getConfig(configData: dict):
elif subType == "temphumi":
humiList.append(subId)
pointNum = subMachineData.get("pointNum")
d2["pointNum"] = pointNum
# 如果通道号列表正确
if splitChannel:
cmds = []
......
......@@ -12,9 +12,8 @@ from common.filePath.filePath import logging
monkey.patch_all()
# 根据仓房类型不同对温度、空气温湿度、虫害、气体、通风控制数据作解析,水份类型数据直接全部返回
# 根据仓房类型不同对温度、空气温湿度、虫害、气体、通风控制数据作解析,水份类型数据直接全部返回
def checkData(data: bytes, houseType: str, measureType: str, pointnum):
# 收到的所有数据用小写表示便于处理
data = b2a_hex(data).decode().lower()
......@@ -26,11 +25,13 @@ def checkData(data: bytes, houseType: str, measureType: str, pointnum):
if flag == "a1" or measureType == "temp":
tempData = data[14:]
realTemp = []
# 温度换算
# 温度换算,异常坏点数据取 None 值
for i in range(0, len(tempData), 4):
if tempData[i] == "8":
tempValue = int(tempData[i+1:i+4], 16) * 0.0625
realTemp.append(tempValue)
else:
realTemp.append(None)
logging.info(f"该通道温度实际数据为{realTemp}")
return realTemp
......@@ -83,7 +84,7 @@ def checkData(data: bytes, houseType: str, measureType: str, pointnum):
# 水份数据
elif flag == "ac" and measureType == "water":
return data
return [data]
# 通风控制数据
# 返回数据的最后两个字节代表1-16路的开关状态
......@@ -122,20 +123,13 @@ def checkData(data: bytes, houseType: str, measureType: str, pointnum):
if crcValue == crc16s(tempData):
tempData = data[14:-4]
realTemp = []
# 对温度数据进行解析处理
# 对温度数据进行解析处理,坏点补上 None 值
for i in range(0, len(tempData), 4):
if i[0] == "8":
tempValue = int(i[1:], 16) / 10
realTemp.append(tempValue)
else:
realTemp.append(None)
# 根据点数来截取相关数据
if pointnum != None:
pn = int(pointnum)
return realTemp[0:pn]
else:
return []
else:
logging.warning(f"crc 检验失败,数据不正确!")
return []
......@@ -201,12 +195,27 @@ def measureit(houseId, measureType="all", timeout=2):
dataList = []
for subId in subIds:
# 如果命令列表数据不为空
addr, cmds, pointNum = gc.getCmdsBySubId(subId)
addr, cmds, pointNums = gc.getCmdsBySubId(subId)
if cmds:
for cmd in cmds:
for index, cmd in enumerate(cmds):
# 根据每个通道的点数去截取数据
pointNum = pointNums[index]
# 测试客户端
data = measureClient(addr, a2b_hex(
cmd), measureType, houseType, pointNum, timeout)
# 判断返回的温度数据长度是否足够
dataLen = len(data)
# 如果点数不够的话
if dataLen < int(pointNum):
needLen = int(pointNum) - dataLen
data.extend([None]*needLen)
elif dataLen > int(pointNum):
data = data[:int(pointNum)]
else:
pass
logging.info(f"此通道温度数据为{data}")
dataList.append(data)
......@@ -237,7 +246,7 @@ def measureit(houseId, measureType="all", timeout=2):
logging.info(exterData)
exterList.extend(exterData)
logging.info(f"内温内湿数据为{interList},外温外湿数据为{exterData}")
return interList, exterList
return [interList, exterList]
elif measureType in ["bug", "air", "weather"]:
# 气体、虫害、气象站只有一个分机
......@@ -263,9 +272,10 @@ def measureit(houseId, measureType="all", timeout=2):
if cmds:
# 单条指令
cmd = cmds[0]
# 注意到为了保证measureClient 函数的返回值一致,需要将水分值放在list里,所以使用的时候采用 data[0]
data = measureClient(addr, a2b_hex(
cmd), measureType, houseType, pointNum, timeout)
waterList = checkWater(cerealsSpecies, data)
waterList = checkWater(cerealsSpecies, data[0])
dataList = waterList
logging.info(f"全部水份数据为-{dataList} ")
......@@ -289,7 +299,7 @@ def measureit(houseId, measureType="all", timeout=2):
return []
except Exception as e:
logging.info(f"发生错误:{e}")
pass
return []
# 控制处理
......@@ -309,11 +319,7 @@ def controlit(houseId, measureType="wind", flag="on", timeout=3):
cmd), measureType, houseType, None, timeout)
result.extend(data)
time.sleep(2)
# 对返回的结果解析
# if "50" in result or "a0" in result:
# return "sucess"
# else:
# return "fail"
else:
logging.warning(f"无通风命令,请检查通风设置参数是否正确")
return []
......
......@@ -12,6 +12,8 @@ monkey.patch_all()
# 根据仓房类型不同对温度、空气温湿度、虫害、气体、通风控制数据作解析,水份类型数据直接全部返回
def checkData(data: bytes, houseType: str, measureType: str, pointnum):
# 收到的所有数据用小写表示便于处理
data = b2a_hex(data).decode().lower()
......@@ -23,11 +25,13 @@ def checkData(data: bytes, houseType: str, measureType: str, pointnum):
if flag == "a1" or measureType == "temp":
tempData = data[14:]
realTemp = []
# 温度换算
# 温度换算,异常坏点数据取 None 值
for i in range(0, len(tempData), 4):
if tempData[i] == "8":
tempValue = int(tempData[i+1:i+4], 16) * 0.0625
realTemp.append(tempValue)
else:
realTemp.append(None)
logging.info(f"该通道温度实际数据为{realTemp}")
return realTemp
......@@ -80,7 +84,7 @@ def checkData(data: bytes, houseType: str, measureType: str, pointnum):
# 水份数据
elif flag == "ac" and measureType == "water":
return data
return [data]
# 通风控制数据
# 返回数据的最后两个字节代表1-16路的开关状态
......@@ -119,19 +123,13 @@ def checkData(data: bytes, houseType: str, measureType: str, pointnum):
if crcValue == crc16s(tempData):
tempData = data[14:-4]
realTemp = []
# 对温度数据进行解析处理,坏点补上 None 值
for i in range(0, len(tempData), 4):
if tempData[i] == "8":
tempValue = int(tempData[i+1:i+4], 16) * 0.0625
if i[0] == "8":
tempValue = int(i[1:], 16) / 10
realTemp.append(tempValue)
else:
realTemp.append(None)
# 根据点数来截取相关数据
if pointnum != None:
pn = int(pointnum)
return realTemp[0:pn]
else:
return []
else:
logging.warning(f"crc 检验失败,数据不正确!")
return []
......@@ -197,12 +195,27 @@ def measureit(houseId, measureType="all", timeout=2):
dataList = []
for subId in subIds:
# 如果命令列表数据不为空
addr, cmds, pointNum = gc.getCmdsBySubId(subId)
addr, cmds, pointNums = gc.getCmdsBySubId(subId)
if cmds:
for cmd in cmds:
for index, cmd in enumerate(cmds):
# 根据每个通道的点数去截取数据
pointNum = pointNums[index]
# 测试客户端
data = measureClient(addr, a2b_hex(
cmd), measureType, houseType, pointNum, timeout)
# 判断返回的温度数据长度是否足够
dataLen = len(data)
# 如果点数不够的话
if dataLen < int(pointNum):
needLen = int(pointNum) - dataLen
data.extend([None]*needLen)
elif dataLen > int(pointNum):
data = data[:int(pointNum)]
else:
pass
logging.info(f"此通道温度数据为{data}")
dataList.append(data)
......@@ -233,7 +246,7 @@ def measureit(houseId, measureType="all", timeout=2):
logging.info(exterData)
exterList.extend(exterData)
logging.info(f"内温内湿数据为{interList},外温外湿数据为{exterData}")
return interList, exterList
return [interList, exterList]
elif measureType in ["bug", "air", "weather"]:
# 气体、虫害、气象站只有一个分机
......@@ -259,9 +272,10 @@ def measureit(houseId, measureType="all", timeout=2):
if cmds:
# 单条指令
cmd = cmds[0]
# 注意到为了保证measureClient 函数的返回值一致,需要将水分值放在list里,所以使用的时候采用 data[0]
data = measureClient(addr, a2b_hex(
cmd), measureType, houseType, pointNum, timeout)
waterList = checkWater(cerealsSpecies, data)
waterList = checkWater(cerealsSpecies, data[0])
dataList = waterList
logging.info(f"全部水份数据为-{dataList} ")
......@@ -285,7 +299,7 @@ def measureit(houseId, measureType="all", timeout=2):
return []
except Exception as e:
logging.info(f"发生错误:{e}")
pass
return []
# 控制处理
......@@ -305,11 +319,7 @@ def controlit(houseId, measureType="wind", flag="on", timeout=3):
cmd), measureType, houseType, None, timeout)
result.extend(data)
time.sleep(2)
# 对返回的结果解析
# if "50" in result or "a0" in result:
# return "sucess"
# else:
# return "fail"
else:
logging.warning(f"无通风命令,请检查通风设置参数是否正确")
return []
......
......@@ -6,7 +6,7 @@ PORT = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
senddata = {"id": "1", "measure": ["temp"]}
senddata = {"id": "723", "measure": ["temp"]}
cmd = json.dumps(senddata)
s.send(cmd.encode())
......
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