Commit d1924aa9 by patpat

增加网络切换功能、增加以太网ip显示

parent 3fbc63fc
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" /> <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_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
......
...@@ -8,6 +8,7 @@ import com.bgycc.smartcanteen.Storage.PayStorage ...@@ -8,6 +8,7 @@ import com.bgycc.smartcanteen.Storage.PayStorage
import com.bgycc.smartcanteen.action.UpdateAction import com.bgycc.smartcanteen.action.UpdateAction
import com.bgycc.smartcanteen.helper.TimerHelper import com.bgycc.smartcanteen.helper.TimerHelper
import com.bgycc.smartcanteen.helper.WifiHelpler import com.bgycc.smartcanteen.helper.WifiHelpler
import com.bgycc.smartcanteen.manager.NetworkManager
import com.bgycc.smartcanteen.util.LogUtil import com.bgycc.smartcanteen.util.LogUtil
import com.bgycc.smartcanteen.module.Device import com.bgycc.smartcanteen.module.Device
import com.blankj.utilcode.constant.PermissionConstants import com.blankj.utilcode.constant.PermissionConstants
...@@ -55,6 +56,7 @@ class App : Application() { ...@@ -55,6 +56,7 @@ class App : Application() {
} }
sInstances[getProcessNameCompat()] = this sInstances[getProcessNameCompat()] = this
initLog() initLog()
NetworkManager.initialize(this)
WifiHelpler.initialize(this) WifiHelpler.initialize(this)
PayStorage.initialize(this) PayStorage.initialize(this)
Device.initialize(this) Device.initialize(this)
...@@ -91,7 +93,7 @@ class App : Application() { ...@@ -91,7 +93,7 @@ class App : Application() {
} }
private fun initLog() { private fun initLog() {
LogUtil.setEnable(AppConfig.DEBUG) LogUtil.setEnable(BuildConfig.DEBUG)
val config = LogUtils.getConfig() val config = LogUtils.getConfig()
config.saveDays = 7 config.saveDays = 7
} }
......
...@@ -7,7 +7,6 @@ public class AppConfig { ...@@ -7,7 +7,6 @@ public class AppConfig {
PROD PROD
} }
public static final Server SERVER = Server.PROD; public static final Server SERVER = Server.PROD;
public static final boolean DEBUG = false;
public static String getMainWebSocketServerUrl() { public static String getMainWebSocketServerUrl() {
switch (SERVER) { switch (SERVER) {
......
...@@ -22,6 +22,7 @@ import com.bgycc.smartcanteen.event.* ...@@ -22,6 +22,7 @@ import com.bgycc.smartcanteen.event.*
import com.bgycc.smartcanteen.helper.EthernetHelper import com.bgycc.smartcanteen.helper.EthernetHelper
import com.bgycc.smartcanteen.helper.TimerHelper import com.bgycc.smartcanteen.helper.TimerHelper
import com.bgycc.smartcanteen.helper.WifiHelpler 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.event.ConnectStateEvent
import com.bgycc.smartcanteen.server.websocket.MainWebSocket import com.bgycc.smartcanteen.server.websocket.MainWebSocket
import com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent import com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent
...@@ -100,7 +101,6 @@ class MainActivity : BaseActivity() { ...@@ -100,7 +101,6 @@ class MainActivity : BaseActivity() {
_setting_img.startAnimation(anim) _setting_img.startAnimation(anim)
} }
_version.text = "Version: v${App.getVersionName()}(${App.getVersionCode()})" _version.text = "Version: v${App.getVersionName()}(${App.getVersionCode()})"
_eth.text = "Ethernet Mac: ${EthernetHelper.getMacAddress()}"
_sn.text = "SN: ${App.getDeviceSN()}" _sn.text = "SN: ${App.getDeviceSN()}"
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
_message.setOnClickListener { _message.setOnClickListener {
...@@ -120,6 +120,7 @@ class MainActivity : BaseActivity() { ...@@ -120,6 +120,7 @@ class MainActivity : BaseActivity() {
private fun initTimer() { private fun initTimer() {
mTimerId = TimerHelper.loop({ _, _ -> mTimerId = TimerHelper.loop({ _, _ ->
mHandler.post { mHandler.post {
_eth.text = "Ethernet Mac: ${EthernetHelper.getMacAddress()} IP: ${EthernetHelper.getIpString()}"
_wifi.text = "Wifi Mac: ${WifiHelpler.getMacAddress()} SSID: ${WifiHelpler.getSSID()} IP: ${WifiHelpler.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()) _time.text = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date())
} }
...@@ -194,10 +195,11 @@ class MainActivity : BaseActivity() { ...@@ -194,10 +195,11 @@ class MainActivity : BaseActivity() {
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: ConnectStateEvent) { fun onMessageEvent(event: ConnectStateEvent) {
val base = "Server: " + String.format(AppConfig.getMainWebSocketServerUrl(), App.getDeviceSN()) + " " val base = "Server: " + String.format(AppConfig.getMainWebSocketServerUrl(), App.getDeviceSN()) + " "
if (event.state == ConnectStateEvent.OFFLINE) _server.text = base + "未连接" if (event.state == ConnectStateEvent.OFFLINE) _server.text = base + "${NetworkManager.currentTypeString} 未连接"
else if (event.state == ConnectStateEvent.CONNECTING) _server.text = base + "正在连接..." else if (event.state == ConnectStateEvent.CONNECTING) _server.text = base + "${NetworkManager.currentTypeString} 正在连接..."
else if (event.state == ConnectStateEvent.CONNECTED) _server.text = base + "已连接" else if (event.state == ConnectStateEvent.CONNECTED) _server.text = base + "${NetworkManager.currentTypeString} 已连接"
else if (event.state == ConnectStateEvent.RECONNECTING) _server.text = base + "正在重连..." 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) @Subscribe(threadMode = ThreadMode.MAIN)
......
package com.bgycc.smartcanteen.helper; package com.bgycc.smartcanteen.helper;
import java.net.InetAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.util.Enumeration;
public class EthernetHelper { public class EthernetHelper {
...@@ -15,4 +17,18 @@ public class EthernetHelper { ...@@ -15,4 +17,18 @@ public class EthernetHelper {
return "02:00:00:00:00:00"; 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";
}
} }
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
...@@ -8,6 +8,7 @@ import com.bgycc.smartcanteen.action.ActionEnum; ...@@ -8,6 +8,7 @@ import com.bgycc.smartcanteen.action.ActionEnum;
import com.bgycc.smartcanteen.action.PayOfflineAction; import com.bgycc.smartcanteen.action.PayOfflineAction;
import com.bgycc.smartcanteen.action.UpdateAction; import com.bgycc.smartcanteen.action.UpdateAction;
import com.bgycc.smartcanteen.helper.TimerHelper; 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.ConnectStateEvent;
import com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent; import com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent;
import com.bgycc.smartcanteen.server.websocket.event.SendMessageEvent; import com.bgycc.smartcanteen.server.websocket.event.SendMessageEvent;
...@@ -46,6 +47,7 @@ public class MainWebSocket extends WebSocketClient { ...@@ -46,6 +47,7 @@ public class MainWebSocket extends WebSocketClient {
private static MainWebSocket sInstance; private static MainWebSocket sInstance;
private static String sDeviceSN; private static String sDeviceSN;
private static int sReconnectTimes = 0;
public static void initialize() { public static void initialize() {
if (sInstance == null) { if (sInstance == null) {
...@@ -60,11 +62,18 @@ public class MainWebSocket extends WebSocketClient { ...@@ -60,11 +62,18 @@ public class MainWebSocket extends WebSocketClient {
@Override @Override
public void run(long id, boolean isLastTime) { public void run(long id, boolean isLastTime) {
if (sInstance.isClosed()) { if (sInstance.isClosed()) {
if (sReconnectTimes < 3) {
sReconnectTimes++;
sInstance.reconnect(); sInstance.reconnect();
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.RECONNECTING)); EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.RECONNECTING));
} else {
sReconnectTimes = 0;
NetworkManager.INSTANCE.switchNetwork();
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CHANGE_NETWORK));
}
} }
} }
}, 5000); }, 2000);
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -6,6 +6,7 @@ public class ConnectStateEvent { ...@@ -6,6 +6,7 @@ public class ConnectStateEvent {
public static final int CONNECTING = 1; public static final int CONNECTING = 1;
public static final int CONNECTED = 2; public static final int CONNECTED = 2;
public static final int RECONNECTING = 3; public static final int RECONNECTING = 3;
public static final int CHANGE_NETWORK = 100;
public int state = OFFLINE; public int state = OFFLINE;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment