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
d1924aa9
authored
Nov 19, 2019
by
patpat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加网络切换功能、增加以太网ip显示
parent
3fbc63fc
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
102 additions
and
10 deletions
+102
-10
app/src/main/AndroidManifest.xml
+2
-0
app/src/main/java/com/bgycc/smartcanteen/App.kt
+3
-1
app/src/main/java/com/bgycc/smartcanteen/AppConfig.java
+0
-1
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.kt
+7
-5
app/src/main/java/com/bgycc/smartcanteen/helper/EthernetHelper.java
+16
-0
app/src/main/java/com/bgycc/smartcanteen/manager/NetworkManager.kt
+61
-0
app/src/main/java/com/bgycc/smartcanteen/server/websocket/MainWebSocket.java
+12
-3
app/src/main/java/com/bgycc/smartcanteen/server/websocket/event/ConnectStateEvent.java
+1
-0
No files found.
app/src/main/AndroidManifest.xml
View file @
d1924aa9
...
...
@@ -4,6 +4,8 @@
<uses-permission
android:name=
"android.permission.RECEIVE_BOOT_COMPLETED"
/>
<uses-permission
android:name=
"android.permission.INTERNET"
/>
<uses-permission
android:name=
"android.permission.WRITE_SETTINGS"
/>
<uses-permission
android:name=
"android.permission.CHANGE_NETWORK_STATE"
/>
<uses-permission
android:name=
"android.permission.ACCESS_NETWORK_STATE"
/>
<uses-permission
android:name=
"android.permission.ACCESS_WIFI_STATE"
/>
<uses-permission
android:name=
"android.permission.CHANGE_WIFI_STATE"
/>
...
...
app/src/main/java/com/bgycc/smartcanteen/App.kt
View file @
d1924aa9
...
...
@@ -8,6 +8,7 @@ import com.bgycc.smartcanteen.Storage.PayStorage
import
com.bgycc.smartcanteen.action.UpdateAction
import
com.bgycc.smartcanteen.helper.TimerHelper
import
com.bgycc.smartcanteen.helper.WifiHelpler
import
com.bgycc.smartcanteen.manager.NetworkManager
import
com.bgycc.smartcanteen.util.LogUtil
import
com.bgycc.smartcanteen.module.Device
import
com.blankj.utilcode.constant.PermissionConstants
...
...
@@ -55,6 +56,7 @@ class App : Application() {
}
sInstances
[
getProcessNameCompat
()]
=
this
initLog
()
NetworkManager
.
initialize
(
this
)
WifiHelpler
.
initialize
(
this
)
PayStorage
.
initialize
(
this
)
Device
.
initialize
(
this
)
...
...
@@ -91,7 +93,7 @@ class App : Application() {
}
private
fun
initLog
()
{
LogUtil
.
setEnable
(
App
Config
.
DEBUG
)
LogUtil
.
setEnable
(
Build
Config
.
DEBUG
)
val
config
=
LogUtils
.
getConfig
()
config
.
saveDays
=
7
}
...
...
app/src/main/java/com/bgycc/smartcanteen/AppConfig.java
View file @
d1924aa9
...
...
@@ -7,7 +7,6 @@ public class AppConfig {
PROD
}
public
static
final
Server
SERVER
=
Server
.
PROD
;
public
static
final
boolean
DEBUG
=
false
;
public
static
String
getMainWebSocketServerUrl
()
{
switch
(
SERVER
)
{
...
...
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.kt
View file @
d1924aa9
...
...
@@ -22,6 +22,7 @@ import com.bgycc.smartcanteen.event.*
import
com.bgycc.smartcanteen.helper.EthernetHelper
import
com.bgycc.smartcanteen.helper.TimerHelper
import
com.bgycc.smartcanteen.helper.WifiHelpler
import
com.bgycc.smartcanteen.manager.NetworkManager
import
com.bgycc.smartcanteen.server.websocket.event.ConnectStateEvent
import
com.bgycc.smartcanteen.server.websocket.MainWebSocket
import
com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent
...
...
@@ -100,7 +101,6 @@ class MainActivity : BaseActivity() {
_setting_img
.
startAnimation
(
anim
)
}
_version
.
text
=
"Version: v${App.getVersionName()}(${App.getVersionCode()})"
_eth
.
text
=
"Ethernet Mac: ${EthernetHelper.getMacAddress()}"
_sn
.
text
=
"SN: ${App.getDeviceSN()}"
if
(
BuildConfig
.
DEBUG
)
{
_message
.
setOnClickListener
{
...
...
@@ -120,6 +120,7 @@ class MainActivity : BaseActivity() {
private
fun
initTimer
()
{
mTimerId
=
TimerHelper
.
loop
({
_
,
_
->
mHandler
.
post
{
_eth
.
text
=
"Ethernet Mac: ${EthernetHelper.getMacAddress()} IP: ${EthernetHelper.getIpString()}"
_wifi
.
text
=
"Wifi Mac: ${WifiHelpler.getMacAddress()} SSID: ${WifiHelpler.getSSID()} IP: ${WifiHelpler.getIpString()}"
_time
.
text
=
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
,
Locale
.
getDefault
()).
format
(
Date
())
}
...
...
@@ -194,10 +195,11 @@ class MainActivity : BaseActivity() {
@Subscribe
(
threadMode
=
ThreadMode
.
MAIN
)
fun
onMessageEvent
(
event
:
ConnectStateEvent
)
{
val
base
=
"Server: "
+
String
.
format
(
AppConfig
.
getMainWebSocketServerUrl
(),
App
.
getDeviceSN
())
+
" "
if
(
event
.
state
==
ConnectStateEvent
.
OFFLINE
)
_server
.
text
=
base
+
"未连接"
else
if
(
event
.
state
==
ConnectStateEvent
.
CONNECTING
)
_server
.
text
=
base
+
"正在连接..."
else
if
(
event
.
state
==
ConnectStateEvent
.
CONNECTED
)
_server
.
text
=
base
+
"已连接"
else
if
(
event
.
state
==
ConnectStateEvent
.
RECONNECTING
)
_server
.
text
=
base
+
"正在重连..."
if
(
event
.
state
==
ConnectStateEvent
.
OFFLINE
)
_server
.
text
=
base
+
"${NetworkManager.currentTypeString} 未连接"
else
if
(
event
.
state
==
ConnectStateEvent
.
CONNECTING
)
_server
.
text
=
base
+
"${NetworkManager.currentTypeString} 正在连接..."
else
if
(
event
.
state
==
ConnectStateEvent
.
CONNECTED
)
_server
.
text
=
base
+
"${NetworkManager.currentTypeString} 已连接"
else
if
(
event
.
state
==
ConnectStateEvent
.
RECONNECTING
)
_server
.
text
=
base
+
"${NetworkManager.currentTypeString} 正在重连..."
else
if
(
event
.
state
==
ConnectStateEvent
.
CHANGE_NETWORK
)
_server
.
text
=
base
+
"${NetworkManager.currentTypeString} 正在切换..."
}
@Subscribe
(
threadMode
=
ThreadMode
.
MAIN
)
...
...
app/src/main/java/com/bgycc/smartcanteen/helper/EthernetHelper.java
View file @
d1924aa9
package
com
.
bgycc
.
smartcanteen
.
helper
;
import
java.net.InetAddress
;
import
java.net.NetworkInterface
;
import
java.util.Enumeration
;
public
class
EthernetHelper
{
...
...
@@ -15,4 +17,18 @@ public class EthernetHelper {
return
"02:00:00:00:00:00"
;
}
public
static
String
getIpString
()
{
try
{
NetworkInterface
networkInterface
=
NetworkInterface
.
getByName
(
"eth0"
);
for
(
Enumeration
<
InetAddress
>
enumIpAddr
=
networkInterface
.
getInetAddresses
();
enumIpAddr
.
hasMoreElements
();)
{
InetAddress
inetAddress
=
enumIpAddr
.
nextElement
();
if
(!
inetAddress
.
isLoopbackAddress
())
{
String
address
=
inetAddress
.
getHostAddress
();
if
(!
address
.
contains
(
"::"
))
return
address
;
}
}
}
catch
(
Exception
ex
)
{}
return
"0.0.0.0"
;
}
}
app/src/main/java/com/bgycc/smartcanteen/manager/NetworkManager.kt
0 → 100644
View file @
d1924aa9
package
com.bgycc.smartcanteen.manager
import
android.content.Context
import
android.net.ConnectivityManager
import
android.net.Network
import
android.net.NetworkCapabilities
import
android.net.NetworkRequest
import
android.os.Build
import
com.bgycc.smartcanteen.util.LogUtil
object
NetworkManager
{
private
val
TAG
=
NetworkManager
::
class
.
java
.
simpleName
val
currentTypeString
:
String
get
()
{
return
when
(
mTransportType
)
{
NetworkCapabilities
.
TRANSPORT_ETHERNET
->
"以太网"
NetworkCapabilities
.
TRANSPORT_WIFI
->
"Wifi"
else
->
"未知"
}
}
private
var
mConnectivityManager
:
ConnectivityManager
?
=
null
private
var
mLastSwitchTime
=
0L
private
var
mTransportType
=
NetworkCapabilities
.
TRANSPORT_ETHERNET
fun
initialize
(
context
:
Context
)
{
mConnectivityManager
=
context
.
getSystemService
(
Context
.
CONNECTIVITY_SERVICE
)
as
ConnectivityManager
}
fun
switchNetwork
()
{
// val time = System.currentTimeMillis()
// if (time - mLastSwitchTime < 2000) return
// mLastSwitchTime = time
mTransportType
=
when
(
mTransportType
)
{
NetworkCapabilities
.
TRANSPORT_ETHERNET
->
NetworkCapabilities
.
TRANSPORT_WIFI
else
->
NetworkCapabilities
.
TRANSPORT_ETHERNET
}
val
request
=
NetworkRequest
.
Builder
()
request
.
addTransportType
(
mTransportType
)
mConnectivityManager
?.
requestNetwork
(
request
.
build
(),
object
:
ConnectivityManager
.
NetworkCallback
()
{
override
fun
onAvailable
(
network
:
Network
?)
{
LogUtil
.
i
(
TAG
,
"onAvailable $network"
)
super
.
onAvailable
(
network
)
if
(
Build
.
VERSION
.
SDK_INT
>=
23
)
{
mConnectivityManager
?.
bindProcessToNetwork
(
network
)
}
else
{
ConnectivityManager
.
setProcessDefaultNetwork
(
network
)
}
mConnectivityManager
?.
unregisterNetworkCallback
(
this
)
}
override
fun
onUnavailable
()
{
LogUtil
.
i
(
TAG
,
"onUnavailable"
)
super
.
onUnavailable
()
}
})
}
}
\ No newline at end of file
app/src/main/java/com/bgycc/smartcanteen/server/websocket/MainWebSocket.java
View file @
d1924aa9
...
...
@@ -8,6 +8,7 @@ import com.bgycc.smartcanteen.action.ActionEnum;
import
com.bgycc.smartcanteen.action.PayOfflineAction
;
import
com.bgycc.smartcanteen.action.UpdateAction
;
import
com.bgycc.smartcanteen.helper.TimerHelper
;
import
com.bgycc.smartcanteen.manager.NetworkManager
;
import
com.bgycc.smartcanteen.server.websocket.event.ConnectStateEvent
;
import
com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent
;
import
com.bgycc.smartcanteen.server.websocket.event.SendMessageEvent
;
...
...
@@ -46,6 +47,7 @@ public class MainWebSocket extends WebSocketClient {
private
static
MainWebSocket
sInstance
;
private
static
String
sDeviceSN
;
private
static
int
sReconnectTimes
=
0
;
public
static
void
initialize
()
{
if
(
sInstance
==
null
)
{
...
...
@@ -60,11 +62,18 @@ public class MainWebSocket extends WebSocketClient {
@Override
public
void
run
(
long
id
,
boolean
isLastTime
)
{
if
(
sInstance
.
isClosed
())
{
sInstance
.
reconnect
();
EventBus
.
getDefault
().
post
(
new
ConnectStateEvent
(
ConnectStateEvent
.
RECONNECTING
));
if
(
sReconnectTimes
<
3
)
{
sReconnectTimes
++;
sInstance
.
reconnect
();
EventBus
.
getDefault
().
post
(
new
ConnectStateEvent
(
ConnectStateEvent
.
RECONNECTING
));
}
else
{
sReconnectTimes
=
0
;
NetworkManager
.
INSTANCE
.
switchNetwork
();
EventBus
.
getDefault
().
post
(
new
ConnectStateEvent
(
ConnectStateEvent
.
CHANGE_NETWORK
));
}
}
}
},
5
000
);
},
2
000
);
}
catch
(
URISyntaxException
e
)
{
e
.
printStackTrace
();
}
...
...
app/src/main/java/com/bgycc/smartcanteen/server/websocket/event/ConnectStateEvent.java
View file @
d1924aa9
...
...
@@ -6,6 +6,7 @@ public class ConnectStateEvent {
public
static
final
int
CONNECTING
=
1
;
public
static
final
int
CONNECTED
=
2
;
public
static
final
int
RECONNECTING
=
3
;
public
static
final
int
CHANGE_NETWORK
=
100
;
public
int
state
=
OFFLINE
;
...
...
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