Commit 6556b53e authored by liyuanhong's avatar liyuanhong

M300 GPS协议开发完成

parent b6c6afdc
#encoding:utf-8
from lib.protocol.m300.M300Base import M300Base
'''
定义心跳议类
'''
class GPS_protocol_m300(M300Base):
def __init__(self,waterCode = "0003",DEV_ID = "M121501010001",dateInfo="2020-03-27 14:34:22",latitude=40.22077,longitude=116.23128 \
,positionStar=2,speed=66.0,direction=55.3,altitude=11.0,ACCStatus=0,valtage=36.0,OBDSpeed=66.4,valid=129 \
,tripMark=0):
super().__init__() # 不执行该方法,无法使用父类里面定义的属性
self.waterCode = waterCode #消息流水号
self.DEV_ID = DEV_ID #设备Id
self.dateInfo = dateInfo #日期
self.latitude = latitude #维度
self.longitude = longitude #经度
self.positionStar = positionStar #定位星数
self.speed = speed #速度
self.direction = direction #方向角
self.altitude = altitude #海拔高度
self.ACCStatus = ACCStatus #ACC状态
self.valtage = valtage #汽车电瓶电压
self.OBDSpeed = OBDSpeed #汽车OBD速度
self.valid = valid #GPS定位是否有效
self.tripMark = tripMark #驾驶循环标签
self.reserve = "0000000000" #保留字段 #设备Id
#################################################
# 生成消息
#################################################
def generateMsg(self):
msg = self.IDENTIFY
FUNID = "0020" #功能id
waterCode = self.waterCode #消息流水号
DEV_ID = self.devid2hexString(self.DEV_ID) #设备id
msgBody = self.getMsgBody() # 消息体
msgLen = int(len(msgBody) / 2)
property = self.getMsgProperty(msgBodyLen=msgLen,encryptionType=0)
checkCode = self.getCheckCode(FUNID + waterCode + DEV_ID + property + msgBody)
msg = msg + FUNID + waterCode + DEV_ID + property + msgBody + checkCode + self.IDENTIFY
return msg
#################################################
# 获取消息体
#################################################
def getMsgBody(self):
dateInfo = self.getDateInfo(self.dateInfo) #日期
latitude = self.int2hexStringByBytes(int(self.latitude * 1000000),4) #维度
longitude = self.int2hexStringByBytes(int(self.longitude * 1000000),4) #经度
positionStar = self.int2hexStringByBytes(self.positionStar) #定位星数
speed = self.int2hexStringByBytes(int(self.speed * 10),2) #速度
direction = self.int2hexStringByBytes(int(self.direction * 10),2) #方向角
altitude = self.int2hexStringByBytes(int(self.altitude * 10),4) #海拔高度
ACCStatus = self.int2hexStringByBytes(self.ACCStatus) #ACC状态
valtage = self.int2hexStringByBytes(int(self.valtage * 10),2) #汽车电瓶电压
OBDSpeed = self.int2hexStringByBytes(int(self.OBDSpeed * 10),2) #汽车OBD速度
valid = self.getValid() #GPS定位是否有效
tripMark = self.int2hexStringByBytes(self.tripMark,2) #驾驶循环标签
reserve = self.reserve #保留字段
data = dateInfo + latitude + longitude + positionStar + speed
data = data + direction + altitude + ACCStatus + valtage + OBDSpeed
data = data + valid + tripMark + reserve
return data
#获取时间信息
def getDateInfo(self,data):
year = self.int2hexStringByBytes(int(data[2:4]))
month = self.int2hexStringByBytes(int(data[5:7]))
day = self.int2hexStringByBytes(int(data[8:10]))
hour = self.int2hexStringByBytes(int(data[11:13]))
miniute = self.int2hexStringByBytes(int(data[14:16]))
seconds = self.int2hexStringByBytes(int(data[17:]))
dataHex = year + month + day + hour + miniute + seconds
return dataHex
def getValid(self):
isGPSValid = 1 #Gps是否当前定位有效数据 1/0 是/否
isFixMod = 0 #车机是否处于修车模式 1218/0 是/否
data = isGPSValid + isFixMod
dataHex = self.int2hexStringByBytes(data)
return dataHex
if __name__ == "__main__":
print(GPS_protocol_m300().generateMsg())
#encoding:utf-8
from lib.protocol.m300.M300Base import M300Base
'''
定义心跳议类
'''
class Heartbeat_protocol_300(M300Base):
def __init__(self,waterCode = "0003",DEV_ID = "M121501010001"):
super().__init__() # 不执行该方法,无法使用父类里面定义的属性
self.waterCode = waterCode #消息流水号
self.DEV_ID = DEV_ID #设备Id
#################################################
# 生成消息
#################################################
def generateMsg(self):
msg = self.IDENTIFY
FUNID = "0004" #功能id
waterCode = self.waterCode #消息流水号
DEV_ID = self.devid2hexString(self.DEV_ID) #设备id
msgBody = self.getMsgBody() # 消息体
msgLen = int(len(msgBody) / 2)
property = self.getMsgProperty(msgBodyLen=msgLen,encryptionType=0)
checkCode = self.getCheckCode(FUNID + waterCode + DEV_ID + property + msgBody)
msg = msg + FUNID + waterCode + DEV_ID + property + msgBody + checkCode + self.IDENTIFY
return msg
#################################################
# 获取消息体
#################################################
def getMsgBody(self):
data = ""
return data
if __name__ == "__main__":
print(Heartbeat_protocol_300().generateMsg())
......@@ -10,94 +10,20 @@ from lib.protocol.Base import Base
'''
class M300Base(Base):
def __init__(self):
self.IDENTIFY = "7e" #标识位
#######################################################
# 生成一条完整的消息
#######################################################
def generateMsg(self):
msg = ""
funId = 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 getMsgHeader(self):
msgID = self.int2hexStringByBytes(102,2) #消息id
subPkg = 0
msgBodyProperty = self.getMsgBodyProperty(subPkg=subPkg) #消息体属性
phoneNum = self.int2BCD(13146201118) #终端手机号
msgWaterCode = self.int2hexStringByBytes(1) #消息流水号
if subPkg != 8192:
subPkgContent = "" #消息包封装项
else:
subPkgContent = self.getMsgPackage()
data = msgID + msgBodyProperty + phoneNum + msgWaterCode + subPkgContent
return data
#获取消息头,针对图形界面,可传递参数
def getMsgHeader_GUI(self,msgID,phoneNum,msgWaterCode,encryptionType,subPkg): #消息id
msgID = msgID
subPkg = subPkg
msgBodyProperty = self.getMsgBodyProperty_GUI(msgBodyLen=int(len(self.getMsgBody()) / 2),encryptionType=encryptionType,subPkg=subPkg) #消息体属性
phoneNum = self.int2BCD(phoneNum) #终端手机号
msgWaterCode = self.int2hexStringByBytes(msgWaterCode,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
#获取消息体属性,针对图形界面,可传递参数
def getMsgBodyProperty_GUI(self,msgBodyLen=128,encryptionType=0,subPkg=0):
#获取消息属性
def getMsgProperty(self,msgBodyLen=128,encryptionType=0):
if msgBodyLen >= 512:
raise RuntimeError('消息体长度超长!')
msgBodyLen = msgBodyLen #消息体长度
encryptionType = encryptionType #加密方式
subPkg = subPkg #分包
retain = 0 #保留位
data = msgBodyLen + encryptionType + subPkg + retain
data = msgBodyLen + encryptionType + retain
dataHex = self.int2hexStringByBytes(data,2)
return dataHex
#获取消息封装项
def getMsgPackage(self):
pkgCounts = 2 #消息报包总数
pkgCountsHex = self.int2hexStringByBytes(2,2)
pkgNumsHex = ""
for i in range(0,pkgCounts):
pkgNum = i
dataHex = self.int2hexStringByBytes(pkgNum,2)
pkgNumsHex = pkgNumsHex + dataHex
msgPackage = pkgCountsHex + pkgNumsHex
return msgPackage
#######################################################
# 获取消息体
#######################################################
......@@ -121,9 +47,6 @@ class M300Base(Base):
# 替换消息中的7e7d字符
#######################################################
def replace7e7d(self,data):
# data = data.replace("7d","7d01")
# data = data.replace("7e","7d02")
tmpR = data
tmp = tmpR[0:2]
tmpA = tmpR[0:2]
......@@ -197,105 +120,16 @@ class M300Base(Base):
data = "0" + data
return data
#######################################################
# 获取UTC时间转换位BCD格式
#######################################################
def getBCDTime(self,data="2020-02-04 18:57:04"):
now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
data = data
data = data.replace("-","")
data = data.replace(" ","")
data = data.replace(":","")
data = data[2:]
data = self.int2BCD(int(data))
return data
#######################################################
# 字符串转换为GBK的16进制
#######################################################
def GBKString2Hex(self,data):
data = str(data.encode("gbk"))
dataHex = self.str2Hex(data[2:len(data) - 1])
return dataHex
#######################################################
# 16进制转换为GBK字符串
#######################################################
def hex2GBKString(self,dataHex):
dataStr = self.hex2Str(dataHex)
data = bytes(map(ord,str(dataStr)))
data = data.decode("gbk")
return data
#######################################################
# 获取一个随机数,也可以指定获取数组中的随机字符串
#######################################################
def getRandomNum(self,s=1,e=1,intArr=[],mult=0):
if intArr == []:
data = random.randint(s, e)
else:
if mult == 0:
if type(intArr[0]) == int:
data = int(random.choice(intArr))
elif type(intArr[0]) == str:
data = random.choice(intArr)
else:
if type(intArr[0]) == int:
if len(intArr) < mult:
raise RuntimeError('个数超过数组长度!')
temp = []
data = 0
for i in range(0,mult):
num = int(random.choice(intArr))
if num in temp:
# num = int(random.choice(intArr))
num = 0
temp.append(num)
data = data + num
elif type(intArr[0]) == str:
if len(intArr) < mult:
raise RuntimeError('个数超过数组长度!')
temp = []
data = ""
for i in range(0,mult):
num = random.choice(intArr)
if num in temp:
# num = int(random.choice(intArr))
num = "0"
temp.append(num)
data = data + num
return data
#######################################################
# 获取一个随机字符串
#######################################################
def getRandomStr(self,counts,strs=""):
if strs == "":
data = random.sample("0123456789abcdefghijkmlnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-", counts)
else:
data = []
for s in range(0,counts):
data.append(random.choice(strs))
temp = ""
for ch in data:
temp = temp +ch
return temp
#######################################################
# 获取随机时间
# type:0、获取年月日时分秒 1、获取年月日 2、获取时分秒
#######################################################
def getRandomDate(self,s=631123200,e=1577808000,type=0):
timeStamp = random.randint(s, e)
timeArray = time.localtime(timeStamp)
if type == 0:
theTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
elif type == 1:
theTime = time.strftime("%Y-%m-%d", timeArray)
elif type == 2:
theTime = time.strftime("%H:%M:%S", timeArray)
return theTime
#####################################################
# 设备id转换为16进制的设备id
#####################################################
def devid2hexString(self, id):
# 获取第一个字符的ASCII值
ascii = ord(id[0:1])
# 将10进制的ASCII值转换为16进制
ascii = self.int2hexStringByBytes(ascii)
devid = str(ascii) + id[1:]
return devid
if __name__ == "__main__":
......
......@@ -161,7 +161,7 @@ class ProtocolBase(Base):
timeArr = []
timeArr.append(timeStr[2:4])
timeArr.append(timeStr[5:7])
timeArr.append(timeStr[8:11])
timeArr.append(timeStr[8:10])
timeArr.append(timeStr[11:13])
timeArr.append(timeStr[14:16])
timeArr.append(timeStr[17:19])
......
#coding:utf-8
import binascii
import socket
from lib.protocol.m300.GPS_protocol_m300 import GPS_protocol_m300
from lib.protocol.m300.Heartbeat_protocol_300 import Heartbeat_protocol_300
from lib.protocol.report.GPSReport_protocol import GPSReport_protocol
from lib.protocol.report.OBDReport_CAN_protocol import OBDReport_CAN_protocol
from lib.protocol.report.OBDReport_CAN_protocol import OBDReport_CAN_protocol
......@@ -23,12 +26,13 @@ from lib.protocol.report.EventReport_protocol import EventReport_protocol
host = "10.100.5.251"
port = 9009
msg = "7e000400e14d2019120315000000957e" #终端上报心跳协议
# msg = Heartbeat_protocol_300().generateMsg() #心跳报文
msg = GPS_protocol_m300().generateMsg() #GPS报文
# msg = "7e000400e14d2019120315000000957e" #终端上报心跳协议
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))
......
#coding:utf-8
from time import sleep
from flask import Blueprint ,Response,request
from configparser import ConfigParser
......@@ -98,6 +99,7 @@ def login():
params["login"]["imsi"],params["login"]["ccid"],params["login"]["imei"])
loginMsg = loginObj.generateLoginMsg()
service.serviceSendMsg(loginMsg,"登录")
sleep(0.2)
versionObj = VersionReport_protocol(1,params["WATER_CODE"],params["carId"],params["version"]["verInfo"], \
params["version"]["compileDate"],params["version"]["GSM"])
versionMsg = versionObj.generateVersionMsg()
......
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