增加运维程序配置的实现

serial
Matthew 1 year ago
parent fd6e18ff26
commit 59535cbbff

@ -7,6 +7,7 @@ import android.content.pm.PackageManager;
import android.os.Environment; import android.os.Environment;
import android.text.TextUtils; import android.text.TextUtils;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -27,6 +28,9 @@ public class MicroPhotoContext {
public final static String DEFAULT_MASTER_URL = "http://180.166.218.222:40101/"; public final static String DEFAULT_MASTER_URL = "http://180.166.218.222:40101/";
public final static String MASTER_URL_CMDID = "cmdid"; public final static String MASTER_URL_CMDID = "cmdid";
public final static int DEFAULT_PROTOCOL = 0xFF00; public final static int DEFAULT_PROTOCOL = 0xFF00;
public final static int DEFAULT_HEARTBEAT_TIME_FOR_SEPARATE_NW = 575; // 9:35
public final static int DEFAULT_HEARTBEAT_FOR_SHARED_NW = 10; // minutes
public static class AppConfig { public static class AppConfig {
public String cmdid; public String cmdid;
@ -41,10 +45,18 @@ public class MicroPhotoContext {
} }
public static class MasterConfig { public static class MasterConfig {
public String url; public String server;
public int port;
public int mntnMode; public int mntnMode;
public int quickHbMode; public int quickHbMode;
public int heartbeatDuration; public int usingAbsHbTime;
public int heartbeat; // Unit minute
public int[] absHeartbeats; // second
public int separateNetwork;
public String getUrl() {
return "http://" + server + ":" + Integer.toString(port);
}
} }
public static String readTextFile(String path) { public static String readTextFile(String path) {
@ -180,9 +192,24 @@ public class MicroPhotoContext {
String content = readTextFile(appPath + "data/Master.json"); String content = readTextFile(appPath + "data/Master.json");
JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
masterConfig.url = jsonObject.optString("url", ""); masterConfig.server = jsonObject.optString("server", "");
masterConfig.port = jsonObject.optInt("port", 80);
masterConfig.mntnMode = jsonObject.optInt("mntnMode", 0); masterConfig.mntnMode = jsonObject.optInt("mntnMode", 0);
masterConfig.quickHbMode = jsonObject.optInt("quickHbMode", 0); masterConfig.quickHbMode = jsonObject.optInt("quickHbMode", 0);
masterConfig.usingAbsHbTime = jsonObject.optInt("usingAbsHbTime", 0);
masterConfig.separateNetwork = jsonObject.optInt("separateNetwork", 0);
// long defaultHearbeat = masterConfig.separateNetwork == 0 ? DEFAULT_HEARTBEAT_FOR_SHARED_NW : DEFAULT_HEARTBEAT_FOR_SEPARATE_NW;
masterConfig.heartbeat = jsonObject.optInt("heartbeat", DEFAULT_HEARTBEAT_FOR_SHARED_NW);
if (jsonObject.has("absHeartbeats")) {
JSONArray jsonHbs = jsonObject.getJSONArray("absHeartbeats");
masterConfig.absHeartbeats = new int[jsonHbs.length()];
for (int idx = 0; idx < jsonHbs.length(); idx++) {
masterConfig.absHeartbeats[idx] = jsonHbs.optInt(idx, 0);
}
} else {
masterConfig.absHeartbeats = new int[1];
masterConfig.absHeartbeats[0] = DEFAULT_HEARTBEAT_TIME_FOR_SEPARATE_NW;
}
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -203,14 +230,29 @@ public class MicroPhotoContext {
dataPath.mkdirs(); dataPath.mkdirs();
} }
String content = readTextFile(appPath + "data/Master.json"); String path = appPath + "data/Master.json";
String content = readTextFile(path);
JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
jsonObject.put("url", masterConfig.url); jsonObject.put("server", masterConfig.server);
jsonObject.put("Server", masterConfig.mntnMode); jsonObject.put("port", masterConfig.port);
jsonObject.put("Port", masterConfig.quickHbMode); jsonObject.put("mntnMode", masterConfig.mntnMode);
jsonObject.put("quickHbMode", masterConfig.quickHbMode);
jsonObject.put("heartbeat", masterConfig.heartbeat);
jsonObject.put("usingAbsHbTime", masterConfig.usingAbsHbTime);
jsonObject.put("separateNetwork", masterConfig.separateNetwork);
JSONArray jsonHbTimes = new JSONArray();
if (masterConfig.absHeartbeats != null) {
for (long item : masterConfig.absHeartbeats) {
jsonHbTimes.put(jsonHbTimes.length(), item);
}
}
jsonObject.put("absHeartbeats", jsonHbTimes);
fos = new FileOutputStream(new File(appPath + "data/App.json")); fos = new FileOutputStream(new File(path));
outputStreamWriter = new OutputStreamWriter(fos, "UTF-8"); outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
outputStreamWriter.write(jsonObject.toString()); outputStreamWriter.write(jsonObject.toString());
} catch (IOException e) { } catch (IOException e) {

@ -0,0 +1,143 @@
package com.xypower.common;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class ThermalInfoUtil {
public static List<String> getThermalInfo() {
/*
if (readFile("/sys/devices/virtual/thermal/thermal_zone0/temp", data) && !data.empty())
{
data.push_back(0);
int temp = atoi((const char*)(&data[0]));
return std::to_string(temp / 1000);
}
*/
List<String> result = new ArrayList<>();
BufferedReader br = null;
try {
File dir = new File("/sys/devices/virtual/thermal/");
File[] files = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
if (Pattern.matches("thermal_zone[0-9]+", file.getName())) {
return true;
}
return false;
}
});
final int SIZE = files.length;
String line = null;
String type = null;
String temp = null;
for (int i = 0; i < SIZE; i++) {
br = new BufferedReader(new FileReader("/sys/class/thermal/thermal_zone" + i + "/type"));
line = br.readLine();
if (line != null) {
type = line;
}
br = new BufferedReader(new FileReader("/sys/class/thermal/thermal_zone" + i + "/temp"));
line = br.readLine();
if (line != null) {
long temperature = Long.parseLong(line);
if (temperature < 0) {
temp = "Unknow";
} else {
temp = (float) (temperature / 1000.0) + "°C";
}
}
result.add(type + " : " + temp);
}
br.close();
} catch (FileNotFoundException e) {
result.add(e.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
public static String getCPUTermperature() {
/*
if (readFile("/sys/devices/virtual/thermal/thermal_zone0/temp", data) && !data.empty())
{
data.push_back(0);
int temp = atoi((const char*)(&data[0]));
return std::to_string(temp / 1000);
}
*/
File file = new File("/sys/class/thermal/thermal_zone0/temp");
if (!file.exists()) {
return "";
}
FileReader fr = null;
BufferedReader br = null;
String temp = "";
String line;
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
line = br.readLine();
if (line != null) {
long temperature = Long.parseLong(line);
if (temperature < 0) {
temp = "";
} else {
temp = Float.toString((float) (temperature / 1000.0));
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return temp;
}
}
Loading…
Cancel
Save