Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
new-socketemulator
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李远洪
new-socketemulator
Commits
6556b53e
Commit
6556b53e
authored
Mar 27, 2020
by
liyuanhong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
M300 GPS协议开发完成
parent
b6c6afdc
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
151 additions
and
182 deletions
+151
-182
lib/protocol/m300/GPS_protocol_m300.py
lib/protocol/m300/GPS_protocol_m300.py
+89
-0
lib/protocol/m300/Heartbeat_protocol_300.py
lib/protocol/m300/Heartbeat_protocol_300.py
+40
-0
lib/protocol/m300/M300Base.py
lib/protocol/m300/M300Base.py
+13
-179
lib/protocol/report/ProtocolBase.py
lib/protocol/report/ProtocolBase.py
+1
-1
lib/socket/protocolTest_M300.py
lib/socket/protocolTest_M300.py
+6
-2
views/protocolTools/M_carSimulater_process.py
views/protocolTools/M_carSimulater_process.py
+2
-0
No files found.
lib/protocol/m300/GPS_protocol_m300.py
0 → 100644
View file @
6556b53e
#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
())
lib/protocol/m300/Heartbeat_protocol_300.py
0 → 100644
View file @
6556b53e
#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
())
lib/protocol/m300/M300Base.py
View file @
6556b53e
...
@@ -10,94 +10,20 @@ from lib.protocol.Base import Base
...
@@ -10,94 +10,20 @@ from lib.protocol.Base import Base
'''
'''
class
M300Base
(
Base
):
class
M300Base
(
Base
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
IDENTIFY
=
"7e"
#标识位
self
.
IDENTIFY
=
"7e"
#标识位
#######################################################
#获取消息属性
# 生成一条完整的消息
def
getMsgProperty
(
self
,
msgBodyLen
=
128
,
encryptionType
=
0
):
#######################################################
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
:
if
msgBodyLen
>=
512
:
raise
RuntimeError
(
'消息体长度超长!'
)
raise
RuntimeError
(
'消息体长度超长!'
)
msgBodyLen
=
msgBodyLen
#消息体长度
msgBodyLen
=
msgBodyLen
#消息体长度
encryptionType
=
encryptionType
#加密方式
encryptionType
=
encryptionType
#加密方式
subPkg
=
subPkg
#分包
retain
=
0
#保留位
retain
=
0
#保留位
data
=
msgBodyLen
+
encryptionType
+
subPkg
+
retain
data
=
msgBodyLen
+
encryptionType
+
retain
dataHex
=
self
.
int2hexStringByBytes
(
data
,
2
)
dataHex
=
self
.
int2hexStringByBytes
(
data
,
2
)
return
dataHex
return
dataHex
#获取消息体属性,针对图形界面,可传递参数
def
getMsgBodyProperty_GUI
(
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
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):
...
@@ -121,9 +47,6 @@ class M300Base(Base):
# 替换消息中的7e7d字符
# 替换消息中的7e7d字符
#######################################################
#######################################################
def
replace7e7d
(
self
,
data
):
def
replace7e7d
(
self
,
data
):
# data = data.replace("7d","7d01")
# data = data.replace("7e","7d02")
tmpR
=
data
tmpR
=
data
tmp
=
tmpR
[
0
:
2
]
tmp
=
tmpR
[
0
:
2
]
tmpA
=
tmpR
[
0
:
2
]
tmpA
=
tmpR
[
0
:
2
]
...
@@ -197,105 +120,16 @@ class M300Base(Base):
...
@@ -197,105 +120,16 @@ class M300Base(Base):
data
=
"0"
+
data
data
=
"0"
+
data
return
data
return
data
#######################################################
#####################################################
# 获取UTC时间转换位BCD格式
# 设备id转换为16进制的设备id
#######################################################
#####################################################
def
getBCDTime
(
self
,
data
=
"2020-02-04 18:57:04"
):
def
devid2hexString
(
self
,
id
):
now_time
=
datetime
.
datetime
.
now
()
.
strftime
(
'
%
Y-
%
m-
%
d
%
H:
%
M:
%
S'
)
# 获取第一个字符的ASCII值
data
=
data
ascii
=
ord
(
id
[
0
:
1
])
data
=
data
.
replace
(
"-"
,
""
)
# 将10进制的ASCII值转换为16进制
data
=
data
.
replace
(
" "
,
""
)
ascii
=
self
.
int2hexStringByBytes
(
ascii
)
data
=
data
.
replace
(
":"
,
""
)
devid
=
str
(
ascii
)
+
id
[
1
:]
data
=
data
[
2
:]
return
devid
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
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
lib/protocol/report/ProtocolBase.py
View file @
6556b53e
...
@@ -161,7 +161,7 @@ class ProtocolBase(Base):
...
@@ -161,7 +161,7 @@ class ProtocolBase(Base):
timeArr
=
[]
timeArr
=
[]
timeArr
.
append
(
timeStr
[
2
:
4
])
timeArr
.
append
(
timeStr
[
2
:
4
])
timeArr
.
append
(
timeStr
[
5
:
7
])
timeArr
.
append
(
timeStr
[
5
:
7
])
timeArr
.
append
(
timeStr
[
8
:
1
1
])
timeArr
.
append
(
timeStr
[
8
:
1
0
])
timeArr
.
append
(
timeStr
[
11
:
13
])
timeArr
.
append
(
timeStr
[
11
:
13
])
timeArr
.
append
(
timeStr
[
14
:
16
])
timeArr
.
append
(
timeStr
[
14
:
16
])
timeArr
.
append
(
timeStr
[
17
:
19
])
timeArr
.
append
(
timeStr
[
17
:
19
])
...
...
lib/socket/protocolTest_M300.py
View file @
6556b53e
#coding:utf-8
#coding:utf-8
import
binascii
import
binascii
import
socket
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.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
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
...
@@ -23,12 +26,13 @@ from lib.protocol.report.EventReport_protocol import EventReport_protocol
host
=
"10.100.5.251"
host
=
"10.100.5.251"
port
=
9009
port
=
9009
msg
=
"7e000400e14d2019120315000000957e"
#终端上报心跳协议
# msg = Heartbeat_protocol_300().generateMsg() #心跳报文
msg
=
GPS_protocol_m300
()
.
generateMsg
()
#GPS报文
# msg = "7e000400e14d2019120315000000957e" #终端上报心跳协议
print
(
msg
)
print
(
msg
)
BUF_SIZE
=
1024
BUF_SIZE
=
1024
client
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
client
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
client
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_KEEPALIVE
,
1
)
# 在客户端开启心跳
client
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_KEEPALIVE
,
1
)
# 在客户端开启心跳
client
.
connect
((
host
,
port
))
client
.
connect
((
host
,
port
))
...
...
views/protocolTools/M_carSimulater_process.py
View file @
6556b53e
#coding:utf-8
#coding:utf-8
from
time
import
sleep
from
flask
import
Blueprint
,
Response
,
request
from
flask
import
Blueprint
,
Response
,
request
from
configparser
import
ConfigParser
from
configparser
import
ConfigParser
...
@@ -98,6 +99,7 @@ def login():
...
@@ -98,6 +99,7 @@ def login():
params
[
"login"
][
"imsi"
],
params
[
"login"
][
"ccid"
],
params
[
"login"
][
"imei"
])
params
[
"login"
][
"imsi"
],
params
[
"login"
][
"ccid"
],
params
[
"login"
][
"imei"
])
loginMsg
=
loginObj
.
generateLoginMsg
()
loginMsg
=
loginObj
.
generateLoginMsg
()
service
.
serviceSendMsg
(
loginMsg
,
"登录"
)
service
.
serviceSendMsg
(
loginMsg
,
"登录"
)
sleep
(
0.2
)
versionObj
=
VersionReport_protocol
(
1
,
params
[
"WATER_CODE"
],
params
[
"carId"
],
params
[
"version"
][
"verInfo"
],
\
versionObj
=
VersionReport_protocol
(
1
,
params
[
"WATER_CODE"
],
params
[
"carId"
],
params
[
"version"
][
"verInfo"
],
\
params
[
"version"
][
"compileDate"
],
params
[
"version"
][
"GSM"
])
params
[
"version"
][
"compileDate"
],
params
[
"version"
][
"GSM"
])
versionMsg
=
versionObj
.
generateVersionMsg
()
versionMsg
=
versionObj
.
generateVersionMsg
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment