Commit 2b2ab5b7 authored by 李远洪's avatar 李远洪

完成了心跳消息的开发,删除了部分冗余文件

parent ca5e9955
...@@ -10,7 +10,21 @@ class MessageBase(Base): ...@@ -10,7 +10,21 @@ class MessageBase(Base):
def __init__(self): def __init__(self):
self.IDENTIFY = "7e" #标识位 self.IDENTIFY = "7e" #标识位
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
####################################################### #######################################################
# 获取消息头 # 获取消息头
...@@ -19,13 +33,13 @@ class MessageBase(Base): ...@@ -19,13 +33,13 @@ 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,2) #消息流水号 msgWaterCode = self.int2BCD(1) #消息流水号
subPkgContent = "" #消息包封装项 subPkgContent = "" #消息包封装项
data = msgID + msgBodyProperty + phoneNum + msgWaterCode + subPkgContent data = msgID + msgBodyProperty + phoneNum + msgWaterCode + subPkgContent
return data return data
#获取消息体属性 #获取消息体属性
def getMsgBodyProperty(self,msgBodyLen=128,encryptionType=1024,subPkg=8192): def getMsgBodyProperty(self,msgBodyLen=128,encryptionType=0,subPkg=0):
if msgBodyLen >= 512: if msgBodyLen >= 512:
raise RuntimeError('消息体长度超长!') raise RuntimeError('消息体长度超长!')
msgBodyLen = msgBodyLen #消息体长度 msgBodyLen = msgBodyLen #消息体长度
...@@ -41,12 +55,12 @@ class MessageBase(Base): ...@@ -41,12 +55,12 @@ class MessageBase(Base):
# 获取消息体 # 获取消息体
####################################################### #######################################################
def getMsgBody(self): def getMsgBody(self):
pass return ""
####################################################### #######################################################
# 获取校验码 # 获取校验码
####################################################### #######################################################
def getCheckCode(self,data): def getCheckCode(self,data="aa"):
if len(data) % 2 == 1: if len(data) % 2 == 1:
raise RuntimeError('数据段错误!') raise RuntimeError('数据段错误!')
start = data[0:2] start = data[0:2]
...@@ -56,6 +70,14 @@ class MessageBase(Base): ...@@ -56,6 +70,14 @@ class MessageBase(Base):
dataHex = self.int2hexStringByBytes(tmp) dataHex = self.int2hexStringByBytes(tmp)
return dataHex return dataHex
#######################################################
# 替换消息中的7e7d字符
#######################################################
def replace7e7d(self,data):
data = data.replace("7d","7d01")
data = data.replace("7e","7d02")
return data
####################################################### #######################################################
# 字符串转16进制 # 字符串转16进制
####################################################### #######################################################
...@@ -109,3 +131,5 @@ if __name__ == "__main__": ...@@ -109,3 +131,5 @@ if __name__ == "__main__":
# print(MessageBase().getMsgBodyProperty()) # print(MessageBase().getMsgBodyProperty())
# print(MessageBase().int2BCD(13146201117)) # print(MessageBase().int2BCD(13146201117))
print(MessageBase().getCheckCode("abcde0")) print(MessageBase().getCheckCode("abcde0"))
print(MessageBase().getMsgHeader())
print(MessageBase().generateMsg())
\ No newline at end of file
#encoding:utf-8
from lib.protocol.message import MessageBase
'''
定义终端通用应答
'''
class TerminalCommonMsgRes(MessageBase):
def __init__(self):
pass
\ No newline at end of file
#encoding:utf-8
'''
定义终端通用应答
'''
from lib.protocol.message.MessageBase import MessageBase
class TerminalCommonMsgRes_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):
resWaterCode = "0001" #对应的平台消息的流水号
msgId = "0001" #消息id,对应的平台消息的 ID
reslult = self.int2hexStringByBytes(0) #0:成功/确认;1:失败;2:消息有误;3:不支持
data = ""
msg = data + resWaterCode + msgId + reslult
return msg
#######################################################
# 获取消息头
#######################################################
def getMsgHeader(self):
msgID = self.int2hexStringByBytes(102,2) #消息id
msgBodyProperty = self.getMsgBodyProperty() #消息体属性
phoneNum = self.int2BCD(13146201118) #终端手机号
msgWaterCode = self.int2BCD(1) #消息流水号
subPkgContent = "" #消息包封装项
data = msgID + msgBodyProperty + phoneNum + msgWaterCode + subPkgContent
return data
if __name__ == "__main__":
print(TerminalCommonMsgRes_msg().generateMsg())
\ No newline at end of file
#encoding:utf-8
'''
定义终端心跳消息
'''
from lib.protocol.message.MessageBase import MessageBase
class TerminalHeartbeat_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 = "0002"
msgBodyProperty = self.getMsgBodyProperty(len(self.getMsgBody())) #消息体属性
phoneNum = self.int2BCD(13146201118) #终端手机号
msgWaterCode = self.int2BCD(1) #消息流水号
subPkgContent = "" #消息包封装项
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__":
print(TerminalHeartbeat_msg().generateMsg())
print(TerminalHeartbeat_msg().getMsgBodyProperty())
\ No newline at end of file
#coding:utf-8
import binascii
import socket
from lib.protocol.message.MessageBase import MessageBase
from lib.protocol.message.TerminalHeartbeat_msg import TerminalHeartbeat_msg
def back7e7d(data):
data = data.replace("7d02", "7e")
data = data.replace("7d01", "7d")
return data
def binary2ascii(binData):
strs = binascii.b2a_hex(binData)
data = str(strs)[2:]
return data
host = "10.100.9.17"
port = 9001
# msg = MessageBase().generateMsg()
msg = TerminalHeartbeat_msg().generateMsg() #终端心跳
print(msg)
BUF_SIZE = 1024
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # 在客户端开启心跳
client.connect((host, port))
client.send(binascii.a2b_hex(msg))
# client.send(bytes.fromhex(msg))
data = client.recv(BUF_SIZE)
print(data)
print(binascii.b2a_hex(data))
print(binary2ascii(data))
client.close()
#encoding:utf-8
'''
定义平台消息解码辅助类
'''
class MessageDecode():
def __init__(self):
pass
\ No newline at end of file
#coding:utf-8
'''
定义一个处理数据包的辅助方法集合
'''
from binascii import *
from crcmod import *
import struct
#####################################################
# 定义生成校验字段的函数(自己翻译的函数,简化了很多步骤)
#####################################################
def myCrc16(msg):
msg = str2Ascsii(msg)
crc = 0xFFFF
for i in range(0, len(msg)):
for j in range(0, 8):
cl5 = ((crc >> 15 & 1) == 1)
bit = ((msg[i] >> (7 - j) & 1) == 1)
crc <<= 1
# 通过与0xFFFF(即二进制:1111111111111111)做了一个或运算,将其转换为一个有符号的数
crc &= 0xFFFF
if (cl5 ^ bit):
crc ^= 0x1021;
crc = hex(crc) # 将10进制的crc转换为16进制
crc = str(crc)[2:] # 将16进制转换为字符串,并去掉前面的0x
return crc
#####################################################
# 定义生成校验字段的函数
# inputStr:需要传入一个已经转换为16进制的字符串
#####################################################
# add crc 16 check at the end of the string
def crc16(inputStr):
inputStrByte = bytes.fromhex(inputStr)
crc = 0xFFFF
for i in range(0, len(inputStrByte)):
for j in range(0, 8):
c15 = (crc >> 15) == 1
bit = ((inputStrByte[i] >> (7 - j)) & 1) == 1
crc <<= 1
crc &= 0xFFFF
if c15 ^ bit:
crc ^= 0x1021
crc = str(hex(crc))
crc = leftPad(crc[2:], 4)
# outputStr = inputStr + crc
outputStr = crc
return outputStr
# pad zero to the left of the string if not long enough
def leftPad(inputStr, strLen):
if (strLen > len(inputStr)):
outputStr = "0000000000000000000000000000000000000000" + inputStr
outputStr = outputStr[len(outputStr) - strLen:]
return outputStr
else:
return inputStr
# pad zero to the right of the string if not long enough
def rightPad(inputStr, strLen):
if (strLen > len(inputStr)):
outputStr = inputStr + "0000000000000000000000000000000000000000"
outputStr = outputStr[: strLen]
return outputStr
else:
return inputStr
#####################################################
# 将字符串转换为16进制的数字字符串
#####################################################
def str2Hex(s):
s_hex=""
for i in range(len(s)):
s_hex=s_hex+hex(ord(s[i]))[2:]+" "
return s_hex
#####################################################
# 将字符串转换为16进制的数字字符串,并去掉空格
#####################################################
def str2HexStrip(s):
s = str2Hex(s)
s2 = s.replace(" ","")
return s2
#####################################################
# 将字符串转换为对应的ascii值数组
#####################################################
def str2Ascsii(s):
asciiArr = []
for i in range(0,len(s)):
asciiValue = ord(s[i])
asciiArr.append(asciiValue)
return asciiArr
if __name__ == "__main__":
print(crc16(str2Hex("aa")))
print(str2Hex("aa"))
print(str2HexStrip("aa"))
print(str2Ascsii("aa"))
print(myCrc16("aa"))
print(crc16(str2HexStrip("aa")))
#coding:utf-8
'''
定义一个获取经纬度的辅助方法集合
'''
import json
#####################################################
# 获取文件的金纬度数据,返回一个经纬度数组
# path:传入文件路径
#####################################################
def getLocationInfo(path):
with open(path,"r",encoding='utf-8') as fi:
loc_data = fi.readlines()
strData = ""
for lineD in loc_data:
strData += lineD
json_data = json.loads(strData)
return json_data["locationInfo"]
if __name__ == "__main__":
print(getLocationInfo("../../data/protocolTools/GPSLine_1.json"))
\ No newline at end of file
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