From 448c2cf6ba9cab5ce53fe883c4db8b5c011fc123 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 16 Apr 2024 12:42:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=90=E7=BB=B4=E5=90=AF=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E5=88=87=E6=8D=A2simcard?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xypower/mpmaster/AppMaster.java | 212 +++++++++++------- .../com/xypower/mpmaster/MpMasterService.java | 7 +- 2 files changed, 140 insertions(+), 79 deletions(-) diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index 430a38ba..1fb1470f 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -1,6 +1,11 @@ package com.xypower.mpmaster; import android.content.Context; +import android.net.ConnectivityManager; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.net.NetworkInfo; +import android.net.NetworkRequest; import android.os.Environment; import android.os.PowerManager; import android.os.SystemClock; @@ -103,111 +108,162 @@ public class AppMaster { @Override public void run() { + final boolean separateNetwork = mService.isSeparateNetwork(); - // mMasterUrl = "http://192.168.50.100/mntn/"; - - - String masterUrl = mMasterUrl; - - if (TextUtils.isEmpty(masterUrl)) { - return; - } + if (separateNetwork) { + try { + SysApi.selectSimCard4Data(mService.getApplicationContext(), 2); + Thread.sleep(100); + + boolean connected = false; + ConnectivityManager connectivityManager = (ConnectivityManager) mService.getSystemService(Context.CONNECTIVITY_SERVICE); + + for (int idx = 0; idx < 8; idx++) { + Network[] networks = connectivityManager.getAllNetworks(); + if (networks != null && networks.length > 0) { + for (Network nw : networks) { + NetworkInfo ni = connectivityManager.getNetworkInfo(nw); + + if (ni.getType() == 0) { + break; + } + } + } - HttpURLConnection httpURLConnection = null; - InputStream inputStream = null; - Date now = new Date(); - now.setHours(0); - now.setMinutes(0); - now.setSeconds(0); + if (connected) { + break; + } - long startTime = now.getTime() / 1000; - long endTime = startTime + 86400 - 1; + Thread.sleep(1000); + } - try { - if (masterUrl.indexOf('?') != -1) { - masterUrl += "&" + MicroPhotoContext.MASTER_URL_CMDID + "=" + URLEncoder.encode(mCmdid, "UTF-8"); - } else { - masterUrl += "?" + MicroPhotoContext.MASTER_URL_CMDID + "=" + URLEncoder.encode(mCmdid, "UTF-8"); - } - URL url = new URL(masterUrl); - httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setConnectTimeout(15000); - httpURLConnection.setReadTimeout(15000); - httpURLConnection.setRequestMethod("POST"); - // httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); - httpURLConnection.setDoInput(true); - httpURLConnection.setDoOutput(true); - - httpURLConnection.setRequestProperty("Accept-Cmds", "Multiple"); - - List> postParams = new ArrayList<>(); - postParams.add(new Pair("id", mCmdid)); - postParams.add(new Pair("XyDev", "1")); - postParams.add(new Pair("bootTime", Long.toString(getBootTime()))); - postParams.add(new Pair("i1Version", mService.getMpAppVersion())); - postParams.add(new Pair("oid", mService.getSerialNo())); - postParams.add(new Pair("maintainVersion", mService.getMasterAppVersion())); - - DBHandler dbHandler = null; - try { - dbHandler = new DBHandler(mService); - dbHandler.readStats(startTime, endTime, postParams); - dbHandler.close(); - } catch (Exception ex) { - ex.printStackTrace(); - } finally { - if (dbHandler != null) { + try { + runImpl(); + } catch (Exception ex) { + ex.printStackTrace(); + } finally { try { - dbHandler.close(); + SysApi.selectSimCard4Data(mService.getApplicationContext(), 1); } catch (Exception ex) { ex.printStackTrace(); } - dbHandler = null; } - } - try { - MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(mService); - if (appConfig != null) { - postParams.add(new Pair("heartbeatDuration", Integer.toString(appConfig.heartbeat))); - postParams.add(new Pair("cma", appConfig.server + ":" + Integer.toString(appConfig.port))); - } + } catch (Exception ex) { ex.printStackTrace(); } - + } else { try { - postParams.add(new Pair("mainBoardTmp", ThermalInfoUtil.getCPUTermperature())); + runImpl(); } catch (Exception ex) { ex.printStackTrace(); } + } + } + }).start(); + } + + private void runImpl() { + String masterUrl = mMasterUrl; + + if (TextUtils.isEmpty(masterUrl)) { + return; + } + + HttpURLConnection httpURLConnection = null; + InputStream inputStream = null; + Date now = new Date(); + now.setHours(0); + now.setMinutes(0); + now.setSeconds(0); + long startTime = now.getTime() / 1000; + long endTime = startTime + 86400 - 1; + + try { + if (masterUrl.indexOf('?') != -1) { + masterUrl += "&" + MicroPhotoContext.MASTER_URL_CMDID + "=" + URLEncoder.encode(mCmdid, "UTF-8"); + } else { + masterUrl += "?" + MicroPhotoContext.MASTER_URL_CMDID + "=" + URLEncoder.encode(mCmdid, "UTF-8"); + } + URL url = new URL(masterUrl); + httpURLConnection = (HttpURLConnection) url.openConnection(); + httpURLConnection.setConnectTimeout(15000); + httpURLConnection.setReadTimeout(15000); + httpURLConnection.setRequestMethod("POST"); + // httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); + httpURLConnection.setDoInput(true); + httpURLConnection.setDoOutput(true); + + httpURLConnection.setRequestProperty("Accept-Cmds", "Multiple"); + + List> postParams = new ArrayList<>(); + postParams.add(new Pair("id", mCmdid)); + postParams.add(new Pair("XyDev", "1")); + postParams.add(new Pair("bootTime", Long.toString(getBootTime()))); + postParams.add(new Pair("i1Version", mService.getMpAppVersion())); + postParams.add(new Pair("oid", mService.getSerialNo())); + postParams.add(new Pair("maintainVersion", mService.getMasterAppVersion())); + + DBHandler dbHandler = null; + try { + dbHandler = new DBHandler(mService); + dbHandler.readStats(startTime, endTime, postParams); + dbHandler.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } finally { + if (dbHandler != null) { try { - String battary = getBatteryVoltage() + "V/" + getChargingBatteryVoltage() + "V"; - postParams.add(new Pair("battary", battary)); + dbHandler.close(); } catch (Exception ex) { ex.printStackTrace(); } + dbHandler = null; + } + } - // postParams(httpURLConnection.getOutputStream(), postParams); - buildParams(httpURLConnection.getOutputStream(), postParams); - httpURLConnection.connect(); - inputStream = httpURLConnection.getInputStream(); + try { + MicroPhotoContext.AppConfig appConfig = MicroPhotoContext.getMpAppConfig(mService); + if (appConfig != null) { + postParams.add(new Pair("heartbeatDuration", Integer.toString(appConfig.heartbeat))); + postParams.add(new Pair("cma", appConfig.server + ":" + Integer.toString(appConfig.port))); + } + } catch (Exception ex) { + ex.printStackTrace(); + } - int responseCode = httpURLConnection.getResponseCode(); + try { + postParams.add(new Pair("mainBoardTmp", ThermalInfoUtil.getCPUTermperature())); + } catch (Exception ex) { + ex.printStackTrace(); + } - if (responseCode == HttpURLConnection.HTTP_OK) { - //在子线程中不能操作UI线程,通过handler在UI线程中进行操作 - // handler.sendEmptyMessage(0x00); - String response = convertStreamToString(inputStream); - process(response); - } - } catch (Exception e) { - e.printStackTrace(); - } + try { + String battary = getBatteryVoltage() + "V/" + getChargingBatteryVoltage() + "V"; + postParams.add(new Pair("battary", battary)); + } catch (Exception ex) { + ex.printStackTrace(); + } + // postParams(httpURLConnection.getOutputStream(), postParams); + buildParams(httpURLConnection.getOutputStream(), postParams); + httpURLConnection.connect(); + inputStream = httpURLConnection.getInputStream(); + + int responseCode = httpURLConnection.getResponseCode(); + + if (responseCode == HttpURLConnection.HTTP_OK) { + //在子线程中不能操作UI线程,通过handler在UI线程中进行操作 + // handler.sendEmptyMessage(0x00); + String response = convertStreamToString(inputStream); + process(response); } - }).start(); + } catch (Exception e) { + e.printStackTrace(); + } + } private String getBatteryVoltage() { diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java index 6b2da8bd..018a3e33 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java @@ -98,6 +98,8 @@ public class MpMasterService extends Service { private long mTimeOfMpAppAlive = 1800000; // 30minutes private int mAbsHeartbeatTimes[] = null; + private boolean mSeparateNetwork = false; + public MpMasterService() { } @Override @@ -165,7 +167,6 @@ public class MpMasterService extends Service { startMpApp(); registerHeartbeatTimer(); - } @Override @@ -211,6 +212,10 @@ public class MpMasterService extends Service { return mCmdid; } + public boolean isSeparateNetwork() { + return mSeparateNetwork; + } + public void startMpApp() { try { final Context context = getApplicationContext();