添加消息提示

This commit is contained in:
murmur 2023-03-20 23:10:13 +08:00
parent 0708bfbd22
commit 741e6ef923

View File

@ -35,7 +35,7 @@ def s2wx(title,description, content):
"""通过自建msgpusher发送至微信 """通过自建msgpusher发送至微信
当前title值无效 当前title值无效
""" """
return # return
# GET 方式 # GET 方式
# res = requests.get(f"{SERVER}/push/{USERNAME}?title={title}" # res = requests.get(f"{SERVER}/push/{USERNAME}?title={title}"
# f"&description={description}&content={content}&token={TOKEN}") # f"&description={description}&content={content}&token={TOKEN}")
@ -45,8 +45,9 @@ def s2wx(title,description, content):
"title": title, "title": title,
"description": description, "description": description,
"content": f""" "content": f"""
### 时间戳: *时戳*
{datetime.datetime.now()} _{datetime.datetime.now()}_
*信息*
{content}""", {content}""",
"token": "tt" "token": "tt"
}) })
@ -102,24 +103,27 @@ def update_pairs(addr, ccid, clear=0):
if ccid and not clear: if ccid and not clear:
# 添加或更新客户端 # 添加或更新客户端
if ccid not in p: if ccid not in p:
t = f""" t = f"""*信息:*
### **终端信息:** 天通终端上线
CCID:{ccid.decode()} CCID:{ccid.decode()}
地址:{str(addr.getpeername())} 地址:{str(addr.getpeername())}
""" """
s2wx("","天通终端上线",t) s2wx("","天通终端上线",t)
p[ccid] = addr # p[ccid] = addr
pp[addr] = ccid pp[addr] = ccid
p.clear
else: else:
del p[ccid] del p[ccid]
del pp[addr] del pp[addr]
print("在线终端已更新。") print("在线终端已更新。")
# 打印在线终端 # 打印在线终端
for i in p: # for i in p:
print(i, "<--->", p[i]) # print(i, "<--->", p[i])
# for i in pp: for i in pp:
# print(i,"-",pp[i]) p[pp[i]] = i
print(pp[i],"<--->",i)
def get_addr(ccid): def get_addr(ccid):
@ -182,6 +186,7 @@ def tt_hh(addr, data):
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)
s2wx("","",f"{len(msgs)}包缓存数据待发。 \n{sccid} --> {ccid}")
for i in range(len(msgs)): for i in range(len(msgs)):
try: try:
add_timestamp() add_timestamp()
@ -193,9 +198,11 @@ def tt_hh(addr, data):
except: except:
# 异常时退出循环不用再尝试发送后续msgs # 异常时退出循环不用再尝试发送后续msgs
print("", i + 1, "包发送失败,停止尝试") print("", i + 1, "包发送失败,停止尝试")
s2wx("","",f"{i + 1}包发送失败,停止尝试。")
break break
if cache[ccid] == []: if cache[ccid] == []:
print("缓存数据全部发送完成。", sccid, "-->", ccid) print("缓存数据全部发送完成。", sccid, "-->", ccid)
s2wx("","",f"缓存数据全部发送完成。 \n{sccid} --> {ccid}")
del cache[ccid] del cache[ccid]
save_cache(cache) save_cache(cache)
@ -215,7 +222,7 @@ def tt_trans(addr, data):
if not sccid: if not sccid:
return -1 return -1
tccid = data[7 + offs : 11 + offs] tccid = data[7 + offs : 11 + offs]
taddr = get_addr(tccid) # taddr = get_addr(tccid)
# 组帧 # 组帧
msg = check_valid(data) msg = check_valid(data)
msg[:5] = b"$TTMX" msg[:5] = b"$TTMX"
@ -227,16 +234,21 @@ def tt_trans(addr, data):
crc = crc ^ i crc = crc ^ i
msg[-1] = crc msg[-1] = crc
if not taddr: if tccid not in p:
# 未找到匹配的在线终端 # 未找到匹配的在线终端
# 更新缓存 # 更新缓存
# {tccid1=[msg1,msg2,...],...} # {tccid1=[msg1,msg2,...],...}
if cache is None:
cache=[]
print(cache,tccid)
cache[tccid].append(msg) cache[tccid].append(msg)
save_cache(cache) save_cache(cache)
print("终端未在线或ccid", tccid, "错误。数据已缓存,待目标终端上线后重发。") print("终端未在线或ccid", tccid, "错误。数据已缓存,待目标终端上线后重发。")
s2wx("","",f"终端未在线或ccid{tccid} 错误。 \n数据已缓存,待目标终端上线后重发。")
return 0 return 0
taddr = get_addr(tccid)
print("匹配在线终端为:", tccid, "<--->", taddr) print("匹配在线终端为:", tccid, "<--->", taddr)
# 发送 # 发送
@ -244,6 +256,7 @@ def tt_trans(addr, data):
add_timestamp() add_timestamp()
taddr.send(msg) taddr.send(msg)
print("数据发送成功。", sccid, "-->", tccid) print("数据发送成功。", sccid, "-->", tccid)
s2wx("","",f"匹配到在线终端, 数据发送成功。 \n{sccid.decode()} ---> {tccid.decode()}")
except Exception: except Exception:
# 发送失败 # 发送失败
taddr.close() taddr.close()
@ -255,6 +268,7 @@ def tt_trans(addr, data):
save_cache(cache) save_cache(cache)
print("发送失败,终端可能已掉线。数据已缓存,待目标终端上线后重发。", taddr) print("发送失败,终端可能已掉线。数据已缓存,待目标终端上线后重发。", taddr)
s2wx("","",f"发送失败,终端{taddr}可能已掉线。 \n数据已缓存,待目标终端上线后重发。")
return 0 return 0
@ -304,11 +318,12 @@ def tt_decode(addr, data):
class MyServer(socketserver.BaseRequestHandler): class MyServer(socketserver.BaseRequestHandler):
def setup(self): def setup(self):
self.request.settimeout(60*2) self.request.settimeout(60*2)
self.err = ""
def handle(self): # 回调 def handle(self): # 回调
add_timestamp() add_timestamp()
print("客户端", self.client_address, "已连接等待上报心跳注册ccid。") print("客户端", self.client_address, "已连接等待上报心跳注册ccid。")
s2wx("天通消息","TCP客户端接入",str(self.client_address) + "已连接等待上报心跳注册ccid。") s2wx("天通消息","TCP客户端接入",f"TCP客户端{str(self.client_address)}已连接等待上报心跳注册ccid。")
conn = self.request conn = self.request
# print(type(conn),conn.fd) # print(type(conn),conn.fd)
@ -316,6 +331,7 @@ class MyServer(socketserver.BaseRequestHandler):
try: try:
data = conn.recv(1024000) data = conn.recv(1024000)
except socket.timeout: except socket.timeout:
self.err = "心跳超时。"
print(self.client_address, "超时。") print(self.client_address, "超时。")
break break
if not data: if not data:
@ -324,6 +340,7 @@ class MyServer(socketserver.BaseRequestHandler):
print( print(
"接收到新数据", self.client_address, ",长度", len(data), "\r\n", data.hex(" ") "接收到新数据", self.client_address, ",长度", len(data), "\r\n", data.hex(" ")
) )
s2wx("","",f"接收到新数据,长度{len(data)}字节。")
if tt_decode(conn, data): if tt_decode(conn, data):
conn.close() conn.close()
print(self.client_address, "疑是非法连接,已切断。") print(self.client_address, "疑是非法连接,已切断。")
@ -332,9 +349,8 @@ class MyServer(socketserver.BaseRequestHandler):
def finish(self): def finish(self):
if self.request in pp: if self.request in pp:
t = f""" t = f"""终端下线了。{self.err}
### **终端信息:** CCID:{pp[self.request].decode()}
CCID:{get_ccid(self.request)}
地址:{str(self.client_address)} 地址:{str(self.client_address)}
""" """
s2wx("","天通终端下线了",t) s2wx("","天通终端下线了",t)
@ -342,7 +358,7 @@ CCID:{get_ccid(self.request)}
update_pairs(self.request,get_ccid(self.request),1) update_pairs(self.request,get_ccid(self.request),1)
else: else:
print("客户端",self.client_address,"已断开。") print("客户端",self.client_address,"已断开。")
s2wx("天通消息","TCP客户端断开",str(self.client_address) + "已断开连接。") s2wx("天通消息","TCP客户端断开",f"TCP客户端{str(self.client_address)}已断开连接。{self.err}")
@ -354,7 +370,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)
s2wx("天通消息","服务器上线","服务器成功上线") s2wx("天通消息","服务器上线","服务器端初始化完成,等待终端连接。")
server.timeout = 10 server.timeout = 10
server.serve_forever() server.serve_forever()