Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
258bc0aa
authored
Jan 08, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
回滚旧版本,以发布生产包
parent
94dcef0e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
69 deletions
+41
-69
app/src/main/java/com/bgycc/smartcanteen/server/websocket/MainWebSocket.java
+41
-69
No files found.
app/src/main/java/com/bgycc/smartcanteen/server/websocket/MainWebSocket.java
View file @
258bc0aa
...
...
@@ -48,82 +48,54 @@ public class MainWebSocket extends WebSocketClient {
public
static
final
String
CODE_FAIL
=
"-1"
;
private
static
MainWebSocket
sInstance
;
private
static
long
lastLoopId
=
-
1
;
private
static
String
sDeviceSN
;
private
static
int
sReconnectTimes
=
0
;
private
static
int
sAutoCheckCount
=
0
;
public
static
void
initialize
()
{
if
(
sInstance
!=
null
)
{
return
;
}
sDeviceSN
=
App
.
Companion
.
getDeviceSN
();
if
(
sDeviceSN
.
isEmpty
())
{
LogUtils
.
file
(
TAG
,
"device id is empty"
);
return
;
}
EventBus
.
getDefault
().
post
(
new
ConnectStateEvent
(
ConnectStateEvent
.
CONNECTING
));
String
host
=
AppConfig
.
INSTANCE
.
getMainWebSocketServerUrl
(
sDeviceSN
,
BuildConfig
.
VERSION_NAME
);
LogUtil
.
i
(
TAG
,
"try connecting to host: "
+
host
);
try
{
sInstance
=
new
MainWebSocket
(
new
URI
(
host
));
}
catch
(
URISyntaxException
e
)
{
LogUtil
.
i
(
TAG
,
"invalidate host: "
+
host
,
e
);
}
sInstance
.
setConnectionLostTimeout
(
10
);
sInstance
.
connect
();
// 保证只有一个loop在运行
if
(
lastLoopId
!=
-
1
)
{
TimerHelper
.
INSTANCE
.
cancel
(
lastLoopId
);
}
lastLoopId
=
TimerHelper
.
INSTANCE
.
loop
(
new
TimerHelper
.
LoopTask
()
{
@Override
public
void
run
(
long
id
,
boolean
isLastTime
)
{
try
{
if
(
sInstance
==
null
)
{
LogUtils
.
file
(
TAG
,
"instance of socket could not be null!!!"
);
initialize
();
return
;
}
if
(
sInstance
.
isClosed
())
{
doReconnect
();
}
else
{
doHeartbeat
();
}
}
catch
(
Exception
e
)
{
LogUtils
.
file
(
TAG
,
"fatal error: "
+
e
.
getMessage
());
}
}
private
void
doReconnect
()
{
if
(
sReconnectTimes
<
2
)
{
sReconnectTimes
++;
sInstance
.
reconnect
();
EventBus
.
getDefault
().
post
(
new
ConnectStateEvent
(
ConnectStateEvent
.
RECONNECTING
));
}
else
{
sReconnectTimes
=
0
;
NetworkManager
.
INSTANCE
.
switchNetwork
();
EventBus
.
getDefault
().
post
(
new
ConnectStateEvent
(
ConnectStateEvent
.
CHANGE_NETWORK
));
}
sAutoCheckCount
=
0
;
}
private
void
doHeartbeat
()
{
// 每10分钟发一次心跳包给后端
sAutoCheckCount
-=
2
;
if
(
sAutoCheckCount
<
0
)
{
sAutoCheckCount
=
10
*
60
;
try
{
JSONObject
data
=
new
JSONObject
();
data
.
put
(
FieldEnum
.
equipmentId
.
name
(),
sDeviceSN
);
action
(
ActionEnum
.
AUTO_CHECK_DEVICE
.
name
(),
data
,
null
);
}
catch
(
Exception
e
)
{
LogUtil
.
e
(
TAG
,
"action error: "
+
e
.
getMessage
(),
e
);
if
(
sInstance
==
null
)
{
try
{
sDeviceSN
=
App
.
Companion
.
getDeviceSN
();
if
(
sDeviceSN
.
isEmpty
())
return
;
EventBus
.
getDefault
().
post
(
new
ConnectStateEvent
(
ConnectStateEvent
.
CONNECTING
));
sInstance
=
new
MainWebSocket
(
new
URI
(
AppConfig
.
INSTANCE
.
getMainWebSocketServerUrl
(
sDeviceSN
,
BuildConfig
.
VERSION_NAME
)));
sInstance
.
setConnectionLostTimeout
(
10
);
sInstance
.
connect
();
TimerHelper
.
INSTANCE
.
loop
(
new
TimerHelper
.
LoopTask
()
{
@Override
public
void
run
(
long
id
,
boolean
isLastTime
)
{
if
(
sInstance
.
isClosed
())
{
if
(
sReconnectTimes
<
2
)
{
sReconnectTimes
++;
sInstance
.
reconnect
();
EventBus
.
getDefault
().
post
(
new
ConnectStateEvent
(
ConnectStateEvent
.
RECONNECTING
));
}
else
{
sReconnectTimes
=
0
;
NetworkManager
.
INSTANCE
.
switchNetwork
();
EventBus
.
getDefault
().
post
(
new
ConnectStateEvent
(
ConnectStateEvent
.
CHANGE_NETWORK
));
}
sAutoCheckCount
=
0
;
}
else
{
// 每10分钟发一次心跳包给后端
sAutoCheckCount
-=
2
;
if
(
sAutoCheckCount
<
0
)
{
sAutoCheckCount
=
10
*
60
;
try
{
JSONObject
data
=
new
JSONObject
();
data
.
put
(
FieldEnum
.
equipmentId
.
name
(),
sDeviceSN
);
action
(
ActionEnum
.
AUTO_CHECK_DEVICE
.
name
(),
data
,
null
);
}
catch
(
Exception
e
)
{
LogUtil
.
e
(
TAG
,
"action error: "
+
e
.
getMessage
(),
e
);
}
}
}
}
}
},
2000
,
-
1
,
2000
);
}
catch
(
URISyntaxException
e
)
{
LogUtils
.
file
(
TAG
,
"fatal error: "
+
e
.
getMessage
());
}
}
,
2000
,
-
1
,
2000
);
}
}
public
static
boolean
isInited
()
{
...
...
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