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

完成终端注册和车两安全状态开发

parent 49c66c3e
......@@ -6,6 +6,8 @@
import datetime
from lib.protocol.message.MessageBase import MessageBase
from lib.protocol.message.data.CarSafeStatusInfo import CarSafeStatusInfo
from lib.protocol.message.data.SaloonCarOBD_data import SaloonCarOBD_data
class Location_msg(MessageBase):
......@@ -60,12 +62,13 @@ class Location_msg(MessageBase):
#######################################################
def getLocationExtraInfo(self):
msg = ""
extraInfoId = self.int2hexStringByBytes(1)
# 里程,DWORD,1 / 10km,对应车上里程表读数;不支持OBD时,为基于GPS车速统计的车辆累计行驶总里程。
extra_01 = "01" + self.int2hexStringByBytes(4) + self.int2hexStringByBytes(20202020,4)
#油量,WORD,1/10L,对应车上油量表读数
extra_02 = "02" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(5200,2)
#超速报警附加信息
extra_11 = "11" + self.int2hexStringByBytes(len(self.getOverSpeedAlarmExtraInfo()) / 2) + self.getOverSpeedAlarmExtraInfo()
extra_11 = "11" + self.int2hexStringByBytes(int(len(self.getOverSpeedAlarmExtraInfo()) / 2)) + self.getOverSpeedAlarmExtraInfo()
#进出区域/路线报警附加信息见
extra_12 = "12" + self.int2hexStringByBytes(6) + self.getInOutAreaAlarmExtraInfo()
#路段行驶时间不足/过长报警附加信息见
......@@ -76,7 +79,25 @@ class Location_msg(MessageBase):
extra_30 = "30" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(33)
#BYTE,GNSS 定位卫星数
extra_31 = "31" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(2)
extra_EA = "EA"
#基础数据项列表
extra_EA = "EA" + self.int2hexStringByBytes(int(len(self.getBaseDataList()) / 2)) + self.getBaseDataList()
#轿车 OBD 数据流
extra_EB = "EB" + self.int2hexStringByBytes(int(len(SaloonCarOBD_data().generateSaloonCarOBDData()) / 2)) + SaloonCarOBD_data().generateSaloonCarOBDData()
#TODO 以下需要去实现
# #货车 OBD 数据流
# extra_EC = "EC"
# #新能源 OBD 数据流
# extra_ED = "ED"
# #外设数据项列表
# extra_EE = "EE"
# #报警事件 ID 数据项列表
# extra_FA = "FA"
data = extra_01 + extra_02 + extra_11 + extra_12 + extra_13
data = data + extra_2A + extra_30 + extra_31 + extra_EA
extraInfoLen = self.int2hexStringByBytes(int(len(data) / 2))
data = extraInfoId + extraInfoLen + data
return data
#获取超速报警附加信息
......@@ -138,9 +159,21 @@ class Location_msg(MessageBase):
dataId_0005 = "0005" + self.int2hexStringByBytes(4) + self.int2hexStringByBytes(360000,4)
dataId_0006 = "0006" + self.int2hexStringByBytes(4) + self.int2hexStringByBytes(72000,4)
dataId_0007 = "0007" + self.int2hexStringByBytes(4) + self.int2hexStringByBytes(480000,4)
dataId_0010 = "0010" + self.int2hexStringByBytes(len(self.getSpeedupInOneSeconds()) / 2) + self.getSpeedupInOneSeconds()
dataId_0011 = "0011" + ""
dataId_0010 = "0010" + self.int2hexStringByBytes(int(len(self.getSpeedupInOneSeconds()) / 2)) + self.getSpeedupInOneSeconds()
dataId_0011 = "0011" + self.int2hexStringByBytes(int(len(CarSafeStatusInfo().generateSecurityStatusData()) / 2)) + CarSafeStatusInfo().generateSecurityStatusData()
dataId_0012 = "0012" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(36,2)
dataId_0013 = "0013" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(6)
#TODO 由于被置灰,所以没有实现
# dataId_0015 = "0015" + self.int2hexStringByBytes(2)
# dataId_0016 = "0016"
#TODO 暂不支持
# dataId_0017 = "0017" + self.int2hexStringByBytes(2)
dataId_001D = "001D" + self.int2hexStringByBytes(1) + "01"
data = dataId_0001 + dataId_0002 + dataId_0003 + dataId_0004 + dataId_0005
data = data + dataId_0006 + dataId_0007 + dataId_0010 + dataId_0011 + dataId_0012
data = data + dataId_0013 + dataId_001D
return data
'''扩展状态标志位,见 表 C1EXT1'''
def getExpandStatusBit(self):
......@@ -197,10 +230,6 @@ class Location_msg(MessageBase):
for i in range(0,pointCount):
data = data + self.int2hexStringByBytes(speedupVal + 10,2)
return data
'''车辆安防状态信息,见 表 C1EXT4'''
def getCarSecurityStatusInfo(self):
data = ""
return data
......
......@@ -127,9 +127,11 @@ class MessageBase(Base):
if __name__ == "__main__":
# print(MessageBase().str2Hex("uvwxyz"))
# print(MessageBase().str2Ascsii("uvwxyz"))
# print(MessageBase().int2hexStringByBytes(22,2))
print(MessageBase().int2hexStringByBytes(220400566542345564784802,20))
print(MessageBase().str2Hex("a865h643gfdj64fd7432"))
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().getCheckCode("8001000501314620111800000000000200"))
# print(MessageBase().getMsgHeader())
# print(MessageBase().generateMsg())
\ No newline at end of file
#encoding:utf-8
'''
定义终端注册消息
'''
from lib.protocol.message.MessageBase import MessageBase
class TerminalRegister_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 = ""
#市县域 ID (标示终端安装车辆所在的省域,0 保留,由平台取默认值。省 域 ID 采用 GB/T 2260 中规定的行政区划代 码六位中前两)
provinceId = "0103"
#制造商 ID (5 个字节,终端制造商编码)
manufacturerId = self.str2Hex("11010")
#终端型号 (20 个字节,此终端型号由制造商自行定义,位数不足时,后 补“0X00”。)
terminalType = self.str2Hex("a865h643gfdj64fd7432")
#终端 ID (7 个字节,由大写字母和数字组成,此终端 ID 由制造商自行 定义,位数不足时,后补“0X00”)
terminalId = self.str2Hex("H6uyt08")
#车牌颜色 (车牌颜色,按照 JT/T415-2006 的 5.4.12。未上牌时,取值 为 0) 1:蓝色 2:黄色 3:黑色 4:白色 9:其他
licencePlateColor = self.int2hexStringByBytes(1)
#车辆标识 (车牌颜色为 0 时,表示车辆 VIN;否则,表示公安交通管理 部门颁发的机动车号牌)
carSign = self.str2Hex(str("渝B23CX".encode("gbk")))
msg = msg + provinceId + manufacturerId + terminalType + terminalId + licencePlateColor + carSign
return msg
#######################################################
# 获取消息头
#######################################################
def getMsgHeader(self):
# msgID = self.int2hexStringByBytes(102,2) #消息id
msgID = "0100"
msgBodyProperty = self.getMsgBodyProperty(len(self.getMsgBody())) #消息体属性
phoneNum = self.int2BCD(13146201119) #终端手机号
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(TerminalRegister_msg().generateMsg())
\ No newline at end of file
#encoding:utf-8
'''
定义安防状态信息
'''
from lib.protocol.message.MessageBase import MessageBase
class CarSafeStatusInfo(MessageBase):
def __init__(self):
super().__init__()
pass
#####################################################
# 创建安防状态数据
#####################################################
def generateSecurityStatusData(self):
data = ""
statusCode = "ffffffffffffffffffff" #状态掩码
securityStatus = self.getSecurityStatusHex() #安全状态
doorStatus = self.getDoorStatusHex() #门状态
lockStatus = self.getLockStatusHex() #锁状态
windowStatus = self.getWindowStatusHex() #窗户状态
lightStatus = self.getLightStatusHex() #灯光状态
onoffStatusA = self.getOnoffStatusAHex() #开关状态A
onoffStatusB = self.getOnoffStatusBHex() #开关状态B
retain1 = "00" #预留
retain2 = "00" #预留
retain3 = "00" # 预留
data = data + statusCode + securityStatus + doorStatus + lockStatus + windowStatus + lightStatus + onoffStatusA + onoffStatusB + retain1 + retain2 + retain3
return data
#####################################################
# 获取安全状态16进制数据
# 按照高位在前,低位在后的规则
#####################################################
def getSecurityStatusHex(self):
accStatus = 1 #acc状态,1:开 0:关
defenseStatus = 2 #设防撤防状态,2:设防 0:撤防
brakeStatus = 0 #脚刹状态,4:踩下 0:松开
acceleratorStatus = 8 #是否踩油门,8:踩下 0:松开
handBrakeStatus = 0 #手刹状态,16:拉起手刹 0:松开手刹
mainSafetyBelt = 32 #主驾驶安全带,32:插入安全带 0:松开安全带
subSafetyBelt = 64 #副驾驶安全带,64:插入安全带 0:松开安全带
retain = 0 #预留字段
val = accStatus +defenseStatus +brakeStatus +acceleratorStatus + handBrakeStatus + mainSafetyBelt + subSafetyBelt + retain
hexData = self.int2hexStringByBytes(val)
return hexData
#####################################################
# 获取门状态16进制数据
#####################################################
def getDoorStatusHex(self):
lfDoorStatus = 0 #左前门,1,:开 0:关
rfDoorStatus = 0 #右前门,2:开 0:关
lbDoorStatus = 0 #左后门,4:开 0:关
rbDoorStatus = 0 #右后门,8:开 0:关
trunk = 0 #后备箱,32:开 0:关
enginCover = 0 #发送机盖:64:开 0:关
retain1 = 0 #预留字段
retain2 = 0 #预留字段
val = lfDoorStatus + rfDoorStatus + lbDoorStatus +rbDoorStatus + trunk + enginCover + retain1 +retain2
hexData = self.int2hexStringByBytes(val)
return hexData
#####################################################
# 获取锁状态16进制数据
#####################################################
def getLockStatusHex(self):
lfDoorLockStatus = 0 #左前门锁状态,1:开 0:关
rfDoorLockStatus = 0 #右前门锁状态,2:开 0:关
lbDoorLockStatus = 0 #左后门锁状态,4:开 0:关
rbDoorLockStatus = 0 #有后门锁状态,8:开 0:关
retain1 = 0
retain2 = 0
retain3 = 0
retain4 = 0
val = lfDoorLockStatus + rfDoorLockStatus + lbDoorLockStatus + rbDoorLockStatus + retain1 +retain2 + retain3 +retain4
hexData = self.int2hexStringByBytes(val)
return hexData
#####################################################
# 获取窗户状态16进制数据
#####################################################
def getWindowStatusHex(self):
lfWindowStatus = 0 #左前窗,1:开 0:关
rfWindowStatus = 0 #右前窗,2:开 0:关
lbWindowStatus = 0 # 左后窗,4:开 0:关
rbWindowStatus = 0 # 右后窗,8:开 0:关
topWindowStatus = 0 #天窗开关,16:开 0:关
lTurnLight = 0 #左转向灯,32:开 0:关
rTurnLight = 0 #右转向灯,64:开 0:关
readLight = 0 #阅读灯,128:开 0:关
val = lfWindowStatus + rfWindowStatus + lbWindowStatus + rbWindowStatus + topWindowStatus + lTurnLight + rTurnLight + readLight
hexData = self.int2hexStringByBytes(val)
return hexData
#####################################################
# 获取灯光状态16进制数据
#####################################################
def getLightStatusHex(self):
lowHeadlight = 0 #近光灯,1:开 0:关
highHeadlight = 0 #远光灯,2:开 0:关
ffogLight = 0 #前雾灯,4:开 0:关
bfogLight = 0 #后雾灯,8:开 0:关
dangerLight = 0 #危险灯,16:开 0:关
backCarLight = 0 #倒车灯,32:开 0:关
autoLight = 0 #auto灯,64:开 0:关
widthLight = 0 #示宽灯,128:开 0:关
val = lowHeadlight + highHeadlight + ffogLight + bfogLight + dangerLight + backCarLight + autoLight + widthLight
hexData = self.int2hexStringByBytes(val)
return hexData
#####################################################
# 获取开关状态A16进制数据
#####################################################
def getOnoffStatusAHex(self):
machineOilWarning = 0 #机油报警,1:开 0:关
oilWarning = 0 #燃油报警,2:开 0:关
wiperWarning = 0 #雨刷报警,4:开 0:关
loudsspeakerWaring = 0 #喇叭报警,8:开 0:关
airConditionerWaring = 0 #空调,16:开 0:关
backMirrorWaring = 0 #后视镜状态:32开 0:关
retain1 = 0
retain2 = 0
val = machineOilWarning + oilWarning + wiperWarning + loudsspeakerWaring + airConditionerWaring + backMirrorWaring + retain1 + retain2
hexData = self.int2hexStringByBytes(val)
return hexData
#####################################################
# 获取开关状态B16进制数据
#####################################################
def getOnoffStatusBHex(self):
retain1 = 0
retain2 = 0
retain3 = 0
retain4 = 0
#档位,0:p 16:R 32:N 48:D 64:1挡 80:2挡 96:3挡 112:4挡 128:5挡 144:16挡 160:M挡 176:S挡
gears = 0
val = retain1 + retain2 + retain3 + retain4 + gears
hexData = self.int2hexStringByBytes(val)
return hexData
\ No newline at end of file
#encoding:utf-8
'''
定义轿车OBD数据
'''
from lib.protocol.message.MessageBase import MessageBase
class SaloonCarOBD_data(MessageBase):
def __init__(self):
super().__init__()
pass
#####################################################
# 创建轿车OBD数据
#####################################################
def generateSaloonCarOBDData(self):
data = ""
#发动机转速 , 0 - 8000 rpm
engineSpeed = "60C0" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(3000,2)
#车速 , 0 - 240 Km/h
carSpeed = "60D0" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(60)
#剩余油量 ,剩余油量,单位 L 或%Bit15=0 百分比%,OBD 都为百分比Bit15=1 单位 L,显示值为上传值/10
surplusOil = "62f0" + self.int2hexStringByBytes(2) + self.getSurplusOil()
#冷却液温度 ,-40.0℃ 到 +210℃,上传值减去 40
coolingLiquidTemperature = "6050" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(76)
#进气口温度 ,-40.0℃ 到 +210℃,上传值减去 40
airInletTemperature = "60F0" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(88)
#进气(岐管绝对)压力 , 0 - 250kpa
intakeManifoldPressure = "60B0" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(20)
#大气压力 , 0 - 125kpa
atmosphericPressure = "6330" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(28)
#环境温度 , -40.0℃ 到 +210℃,上传值减去 40
envTemperature = "6460" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(65)
#加速踏板位置 , 0% - 100%
acceleratorLocation = "6490" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(32)
#燃油压力 , 0 - 500kpa
oilPressure = "60A0" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(276,2)
#故障码状态 , 发动机故障码状态
troubleCodeStatus = "6014" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(0)
#故障码个数
troubleCodeNum = "6010" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(2)
#空气流量 , 0.1 实际值为上传值/10 0.1g/s
airFlow = "6100" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(550,2)
#绝对气门位置
valveLocation = "6110" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(501,2)
#自发动机启动运行时间 sec
engineRunTime = "61F0" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(3700,2)
#故障行驶里程 , Km
troubleMileage = "6210" + self.int2hexStringByBytes(4) + self.int2hexStringByBytes(4508,4)
#计算负荷值
calculateLoadValue = "6040" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(44)
#长期燃油修正(气缸列 1 和 3)
fuelTrim = "6070" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(56,2)
#第一缸点火正时提前角 ,显示值为上传值-64
fireAngle = "60E0" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(80)
#前刹车片磨损 , 0 正常/否则,显示对应数据,单位:级
frontBrakeBlockAbrasion = "6701" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(0)
#后刹车片磨损 , 0 正常/否则,显示对应数据,单位:级
backBrakeBlockAbrasion = "6702" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(0)
#制动液液位 , 0:不正 1:正常 其他:无法使用
brakeFluidLocation = "6703" + self.int2hexStringByBytes(1) + self.int2hexStringByBytes(1)
#机油液位 , 显示值为上传值/1000 单位 毫米
engineOilLocation = "6704" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(20000,2)
#胎压报警 0:当前无警告 1:存在胎压失压 其他:不可用
tirePressureAlarm = "6705" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(0,2)
#冷却液液位 , 显示值为上传值-48
coolingLiquidLocation = "6706" + self.int2hexStringByBytes(2) + self.int2hexStringByBytes(65,2)
#续航里程 0.1 km ; 显示值为上传值/10
enduranceMileage = "6707" + self.int2hexStringByBytes(4) + self.int2hexStringByBytes(380,4)
data = data + engineSpeed + carSpeed + surplusOil + coolingLiquidTemperature + airInletTemperature
data = data + intakeManifoldPressure + atmosphericPressure + envTemperature + acceleratorLocation + oilPressure
data = data + troubleCodeStatus + troubleCodeNum + airFlow + valveLocation + engineRunTime
data = data + troubleMileage + calculateLoadValue + fuelTrim + fireAngle + frontBrakeBlockAbrasion
data = data + backBrakeBlockAbrasion + brakeFluidLocation + engineOilLocation + tirePressureAlarm + coolingLiquidLocation
data = data + enduranceMileage
return data
#####################################################
# 获取剩余油量
#####################################################
def getSurplusOil(self):
# 剩余油量 ,剩余油量,单位 L 或%Bit15=0 百分比%,OBD 都为百分比Bit15=1 单位 L,显示值为上传值/10 (32768)
units = 32768
surplusOil = 800
data = units + surplusOil
dataHex = self.int2hexStringByBytes(data,2)
return dataHex
if __name__ == "__main__":
print(SaloonCarOBD_data().generateSaloonCarOBDData())
\ No newline at end of file
#encoding:utf-8
'''
定义终端注销消息
'''
from lib.protocol.message.MessageBase import MessageBase
class TerminalCancle_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 = "0003"
msgBodyProperty = self.getMsgBodyProperty(len(self.getMsgBody())) #消息体属性
phoneNum = self.int2BCD(13146201119) #终端手机号
msgWaterCode = self.int2hexStringByBytes(1,2) #消息流水号
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
\ No newline at end of file
#coding:utf-8
import binascii
import socket
from lib.protocol.message.Location_msg import Location_msg
from lib.protocol.message.MessageBase import MessageBase
from lib.protocol.message.TerminalHeartbeat_msg import TerminalHeartbeat_msg
from lib.protocol.message.TerminalRegister_msg import TerminalRegister_msg
from lib.protocol.message.data.TerminalCancle_msg import TerminalCancle_msg
from lib.protocol.messageRes.PlatformCommon_res import PlatformCommon_res
host = "10.100.9.17"
port = 9001
# msg = MessageBase().generateMsg()
msg = TerminalHeartbeat_msg().generateMsg() #终端心跳
# msg = TerminalHeartbeat_msg().generateMsg() #终端心跳
# msg = TerminalRegister_msg().generateMsg() #终端注册
msg = TerminalCancle_msg().generateMsg() #终端注销
# msg = Location_msg().generateMsg() #位置信息汇报
print(msg)
BUF_SIZE = 1024
......
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