Commit fe8cf72f authored by 李远洪's avatar 李远洪

添加了一个函数

parent ea864fb4
......@@ -156,16 +156,48 @@ class MessageBase(Base):
data = self.int2BCD(int(data))
return data
#######################################################
# 获取UTC时间转换位BCD格式
#######################################################
def GBKString2Hex(self,data):
data = str("渝B23CX".encode("gbk"))
data = data[2:len(data) - 1]
print(data)
print(type(data))
print(type("\xd3\xe5B23CX"))
print(data == "\xd3\xe5B23CX")
print(data)
print(b"\xd3\xe5B23CX")
data = self.str2Hex(data)
return data
#######################################################
# 16进制转字符串
#######################################################
def hex2Str(self,data):
theStr = ""
while data != "":
tmp = data[:2]
data = data[2:]
theStr = theStr + chr(int(tmp,16))
return theStr
if __name__ == "__main__":
# print(MessageBase().str2Hex("uvwxyz"))
# print(MessageBase().str2Ascsii("uvwxyz"))
print(MessageBase().int2hexStringByBytes(220400566542345564784802,20))
print(MessageBase().str2Ascsii("uvwxyz"))
# print(MessageBase().int2hexStringByBytes(220400566542345564784802,20))
print(MessageBase().str2Hex("a865h643gfdj64fd7432"))
print(MessageBase().str2Hex("\xd3\xe5B23CX"))
print(MessageBase().hex2Str("61383635683634336766646a3634666437343332"))
# print(MessageBase().str2Hex("\xd3\xe5B23CX"))
# print(MessageBase().getMsgBodyProperty())
# print(MessageBase().int2BCD(13146201117))
# print(MessageBase().getCheckCode("8001000501314620111800000000000200"))
# print(MessageBase().getMsgHeader())
# print(MessageBase().generateMsg())
\ No newline at end of file
# print(MessageBase().generateMsg())
# print(MessageBase().GBKString2Hex("渝B23CX"))
#encoding:utf-8
'''
定义终端版本信息主动上报
'''
from lib.protocol.message.MessageBase import MessageBase
class TerminalVersionInfo_msg(MessageBase):
def __init__(self):
super().__init__() #不执行该方法,无法使用父类里面定义的属性
pass
#######################################################
# 生成一条完整的消息
#######################################################
def generateMsg(self):
msg = ""
msgHeader = self.getMsgHeader()
msgBody = self.getMsgBody()
checkCode = self.getCheckCode(msgHeader + msgBody)
msg = msg + self.IDENTIFY
info = msgHeader + msgBody + checkCode
info = self.replace7e7d(info)
msg = msg + info
msg = msg + self.IDENTIFY
return msg
#######################################################
# 获取消息体
#######################################################
def getMsgBody(self):
msg = ""
return msg
#######################################################
# 获取消息头
#######################################################
def getMsgHeader(self):
# msgID = self.int2hexStringByBytes(102,2) #消息id
msgID = "0205"
subPkg = 0
msgBodyProperty = self.getMsgBodyProperty(msgBodyLen=int(len(self.getMsgBody()) / 2),subPkg=subPkg) #消息体属性
phoneNum = self.int2BCD(13146201119) #终端手机号
msgWaterCode = self.int2hexStringByBytes(1,2) #消息流水号
if subPkg != 8192:
subPkgContent = "" #消息包封装项
else:
subPkgContent = self.getMsgPackage()
data = msgID + msgBodyProperty + phoneNum + msgWaterCode + subPkgContent
return data
#获取消息体属性
def getMsgBodyProperty(self,msgBodyLen=128,encryptionType=0,subPkg=0):
if msgBodyLen >= 512:
raise RuntimeError('消息体长度超长!')
msgBodyLen = msgBodyLen #消息体长度
encryptionType = encryptionType #加密方式
subPkg = subPkg #分包
retain = 0 #保留位
data = msgBodyLen + encryptionType + subPkg + retain
dataHex = self.int2hexStringByBytes(data,2)
return dataHex
if __name__ == "__main__":
pass
\ No newline at end of file
......@@ -18,8 +18,8 @@ port = 9001
# msg = MessageBase().generateMsg()
# msg = TerminalCommonMsgRes_msg().generateMsg() #终端通用应答
msg = TerminalHeartbeat_msg().generateMsg() #终端心跳
# msg = TerminalRegister_msg().generateMsg() #终端注册
# msg = TerminalHeartbeat_msg().generateMsg() #终端心跳
msg = TerminalRegister_msg().generateMsg() #终端注册
# msg = TerminalCancle_msg().generateMsg() #终端注销
# msg = TerminalAuthenticate_msg().generateMsg() #终端鉴权
# msg = Location_msg().generateMsg() #位置信息汇报
......@@ -35,13 +35,13 @@ client.send(binascii.a2b_hex(msg))
# client.send(bytes.fromhex(msg))
data = client.recv(BUF_SIZE)
print(binascii.a2b_hex(msg))
print(data)
# print(binascii.b2a_hex(data))
# print(PlatformCommon_res(data).binary2ascii(data))
# print(PlatformCommon_res(data).getOriginalMsg())
print(PlatformCommon_res(data).getMsg())
# print(TerminalRegister_res(data).getMsg())
# print(PlatformCommon_res(data).getMsg())
print(TerminalRegister_res(data).getMsg())
client.close()
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