实现通过短信修改配置的通用机制

master
Matthew 10 months ago
parent 1581663e7a
commit f68567d21d

@ -31,6 +31,7 @@ public class MicroPhotoContext {
public static final String PACKAGE_NAME_MPMASTER = "com.xypower.mpmaster"; public static final String PACKAGE_NAME_MPMASTER = "com.xypower.mpmaster";
public static final String ACTION_HEARTBEAT_MP = "com.xypower.mpapp.ACT_HB"; public static final String ACTION_HEARTBEAT_MP = "com.xypower.mpapp.ACT_HB";
public static final String ACTION_RESTART_MP = "com.xypower.mpapp.ACT_RESTART"; public static final String ACTION_RESTART_MP = "com.xypower.mpapp.ACT_RESTART";
public static final String ACTION_UPDATE_CONFIGS_MP = "com.xypower.mpapp.ACT_UPD_CFG";
public final static String DEFAULT_MASTER_SERVER = "61.169.135.150"; public final static String DEFAULT_MASTER_SERVER = "61.169.135.150";
public final static int DEFAULT_MASTER_PORT = 40101; public final static int DEFAULT_MASTER_PORT = 40101;

@ -1032,7 +1032,7 @@ public class MpMasterService extends Service {
public void reloadMpAppConfigs() { public void reloadMpAppConfigs() {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setAction(ACTION_UPDATE_CONFIGS); intent.setAction(MicroPhotoContext.ACTION_UPDATE_CONFIGS_MP);
intent.setPackage(MicroPhotoContext.PACKAGE_NAME_MPAPP); intent.setPackage(MicroPhotoContext.PACKAGE_NAME_MPAPP);
sendBroadcast(intent); sendBroadcast(intent);
} }

@ -28,11 +28,14 @@ import androidx.core.app.ActivityCompat;
import com.dev.devapi.api.SysApi; import com.dev.devapi.api.SysApi;
import com.xypower.common.FilesUtils; import com.xypower.common.FilesUtils;
import com.xypower.common.JSONUtils;
import com.xypower.common.MicroPhotoContext; import com.xypower.common.MicroPhotoContext;
import com.xypower.common.NetworkUtils; import com.xypower.common.NetworkUtils;
import com.xypower.common.RegexUtil; import com.xypower.common.RegexUtil;
import com.xypower.mpmaster.MpMasterService; import com.xypower.mpmaster.MpMasterService;
import org.json.JSONObject;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -40,6 +43,7 @@ import java.util.Base64;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -454,9 +458,14 @@ public class SimUtil {
ifmessageCorrect = true; ifmessageCorrect = true;
sendmessage = getSimcardInfo(context); sendmessage = getSimcardInfo(context);
} else if (content.contains(SmsTypeEnum.SET_AUTO_TIME.value())) { } else if (content.contains(SmsTypeEnum.SET_AUTO_TIME.value())) {
sendtype = SmsTypeEnum.SIMCARD.value(); sendtype = SmsTypeEnum.SET_AUTO_TIME.value();
ifmessageCorrect = true; ifmessageCorrect = true;
sendmessage = setAutoTime(context, content); sendmessage = setAutoTime(context, content);
} else if (content.contains(SmsTypeEnum.UPD_CFG_FILE.value())) {
sendtype = SmsTypeEnum.UPD_CFG_FILE.value();
ifmessageCorrect = true;
sendmessage = " OK";
updateConfigFile(context, content);
} else if (content.contains(SmsTypeEnum.SET_TP.value())) { } else if (content.contains(SmsTypeEnum.SET_TP.value())) {
sendtype = SmsTypeEnum.SET_TP.value(); sendtype = SmsTypeEnum.SET_TP.value();
String[] split1 = StringUtils.splitString1(content); String[] split1 = StringUtils.splitString1(content);
@ -686,6 +695,101 @@ public class SimUtil {
return result; return result;
} }
private static void updateConfigFile(Context context, String content) {
String result = "";
Map<String, String> fields = new HashMap<>();
int rebootMpApp = 0;
try {
String[] parts = TextUtils.split(content.substring(SmsTypeEnum.UPD_CFG_FILE.value().length()), "(\\s|,|)");
int fileType = 0;
int numberOfFields = 0;
if (parts != null) {
for (String part : parts) {
if (TextUtils.isEmpty(part)) {
continue;
}
int pos = part.indexOf("=");
if (pos == -1) {
continue;
}
String key = part.substring(0, pos);
String value = part.substring(pos + 1);
if (TextUtils.equals(key, "f")) {
fileType = Integer.parseInt(value);
} else if (TextUtils.equals(key, "c")) {
numberOfFields = Integer.parseInt(value);
} else if (TextUtils.equals(key, "r")) {
rebootMpApp = Integer.parseInt(value);
} else {
fields.put(key, value);
}
}
String filePath = null;
String fileName = null;
switch (fileType) {
case 1:
filePath = MicroPhotoContext.buildMpAppDir(context) + "/data/";
fileName = "App.json";
break;
case 2:
filePath = MicroPhotoContext.buildMasterAppDir(context) + "/data/";
fileName = "Master.json";
break;
case 91:
case 92:
case 93:
case 94:
case 95:
case 96:
case 97:
case 98:
case 99:
filePath = MicroPhotoContext.buildMpAppDir(context) + "/data/channels/";
fileName = Integer.toString(fileType - 90) + ".json";
break;
default:
break;
}
if (!TextUtils.isEmpty(filePath) && !TextUtils.isEmpty(fileName) && numberOfFields > 0) {
for (int idx = 0; idx <= numberOfFields; idx++) {
String idxStr = Integer.toString(idx);
// JSONObject jsonConfig = jsonConfigs.getJSONObject(idx);
String configName = fields.containsKey("n" + idxStr) ? fields.get("n" + idxStr) : null;
int configType = fields.containsKey("t" + idxStr) ? Integer.parseInt(fields.get("t" + idxStr)) : 0;
String configValue = fields.containsKey("v" + idxStr) ? fields.get("v" + idxStr) : null;
if (configType == 0) { // Number
Long val = Long.parseLong(configValue);
JSONUtils.updateConfigFile(filePath, fileName, configName, configType, val);
} else if (configType == 2) { // Float
Float val = Float.parseFloat(configValue);
JSONUtils.updateConfigFile(filePath, fileName, configName, configType, val);
} else {
JSONUtils.updateConfigFile(filePath, fileName, configName, configType, configValue);
}
}
if (rebootMpApp != 0) {
MicroPhotoContext.restartMpApp(context);
} else {
Intent intent = new Intent();
intent.setAction(MicroPhotoContext.ACTION_UPDATE_CONFIGS_MP);
intent.setPackage(MicroPhotoContext.PACKAGE_NAME_MPAPP);
context.sendBroadcast(intent);
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static String getSimStateName(int simState) { public static String getSimStateName(int simState) {
switch (simState) { switch (simState) {
case TelephonyManager.SIM_STATE_UNKNOWN: case TelephonyManager.SIM_STATE_UNKNOWN:

@ -46,6 +46,7 @@ public enum SmsTypeEnum {
RESTORE("yw+at+Restore"), //恢复出厂设置 RESTORE("yw+at+Restore"), //恢复出厂设置
SIMCARD("at+str=sim"), SIMCARD("at+str=sim"),
SET_AUTO_TIME("at-auto-time"), // act=[on/off] type=[net/gps] SET_AUTO_TIME("at-auto-time"), // act=[on/off] type=[net/gps]
UPD_CFG_FILE("at-updcfg"), // f=[1/2/3] c=[field count] n1=[field name] t1=[0/1/2] v1=
GET_GPS("yw+at+getGPS");//GPS数据获取 GET_GPS("yw+at+getGPS");//GPS数据获取

Loading…
Cancel
Save