运维启动时切换simcard

serial
Matthew 1 year ago
parent 8b876e4206
commit 448c2cf6ba

@ -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<Pair<String, String>> postParams = new ArrayList<>();
postParams.add(new Pair<String, String>("id", mCmdid));
postParams.add(new Pair<String, String>("XyDev", "1"));
postParams.add(new Pair<String, String>("bootTime", Long.toString(getBootTime())));
postParams.add(new Pair<String, String>("i1Version", mService.getMpAppVersion()));
postParams.add(new Pair<String, String>("oid", mService.getSerialNo()));
postParams.add(new Pair<String, String>("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<String, String>("heartbeatDuration", Integer.toString(appConfig.heartbeat)));
postParams.add(new Pair<String, String>("cma", appConfig.server + ":" + Integer.toString(appConfig.port)));
}
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
try {
postParams.add(new Pair<String, String>("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<Pair<String, String>> postParams = new ArrayList<>();
postParams.add(new Pair<String, String>("id", mCmdid));
postParams.add(new Pair<String, String>("XyDev", "1"));
postParams.add(new Pair<String, String>("bootTime", Long.toString(getBootTime())));
postParams.add(new Pair<String, String>("i1Version", mService.getMpAppVersion()));
postParams.add(new Pair<String, String>("oid", mService.getSerialNo()));
postParams.add(new Pair<String, String>("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<String, String>("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<String, String>("heartbeatDuration", Integer.toString(appConfig.heartbeat)));
postParams.add(new Pair<String, String>("cma", appConfig.server + ":" + Integer.toString(appConfig.port)));
}
} catch (Exception ex) {
ex.printStackTrace();
}
int responseCode = httpURLConnection.getResponseCode();
try {
postParams.add(new Pair<String, String>("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<String, String>("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() {

@ -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();

Loading…
Cancel
Save