格式化

This commit is contained in:
murmur 2023-03-13 19:27:54 +08:00
parent a6ff83f9b7
commit b1de714ddc

View File

@ -15,8 +15,9 @@ p = {}
# 地址 ==ccid # 地址 ==ccid
pp = {} pp = {}
def add_timestamp(): def add_timestamp():
print(datetime.datetime.now(),":") print(datetime.datetime.now(), ":")
def save_cache(dic): def save_cache(dic):
@ -95,6 +96,7 @@ def get_ccid(addr):
+--------+--------------+----------+----------+--------------+------------------------+--------+ +--------+--------------+----------+----------+--------------+------------------------+--------+
""" """
def tt_hh(addr, data): def tt_hh(addr, data):
"""处理心跳""" """处理心跳"""
@ -102,8 +104,8 @@ def tt_hh(addr, data):
ccid = data[7 + offs : 11 + offs] ccid = data[7 + offs : 11 + offs]
update_pairs(addr, ccid) update_pairs(addr, ccid)
# 组帧再返回 # 组帧再返回
msg=data[:] msg = data[:]
msg[12:-1]=val = '%04x'%int(time.time()) msg[12:-1] = val = "%04x" % int(time.time())
# 计算校验和 # 计算校验和
crc = 0 crc = 0
for i in msg[:-1]: for i in msg[:-1]:
@ -117,8 +119,8 @@ def tt_hh(addr, data):
# 仅有新终端上线时才重发 # 仅有新终端上线时才重发
if ccid in cache: if ccid in cache:
msgs = cache[ccid][:] # 浅拷贝 msgs = cache[ccid][:] # 浅拷贝
sccid=msgs[0][7 + offs : 11 + offs] sccid = msgs[0][7 + offs : 11 + offs]
print("",len(msgs),"包缓存数据待发。",sccid,"-->",ccid) print("", len(msgs), "包缓存数据待发。", sccid, "-->", ccid)
for i in range(len(msgs)): for i in range(len(msgs)):
try: try:
add_timestamp() add_timestamp()
@ -126,24 +128,25 @@ def tt_hh(addr, data):
addr.send(msgs[i]) addr.send(msgs[i])
# 成功则清空已发送成功的缓存数据 # 成功则清空已发送成功的缓存数据
cache[ccid].pop(0) cache[ccid].pop(0)
print("",i+1,"包缓存数据发送完成。") print("", i + 1, "包缓存数据发送完成。")
except: except:
# 异常时退出循环不用再尝试发送后续msgs # 异常时退出循环不用再尝试发送后续msgs
print("",i+1,"包发送失败,停止尝试") print("", i + 1, "包发送失败,停止尝试")
break break
if cache[ccid] == []: if cache[ccid] == []:
print("缓存数据全部发送完成。",sccid,"-->",ccid) print("缓存数据全部发送完成。", sccid, "-->", ccid)
del cache[ccid] del cache[ccid]
save_cache(cache) save_cache(cache)
return 0 return 0
def check_valid(data): def check_valid(data):
# 检查数据有效性 # 检查数据有效性
return bytearray(data[:]) return bytearray(data[:])
def tt_trans(addr, data): def tt_trans(addr, data):
"""发送数据到指定ccid""" """发送数据到指定ccid"""
@ -171,7 +174,7 @@ def tt_trans(addr, data):
cache[tccid].append(msg) cache[tccid].append(msg)
save_cache(cache) save_cache(cache)
print("终端未在线或ccid",tccid,"错误。数据已缓存,待目标终端上线后重发。") print("终端未在线或ccid", tccid, "错误。数据已缓存,待目标终端上线后重发。")
return 0 return 0
print("匹配在线终端为:", tccid, "<--->", taddr) print("匹配在线终端为:", tccid, "<--->", taddr)
@ -179,7 +182,7 @@ def tt_trans(addr, data):
try: try:
add_timestamp() add_timestamp()
taddr.send(msg) taddr.send(msg)
print("数据发送成功。",sccid,"-->",tccid) print("数据发送成功。", sccid, "-->", tccid)
except Exception: except Exception:
# 发送失败 # 发送失败
taddr.close() taddr.close()
@ -197,11 +200,11 @@ def tt_trans(addr, data):
def data_split(data): def data_split(data):
# 按帧头分割长数据 # 按帧头分割长数据
h= b"$TTMS" #b'\x24\x54\x54\x4d\x53' h = b"$TTMS" # b'\x24\x54\x54\x4d\x53'
dlist= data.split(h)[1:] dlist = data.split(h)[1:]
for i in range(len(dlist)): for i in range(len(dlist)):
dlist[i] = h + dlist[i] dlist[i] = h + dlist[i]
print("解析为",len(dlist),"包数据。") print("解析为", len(dlist), "包数据。")
return dlist return dlist
@ -215,7 +218,7 @@ def tt_decode(addr, data):
# 依据帧头和最小长度初判数据有效性 # 依据帧头和最小长度初判数据有效性
# 只要接收数据给的缓存足够大,即使是多条数据组成的长数据也默认帧格式是完整的,前几个字节必须是帧头 # 只要接收数据给的缓存足够大,即使是多条数据组成的长数据也默认帧格式是完整的,前几个字节必须是帧头
if data[:5] != b"$TTMS" or len(data) < 12 : if data[:5] != b"$TTMS" or len(data) < 12:
return -1 return -1
dlist = data_split(data) dlist = data_split(data)
@ -231,18 +234,16 @@ def tt_decode(addr, data):
flag = -1 flag = -1
break break
else: else:
flag=0 flag = 0
else: else:
break break
return flag return flag
class MyServer(socketserver.BaseRequestHandler): class MyServer(socketserver.BaseRequestHandler):
def handle(self): # 回调 def handle(self): # 回调
add_timestamp() add_timestamp()
print("终端", self.client_address,"已上线等待上报心跳注册ccid。") print("终端", self.client_address, "已上线等待上报心跳注册ccid。")
conn = self.request conn = self.request
# print(type(conn),conn.fd) # print(type(conn),conn.fd)
@ -266,7 +267,7 @@ if __name__ == "__main__":
server = socketserver.ThreadingTCPServer(("", 7222), MyServer) server = socketserver.ThreadingTCPServer(("", 7222), MyServer)
add_timestamp() add_timestamp()
print("服务端初始化成功。",server.server_address) print("服务端初始化成功。", server.server_address)
server.serve_forever() server.serve_forever()