Commit a6cf06dd authored by liyuanhong's avatar liyuanhong

增加了控制是否发送OBD消息的功能

parent 5f953db0
# coding: utf-8
import binascii
import socket
import time
import os
din = "M202003060520"
din = "M202008270021"
#####################################################
# 数字转换为16进制字符串
#####################################################
......@@ -81,123 +83,135 @@ def rightPad(inputStr, strLen):
##############################################################################
def getEventPkg():
with open("event.txt", "r", encoding="utf-8") as fi:
content = fi.readlines()
pkg = ""
msgs = []
pkgCounts = 0
for i in content:
onePkg = i[21:].replace("\n", "")
onePkg = onePkg[30:][:-4]
onePkg = onePkg[2:]
pkg = pkg + onePkg
pkgCounts = pkgCounts + 1
if len(pkg) > 2000:
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0021"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
pkg = ""
pkgCounts = 0
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0021"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
if os.path.exists('event.txt'):
with open("event.txt", "r", encoding="utf-8") as fi:
content = fi.readlines()
pkg = ""
msgs = []
pkgCounts = 0
for i in content:
onePkg = i[21:].replace("\n", "")
onePkg = onePkg[30:][:-4]
onePkg = onePkg[2:]
pkg = pkg + onePkg
pkgCounts = pkgCounts + 1
if len(pkg) > 2000:
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0021"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
pkg = ""
pkgCounts = 0
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0021"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
with open("event_big.txt", "w", encoding="utf-8") as fi2:
for txt in msgs:
fi2.write(txt + "\n")
return msgs
else:
print("event.txt is not exist")
return ["4040000b00034d1215010100010003f50b"]
def getGpsPkg():
with open("gps.txt", "r", encoding="utf-8") as fi:
content = fi.readlines()
pkg = ""
msgs = []
pkgCounts = 0
for i in content:
onePkg = i[21:].replace("\n", "")
onePkg = onePkg[30:][:-4]
onePkg = onePkg[2:]
pkg = pkg + onePkg
pkgCounts = pkgCounts + 1
if len(pkg) > 2000:
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0010"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
pkg = ""
pkgCounts = 0
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0010"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
if os.path.exists('gps.txt'):
with open("gps.txt", "r", encoding="utf-8") as fi:
content = fi.readlines()
pkg = ""
msgs = []
pkgCounts = 0
for i in content:
onePkg = i[21:].replace("\n", "")
onePkg = onePkg[30:][:-4]
onePkg = onePkg[2:]
pkg = pkg + onePkg
pkgCounts = pkgCounts + 1
if len(pkg) > 2000:
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0010"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
pkg = ""
pkgCounts = 0
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0010"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
with open("gps_big.txt", "w", encoding="utf-8") as fi2:
for txt in msgs:
fi2.write(txt + "\n")
return msgs
else:
print("gps.txt is not exist")
return ["4040000b00034d1215010100010003f50b"]
def getObdPkg():
with open("obd.txt", "r", encoding="utf-8") as fi:
content = fi.readlines()
pkg = ""
msgs = []
pkgCounts = 0
for i in content:
onePkg = i[21:].replace("\n", "")
onePkg = onePkg[30:][:-4]
onePkg = onePkg[2:]
print(onePkg)
pkg = pkg + onePkg
pkgCounts = pkgCounts + 1
if len(pkg) > 2000:
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0012"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
pkg = ""
pkgCounts = 0
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0012"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
with open("obd_big.txt", "w", encoding="utf-8") as fi2:
for txt in msgs:
fi2.write(txt + "\n")
return msgs
if os.path.exists('obd.txt'):
with open("obd.txt", "r", encoding="utf-8") as fi:
content = fi.readlines()
pkg = ""
msgs = []
pkgCounts = 0
for i in content:
onePkg = i[21:].replace("\n", "")
onePkg = onePkg[30:][:-4]
onePkg = onePkg[2:]
print(onePkg)
pkg = pkg + onePkg
pkgCounts = pkgCounts + 1
if len(pkg) > 2000:
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0012"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
pkg = ""
pkgCounts = 0
pkgCounts = int2hexStringByBytes(pkgCounts)
HEADER = "4040"
WATER_CODE = int2hexStringByBytes(1, 2)
DEV_ID = devid2hexString(din)
FUN_ID = "0012"
LENGTH = int2hexStringByBytes(int(len(WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg) / 2), 2)
CHECK_CODE = crc16(HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg)
msg = HEADER + LENGTH + WATER_CODE + DEV_ID + FUN_ID + pkgCounts + pkg + CHECK_CODE
msgs.append(msg)
with open("obd_big.txt", "w", encoding="utf-8") as fi2:
for txt in msgs:
fi2.write(txt + "\n")
return msgs
else:
print("obd.txt is not exist")
return ["4040000b00034d1215010100010003f50b"]
def getLenOfFile():
......@@ -207,8 +221,8 @@ def getLenOfFile():
def sendMsg(msgs):
host = "10.100.12.32"
port = 9008
host = "172.19.7.13"
port = 49008
BUF_SIZE = 1024
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
......@@ -228,8 +242,10 @@ def sendMsg(msgs):
if __name__ == "__main__":
msg = getEventPkg()
sendMsg(msg)
time.sleep(1)
msg = getGpsPkg()
sendMsg(msg)
time.sleep(1)
msg = getObdPkg()
sendMsg(msg)
......
......@@ -48,6 +48,7 @@ class ProtocolSimulaterService():
self.sendType = 0
self.GPSValid = 1 #用来控制GPS数据是有效还是无效 0:无效 1:有效
self.lngLatIsOk = 1 #用来控制经纬度是否都为0 0:都为0 1:正常
self.hasOBD = 1 #行驶数据是否包含obd数据 0:否 1:是
# 定义要发送的obd数据
self.OBDdata = {"fireStatus":1,"ACCStatus":0,"engineSpeed":3000,"speed":0,"meterMileage":6000,"totailMileage":600,"totalOilExpen":30,"totalRunTime":10,"surplusOil":505}
# 定义初始的obd数据,与上面的OBD数据保持一致,主要用于汽车行驶过程中数据变化量的计算
......@@ -97,6 +98,8 @@ class ProtocolSimulaterService():
self.data["other"]["valtage"] = data
def setFixPosition(self,data):
self.fixPosition = data
def setHasOBD(self,data):
self.hasOBD = data
def getWebsocket(self):
......@@ -213,11 +216,12 @@ class ProtocolSimulaterService():
self.carDataObj.setTotalTime(self.carData["travelData"]["totalTime"] + self.OBDdata["totalRunTime"] - self.OBDdataOri["totalRunTime"])
self.OBDdataOri["totalRunTime"] = self.OBDdata["totalRunTime"]
if OBDMsg != "":
self.sendMsg(OBDMsg)
self.sn = self.sn + 1
type = self.getMsgFunId(OBDMsg)
info = type + ">>>>:" + OBDMsg
self.websocket.sendMsgToClient(info,self.websocketId)
if self.hasOBD == 1:
self.sendMsg(OBDMsg)
self.sn = self.sn + 1
type = self.getMsgFunId(OBDMsg)
info = type + ">>>>:" + OBDMsg
self.websocket.sendMsgToClient(info,self.websocketId)
if gpsMsg != "":
sleep(0.1)
self.sendMsg(gpsMsg)
......
......@@ -202,6 +202,7 @@
<span style="margin-left:10px;display: inline-block;"><button type="button" class="btn btn-primary" onclick="changeTravelDirection()">改变行驶方向</button></span>
<span style="margin-left:10px;display: inline-block;"><label>电瓶电压(v) :</label><input style="width:80px;margin-right:5px;" id="change_voltage" type="text" class="form-control" value="12"><button type="button" class="btn btn-primary" onclick="changeVoltage()">改变电瓶电压</button></span>
<span style="margin-left:10px;display: inline-block;"><button title="点击后,车机将会在当前位置发送gps和obd数据" type="button" class="btn btn-primary" onclick="fixCurPosition(this)">固定在当前位置</button></span>
<span style="margin-left:10px;display: inline-block;"><button type="button" class="btn btn-primary" onclick="setOBDSend(this)">关闭OBD消息</button></span>
</div>
</div>
</div>
......@@ -1185,6 +1186,37 @@ function fixCurPosition(e){
$(e).text("固定在当前位置")
}
}
//控制是否发送OBD消息
function setOBDSend(e){
var butTxt = $(e).text()
if(butTxt == "关闭OBD消息"){
var data = {}
var carId = $("#carId").val()
data["carId"] = carId
data["hasOBD"] = 0
//会话session数据
data["session"] = {}
var sessionId = $("#curSession").val()
data["session"]["sessionId"] = sessionId
url = "/protocolTools/M_carSimulater_process/setOBDSend";
var host = window.location.host;
sendjson(data,url);
$(e).text("恢复OBD消息");
}else{
var data = {}
var carId = $("#carId").val()
data["carId"] = carId
data["hasOBD"] = 1
//会话session数据
data["session"] = {}
var sessionId = $("#curSession").val()
data["session"]["sessionId"] = sessionId
url = "/protocolTools/M_carSimulater_process/setOBDSend";
var host = window.location.host;
sendjson(data,url);
$(e).text("关闭OBD消息");
}
}
// -------------------------------- 其他操作js代码-------------------------------------------
//设置隐藏或显示其他操作区域
function isShowOtheroperCtrArea(e){
......
......@@ -1605,6 +1605,35 @@ def fixCurPosition():
data["message"] = "Error: 设置是否固定gps点失败!"
return Response(json.dumps(data), mimetype='application/json')
##########################################
# 【接口类型】行驶过程中是否发送OBD消息
##########################################
@M_carSimulater_process.route("/setOBDSend",methods=['POST'])
def setOBDSend():
params = request.get_data()
params = json.loads(params.decode("utf-8"))
sessionId = params["session"]["sessionId"]
hasOBD = int(params["hasOBD"])
data = {}
if not sessionId in connects.keys():
data["status"] = "4003"
data["message"] = "Error: 未启动服务,不可设置是否发送OBD消息!"
return Response(json.dumps(data), mimetype='application/json')
service = connects[sessionId]["service"]
try:
service.setHasOBD(hasOBD)
data["status"] = "200"
data["message"] = "设置是否发送OBD消息成功!"
except BaseException as e:
# 打印异常信息
traceback.print_exc()
data["status"] = "4003"
data["message"] = "Error: 设置是否发送OBD消息失败!"
return Response(json.dumps(data), mimetype='application/json')
#--------------------------------------- 其他操作逻辑 ---------------------------------------
##########################################
# 【接口类型】处理发送的故障码报文
......
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