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
4286c251
authored
Jan 07, 2020
by
pye52
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'develop' into test
parents
876d6256
94dcef0e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
93 additions
and
49 deletions
+93
-49
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.kt
+0
-5
app/src/main/java/com/bgycc/smartcanteen/server/websocket/MainWebSocket.java
+72
-43
app/src/main/java/com/bgycc/smartcanteen/util/LogUtil.java
+20
-0
build.gradle
+1
-1
No files found.
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.kt
View file @
4286c251
...
...
@@ -2,7 +2,6 @@ package com.bgycc.smartcanteen.activity
import
android.content.Context
import
android.media.AudioManager
import
android.media.SoundPool
import
android.os.Bundle
import
android.os.Handler
import
android.util.Log
...
...
@@ -14,7 +13,6 @@ import com.bgycc.smartcanteen.App
import
com.bgycc.smartcanteen.AppConfig
import
com.bgycc.smartcanteen.BuildConfig
import
com.bgycc.smartcanteen.R
import
com.bgycc.smartcanteen.action.*
import
com.bgycc.smartcanteen.event.*
import
com.bgycc.smartcanteen.helper.EthernetHelper
import
com.bgycc.smartcanteen.helper.TTSHelper
...
...
@@ -29,15 +27,12 @@ import com.bgycc.smartcanteen.task.ButtonTask
import
com.bgycc.smartcanteen.task.QRCodeTask
import
com.bgycc.smartcanteen.util.StringUtil
import
com.blankj.utilcode.util.LogUtils
import
com.blankj.utilcode.util.ToastUtils
import
org.greenrobot.eventbus.EventBus
import
org.greenrobot.eventbus.Subscribe
import
org.greenrobot.eventbus.ThreadMode
import
java.text.SimpleDateFormat
import
java.util.*
import
kotlinx.android.synthetic.main.activity_main.*
import
org.json.JSONObject
import
java.lang.Exception
class
MainActivity
:
BaseActivity
()
{
...
...
app/src/main/java/com/bgycc/smartcanteen/server/websocket/MainWebSocket.java
View file @
4286c251
...
...
@@ -13,6 +13,8 @@ import com.bgycc.smartcanteen.server.websocket.event.ConnectStateEvent;
import
com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent
;
import
com.bgycc.smartcanteen.server.websocket.event.SendMessageEvent
;
import
com.bgycc.smartcanteen.util.LogUtil
;
import
com.blankj.utilcode.util.LogUtils
;
import
org.greenrobot.eventbus.EventBus
;
import
org.java_websocket.client.WebSocketClient
;
import
org.java_websocket.drafts.Draft_6455
;
...
...
@@ -46,54 +48,82 @@ 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
)
{
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
)
{
}
}
}
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
;
}
},
2000
,
-
1
,
2000
);
}
catch
(
URISyntaxException
e
)
{
e
.
printStackTrace
();
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
);
}
}
}
},
2000
,
-
1
,
2000
);
}
public
static
boolean
isInited
()
{
...
...
@@ -256,8 +286,7 @@ public class MainWebSocket extends WebSocketClient {
@Override
public
void
onError
(
Exception
ex
)
{
LogUtil
.
i
(
TAG
,
"onError"
);
ex
.
printStackTrace
();
LogUtil
.
i
(
TAG
,
"onError"
,
ex
);
}
public
static
class
Request
{
...
...
app/src/main/java/com/bgycc/smartcanteen/util/LogUtil.java
View file @
4286c251
...
...
@@ -12,21 +12,41 @@ public class LogUtil {
sEnable
=
enable
;
}
public
static
void
i
(
String
tag
,
String
log
,
Exception
e
)
{
if
(!
sEnable
)
return
;
Log
.
i
(
tag
,
log
,
e
);
}
public
static
void
i
(
String
tag
,
String
log
)
{
if
(!
sEnable
)
return
;
Log
.
i
(
tag
,
log
);
}
public
static
void
d
(
String
tag
,
String
log
,
Exception
e
)
{
if
(!
sEnable
)
return
;
Log
.
d
(
tag
,
log
,
e
);
}
public
static
void
d
(
String
tag
,
String
log
)
{
if
(!
sEnable
)
return
;
Log
.
d
(
tag
,
log
);
}
public
static
void
w
(
String
tag
,
String
log
,
Exception
e
)
{
if
(!
sEnable
)
return
;
Log
.
w
(
tag
,
log
,
e
);
}
public
static
void
w
(
String
tag
,
String
log
)
{
if
(!
sEnable
)
return
;
Log
.
w
(
tag
,
log
);
}
public
static
void
e
(
String
tag
,
String
log
,
Exception
e
)
{
if
(!
sEnable
)
return
;
Log
.
e
(
tag
,
log
,
e
);
}
public
static
void
e
(
String
tag
,
String
log
)
{
if
(!
sEnable
)
return
;
Log
.
e
(
tag
,
log
);
...
...
build.gradle
View file @
4286c251
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript
{
ext
.
kotlin_version
=
'1.3.
1
1'
ext
.
kotlin_version
=
'1.3.
6
1'
repositories
{
maven
{
url
'https://maven.aliyun.com/repository/google'
}
maven
{
url
'https://maven.aliyun.com/repository/jcenter'
}
...
...
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