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

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

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