Commit 32ef4e2f authored by liyuanhong's avatar liyuanhong

M500 和新车机模拟器,增加了方向角的功能

parent c2a364ff
{"time": {"dateTime": "2020-04-24 11:28:51", "date": "2020-04-24", "time": "11:28:51"}, "curDayTravel": {"todayTotalMilleage": 3675, "todayTotalOil": 434, "todayTotalTime": 277, "theMilleage": 640, "theOil": 40, "theTime": 40}, "travelData": {"totalMilleage": 3675, "totalOil": 434, "totalTime": 277}}
\ No newline at end of file
{"time": {"dateTime": "2020-04-26 16:03:38", "date": "2020-04-26", "time": "16:03:38"}, "curDayTravel": {"todayTotalMilleage": 4191, "todayTotalOil": 381, "todayTotalTime": 254, "theMilleage": 4191, "theOil": 381, "theTime": 254}, "travelData": {"totalMilleage": 7866, "totalOil": 815, "totalTime": 531}}
\ No newline at end of file
......@@ -223,7 +223,7 @@ class MessageBase(Base):
dataHex = ""
strLen = len(data)
for i in range(0,strLen):
if re.search("[0-9a-zA-Z]",data[i]) != None:
if re.search("[0-9a-zA-Z-_]",data[i]) != None:
dataHex = dataHex + self.str2Hex(data[i])
else:
temp = str(data[i].encode("gbk")).replace("\\x","")
......
......@@ -258,10 +258,11 @@ class GPSReport_protocol(ProtocolBase):
# 方向角2个字节表示
#####################################################
def getDirectionAngle(self,num):
angleStr = str(num)
angleStr = angleStr.replace(".", "") # 去掉经度小数点
angleHex = hex(int(angleStr))
angleHex = self.leftPad(str(angleHex)[2:], 4)
# angleStr = str(num)
# angleStr = angleStr.replace(".", "")
# angleHex = hex(int(angleStr))
# angleHex = self.leftPad(str(angleHex)[2:], 4)
angleHex = self.int2hexStringByBytes(int(num * 10),2)
return angleHex
#####################################################
......
......@@ -5,6 +5,7 @@
'''
import binascii
import json
import math
import os
import threading
import time
......@@ -34,6 +35,7 @@ class MessageSimulaterService():
self.carId = "" #车机号
self.sn = 0 #消息流水号
self.travelDirection = 0 #行驶方向,0表示正向行驶,1表示反向行驶
self.directAngle = 59 #定义默认方向角
# 定义要发送的obd数据
self.OBDdata = {"msgID": "0200", "phoneNum": "13146201119", "msgWaterCode": "1", "encryptionType": "0", "subPkg": "0",
"pkgCounts": "0", "baseInfo": {"alarmFlag": 0, "status": 262402, "latitude": 29.569133, "longtitude": 106.586571,
......@@ -172,6 +174,7 @@ class MessageSimulaterService():
fireOnParams["baseInfo"]["infoTime"] = curTime
fireOnParams["baseInfo"]["latitude"] = self.gpsLine[self.gpsLineIndex]["lat"]
fireOnParams["baseInfo"]["longtitude"] = self.gpsLine[self.gpsLineIndex]["lng"]
fireOnParams["baseInfo"]["directionAngle"] = self.getDirAngle()
msgObj = Location_msg()
msg = msgObj.generateMsg_GUI(fireOnParams)
self.sendMsg(msg)
......@@ -185,6 +188,7 @@ class MessageSimulaterService():
self.OBDdata["baseInfo"]["infoTime"] = curTime
self.OBDdata["baseInfo"]["latitude"] = self.gpsLine[self.gpsLineIndex]["lat"]
self.OBDdata["baseInfo"]["longtitude"] = self.gpsLine[self.gpsLineIndex]["lng"]
self.OBDdata["baseInfo"]["directionAngle"] = self.getDirAngle()
self.OBDdata["extraInfo"]["EB"]["60C0"] = 0 #发动机转速
self.OBDdata["extraInfo"]["EB"]["60D0"] = 0 #车速
self.OBDdata["extraInfo"]["EB"]["670a"] = self.carData["travelData"]["totalOil"] #总油耗
......@@ -222,6 +226,7 @@ class MessageSimulaterService():
gpsParams["baseInfo"]["infoTime"] = curTime
gpsParams["baseInfo"]["latitude"] = self.gpsLine[self.gpsLineIndex]["lat"]
gpsParams["baseInfo"]["longtitude"] = self.gpsLine[self.gpsLineIndex]["lng"]
gpsParams["baseInfo"]["directionAngle"] = self.getDirAngle()
gpsObj = Location_msg()
gpsMsg = gpsObj.generateMsg_GUI(gpsParams)
elif self.travelStatus == 1: #行驶服务启动
......@@ -242,6 +247,7 @@ class MessageSimulaterService():
gpsParams["baseInfo"]["infoTime"] = curTime
gpsParams["baseInfo"]["latitude"] = self.gpsLine[self.gpsLineIndex]["lat"]
gpsParams["baseInfo"]["longtitude"] = self.gpsLine[self.gpsLineIndex]["lng"]
gpsParams["baseInfo"]["directionAngle"] = self.getDirAngle()
gpsObj = Location_msg()
gpsMsg = gpsObj.generateMsg_GUI(gpsParams)
......@@ -250,6 +256,7 @@ class MessageSimulaterService():
self.OBDdata["baseInfo"]["infoTime"] = curTime
self.OBDdata["baseInfo"]["latitude"] = self.gpsLine[self.gpsLineIndex]["lat"]
self.OBDdata["baseInfo"]["longtitude"] = self.gpsLine[self.gpsLineIndex]["lng"]
self.OBDdata["baseInfo"]["directionAngle"] = self.getDirAngle()
self.OBDdata["extraInfo"]["EB"]["60C0"] = 3000 # 发动机转速
speed = int(self.data["travelData"]["carSpeed"])
oilExpend = int(self.data["travelData"]["oilExpend"])
......@@ -293,6 +300,7 @@ class MessageSimulaterService():
gpsParams["baseInfo"]["infoTime"] = curTime
gpsParams["baseInfo"]["latitude"] = self.gpsLine[self.gpsLineIndex]["lat"]
gpsParams["baseInfo"]["longtitude"] = self.gpsLine[self.gpsLineIndex]["lng"]
gpsParams["baseInfo"]["directionAngle"] = self.getDirAngle()
gpsObj = Location_msg()
gpsMsg = gpsObj.generateMsg_GUI(gpsParams)
......@@ -301,6 +309,7 @@ class MessageSimulaterService():
self.OBDdata["baseInfo"]["infoTime"] = curTime
self.OBDdata["baseInfo"]["latitude"] = self.gpsLine[self.gpsLineIndex]["lat"]
self.OBDdata["baseInfo"]["longtitude"] = self.gpsLine[self.gpsLineIndex]["lng"]
self.OBDdata["baseInfo"]["directionAngle"] = self.getDirAngle()
self.OBDdata["extraInfo"]["EB"]["60C0"] = 3000 # 发动机转速
speed = int(self.data["travelData"]["carSpeed"])
oilExpend = int(self.data["travelData"]["oilExpend"])
......@@ -360,6 +369,7 @@ class MessageSimulaterService():
fireOffParams["baseInfo"]["infoTime"] = curTime
fireOffParams["baseInfo"]["latitude"] = self.gpsLine[gpsLineIndex]["lat"]
fireOffParams["baseInfo"]["longtitude"] = self.gpsLine[gpsLineIndex]["lng"]
fireOffParams["baseInfo"]["directionAngle"] = self.getDirAngle()
msgObj = Location_msg()
msg = msgObj.generateMsg_GUI(fireOffParams)
self.sendMsg(msg)
......@@ -501,3 +511,50 @@ class MessageSimulaterService():
def stopTravel(self):
self.travelStatus = 0
self.serviceStatus = 0
###########################################################
#获取方向角
###########################################################
def getDirAngle(self):
dire = self.directAngle
if self.travelDirection == 0:
if self.gpsLineIndex == 0:
return self.directAngle
lngCut = (float(self.gpsLine[self.gpsLineIndex]["lng"]) - float(self.gpsLine[self.gpsLineIndex - 1]["lng"])) * 1000000
latCut = (float(self.gpsLine[self.gpsLineIndex]["lat"]) - float(self.gpsLine[self.gpsLineIndex - 1]["lat"])) * 1000000
if latCut == 0: #除数维度不能为0
latCut = 1
if lngCut == 0 or latCut == 0:
return self.directAngle
val = lngCut / latCut
dire = math.atan2(1, val) * 180 / math.pi
if lngCut > 0 and latCut > 0:
dire = 90 - dire
if lngCut < 0 and latCut > 0:
dire = 270 + 180 - dire
elif latCut < 0 and lngCut > 0:
dire = 270 - dire
elif lngCut < 0 and latCut < 0:
dire = 180 + 90 - dire
self.directAngle = dire
elif self.travelDirection == 1:
if self.gpsLineIndex == (len(self.gpsLine) - 1):
return self.directAngle
lngCut = (float(self.gpsLine[self.gpsLineIndex]["lng"]) - float(self.gpsLine[self.gpsLineIndex + 1]["lng"])) * 1000000
latCut = (float(self.gpsLine[self.gpsLineIndex]["lat"]) - float(self.gpsLine[self.gpsLineIndex + 1]["lat"])) * 1000000
if latCut == 0: #除数维度不能为0
latCut = 1
if lngCut == 0 or latCut == 0:
return self.directAngle
val = lngCut / latCut
dire = math.atan2(1, val) * 180 / math.pi
if lngCut > 0 and latCut > 0:
dire = 90 - dire
if lngCut < 0 and latCut > 0:
dire = 270 + 180 - dire
elif latCut < 0 and lngCut > 0:
dire = 270 - dire
elif lngCut < 0 and latCut < 0:
dire = 180 + 90 - dire
self.directAngle = dire
return int(dire)
......@@ -5,6 +5,7 @@ M500车机模拟服务类
'''
import binascii
import json
import math
import os
import threading
import time
......@@ -36,6 +37,7 @@ class ProtocolSimulaterService():
self.carId = "" #车机号
self.sn = 0 #消息流水号
self.travelDirection = 0 #行驶方向,0表示正向行驶,1表示反向行驶
self.directAngle = 60 #汽车方向角
# 定义要发送的obd数据
self.OBDdata = {"fireStatus":1,"ACCStatus":0,"engineSpeed":300,"speed":0,"meterMileage":6000,"totailMileage":600,"totalOilExpen":30,"totalRunTime":10}
# 定义初始的obd数据,与上面的OBD数据保持一致,主要用于汽车行驶过程中数据变化量的计算
......@@ -338,10 +340,11 @@ class ProtocolSimulaterService():
self.websocket.send(type + ">>>>:" + gpsMsg)
#根据特定参数,生成GPS消息
def genGPSMsg(self,latitude,longtitude):
def genGPSMsg(self,latitude,longtitude,direct=0):
gpsObj = GPSReport_protocol(DEV_ID=self.carId,WATER_CODE=self.sn)
gpsObj.setLatitude(latitude)
gpsObj.setLongitude(longtitude)
gpsObj.setDirectionAngle(self.getDirAngle())
timeS = int(time.time()) - 8 * 3600
timeArray = time.localtime(timeS)
UTCTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
......@@ -381,8 +384,70 @@ class ProtocolSimulaterService():
msg = OBDObj.generateOBDReportCANMsg()
return msg
###########################################################
#获取方向角
###########################################################
def getDirAngle(self):
dire = self.directAngle
if self.travelDirection == 0:
if self.gpsLineIndex == 0:
return self.directAngle
lngCut = (float(self.gpsLine[self.gpsLineIndex]["lng"]) - float(self.gpsLine[self.gpsLineIndex - 1]["lng"])) * 1000000
latCut = (float(self.gpsLine[self.gpsLineIndex]["lat"]) - float(self.gpsLine[self.gpsLineIndex - 1]["lat"])) * 1000000
if latCut == 0: #除数维度不能为0
latCut = 1
if lngCut == 0 or latCut == 0:
return self.directAngle
val = lngCut / latCut
dire = math.atan2(1, val) * 180 / math.pi
if lngCut > 0 and latCut > 0:
dire = 90 - dire
if lngCut < 0 and latCut > 0:
dire = 270 + 180 - dire
elif latCut < 0 and lngCut > 0:
dire = 270 - dire
elif lngCut < 0 and latCut < 0:
dire = 180 + 90 - dire
self.directAngle = dire
elif self.travelDirection == 1:
if self.gpsLineIndex == (len(self.gpsLine) - 1):
return self.directAngle
lngCut = (float(self.gpsLine[self.gpsLineIndex]["lng"]) - float(self.gpsLine[self.gpsLineIndex + 1]["lng"])) * 1000000
latCut = (float(self.gpsLine[self.gpsLineIndex]["lat"]) - float(self.gpsLine[self.gpsLineIndex + 1]["lat"])) * 1000000
if latCut == 0: #除数维度不能为0
latCut = 1
if lngCut == 0 or latCut == 0:
return self.directAngle
val = lngCut / latCut
dire = math.atan2(1, val) * 180 / math.pi
if lngCut > 0 and latCut > 0:
dire = 90 - dire
if lngCut < 0 and latCut > 0:
dire = 270 + 180 - dire
elif latCut < 0 and lngCut > 0:
dire = 270 - dire
elif lngCut < 0 and latCut < 0:
dire = 180 + 90 - dire
self.directAngle = dire
return int(dire)
#测试方向角转换的函数(测试用)
def getDirAngleTest(self):
lngCut = (116.410665 - 116.423888) * 1000000
latCut = (39.929267 - 39.930484) * 1000000
val = lngCut / latCut
dire = math.atan2(1, val) * 180 / math.pi
if lngCut < 0 and latCut > 0:
pass
dire = 270 + 180 - dire
elif latCut < 0 and lngCut > 0:
dire = 270 - dire
elif lngCut < 0 and latCut < 0:
dire = 180 + 90 - dire
if __name__ == "__main__":
ProtocolSimulaterService().getDirAngleTest()
......
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