增加运维的功能

serial
BlueMatthew 1 year ago
parent 651af4dfe2
commit 85dd793fea

@ -13,6 +13,7 @@ import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.util.zip.GZIPInputStream;
public class FileDownloader {
public boolean download(String urlString, String filePath) {
@ -20,14 +21,17 @@ public class FileDownloader {
if (TextUtils.isEmpty(urlString))
return false;
HttpURLConnection connection = null;
GZIPInputStream gZIPInputStream = null;
boolean res = false;
try {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
// connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setConnectTimeout(5000);
connection.setReadTimeout(60000);
connection.setDoInput(true);
String encoding = connection.getContentEncoding();
InputStream is = connection.getInputStream();
final File temp = new File(filePath);
if (temp.exists())
@ -68,6 +72,8 @@ public class FileDownloader {
return res;
}
public static final void closeSilently(Object closeable) {
try {
if (closeable != null) {

@ -0,0 +1,101 @@
package com.xypower.common;
import android.text.TextUtils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
public class HttpRequest {
public static String get(String urlString) {
File downloadFile = null;
if (TextUtils.isEmpty(urlString))
return "";
HttpURLConnection connection = null;
StringBuilder response = new StringBuilder();
InputStream inputStream = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(60000);
connection.setDoInput(true);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inputStream = connection.getInputStream();
isr = new InputStreamReader(inputStream);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
response.append(line);
}
}
} catch (Exception e) {
} finally {
try {
if (connection != null)
connection.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
try {
if (br != null) {
br.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
if (isr != null) {
isr.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
if (inputStream != null) {
inputStream.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return response.toString();
}
public static final void closeSilently(Object closeable) {
try {
if (closeable != null) {
if (closeable instanceof Closeable) {
((Closeable) closeable).close();
} else if (closeable instanceof Socket) {
((Socket) closeable).close();
} else if (closeable instanceof ServerSocket) {
((ServerSocket) closeable).close();
} else {
throw new IllegalArgumentException("Unknown object to close");
}
}
} catch (IOException e) {
// ignore
}
}
}

@ -10,6 +10,7 @@ import android.util.Base64;
import com.dev.devapi.api.SysApi;
import com.xypower.common.FileDownloader;
import com.xypower.common.FileUploader;
import com.xypower.common.HttpRequest;
import com.xypower.common.InetAddressUtils;
import com.xypower.common.JSONUtils;
import com.xypower.common.MicroPhotoContext;
@ -156,6 +157,7 @@ public class AppMaster {
JSONObject jsonObject = new JSONObject(content);
int isUpgrade = jsonObject.optInt("isUpgrade", 0);
String url = jsonObject.optString("url", null);
long cid = jsonObject.optLong("cid", 0);
int mntnMode = jsonObject.optInt("yw", 0);
int quickHbMode = jsonObject.optInt("kxt", 0);
@ -163,10 +165,10 @@ public class AppMaster {
mService.setMntnMode(mntnMode != 0, quickHbMode != 0);
if (isUpgrade == 1 && !TextUtils.isEmpty(url)) {
upgradeApp(url);
upgradeApp(cid, "upgrade", url);
}
processCmd(jsonObject);
processCmd(cid, jsonObject);
JSONArray cmdObjects = jsonObject.optJSONArray("cmds");
@ -175,7 +177,7 @@ public class AppMaster {
for (int idx = 0; idx < cnt; idx++) {
JSONObject cmdObject = cmdObjects.getJSONObject(idx);
if (cmdObject != null) {
processCmd(cmdObject);
processCmd(cid, cmdObject);
}
}
}
@ -186,9 +188,25 @@ public class AppMaster {
}
private void processCmd(JSONObject jsonObject) {
private void sendResult(long cid, int result, String action) {
String url = mMasterUrl;
try {
if (!url.endsWith("/")) {
url += "/";
}
url += "status/?";
url += MicroPhotoContext.MASTER_URL_CMDID + "=" + URLEncoder.encode(mCmdid, "UTF-8") +
"&cid=" + Long.toString(cid) +
"&res=" + Integer.toString(result) + "&act=" + URLEncoder.encode(action, "UTF-8");
HttpRequest.get(url);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void processCmd(long cid, JSONObject jsonObject) {
String cmd = jsonObject.optString("cmd", "");
long cid = jsonObject.optLong("cid", 0);
if (TextUtils.equals(cmd, CMD_REBOOT_DEV)) {
SysApi.reboot(mService.getApplicationContext());
@ -355,7 +373,7 @@ public class AppMaster {
return true;
}
private void upgradeApp(String url) {
private void upgradeApp(long cid, String action, String url) {
FileDownloader dl = new FileDownloader();
File path = new File(MicroPhotoContext.buildAppDir(mService.getApplicationContext()), "packages");
@ -369,6 +387,7 @@ public class AppMaster {
}
String apkPath = file.getAbsolutePath();
if (dl.download(url, apkPath)) {
sendResult(cid, 1, action);
Context context = mService.getApplicationContext();
SysApi.installApk(context, apkPath, context.getPackageName(), true);
}

Loading…
Cancel
Save