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
af00f223
Commit
af00f223
authored
Apr 27, 2020
by
liyuanhong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加了模拟器多人同时在线使用的功能
parent
ad176d1f
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
493 additions
and
107 deletions
+493
-107
data/messageTools/carData/012201500010.json
data/messageTools/carData/012201500010.json
+1
-1
lib/socket/service/MessageSimulaterService.py
lib/socket/service/MessageSimulaterService.py
+25
-12
lib/socket/service/ProtocolSimulaterService.py
lib/socket/service/ProtocolSimulaterService.py
+29
-15
lib/socket/service/websocket_service.py
lib/socket/service/websocket_service.py
+12
-0
templates/messageTools/message/M_simulater_page.html
templates/messageTools/message/M_simulater_page.html
+134
-7
templates/protocolTools/report/M_carSimulater_page.html
templates/protocolTools/report/M_carSimulater_page.html
+148
-19
test.py
test.py
+6
-2
views/messageTools/M_simulater_process.py
views/messageTools/M_simulater_process.py
+67
-26
views/protocolTools/M_carSimulater_process.py
views/protocolTools/M_carSimulater_process.py
+71
-25
No files found.
data/messageTools/carData/012201500010.json
View file @
af00f223
{
"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
{
"time"
:
{
"dateTime"
:
"2020-04-27 17:44:26"
,
"date"
:
"2020-04-27"
,
"time"
:
"17:44:26"
},
"curDayTravel"
:
{
"todayTotalMilleage"
:
4101
,
"todayTotalOil"
:
276
,
"todayTotalTime"
:
255
,
"theMilleage"
:
1888
,
"theOil"
:
118
,
"theTime"
:
118
},
"travelData"
:
{
"totalMilleage"
:
4101
,
"totalOil"
:
276
,
"totalTime"
:
255
}}
\ No newline at end of file
lib/socket/service/MessageSimulaterService.py
View file @
af00f223
...
...
@@ -28,6 +28,7 @@ class MessageSimulaterService():
self
.
sendDur
=
5
#设置默认多久发一条消息
self
.
serviceStatus
=
0
#服务状态,0表示未启动,1表示启动
self
.
websocket
=
None
#网页与服务的通信socket
self
.
websocketId
=
""
# 当前连接的webSocketId
self
.
timeout
=
1
#socket的超时时间
self
.
gpsLine
=
[]
#GPS 轨迹
self
.
gpsLineIndex
=
0
#GPS 轨迹索引
...
...
@@ -68,8 +69,13 @@ class MessageSimulaterService():
self
.
carId
=
data
def
setData
(
self
,
data
):
self
.
data
=
data
def
setWebsocketId
(
self
):
sleep
(
1
)
self
.
websocketId
=
self
.
websocket
.
getCurrentClientId
()
def
getWebsocket
(
self
):
return
self
.
websocket
def
setWebsocket
(
self
,
data
):
self
.
websocket
=
data
def
sendMsg
(
self
,
msg
):
...
...
@@ -82,7 +88,7 @@ class MessageSimulaterService():
def
serviceSendMsg
(
self
,
msg
):
self
.
sendMsg
(
msg
)
type
=
self
.
getMsgFunId
(
msg
)
self
.
websocket
.
send
(
">>>>"
+
type
+
":"
+
msg
)
self
.
websocket
.
send
MsgToClient
(
">>>>"
+
type
+
":"
+
msg
,
self
.
websocketId
)
########################################################
#车机登录
...
...
@@ -95,7 +101,7 @@ class MessageSimulaterService():
self
.
sendMsg
(
msg
)
type
=
self
.
getMsgFunId
(
msg
)
info
=
type
+
">>>>:"
+
msg
self
.
websocket
.
send
(
info
)
self
.
websocket
.
send
MsgToClient
(
info
,
self
.
websocketId
)
self
.
sn
=
self
.
sn
+
1
verObj
=
TerminalVersionInfo_msg
()
time
.
sleep
(
0.5
)
...
...
@@ -108,7 +114,7 @@ class MessageSimulaterService():
self
.
sendMsg
(
msg
)
type
=
self
.
getMsgFunId
(
msg
)
info
=
type
+
">>>>:"
+
msg
self
.
websocket
.
send
(
info
)
self
.
websocket
.
send
MsgToClient
(
info
,
self
.
websocketId
)
self
.
sn
=
self
.
sn
+
1
########################################################
...
...
@@ -180,7 +186,7 @@ class MessageSimulaterService():
self
.
sendMsg
(
msg
)
type
=
self
.
getMsgFunId
(
msg
)
info
=
type
+
">>>>:"
+
msg
self
.
websocket
.
send
(
info
)
self
.
websocket
.
send
MsgToClient
(
info
,
self
.
websocketId
)
self
.
sn
=
self
.
sn
+
1
time
.
sleep
(
0.1
)
self
.
OBDdata
[
"phoneNum"
]
=
self
.
data
[
"phoneNum"
]
...
...
@@ -198,7 +204,7 @@ class MessageSimulaterService():
self
.
sendMsg
(
obdMsg
)
type
=
self
.
getMsgFunId
(
obdMsg
)
info
=
type
+
">>>>:"
+
obdMsg
self
.
websocket
.
send
(
info
)
self
.
websocket
.
send
MsgToClient
(
info
,
self
.
websocketId
)
self
.
sn
=
self
.
sn
+
1
########################################################
...
...
@@ -276,16 +282,16 @@ class MessageSimulaterService():
elif
self
.
gpsLineIndex
==
len
(
self
.
gpsLine
)
or
self
.
gpsLineIndex
==
-
1
:
#如果反向行驶和反向行驶刚好跑完
if
int
(
self
.
data
[
"travelData"
][
"travelLoop"
])
==
0
:
#没有设置循环行驶
self
.
stopTravel
()
self
.
websocket
.
send
(
"gps轨迹跑完,自动停止行驶!"
)
self
.
websocket
.
send
MsgToClient
(
"gps轨迹跑完,自动停止行驶!"
,
self
.
websocketId
)
else
:
#设置了循环行驶
if
self
.
travelDirection
==
0
:
self
.
gpsLineIndex
=
self
.
gpsLineIndex
-
1
self
.
travelDirection
=
1
self
.
websocket
.
send
(
"gps轨迹正向行驶跑完,变换行驶方向......"
)
self
.
websocket
.
send
MsgToClient
(
"gps轨迹正向行驶跑完,变换行驶方向......"
,
self
.
websocketId
)
else
:
self
.
gpsLineIndex
=
self
.
gpsLineIndex
+
1
self
.
travelDirection
=
0
self
.
websocket
.
send
(
"gps轨迹反向行驶跑完,变换行驶方向......"
)
self
.
websocket
.
send
MsgToClient
(
"gps轨迹反向行驶跑完,变换行驶方向......"
,
self
.
websocketId
)
gpsParams
=
{
"msgID"
:
"0200"
,
"phoneNum"
:
"13146201119"
,
"msgWaterCode"
:
"1"
,
"encryptionType"
:
"0"
,
"subPkg"
:
"0"
,
"pkgCounts"
:
"0"
,
"baseInfo"
:
{
"alarmFlag"
:
0
,
"status"
:
262402
,
"latitude"
:
29.569133
,
...
...
@@ -340,14 +346,14 @@ class MessageSimulaterService():
self
.
sn
=
self
.
sn
+
1
type
=
self
.
getMsgFunId
(
obdMsg
)
info
=
type
+
">>>>:"
+
obdMsg
self
.
websocket
.
send
(
info
)
self
.
websocket
.
send
MsgToClient
(
info
,
self
.
websocketId
)
if
gpsMsg
!=
""
:
time
.
sleep
(
0.1
)
self
.
sendMsg
(
gpsMsg
)
self
.
sn
=
self
.
sn
+
1
type
=
self
.
getMsgFunId
(
gpsMsg
)
info
=
type
+
">>>>:"
+
gpsMsg
self
.
websocket
.
send
(
info
)
self
.
websocket
.
send
MsgToClient
(
info
,
self
.
websocketId
)
sleep
(
self
.
sendDur
)
########################################################
...
...
@@ -375,7 +381,7 @@ class MessageSimulaterService():
self
.
sendMsg
(
msg
)
type
=
self
.
getMsgFunId
(
msg
)
info
=
type
+
">>>>:"
+
msg
self
.
websocket
.
send
(
info
)
self
.
websocket
.
send
MsgToClient
(
info
,
self
.
websocketId
)
self
.
sn
=
self
.
sn
+
1
########################################################
...
...
@@ -402,7 +408,7 @@ class MessageSimulaterService():
type
=
self
.
getMsgFunId
(
d
)
info
=
type
+
"<<<<:"
+
d
time
.
sleep
(
0.5
)
self
.
websocket
.
send
(
info
)
self
.
websocket
.
send
MsgToClient
(
info
,
self
.
websocketId
)
self
.
doResponse
(
d
)
########################################################
...
...
@@ -412,6 +418,13 @@ class MessageSimulaterService():
if
self
.
websocket
==
None
:
t
=
threading
.
Thread
(
target
=
self
.
websocketService
,
args
=
())
t
.
start
()
t3
=
threading
.
Thread
(
target
=
self
.
setWebsocketId
,
args
=
())
t3
.
start
()
# 为websocket服务添加一个新的客户端连接
def
addNewWebsocket
(
self
):
t3
=
threading
.
Thread
(
target
=
self
.
setWebsocketId
,
args
=
())
t3
.
start
()
########################################################
#停止websocket服务
...
...
lib/socket/service/ProtocolSimulaterService.py
View file @
af00f223
...
...
@@ -30,6 +30,7 @@ class ProtocolSimulaterService():
self
.
sendDur
=
5
#设置默认多久发一条消息
self
.
serviceStatus
=
0
#服务状态,0表示未启动,1表示启动
self
.
websocket
=
None
#网页与服务的通信socket
self
.
websocketId
=
""
#当前连接的webSocketId
self
.
timeout
=
1
#socket的超时时间
self
.
gpsLine
=
[]
#GPS 轨迹
self
.
gpsLineIndex
=
0
#GPS 轨迹索引
...
...
@@ -54,8 +55,13 @@ class ProtocolSimulaterService():
self
.
carId
=
data
def
setData
(
self
,
data
):
self
.
data
=
data
def
setWebsocketId
(
self
):
sleep
(
1
)
self
.
websocketId
=
self
.
websocket
.
getCurrentClientId
()
def
getWebsocket
(
self
):
return
self
.
websocket
def
setWebsocket
(
self
,
data
):
self
.
websocket
=
data
def
sendMsg
(
self
,
msg
):
...
...
@@ -68,7 +74,7 @@ class ProtocolSimulaterService():
def
serviceSendMsg
(
self
,
msg
,
type
):
#type字段目前废掉没有实际意义
self
.
sendMsg
(
msg
)
type
=
self
.
getMsgFunId
(
msg
)
self
.
websocket
.
send
(
">>>>"
+
type
+
":"
+
msg
)
self
.
websocket
.
send
MsgToClient
(
">>>>"
+
type
+
":"
+
msg
,
self
.
websocketId
)
def
serviceSend
(
self
):
while
self
.
serviceStatus
==
1
:
...
...
@@ -100,16 +106,16 @@ class ProtocolSimulaterService():
elif
self
.
gpsLineIndex
==
len
(
self
.
gpsLine
)
or
self
.
gpsLineIndex
==
-
1
:
#如果反向行驶和反向行驶刚好跑完
if
int
(
self
.
data
[
"travelData"
][
"travelLoop"
])
==
0
:
#没有设置循环行驶
self
.
stopTravel
()
self
.
websocket
.
send
(
"gps轨迹跑完,自动停止行驶!"
)
self
.
websocket
.
send
MsgToClient
(
"gps轨迹跑完,自动停止行驶!"
,
self
.
websocketId
)
else
:
#设置了循环行驶
if
self
.
travelDirection
==
0
:
self
.
gpsLineIndex
=
self
.
gpsLineIndex
-
1
self
.
travelDirection
=
1
self
.
websocket
.
send
(
"gps轨迹正向行驶跑完,变换行驶方向......"
)
self
.
websocket
.
send
MsgToClient
(
"gps轨迹正向行驶跑完,变换行驶方向......"
,
self
.
websocketId
)
else
:
self
.
gpsLineIndex
=
self
.
gpsLineIndex
+
1
self
.
travelDirection
=
0
self
.
websocket
.
send
(
"gps轨迹反向行驶跑完,变换行驶方向......"
)
self
.
websocket
.
send
MsgToClient
(
"gps轨迹反向行驶跑完,变换行驶方向......"
,
self
.
websocketId
)
OBDMsg
=
self
.
genOBDMsg
(
self
.
OBDdata
[
"fireStatus"
],
self
.
OBDdata
[
"ACCStatus"
],
self
.
OBDdata
[
"engineSpeed"
],
\
self
.
OBDdata
[
"speed"
],
self
.
OBDdata
[
"meterMileage"
],
self
.
OBDdata
[
"totailMileage"
],
\
self
.
OBDdata
[
"totalOilExpen"
],
self
.
OBDdata
[
"totalRunTime"
])
...
...
@@ -141,14 +147,14 @@ class ProtocolSimulaterService():
self
.
sn
=
self
.
sn
+
1
type
=
self
.
getMsgFunId
(
OBDMsg
)
info
=
type
+
">>>>:"
+
OBDMsg
self
.
websocket
.
send
(
info
)
self
.
websocket
.
send
MsgToClient
(
info
,
self
.
websocketId
)
if
gpsMsg
!=
""
:
sleep
(
0.1
)
self
.
sendMsg
(
gpsMsg
)
self
.
sn
=
self
.
sn
+
1
type
=
self
.
getMsgFunId
(
gpsMsg
)
info
=
type
+
">>>>:"
+
gpsMsg
self
.
websocket
.
send
(
info
)
self
.
websocket
.
send
MsgToClient
(
info
,
self
.
websocketId
)
sleep
(
self
.
sendDur
)
def
serviceRev
(
self
):
...
...
@@ -159,7 +165,7 @@ class ProtocolSimulaterService():
d
=
str
(
binascii
.
b2a_hex
(
d
))[
2
:][:
-
1
]
type
=
self
.
getMsgFunId
(
d
)
info
=
type
+
"<<<<:"
+
d
self
.
websocket
.
send
(
info
)
self
.
websocket
.
send
MsgToClient
(
info
,
self
.
websocketId
)
self
.
doResponse
(
d
)
#启动与页面交互的websockt服务
...
...
@@ -182,6 +188,14 @@ class ProtocolSimulaterService():
t
.
start
()
t2
=
threading
.
Thread
(
target
=
self
.
serviceRev
,
args
=
())
t2
.
start
()
t3
=
threading
.
Thread
(
target
=
self
.
setWebsocketId
,
args
=
())
t3
.
start
()
# 为websocket服务添加一个新的客户端连接
def
addNewWebsocket
(
self
):
t3
=
threading
.
Thread
(
target
=
self
.
setWebsocketId
,
args
=
())
t3
.
start
()
#停止定时发送消息的服务
def
stopService
(
self
):
self
.
serviceStatus
=
0
...
...
@@ -229,19 +243,19 @@ class ProtocolSimulaterService():
self
.
sendMsg
(
msg
)
self
.
sn
=
self
.
sn
+
1
type
=
self
.
getMsgFunId
(
msg
)
self
.
websocket
.
send
(
type
+
">>>>设置GPSR通信参数应答:"
+
msg
)
self
.
websocket
.
send
MsgToClient
(
type
+
">>>>设置GPSR通信参数应答:"
+
msg
,
self
.
websocketId
)
elif
msgFunId
==
"8206"
:
msg
=
Common_response
(
resId
=
"8206"
)
.
generateCommonMsg
()
self
.
sendMsg
(
msg
)
self
.
sn
=
self
.
sn
+
1
type
=
self
.
getMsgFunId
(
msg
)
self
.
websocket
.
send
(
type
+
">>>>设置车辆OBD适配信息应答:"
+
msg
)
self
.
websocket
.
send
MsgToClient
(
type
+
">>>>设置车辆OBD适配信息应答:"
+
msg
,
self
.
websocketId
)
elif
msgFunId
==
"8300"
:
msg
=
Update_response
()
.
generateUpdateMsg
()
self
.
sendMsg
(
msg
)
self
.
sn
=
self
.
sn
+
1
type
=
self
.
getMsgFunId
(
msg
)
self
.
websocket
.
send
(
type
+
">>>>升级_平台通知终端远程升级应答:"
+
msg
)
self
.
websocket
.
send
MsgToClient
(
type
+
">>>>升级_平台通知终端远程升级应答:"
+
msg
,
self
.
websocketId
)
#设置GPS轨迹
def
setGpsLine
(
self
,
fileName
):
...
...
@@ -303,13 +317,13 @@ class ProtocolSimulaterService():
type
=
self
.
getMsgFunId
(
firOnEventMsg
)
self
.
sendMsg
(
firOnEventMsg
)
self
.
sn
=
self
.
sn
+
1
self
.
websocket
.
send
(
type
+
">>>>:"
+
firOnEventMsg
)
self
.
websocket
.
send
MsgToClient
(
type
+
">>>>:"
+
firOnEventMsg
,
self
.
websocketId
)
sleep
(
0.1
)
gpsMsg
=
self
.
genGPSMsg
(
self
.
gpsLine
[
0
][
"lat"
],
self
.
gpsLine
[
0
][
"lng"
])
type
=
self
.
getMsgFunId
(
gpsMsg
)
self
.
sendMsg
(
gpsMsg
)
self
.
sn
=
self
.
sn
+
1
self
.
websocket
.
send
(
type
+
">>>>:"
+
gpsMsg
)
self
.
websocket
.
send
MsgToClient
(
type
+
">>>>:"
+
gpsMsg
,
self
.
websocketId
)
sleep
(
0.1
)
OBDMsg
=
self
.
genOBDMsg
(
self
.
OBDdata
[
"fireStatus"
],
1
,
self
.
OBDdata
[
"engineSpeed"
],
\
self
.
OBDdata
[
"speed"
],
self
.
OBDdata
[
"meterMileage"
],
self
.
OBDdata
[
"totailMileage"
],
\
...
...
@@ -317,7 +331,7 @@ class ProtocolSimulaterService():
type
=
self
.
getMsgFunId
(
OBDMsg
)
self
.
sendMsg
(
OBDMsg
)
self
.
sn
=
self
.
sn
+
1
self
.
websocket
.
send
(
type
+
">>>>:"
+
OBDMsg
)
self
.
websocket
.
send
MsgToClient
(
type
+
">>>>:"
+
OBDMsg
,
self
.
websocketId
)
# 熄火,发送熄火事件
def
fireOff
(
self
):
...
...
@@ -331,13 +345,13 @@ class ProtocolSimulaterService():
fireOffEventMsg
=
fireOffEventObj
.
generateEventMsg
()
type
=
self
.
getMsgFunId
(
fireOffEventMsg
)
self
.
sendMsg
(
fireOffEventMsg
)
self
.
websocket
.
send
(
type
+
">>>>:"
+
fireOffEventMsg
)
self
.
websocket
.
send
MsgToClient
(
type
+
">>>>:"
+
fireOffEventMsg
,
self
.
websocketId
)
sleep
(
0.1
)
gpsMsg
=
self
.
genGPSMsg
(
self
.
gpsLine
[
gpsLineIndex
][
"lat"
],
self
.
gpsLine
[
gpsLineIndex
][
"lng"
])
type
=
self
.
getMsgFunId
(
gpsMsg
)
self
.
sendMsg
(
gpsMsg
)
self
.
sn
=
self
.
sn
+
1
self
.
websocket
.
send
(
type
+
">>>>:"
+
gpsMsg
)
self
.
websocket
.
send
MsgToClient
(
type
+
">>>>:"
+
gpsMsg
,
self
.
websocketId
)
#根据特定参数,生成GPS消息
def
genGPSMsg
(
self
,
latitude
,
longtitude
,
direct
=
0
):
...
...
lib/socket/service/websocket_service.py
View file @
af00f223
...
...
@@ -21,6 +21,9 @@ class Websocket_service(SocketBase):
def
setPort
(
self
,
data
):
self
.
port
=
data
def
getCurrentClientId
(
self
):
return
self
.
currentClient
def
startWebsocketServer
(
self
):
server
=
WebsocketServer
(
self
.
port
,
host
=
self
.
host
,
loglevel
=
logging
.
INFO
)
server
.
set_fn_new_client
(
self
.
new_client
)
...
...
@@ -85,6 +88,15 @@ class Websocket_service(SocketBase):
data
=
json
.
dumps
(
data
)
self
.
server
.
send_message
(
self
.
clients
[
self
.
currentClient
],
data
)
#给指定客户端发送消息
def
sendMsgToClient
(
self
,
msg
,
clientId
):
data
=
{}
data
[
"code"
]
=
"0002"
data
[
"client"
]
=
self
.
currentClient
data
[
"msg"
]
=
"收到消息:"
+
msg
data
=
json
.
dumps
(
data
)
self
.
server
.
send_message
(
self
.
clients
[
clientId
],
data
)
if
__name__
==
"__main__"
:
w
=
Websocket_service
()
...
...
templates/messageTools/message/M_simulater_page.html
View file @
af00f223
...
...
@@ -43,6 +43,12 @@
<label>
上报间隔:
</label><input
id=
"durTime"
type=
"text"
class=
"form-control"
value=
"5"
style=
"width:80px;"
/>
<label>
设置超时时间:
</label><input
id=
"timeout"
type=
"text"
class=
"form-control"
value=
"3600"
style=
"width:80px;"
/>
</div>
<div
style=
"display: block; width: 100%; border-width: 1px; border-style: solid; border-color: darkgray; border-radius: 10px; padding: 2px; margin-top: 5px;"
>
<h5><b>
多人在线状态面板:
</b></h5>
<span><label>
我的id:
</label><input
disabled=
disabled
type=
"text"
id=
"curSession"
class=
"form-control"
style=
"display:inline;width:200px;"
value=
""
/></span>
<span><label>
在线人数:
</label><input
disabled=
disabled
type=
"text"
id=
"totalSessionNums"
class=
"form-control"
style=
"display:inline;width:80px;"
value=
"1"
/></span>
<button
id=
"sessionRefresh"
type=
"button"
class=
"btn btn-primary"
onclick=
"getAllUsers(this)"
>
刷新
</button><label
style=
"font-size:10px;width:100px;"
>
刷新后,可在控制台查看在线用户详细信息
</label>
</div>
<div
style=
"width:100%;padding-bottom:10px;border-bottom: 1px solid #eee;"
>
<h4>
点击登录按钮发送报文:
</h4>
<div
style=
"display: block; width: 100%; border-width: 1px; border-style: solid; border-color: darkgray; border-radius: 10px; padding: 2px; margin-top: 5px;"
>
...
...
@@ -142,9 +148,33 @@ function connect(){
var
data
=
getPageData
();
var
host
=
window
.
location
.
host
;
var
url
=
"
http://
"
+
host
+
"
/messageTools/M_simulater_process/porcessConnect
"
;
sendHttpMsg
(
data
,
url
);
startWebSocket
();
$
(
"
#curStatus
"
).
val
(
"
连网
"
);
$
.
ajax
({
url
:
url
,
type
:
"
post
"
,
data
:
JSON
.
stringify
(
data
),
contentType
:
"
application/json
"
,
dataType
:
"
json
"
,
success
:
function
(
data
){
if
(
data
.
status
==
200
){
//window.location.reload()
var
curTime
=
getCurTime
();
var
txt
=
$
(
"
#showFeedback
"
).
val
()
txt
=
txt
+
"
[
"
+
curTime
+
"
]
"
+
data
.
message
+
"
\n
"
$
(
"
#showFeedback
"
).
val
(
txt
)
startWebSocket
();
$
(
"
#curStatus
"
).
val
(
"
连网
"
);
var
userNums
=
parseInt
(
$
(
"
#totalSessionNums
"
).
val
())
+
1
$
(
"
#totalSessionNums
"
).
val
(
userNums
)
}
else
{
var
curTime
=
getCurTime
();
var
txt
=
$
(
"
#showFeedback
"
).
val
()
txt
=
txt
+
"
[
"
+
curTime
+
"
]
"
+
data
.
message
+
"
\n
"
$
(
"
#showFeedback
"
).
val
(
txt
)
alert
(
data
.
message
);
}
}
});
}
//登录
function
carLogin
(){
...
...
@@ -191,9 +221,34 @@ function disConnect(){
var
data
=
getPageData
();
var
host
=
window
.
location
.
host
;
var
url
=
"
http://
"
+
host
+
"
/messageTools/M_simulater_process/porcessDisconnect
"
;
sendHttpMsg
(
data
,
url
);
$
(
"
#curStatus
"
).
val
(
"
断网
"
);
endService
()
$
.
ajax
({
url
:
url
,
type
:
"
post
"
,
data
:
JSON
.
stringify
(
data
),
contentType
:
"
application/json
"
,
dataType
:
"
json
"
,
success
:
function
(
data
){
if
(
data
.
status
==
200
){
//window.location.reload()
var
curTime
=
getCurTime
();
var
txt
=
$
(
"
#showFeedback
"
).
val
()
txt
=
txt
+
"
[
"
+
curTime
+
"
]
"
+
data
.
message
+
"
\n
"
$
(
"
#showFeedback
"
).
val
(
txt
)
$
(
"
#curStatus
"
).
val
(
"
断网
"
);
//endService()
var
userNums
=
parseInt
(
$
(
"
#totalSessionNums
"
).
val
())
-
1
$
(
"
#totalSessionNums
"
).
val
(
userNums
)
ws
.
close
();
}
else
{
var
curTime
=
getCurTime
();
var
txt
=
$
(
"
#showFeedback
"
).
val
()
txt
=
txt
+
"
[
"
+
curTime
+
"
]
"
+
data
.
message
+
"
\n
"
$
(
"
#showFeedback
"
).
val
(
txt
)
alert
(
data
.
message
);
}
}
});
}
//复位
function
reset
(){
...
...
@@ -203,7 +258,8 @@ function reset(){
sendHttpMsg
(
data
,
url
);
$
(
"
#curStatus
"
).
val
(
"
断网
"
);
$
(
"
#showFeedback
"
).
val
(
""
)
endService
()
//endService()
ws
.
close
();
}
//清空日志
function
clearlog
(){
...
...
@@ -264,6 +320,10 @@ function getPageData(){
//GPS轨迹数据
var
gpsLine
=
$
(
"
#selectGPSLine
"
).
val
()
data
[
"
gpsLine
"
]
=
gpsLine
//会话session数据
data
[
"
session
"
]
=
{}
var
sessionId
=
$
(
"
#curSession
"
).
val
()
data
[
"
session
"
][
"
sessionId
"
]
=
sessionId
//登录数据
var
provinceId
=
$
(
"
#provinceId
"
).
val
();
var
countyId
=
$
(
"
#countyId
"
).
val
();
...
...
@@ -467,5 +527,72 @@ function isLogShow(e){
showLog
=
true
;
}
}
//获取随机session id
function
getSessionId
(){
var
sessionNum
=
parseInt
(
Math
.
random
()
*
100000
);
if
(
sessionNum
<
100000
){
sessionNum
=
sessionNum
+
100000
;
}
var
sessionStr
=
""
var
strAll
=
"
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRETUVWXYZ
"
;
var
strAllLen
=
strAll
.
length
;
for
(
var
i
=
0
;
i
<
8
;
i
++
){
var
wh
=
parseInt
(
Math
.
random
()
*
100
);
if
(
wh
>=
strAllLen
){
wh
=
wh
-
strAllLen
}
sessionStr
=
sessionStr
+
strAll
.
charAt
(
wh
)
}
session
=
sessionStr
+
"
_
"
+
sessionNum
return
session
}
//设置车机session Id
(
function
setSessionId
(){
var
sessionId
=
getSessionId
()
$
(
"
#curSession
"
).
val
(
sessionId
)
})()
//获取所有的使用用户
function
getAllUsers
(){
var
data
=
{};
var
host
=
window
.
location
.
host
;
$
.
ajax
({
url
:
"
http://
"
+
host
+
"
/messageTools/M_simulater_process/getConnects
"
,
type
:
"
post
"
,
data
:
data
,
dataType
:
"
json
"
,
success
:
function
(
data
){
if
(
data
.
status
==
200
){
//window.location.reload()
obj
=
data
.
result
console
.
log
(
obj
)
$
(
"
#totalSessionNums
"
).
val
(
data
.
userCounts
)
}
else
{
$
(
"
#searchCardataShow
"
).
val
(
data
.
message
)
alert
(
data
.
message
);
}
}
});
}
//获取所有的使用用户
(
function
(){
var
data
=
{};
var
host
=
window
.
location
.
host
;
$
.
ajax
({
url
:
"
http://
"
+
host
+
"
/messageTools/M_simulater_process/getConnects
"
,
type
:
"
post
"
,
data
:
data
,
dataType
:
"
json
"
,
success
:
function
(
data
){
if
(
data
.
status
==
200
){
//window.location.reload()
obj
=
data
.
result
$
(
"
#totalSessionNums
"
).
val
(
data
.
userCounts
)
}
else
{
}
}
});
})()
</script>
{% endblock %}
\ No newline at end of file
templates/protocolTools/report/M_carSimulater_page.html
View file @
af00f223
...
...
@@ -44,6 +44,12 @@
<span><label>
上报间隔(秒):
</label><input
id=
"durTime"
style=
"width:60px;"
type=
"text"
class=
"form-control"
value=
"5"
></span>
<span><label>
设置超时时间:
</label><input
style=
"width:80px;"
id=
"timeout"
type=
"text"
class=
"form-control"
value=
"36000"
></span>
</div>
<div
style=
"display: block; width: 100%; border-width: 1px; border-style: solid; border-color: darkgray; border-radius: 10px; padding: 2px; margin-top: 5px;"
>
<h5><b>
多人在线状态面板:
</b></h5>
<span><label>
我的id:
</label><input
disabled=
disabled
type=
"text"
id=
"curSession"
class=
"form-control"
style=
"display:inline;width:200px;"
value=
""
/></span>
<span><label>
在线人数:
</label><input
disabled=
disabled
type=
"text"
id=
"totalSessionNums"
class=
"form-control"
style=
"display:inline;width:80px;"
value=
"1"
/></span>
<button
id=
"sessionRefresh"
type=
"button"
class=
"btn btn-primary"
onclick=
"getAllUsers(this)"
>
刷新
</button><label
style=
"font-size:10px;width:100px;"
>
刷新后,可在控制台查看在线用户详细信息
</label>
</div>
<div
style=
"width:100%;padding-bottom:10px;border-bottom: 1px solid #eee;"
>
<h4>
点击登录按钮发送报文:
</h4>
<div
style=
"display: block; width: 100%; border-width: 1px; border-style: solid; border-color: darkgray; border-radius: 10px; padding: 2px; margin-top: 5px;"
>
...
...
@@ -116,7 +122,7 @@
<div
style=
"width:100%;padding-bottom:10px;border-bottom: 1px solid #eee;"
>
<h4>
查询今日车机行驶数据
</h4>
<span>
<button
id=
"searchCarData"
type=
"button"
class=
"btn btn-primary"
id=
"saveSetting"
>
查询
</button>
<button
id=
"searchCarData"
type=
"button"
class=
"btn btn-primary"
>
查询
</button>
</span>
</div>
<H3
style=
"border-bottom: 1px solid #eee;"
>
返回信息:
</H3>
...
...
@@ -136,6 +142,10 @@ function getPageData(){
data
[
"
WATER_CODE
"
]
=
WATER_CODE
data
[
"
durTime
"
]
=
durTime
data
[
"
timeout
"
]
=
timeout
//会话session数据
data
[
"
session
"
]
=
{}
var
sessionId
=
$
(
"
#curSession
"
).
val
()
data
[
"
session
"
][
"
sessionId
"
]
=
sessionId
//登录数据
data
[
"
login
"
]
=
{}
var
cpuId
=
$
(
"
#cpuId
"
).
val
()
...
...
@@ -171,12 +181,34 @@ function getPageData(){
function
connect
(){
var
data
=
getPageData
()
url
=
"
/protocolTools/M_carSimulater_process/createConect
"
;
sendjson
(
data
,
url
);
//setTimeout(function(){
// WebSocketTest()
//},1000); //1秒之后启动websocket连接
WebSocketTest
()
$
(
"
#curStatus
"
).
val
(
"
连网
"
)
var
host
=
window
.
location
.
host
;
$
.
ajax
({
//url:"http://" + host + "/protocolTools/M_carSimulater_process/porcessSocketSetting",
url
:
"
http://
"
+
host
+
url
,
type
:
"
post
"
,
data
:
JSON
.
stringify
(
data
),
contentType
:
"
application/json
"
,
dataType
:
"
json
"
,
success
:
function
(
data
){
if
(
data
.
status
==
200
){
//window.location.reload()
var
curTime
=
getCurTime
();
var
txt
=
$
(
"
#showFeedback
"
).
val
()
txt
=
txt
+
"
[
"
+
curTime
+
"
]
"
+
data
.
message
+
"
\n
"
$
(
"
#showFeedback
"
).
val
(
txt
)
WebSocketTest
()
$
(
"
#curStatus
"
).
val
(
"
连网
"
)
var
userNums
=
parseInt
(
$
(
"
#totalSessionNums
"
).
val
())
+
1
$
(
"
#totalSessionNums
"
).
val
(
userNums
)
}
else
{
var
curTime
=
getCurTime
();
var
txt
=
$
(
"
#showFeedback
"
).
val
()
txt
=
txt
+
"
[
"
+
curTime
+
"
]
"
+
data
.
message
+
"
\n
"
$
(
"
#showFeedback
"
).
val
(
txt
)
alert
(
data
.
message
);
}
}
});
}
//登录
function
login
(){
...
...
@@ -201,9 +233,9 @@ function startTravel(){
}
//停止行驶
function
stopTravel
(){
var
data
=
{}
;
var
data
=
getPageData
()
;
url
=
"
/protocolTools/M_carSimulater_process/stopTravel
"
;
send
(
data
,
url
);
send
json
(
data
,
url
);
$
(
"
#curStatus
"
).
val
(
"
点火
"
)
}
//熄火
...
...
@@ -215,20 +247,50 @@ function unFire(){
}
//断开连接
function
stopConnect
(){
var
data
=
{}
;
var
data
=
getPageData
()
;
url
=
"
/protocolTools/M_carSimulater_process/closeConect
"
;
send
(
data
,
url
);
endService
()
$
(
"
#curStatus
"
).
val
(
"
断网
"
)
var
host
=
window
.
location
.
host
;
$
.
ajax
({
//url:"http://" + host + "/protocolTools/M_carSimulater_process/porcessSocketSetting",
url
:
"
http://
"
+
host
+
url
,
type
:
"
post
"
,
data
:
JSON
.
stringify
(
data
),
contentType
:
"
application/json
"
,
dataType
:
"
json
"
,
success
:
function
(
data
){
if
(
data
.
status
==
200
){
var
curTime
=
getCurTime
();
var
txt
=
$
(
"
#showFeedback
"
).
val
()
txt
=
txt
+
"
[
"
+
curTime
+
"
]
"
+
data
.
message
+
"
\n
"
$
(
"
#showFeedback
"
).
val
(
txt
)
//endService()
$
(
"
#curStatus
"
).
val
(
"
断网
"
)
var
userNums
=
parseInt
(
$
(
"
#totalSessionNums
"
).
val
())
-
1
$
(
"
#totalSessionNums
"
).
val
(
userNums
)
ws
.
close
();
}
else
{
var
curTime
=
getCurTime
();
var
txt
=
$
(
"
#showFeedback
"
).
val
()
txt
=
txt
+
"
[
"
+
curTime
+
"
]
"
+
data
.
message
+
"
\n
"
$
(
"
#showFeedback
"
).
val
(
txt
)
alert
(
data
.
message
);
}
}
});
}
//复位
function
reset
(){
var
data
=
{};
url
=
"
/protocolTools/M_carSimulater_process/reset
"
;
send
(
data
,
url
);
$
(
"
#showFeedback
"
).
val
(
""
)
endService
()
$
(
"
#curStatus
"
).
val
(
"
断网
"
)
var
res
;
res
=
confirm
(
"
复位后会影响其他在线用户的使用,你确定复位吗?
"
);
if
(
res
==
true
){
var
data
=
getPageData
();
url
=
"
/protocolTools/M_carSimulater_process/reset
"
;
sendjson
(
data
,
url
);
$
(
"
#showFeedback
"
).
val
(
""
)
//endService()
$
(
"
#curStatus
"
).
val
(
"
断网
"
)
ws
.
close
();
}
}
//清空日志
function
clearlog
(){
...
...
@@ -436,6 +498,73 @@ function isLogShow(e){
showLog
=
true
;
}
}
//获取水机session id
function
getSessionId
(){
var
sessionNum
=
parseInt
(
Math
.
random
()
*
100000
);
if
(
sessionNum
<
100000
){
sessionNum
=
sessionNum
+
100000
;
}
var
sessionStr
=
""
var
strAll
=
"
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRETUVWXYZ
"
;
var
strAllLen
=
strAll
.
length
;
for
(
var
i
=
0
;
i
<
8
;
i
++
){
var
wh
=
parseInt
(
Math
.
random
()
*
100
);
if
(
wh
>=
strAllLen
){
wh
=
wh
-
strAllLen
}
sessionStr
=
sessionStr
+
strAll
.
charAt
(
wh
)
}
session
=
sessionStr
+
"
_
"
+
sessionNum
return
session
}
//设置车机session Id
(
function
setSessionId
(){
var
sessionId
=
getSessionId
()
$
(
"
#curSession
"
).
val
(
sessionId
)
})()
//获取所有的使用用户
function
getAllUsers
(){
var
data
=
{};
var
host
=
window
.
location
.
host
;
$
.
ajax
({
url
:
"
http://
"
+
host
+
"
/protocolTools/M_carSimulater_process/getConnects
"
,
type
:
"
post
"
,
data
:
data
,
dataType
:
"
json
"
,
success
:
function
(
data
){
if
(
data
.
status
==
200
){
//window.location.reload()
obj
=
data
.
result
console
.
log
(
obj
)
$
(
"
#totalSessionNums
"
).
val
(
data
.
userCounts
)
}
else
{
$
(
"
#searchCardataShow
"
).
val
(
data
.
message
)
alert
(
data
.
message
);
}
}
});
}
//获取所有的使用用户
(
function
(){
var
data
=
{};
var
host
=
window
.
location
.
host
;
$
.
ajax
({
url
:
"
http://
"
+
host
+
"
/protocolTools/M_carSimulater_process/getConnects
"
,
type
:
"
post
"
,
data
:
data
,
dataType
:
"
json
"
,
success
:
function
(
data
){
if
(
data
.
status
==
200
){
//window.location.reload()
obj
=
data
.
result
$
(
"
#totalSessionNums
"
).
val
(
data
.
userCounts
)
}
else
{
}
}
});
})()
</script>
{% endblock %}
</div>
...
...
test.py
View file @
af00f223
#coding: utf-8
import
json
import
logging
from
configparser
import
ConfigParser
...
...
@@ -23,10 +24,13 @@ def startWebsocketServer():
def
mysend
(
client
,
server
,
msg
):
print
(
msg
)
msgObj
=
json
.
loads
(
msg
)
msgtem
=
msgObj
[
"msg"
]
server
.
send_message
(
client
,
msg
)
if
(
msg
==
"_end"
):
#如果收到了_end 消息,那么断开连接
if
(
msg
tem
==
"_end"
):
#如果收到了_end 消息,那么断开连接
print
(
"服务端断开链接..."
)
server
.
shutdown
()
# server.shutdown()
server
.
server_close
()
server
=
WebsocketServer
(
5005
,
host
=
'127.0.0.1'
,
loglevel
=
logging
.
INFO
)
server
.
set_fn_new_client
(
new_client
)
...
...
views/messageTools/M_simulater_process.py
View file @
af00f223
...
...
@@ -13,7 +13,8 @@ from lib.util import fileUtil
M_simulater_process
=
Blueprint
(
'M_simulater_process'
,
__name__
)
connects
=
[]
#用来保存连接的信息
connects
=
{}
#用来保存连接的信息s
websocket
=
None
#保存创建的websocket
##########################################
# 【接口类型】设置socket信息
...
...
@@ -54,18 +55,26 @@ def porcessSocketSetting():
def
porcessConnect
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
try
:
conf_R
=
ConfigParser
()
conf_R
.
read
(
"config/messageTools/carSimulater.conf"
)
if
len
(
connects
)
<
1
:
if
not
sessionId
in
connects
.
keys
()
:
cliSocket
=
ClientSocket
(
conf_R
.
get
(
"socket"
,
"host"
),
conf_R
.
getint
(
"socket"
,
"port"
))
cliSocket
.
connect
()
connect
=
{}
connects
[
sessionId
]
=
connect
socketName
=
"socket_"
+
str
(
len
(
connects
)
+
1
)
connect
[
"name"
]
=
socketName
connects
.
append
(
connect
)
service
=
MessageSimulaterService
()
global
websocket
if
websocket
==
None
:
service
.
startWebsocketService
()
# 如果没有创建websocket服务,则启动新的websocket服务
websocket
=
service
.
getWebsocket
()
else
:
service
.
setWebsocket
(
websocket
)
# 给新服务设置websocket服务对象
service
.
addNewWebsocket
()
# 创建一个新的websocket连接
service
.
setSocket
(
cliSocket
)
service
.
setTimeout
(
int
(
params
[
"timeout"
]))
service
.
setCarId
(
params
[
"phoneNum"
])
...
...
@@ -74,7 +83,8 @@ def porcessConnect():
service
.
startWebsocketService
()
#启动websocket服务
service
.
startReciveService
()
#接收消息的服务
service
.
setGpsLine
(
params
[
"gpsLine"
])
connects
[
0
][
"service"
]
=
service
connect
[
"service"
]
=
connect
connects
[
sessionId
][
"service"
]
=
service
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"创建连接成功!"
else
:
...
...
@@ -94,9 +104,10 @@ def porcessConnect():
def
porcessLogin
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
try
:
connects
[
0
][
"service"
]
.
carLogin
()
connects
[
sessionId
][
"service"
]
.
carLogin
()
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"登录成功!"
except
BaseException
as
e
:
...
...
@@ -113,9 +124,10 @@ def porcessLogin():
def
porcessFireOn
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
try
:
connects
[
0
][
"service"
]
.
fireOn
()
connects
[
sessionId
][
"service"
]
.
fireOn
()
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"点火成功!"
except
BaseException
as
e
:
...
...
@@ -132,10 +144,12 @@ def porcessFireOn():
def
porcessStartTravel
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
try
:
connects
[
0
][
"service"
]
.
startService
()
connects
[
0
][
"service"
]
.
startTravel
()
service
=
connects
[
sessionId
][
"service"
]
service
.
startService
()
service
.
startTravel
()
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"开始行驶成功!"
except
BaseException
as
e
:
...
...
@@ -152,9 +166,10 @@ def porcessStartTravel():
def
porcessStopTravel
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
try
:
connects
[
0
][
"service"
]
.
stopTravel
()
connects
[
sessionId
][
"service"
]
.
stopTravel
()
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"停止行驶成功!"
except
BaseException
as
e
:
...
...
@@ -171,11 +186,13 @@ def porcessStopTravel():
def
porcessFireOff
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
try
:
connects
[
0
][
"service"
]
.
fireOff
()
connects
[
0
][
"service"
]
.
stopTravel
()
connects
[
0
][
"service"
]
.
stopService
()
service
=
connects
[
sessionId
][
"service"
]
service
.
fireOff
()
service
.
stopTravel
()
service
.
stopService
()
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"熄火成功!"
except
BaseException
as
e
:
...
...
@@ -193,13 +210,15 @@ def porcessFireOff():
def
porcessDisconnect
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
try
:
connects
[
0
][
"service"
]
.
stopWebsocketService
()
connects
[
0
][
"service"
]
.
stopTravel
()
connects
[
0
][
"service"
]
.
stopService
()
connects
[
0
][
"service"
]
.
socket
.
close
()
connects
.
pop
(
0
)
service
=
connects
[
sessionId
][
"service"
]
# connects[0]["service"].stopWebsocketService()
service
.
stopTravel
()
service
.
stopService
()
service
.
socket
.
close
()
connects
.
pop
(
sessionId
)
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"断开连接成功!"
except
BaseException
as
e
:
...
...
@@ -216,19 +235,23 @@ def porcessDisconnect():
def
reset
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
global
connects
global
websocket
try
:
for
t
in
connects
:
t
[
"service"
]
.
stopTravel
()
for
key
in
connects
:
service
=
connects
[
key
][
"service"
]
service
.
stopTravel
()
try
:
t
[
"service"
]
.
fireOff
()
service
.
fireOff
()
except
BaseException
as
e1
:
pass
t
[
"service"
]
.
stopWebsocketService
()
t
[
"service"
]
.
stopTravel
()
t
[
"service"
]
.
stopService
()
t
[
"service"
]
.
socket
.
close
()
connects
.
pop
(
0
)
service
.
stopWebsocketService
()
service
.
stopTravel
()
service
.
stopService
()
service
.
socket
.
close
()
connects
=
{}
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"复位成功!"
except
BaseException
as
e
:
...
...
@@ -313,4 +336,22 @@ def searchCarsimulaterData():
traceback
.
print_exc
()
data
[
"status"
]
=
"4003"
data
[
"message"
]
=
"Error: 查询车机数据失败失败!"
return
Response
(
json
.
dumps
(
data
),
mimetype
=
'application/json'
)
\ No newline at end of file
return
Response
(
json
.
dumps
(
data
),
mimetype
=
'application/json'
)
##########################################
# 【接口类型】查询当前连接数,和连接对象
##########################################
@
M_simulater_process
.
route
(
"/getConnects"
,
methods
=
[
'POST'
])
def
getConnects
():
data
=
{}
try
:
data
[
"status"
]
=
"200"
data
[
"userCounts"
]
=
len
(
connects
)
data
[
"result"
]
=
str
(
connects
)
return
Response
(
json
.
dumps
(
data
),
mimetype
=
'application/json'
)
except
BaseException
as
e
:
# 打印异常信息
traceback
.
print_exc
()
data
[
"status"
]
=
"4003"
data
[
"message"
]
=
"Error: 获取在线人数失败失败!"
return
Response
(
json
.
dumps
(
data
),
mimetype
=
'application/json'
)
\ No newline at end of file
views/protocolTools/M_carSimulater_process.py
View file @
af00f223
...
...
@@ -14,7 +14,8 @@ from lib.util import fileUtil
M_carSimulater_process
=
Blueprint
(
'M_carSimulater_process'
,
__name__
)
connects
=
[]
#用来保存连接的信息
connects
=
{}
#用来保存连接的信息s
websocket
=
None
#保存创建的websocket
##########################################
# 【接口类型】设置socket信息
...
...
@@ -56,25 +57,33 @@ def createConect():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
timeout
=
int
(
params
[
"timeout"
])
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
try
:
conf_R
=
ConfigParser
()
conf_R
.
read
(
"config/protocolTools/carSimulater.conf"
)
if
len
(
connects
)
<
1
:
if
not
sessionId
in
connects
.
keys
()
:
cliSocket
=
ClientSocket
(
conf_R
.
get
(
"socket"
,
"host"
),
conf_R
.
getint
(
"socket"
,
"port"
))
cliSocket
.
setTimeOut
(
timeout
)
cliSocket
.
connect
()
connect
=
{}
connects
[
sessionId
]
=
connect
socketName
=
"socket_"
+
str
(
len
(
connects
)
+
1
)
connect
[
"name"
]
=
socketName
connect
[
"obj"
]
=
cliSocket
connects
.
append
(
connect
)
service
=
ProtocolSimulaterService
()
service
.
setSocket
(
cliSocket
)
service
.
setTimeout
(
timeout
)
service
.
setData
(
params
)
service
.
startWebsocketService
()
connects
[
0
][
"service"
]
=
service
global
websocket
if
websocket
==
None
:
service
.
startWebsocketService
()
#如果没有创建websocket服务,则启动新的websocket服务
websocket
=
service
.
getWebsocket
()
else
:
service
.
setWebsocket
(
websocket
)
#给新服务设置websocket服务对象
service
.
addNewWebsocket
()
#创建一个新的websocket连接
connect
[
"service"
]
=
connect
connects
[
sessionId
][
"service"
]
=
service
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"创建连接成功!"
else
:
...
...
@@ -94,9 +103,10 @@ def createConect():
def
login
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
try
:
service
=
connects
[
0
][
"service"
]
service
=
connects
[
sessionId
][
"service"
]
service
.
setCarId
(
params
[
"carId"
])
loginObj
=
LoginReport_protocol
(
params
[
"WATER_CODE"
],
params
[
"carId"
],
params
[
"login"
][
"cpuId"
],
\
params
[
"login"
][
"imsi"
],
params
[
"login"
][
"ccid"
],
params
[
"login"
][
"imei"
])
...
...
@@ -123,12 +133,13 @@ def login():
def
fire
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
durTime
=
int
(
params
[
"durTime"
])
gpsLine
=
params
[
"gpsLine"
]
carId
=
params
[
"carId"
]
data
=
{}
try
:
service
=
connects
[
0
][
"service"
]
service
=
connects
[
sessionId
][
"service"
]
service
.
setCarId
(
carId
)
service
.
setGpsLine
(
gpsLine
)
service
.
fireOn
()
...
...
@@ -148,11 +159,12 @@ def fire():
def
startTravel
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
durTime
=
int
(
params
[
"durTime"
])
gpsLine
=
params
[
"gpsLine"
]
data
=
{}
try
:
service
=
connects
[
0
][
"service"
]
service
=
connects
[
sessionId
][
"service"
]
service
.
setSendDur
(
durTime
)
#设置车机多久循环发送一次消息
service
.
setGpsLine
(
gpsLine
)
service
.
startService
()
...
...
@@ -171,9 +183,12 @@ def startTravel():
##########################################
@
M_carSimulater_process
.
route
(
"/stopTravel"
,
methods
=
[
'POST'
])
def
stopTravel
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
try
:
service
=
connects
[
0
][
"service"
]
service
=
connects
[
sessionId
][
"service"
]
service
.
stopTravel
()
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"停止行驶成功!"
...
...
@@ -191,14 +206,16 @@ def stopTravel():
def
unFire
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
gpsLine
=
params
[
"gpsLine"
]
carId
=
params
[
"carId"
]
data
=
{}
try
:
connects
[
0
][
"service"
]
.
setGpsLine
(
gpsLine
)
connects
[
0
][
"service"
]
.
setCarId
(
carId
)
connects
[
0
][
"service"
]
.
fireOff
()
connects
[
0
][
"service"
]
.
stopTravel
()
service
=
connects
[
sessionId
][
"service"
]
service
.
setGpsLine
(
gpsLine
)
service
.
setCarId
(
carId
)
service
.
fireOff
()
service
.
stopTravel
()
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"熄火成功!"
except
BaseException
as
e
:
...
...
@@ -213,17 +230,21 @@ def unFire():
##########################################
@
M_carSimulater_process
.
route
(
"/closeConect"
,
methods
=
[
'POST'
])
def
closeConect
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
sessionId
=
params
[
"session"
][
"sessionId"
]
data
=
{}
try
:
if
len
(
connects
)
<
1
:
data
[
"status"
]
=
"4003"
data
[
"message"
]
=
"没有可关闭的连接!"
else
:
connects
[
0
][
"service"
]
.
stopTravel
()
connects
[
0
][
"service"
]
.
stopWebsocketService
()
connects
[
0
][
"service"
]
.
stopService
()
connects
[
0
][
"service"
]
.
closeSocket
()
connects
.
pop
(
0
)
service
=
connects
[
sessionId
][
"service"
]
service
.
stopTravel
()
# service.stopWebsocketService()
service
.
stopService
()
service
.
closeSocket
()
connects
.
pop
(
sessionId
)
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"关闭连接成功!"
except
BaseException
as
e
:
...
...
@@ -231,6 +252,7 @@ def closeConect():
traceback
.
print_exc
()
data
[
"status"
]
=
"4003"
data
[
"message"
]
=
"Error: 关闭连接失败!"
print
(
connects
)
return
Response
(
json
.
dumps
(
data
),
mimetype
=
'application/json'
)
##########################################
...
...
@@ -238,18 +260,24 @@ def closeConect():
##########################################
@
M_carSimulater_process
.
route
(
"/reset"
,
methods
=
[
'POST'
])
def
reset
():
params
=
request
.
get_data
()
params
=
json
.
loads
(
params
.
decode
(
"utf-8"
))
data
=
{}
global
connects
global
websocket
try
:
for
t
in
connects
:
t
[
"service"
]
.
stopTravel
()
for
key
in
connects
:
service
=
connects
[
key
][
"service"
]
service
.
stopTravel
()
try
:
t
[
"service"
]
.
fireOff
()
service
.
fireOff
()
except
BaseException
as
e1
:
pass
t
[
"service"
]
.
stopWebsocketService
()
t
[
"service"
]
.
stopService
()
t
[
"service"
]
.
closeSocket
()
connects
.
pop
(
0
)
service
.
stopWebsocketService
()
service
.
stopService
()
service
.
closeSocket
()
websocket
=
None
connects
=
{}
data
[
"status"
]
=
"200"
data
[
"message"
]
=
"复位成功!"
except
BaseException
as
e
:
...
...
@@ -336,3 +364,21 @@ def searchCarsimulaterData():
data
[
"message"
]
=
"Error: 查询车机数据失败失败!"
return
Response
(
json
.
dumps
(
data
),
mimetype
=
'application/json'
)
##########################################
# 【接口类型】查询当前连接数,和连接对象
##########################################
@
M_carSimulater_process
.
route
(
"/getConnects"
,
methods
=
[
'POST'
])
def
getConnects
():
data
=
{}
try
:
data
[
"status"
]
=
"200"
data
[
"userCounts"
]
=
len
(
connects
)
data
[
"result"
]
=
str
(
connects
)
return
Response
(
json
.
dumps
(
data
),
mimetype
=
'application/json'
)
except
BaseException
as
e
:
# 打印异常信息
traceback
.
print_exc
()
data
[
"status"
]
=
"4003"
data
[
"message"
]
=
"Error: 获取在线人数失败失败!"
return
Response
(
json
.
dumps
(
data
),
mimetype
=
'application/json'
)
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