添加消息提示

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