Commit 3c59b22e authored by 方开's avatar 方开 🍔
Browse files

Merge branch 'redev' into 'master'

Redev

See merge request fangkai/cloud-machine!2
parents 79075925 2deb9c27
......@@ -85,18 +85,18 @@ def getConfig(configData: dict):
subId, cmds = genSubMachineCmd(
subMachineData, "01")
pointNum = subMachineData.get("pointNum")
# 空气温湿度通道号为23 代表外温外湿,需要设置全局指令
# 空气温湿度通道号为2f 代表外温外湿,需要设置全局指令
for cmd in cmds:
if cmd[-2:] == "23":
if cmd[-2:] == "2f":
# 将外温外湿指令从该仓删除作为全仓指令
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
splitChannel = splitChanID(channelIds)
# 如果通道号列表正确
if splitChannel:
cmds = []
......
{
"1": {
"723": {
"ip": "127.0.0.1",
"port": 7000,
"housetype": "control",
......
This diff is collapsed.
......@@ -12,6 +12,15 @@ 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):
# 收到的所有数据用小写表示便于处理
......@@ -24,11 +33,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
......@@ -81,26 +92,27 @@ def checkData(data: bytes, houseType: str, measureType: str, pointnum):
# 水份数据
elif flag == "ac" and measureType == "water":
return data
return [data]
# 通风控制数据
# 返回数据的最后两个字节代表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,
......@@ -120,20 +132,14 @@ 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
if tempData[i] == "8":
tempValue = int(tempData[i+1:i+4], 16) * 0.0625
realTemp.append(tempValue)
else:
realTemp.append(None)
# 根据点数来截取相关数据
if pointnum != None:
pn = int(pointnum)
return realTemp[0:pn]
else:
return []
return realTemp
else:
logging.warning(f"crc 检验失败,数据不正确!")
return []
......@@ -148,11 +154,11 @@ def checkData(data: bytes, houseType: str, measureType: str, pointnum):
# 分别取出温度和湿度数据
tempData = data[14:18]
humiData = data[18:22]
# 温湿度换算成实际值
tempValue = int(tempData, 16) / 10
humiValue = int(humiData, 16) / 10
tempValue = int(tempData[1:4], 16) * 0.0625
humiValue = int(humiData[1:4], 16) / 10
realTempHumi = [tempValue, humiValue]
return realTempHumi
else:
logging.info(f"crc 检验失败,数据不正确!")
return []
......@@ -199,12 +205,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)
......@@ -231,11 +252,11 @@ def measureit(houseId, measureType="all", timeout=2):
exterCmd = exterCmdData[1]
logging.info("准备测试外温外湿")
exterData = measureClient(addr, a2b_hex(
exterCmd), measureType, houseType, pointNum, timeout)
exterCmd), measureType, "multi", pointNum, timeout)
logging.info(exterData)
exterList.extend(exterData)
logging.info(f"内温内湿数据为{interList},外温外湿数据为{exterData}")
return interList, exterList
return [interList, exterList]
elif measureType in ["bug", "air", "weather"]:
# 气体、虫害、气象站只有一个分机
......@@ -261,9 +282,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} ")
......@@ -287,7 +309,7 @@ def measureit(houseId, measureType="all", timeout=2):
return []
except Exception as e:
logging.info(f"发生错误:{e}")
pass
return []
# 控制处理
......@@ -307,11 +329,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 []
......@@ -336,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:
......@@ -396,11 +423,12 @@ def recv(tcpSocket):
logging.info("接收心跳包成功")
elif "measure" in data or "control" in data:
# 命令处理后发送给服务器
if handleCommand(data) == None:
result = handleCommand(data)
if result == None:
logging.info("解析指令和返回数据失败")
pass
else:
sendData = handleCommand(data) + "eeffeeff"
sendData = result + "eeffeeff"
tcpSocket.send(sendData.encode())
logging.info("发送给服务器数据完毕")
else:
......
......@@ -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,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 []
......@@ -198,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)
......@@ -234,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"]:
# 气体、虫害、气象站只有一个分机
......@@ -260,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} ")
......@@ -286,7 +299,7 @@ def measureit(houseId, measureType="all", timeout=2):
return []
except Exception as e:
logging.info(f"发生错误:{e}")
pass
return []
# 控制处理
......@@ -306,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