Commit 84b6d267 authored by 李远洪's avatar 李远洪

完成平台通用应答消息解析类

parent 2b2ab5b7
...@@ -33,8 +33,8 @@ class MessageBase(Base): ...@@ -33,8 +33,8 @@ class MessageBase(Base):
msgID = self.int2hexStringByBytes(102,2) #消息id msgID = self.int2hexStringByBytes(102,2) #消息id
msgBodyProperty = self.getMsgBodyProperty() #消息体属性 msgBodyProperty = self.getMsgBodyProperty() #消息体属性
phoneNum = self.int2BCD(13146201118) #终端手机号 phoneNum = self.int2BCD(13146201118) #终端手机号
msgWaterCode = self.int2BCD(1) #消息流水号 msgWaterCode = self.int2hexStringByBytes(1) #消息流水号
subPkgContent = "" #消息包封装项 subPkgContent = "" #消息包封装项s
data = msgID + msgBodyProperty + phoneNum + msgWaterCode + subPkgContent data = msgID + msgBodyProperty + phoneNum + msgWaterCode + subPkgContent
return data return data
...@@ -130,6 +130,6 @@ if __name__ == "__main__": ...@@ -130,6 +130,6 @@ if __name__ == "__main__":
# print(MessageBase().int2hexStringByBytes(22,2)) # print(MessageBase().int2hexStringByBytes(22,2))
# print(MessageBase().getMsgBodyProperty()) # print(MessageBase().getMsgBodyProperty())
# print(MessageBase().int2BCD(13146201117)) # print(MessageBase().int2BCD(13146201117))
print(MessageBase().getCheckCode("abcde0")) print(MessageBase().getCheckCode("8001000501314620111800000000000200"))
print(MessageBase().getMsgHeader()) print(MessageBase().getMsgHeader())
print(MessageBase().generateMsg()) print(MessageBase().generateMsg())
\ No newline at end of file
...@@ -40,7 +40,7 @@ class TerminalHeartbeat_msg(MessageBase): ...@@ -40,7 +40,7 @@ class TerminalHeartbeat_msg(MessageBase):
# msgID = self.int2hexStringByBytes(102,2) #消息id # msgID = self.int2hexStringByBytes(102,2) #消息id
msgID = "0002" msgID = "0002"
msgBodyProperty = self.getMsgBodyProperty(len(self.getMsgBody())) #消息体属性 msgBodyProperty = self.getMsgBodyProperty(len(self.getMsgBody())) #消息体属性
phoneNum = self.int2BCD(13146201118) #终端手机号 phoneNum = self.int2BCD(13146201119) #终端手机号
msgWaterCode = self.int2BCD(1) #消息流水号 msgWaterCode = self.int2BCD(1) #消息流水号
subPkgContent = "" #消息包封装项 subPkgContent = "" #消息包封装项
data = msgID + msgBodyProperty + phoneNum + msgWaterCode + subPkgContent data = msgID + msgBodyProperty + phoneNum + msgWaterCode + subPkgContent
......
#encoding:utf-8
'''
定义平台通用应答消息解码类
'''
from lib.protocol.message.MessageBase import MessageBase
from lib.protocol.messageRes.ResponseBase import ResponseBase
class PlatformCommon_res(ResponseBase):
def __init__(self,msg):
super().__init__()
if type(msg) == bytes:
self.msg = self.binary2ascii(msg)
else:
self.msg = msg
pass
#######################################################
# 获取消息
#######################################################
def getMsg(self):
json_msg = {}
json_msg["header"] = self.getMsgHeader()
json_msg["body"] = self.getMsgBody()
json_msg["checkCode"] = self.getCheckCode()
json_msg["calculateCheckCode"] = self.getCalculateCheckCode() #自己计算消息后得到的校验码
return json_msg
#######################################################
# 获取消息头
#######################################################
def getMsgHeader(self):
json_header = {}
data = self.removeIdentify(self.msg)
data = self.restore_7e7d(data)
header = data[:24]
msgId = header[:4] #消息id
msgBodyProperty = header[4:8] #消息体属性
phoneNum = header[8:20] #终端手机号
msgWaterCode = header[20:24] #消息流水号
json_header["msgId"] = int(msgId,16)
json_header["msgBodyProperty"] = self.getMsgBodyProperty(msgBodyProperty)
json_header["phoneNum"] = phoneNum[1:]
json_header["msgWaterCode"] = int(msgWaterCode,16)
return json_header
#获取消息体属性
def getMsgBodyProperty(self,data):
data = self.int2binStr(int(data),2)
data = self.restore_7e7d(data)
json_data = {}
subPkg = data[2:3] #分包
encryptionType = data[3:6] #加密方式
msgBodyLen = data[6:] #消息体长度
json_data["subPkg"] = int(subPkg,2)
json_data["encryptionType"] = int(encryptionType,2)
json_data["msgBodyLen"] = int(msgBodyLen,2)
return json_data
#######################################################
# 获取消息体
#######################################################
def getMsgBody(self):
json_body = {}
data = self.removeIdentify(self.msg)
data = self.restore_7e7d(data)
dataLen = len(data)
body = data[24:dataLen - 2]
resWaterCode = body[:4] #应答流水号
resId = body[4:8] #应答id
result = body[8:] #结果
json_body["resWaterCode"] = int(resWaterCode,16)
json_body["resId"] = int(resId,16)
json_body["result"] = int(result,16)
return json_body
#######################################################
# 获取校验码
#######################################################
def getCheckCode(self):
data = self.removeIdentify(self.msg)
data = self.restore_7e7d(data)
dataLen = len(data)
checkCode = data[dataLen - 2:]
return checkCode
#######################################################
# 计算消息得到校验码
#######################################################
def getCalculateCheckCode(self):
data = self.removeIdentify(self.msg)
data = self.restore_7e7d(data)
dataLen = len(data)
data = data[:dataLen - 2]
calculateCheckCode = MessageBase().getCheckCode(data)
return calculateCheckCode
#######################################################
# 获取最原始的消息数据(没有替换7e,7d之前的状态)
#######################################################
def getOriginalMsg(self):
data = self.removeIdentify(self.msg)
data = self.restore_7e7d(data)
data = "7e" + data + "7e"
return data
#encoding:utf-8
import binascii
from lib.protocol.Base import Base
class ResponseBase(Base):
def __init__(self):
pass
#######################################################
# 二进制转换为ascii码
#######################################################
def binary2ascii(self,binData):
strs = binascii.b2a_hex(binData)
strsLen = len(str(strs))
data = str(strs)[2:strsLen - 1]
return data
#######################################################
# 还原消息中被转换过的7e和7d
#######################################################
def restore_7e7d(self,data):
data = data.replace("7d02", "7e")
data = data.replace("7d01", "7d")
return data
#######################################################
# 去除标识位
#######################################################
def removeIdentify(self,data):
dataLen = len(data)
data = data[2:dataLen - 2]
return data
#######################################################
# 10进制转换为2进制字符串
#######################################################
def int2binStr(self,data,bytescount=1):
binStr = bin(data)
binStr = binStr[2:]
bytesLen = bytescount * 8
while len(binStr) < bytesLen:
binStr = "0" + binStr
return binStr
if __name__ == "__main__":
print(ResponseBase().int2binStr(7,2))
\ No newline at end of file
...@@ -3,6 +3,7 @@ import binascii ...@@ -3,6 +3,7 @@ import binascii
import socket import socket
from lib.protocol.message.MessageBase import MessageBase from lib.protocol.message.MessageBase import MessageBase
from lib.protocol.message.TerminalHeartbeat_msg import TerminalHeartbeat_msg from lib.protocol.message.TerminalHeartbeat_msg import TerminalHeartbeat_msg
from lib.protocol.messageRes.PlatformCommon_res import PlatformCommon_res
def back7e7d(data): def back7e7d(data):
...@@ -31,8 +32,13 @@ client.connect((host, port)) ...@@ -31,8 +32,13 @@ client.connect((host, port))
client.send(binascii.a2b_hex(msg)) client.send(binascii.a2b_hex(msg))
# client.send(bytes.fromhex(msg)) # client.send(bytes.fromhex(msg))
data = client.recv(BUF_SIZE) data = client.recv(BUF_SIZE)
print(data) print(data)
print(binascii.b2a_hex(data)) print(binascii.b2a_hex(data))
print(binary2ascii(data)) print(PlatformCommon_res(data).binary2ascii(data))
print(PlatformCommon_res(data).getOriginalMsg())
print(PlatformCommon_res(data).getMsg())
client.close() 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