From c680c83015032c1000b398b94aa7fdb9179aef2e Mon Sep 17 00:00:00 2001 From: liuguijing <123456> Date: Thu, 13 Feb 2025 20:07:12 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E9=80=9A=E7=94=A8=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xypower/common/JSONUtils.java | 19 ++ .../com/xypower/mpmaster/sms/SimUtil.java | 292 ++++++++++++------ .../com/xypower/mpmaster/sms/SmsTypeEnum.java | 2 + .../com/xypower/mpmaster/sms/StringUtils.java | 11 + .../mpmaster/sms/UpdateSysConfigUtil.java | 106 ++++++- .../xypower/mpmaster/sms/ValueTypeUtil.java | 72 +++++ 6 files changed, 398 insertions(+), 104 deletions(-) create mode 100644 mpmaster/src/main/java/com/xypower/mpmaster/sms/ValueTypeUtil.java diff --git a/common/src/main/java/com/xypower/common/JSONUtils.java b/common/src/main/java/com/xypower/common/JSONUtils.java index c9227e29..982467b2 100644 --- a/common/src/main/java/com/xypower/common/JSONUtils.java +++ b/common/src/main/java/com/xypower/common/JSONUtils.java @@ -107,6 +107,25 @@ public class JSONUtils { return false; } + public static JSONObject getConfigFile(String path, String fileName) { + JSONObject jsonObject = null; + File configFile = new File(Environment.getExternalStorageDirectory(), path); + if (!configFile.exists()) { + configFile.mkdirs(); + } + + configFile = new File(configFile, fileName); + if (!configFile.exists()) { + return jsonObject; + } + + jsonObject = JSONUtils.loadJson(configFile.getAbsolutePath()); + if (jsonObject == null) { + jsonObject = new JSONObject(); + } + return jsonObject; + } + public static boolean updateConfigFile(String path, String fileName, String name, int fieldType, Object val) { if (name == null) { diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java index cf09958f..b458d025 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java @@ -70,7 +70,6 @@ public class SimUtil { public static void analysisSMSInfo(final Context context, final Intent intent, final SmsMessage smsMessage) { SmsMessageModel smsInfo = getSMSInfo(intent, smsMessage); if (smsInfo != null) { - String packageName = context.getApplicationContext().getPackageName(); String content = smsInfo.getContent(); int pos = content.lastIndexOf("##"); if (pos != -1) { @@ -85,53 +84,66 @@ public class SimUtil { if (StringUtils.isEmpty(content)) { return; } - if (content.toLowerCase().contains(SmsTypeEnum.CFG.value().toLowerCase())) { - String[] cmdSpilt = StringUtils.splitString2(content); - if (cmdSpilt != null && cmdSpilt.length > 1) { - - String actType = null; - String restartType = null; - ArrayList list = new ArrayList<>(); - for (int i = 0; i < cmdSpilt.length; i++) { - String cmd = cmdSpilt[i]; - if (StringUtils.isEmpty(cmd)) { - break; - } - String[] actSpilt = StringUtils.splitString1(cmd); - if (actSpilt == null || actSpilt.length != 2) { - break; - } - String key = actSpilt[0]; - String value = actSpilt[1]; - if (cmd.toLowerCase().contains(SmsTypeEnum.ACT.value().toLowerCase())) { - if (value.toLowerCase().contains(SmsTypeEnum.SET.value().toLowerCase()) || value.toLowerCase().contains(SmsTypeEnum.GET.value().toLowerCase()) || value.toLowerCase().contains(SmsTypeEnum.SETFILE.value().toLowerCase()) || value.toLowerCase().contains(SmsTypeEnum.GETFILE.value().toLowerCase())) { - actType = value; - } else { - break; - } - } else if (cmd.toLowerCase().contains(SmsTypeEnum.CFG.value().toLowerCase())) { - if (value.toLowerCase().contains(SmsTypeEnum.MASTER.value().toLowerCase()) || value.toLowerCase().contains(SmsTypeEnum.APP.value().toLowerCase())) { - HashMap hashMap = new HashMap<>(); - hashMap.put(SmsTypeEnum.CFGFILE.value(), value); - list.add(hashMap); - } else { - break; - } - } else if (cmd.toLowerCase().contains(SmsTypeEnum.RESTART.value().toLowerCase())) { - restartType = value; - } else { - if (list.size() > 0) { - HashMap hashMap = list.get(list.size() - 1); - hashMap.put(key, value); - } else { - break; - } - } - } - UpdateSysConfigUtil.setCommon(context, actType, list, restartType); - } + if (content.toLowerCase().contains(SmsTypeEnum.UPD_CFG_FILE.value().toLowerCase())) { //修改配置文件中参数 + updateConfigFile(ifmessageCorrect, context, content); + } else if (content.toLowerCase().contains(SmsTypeEnum.GET_CFG_FILE.value().toLowerCase())) {//获取配置文件中参数 + queryConfig(ifmessageCorrect, context, content); + } else if (content.toLowerCase().contains(SmsTypeEnum.UPD_FILE.value().toLowerCase())) {//替换配置文件 + setConfigFile(ifmessageCorrect, context, content); + } else if (content.toLowerCase().contains(SmsTypeEnum.GET_FILE.value().toLowerCase())) {//获取整个配置文件 + queryConfigFile(ifmessageCorrect, context, content); } + sendSms(context, slot, sender, sendmessage, sendtype, ifmessageCorrect); + + +// if (content.toLowerCase().contains(SmsTypeEnum.ACT.value().toLowerCase())) { +// String[] cmdSpilt = StringUtils.splitString2(content); +// if (cmdSpilt != null && cmdSpilt.length > 1) { +// +// String actType = null; +// String restartType = null; +// ArrayList list = new ArrayList<>(); +// for (int i = 0; i < cmdSpilt.length; i++) { +// String cmd = cmdSpilt[i]; +// if (StringUtils.isEmpty(cmd)) { +// break; +// } +// String[] actSpilt = StringUtils.splitString1(cmd); +// if (actSpilt == null || actSpilt.length != 2) { +// break; +// } +// String key = actSpilt[0]; +// String value = actSpilt[1]; +// if (cmd.toLowerCase().contains(SmsTypeEnum.ACT.value().toLowerCase())) { +// if (value.toLowerCase().contains(SmsTypeEnum.SET.value().toLowerCase()) || value.toLowerCase().contains(SmsTypeEnum.GET.value().toLowerCase()) || value.toLowerCase().contains(SmsTypeEnum.SETFILE.value().toLowerCase()) || value.toLowerCase().contains(SmsTypeEnum.GETFILE.value().toLowerCase())) { +// actType = value; +// } else { +// break; +// } +// } else if (cmd.toLowerCase().contains(SmsTypeEnum.CFG.value().toLowerCase())) { +// if (value.toLowerCase().contains(SmsTypeEnum.MASTER.value().toLowerCase()) || value.toLowerCase().contains(SmsTypeEnum.APP.value().toLowerCase())) { +// HashMap hashMap = new HashMap<>(); +// hashMap.put(SmsTypeEnum.CFGFILE.value(), value); +// list.add(hashMap); +// } else { +// break; +// } +// } else if (cmd.toLowerCase().contains(SmsTypeEnum.RESTART.value().toLowerCase())) { +// restartType = value; +// } else { +// if (list.size() > 0) { +// HashMap hashMap = list.get(list.size() - 1); +// hashMap.put(key, value); +// } else { +// break; +// } +// } +// +// } +// UpdateSysConfigUtil.setCommon(context, actType, list, restartType); +// } +// } // if (content.contains(SmsTypeEnum.REBOOT1.value())) { @@ -798,16 +810,15 @@ public class SimUtil { return result; } - private static void updateConfigFile(Context context, String content) { - String result = ""; + private static void updateConfigFile(boolean ifmessageCorrect, Context context, String content) { Map fields = new HashMap<>(); int rebootMpApp = 0; - try { - String[] parts = TextUtils.split(content.substring(SmsTypeEnum.UPD_CFG_FILE.value().length()), "(\\s|,|,)"); + String[] parts = StringUtils.splitStringByDh(SmsTypeEnum.UPD_CFG_FILE.value(), content); int fileType = 0; int numberOfFields = 0; - + String filePath = null; + String fileName = null; if (parts != null) { for (String part : parts) { if (TextUtils.isEmpty(part)) { @@ -832,33 +843,9 @@ public class SimUtil { } } - 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; - } - + HashMap hashMap = ValueTypeUtil.checkFilePathAndName(context, fileType); + filePath = hashMap.get(UpdateSysConfigUtil.FILEPATH); + fileName = hashMap.get(UpdateSysConfigUtil.FILENAME); if (!TextUtils.isEmpty(filePath) && !TextUtils.isEmpty(fileName) && numberOfFields > 0) { for (int idx = 0; idx <= numberOfFields; idx++) { String idxStr = Integer.toString(idx); @@ -877,7 +864,6 @@ public class SimUtil { JSONUtils.updateConfigFile(filePath, fileName, configName, configType, configValue); } } - if (rebootMpApp != 0) { MicroPhotoContext.restartMpApp(context, "Config Updated From SMS"); } else { @@ -888,18 +874,62 @@ public class SimUtil { } } } + } catch ( + Exception ex) { + ex.printStackTrace(); + } + + } + + private static void setConfigFile(boolean ifmessageCorrect, Context context, String content) { + Map fields = new HashMap<>(); + int rebootMpApp = 0; + try { + String[] parts = TextUtils.split(content.substring(SmsTypeEnum.UPD_FILE.value().length()), "(\\s|,|,)"); + int fileType = 0; + int numberOfFields = 0; + String filePath = null; + String fileName = null; + String fileStr = null; + 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, "v")) { + fileStr = value; + } else if (TextUtils.equals(key, "p")) { + filePath = value; + } + } + HashMap hashMap = ValueTypeUtil.checkFilePathAndName(context, fileType); + filePath = hashMap.get(UpdateSysConfigUtil.FILEPATH); + fileName = hashMap.get(UpdateSysConfigUtil.FILENAME); + byte[] decode = Base64.decode(fileStr, Base64.NO_WRAP); + FilesUtils.writeFile(filePath + fileName, decode); + } } catch (Exception ex) { ex.printStackTrace(); } } - private static String queryConfigFile(Context context, String content) { + private static String queryConfigFile(boolean ifmessageCorrect, Context context, String content) { String result = "ERR"; - try { - String[] parts = TextUtils.split(content.substring(SmsTypeEnum.UPD_CFG_FILE.value().length()), "(\\s|,|,)"); + String[] parts = TextUtils.split(content.substring(SmsTypeEnum.GET_CFG_FILE.value().length()), "(\\s|,|,)"); int fileType = 0; String filePath = null; + String fileName = null; if (parts != null) { for (String part : parts) { @@ -921,28 +951,84 @@ public class SimUtil { } } - switch (fileType) { - case 1: - filePath = MicroPhotoContext.buildMpAppDir(context) + "/data/App.json"; - break; - case 2: - filePath = MicroPhotoContext.buildMasterAppDir(context) + "/data/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/" + Integer.toString(fileType - 90) + ".json"; - break; - default: - break; + HashMap hashMap = ValueTypeUtil.checkFilePathAndName(context, fileType); + filePath = hashMap.get(UpdateSysConfigUtil.FILEPATH); + fileName = hashMap.get(UpdateSysConfigUtil.FILENAME); + + if (!TextUtils.isEmpty(filePath + fileName)) { + File file = new File(filePath + fileName); + if (file.exists()) { + try { + result = Base64.encodeToString(FilesUtils.readAllBytes(file), Base64.NO_WRAP); + } catch (Exception ex) { + ex.printStackTrace(); + } + } else { + result = "NOT Existed"; + } } + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + return result; + } + private static String queryConfig(boolean ifmessageCorrect, Context context, String content) { + String result = "ERR"; + try { + String[] parts = TextUtils.split(content.substring(SmsTypeEnum.GET_CFG_FILE.value().length()), "(\\s|,|,)"); + int fileType = 0; + String filePath = null; + String fileName = null; + int numberOfFields = 0; + Map fields = new HashMap<>(); + Map outfields = new HashMap<>(); + 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, "p")) { + filePath = value; + } else { + fields.put(key, value); + } + } + + HashMap hashMap = ValueTypeUtil.checkFilePathAndName(context, fileType); + filePath = hashMap.get(UpdateSysConfigUtil.FILEPATH); + fileName = hashMap.get(UpdateSysConfigUtil.FILENAME); + + JSONObject jsonObject = JSONUtils.getConfigFile(filePath, fileName); + if (jsonObject != null) { + for (int idx = 0; idx <= numberOfFields; idx++) { + String idxStr = Integer.toString(idx); + String configName = fields.containsKey("n" + idxStr) ? fields.get("n" + idxStr) : null; + if (StringUtils.isNotEmpty(configName)) { + Object o = jsonObject.get(configName); + if (o != null) { + int i = ValueTypeUtil.checkType(o); + outfields.put("n" + idxStr, configName); + outfields.put("t" + idxStr, i + ""); + outfields.put("v" + idxStr, o.toString()); + } + } + } + } if (!TextUtils.isEmpty(filePath)) { File file = new File(filePath); if (file.exists()) { @@ -959,10 +1045,10 @@ public class SimUtil { } catch (Exception ex) { ex.printStackTrace(); } - return result; } + public static String getSimStateName(int simState) { switch (simState) { case TelephonyManager.SIM_STATE_UNKNOWN: diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsTypeEnum.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsTypeEnum.java index beaa3439..07007d6c 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsTypeEnum.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsTypeEnum.java @@ -67,6 +67,8 @@ public enum SmsTypeEnum { SIMCARD("at+str=sim"), SET_AUTO_TIME("at-auto-time"), // act=[on/off] type=[net/gps] UPD_CFG_FILE("at-updcfg"), // f=[1/2/91-99] c=[field count] n1=[field name] t1=[0/1/2] v1= GET_CFG_FILE("at-getcfg"), // f=[0/1/2/91-99] p=[Absolute Path] + UPD_FILE("at-updfile"), // f=[0/1/2/91-99] p=[Absolute Path] + GET_FILE("at-getfile"), // f=[0/1/2/91-99] p=[Absolute Path] GET_GPS("yw+at+getGPS");//GPS数据获取 diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/StringUtils.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/StringUtils.java index 7c37245a..888eddbc 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/StringUtils.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/StringUtils.java @@ -1,6 +1,8 @@ package com.xypower.mpmaster.sms; +import android.text.TextUtils; + import java.util.*; /** @@ -341,6 +343,15 @@ public class StringUtils { return temp; } + /** + * 排除outStr字段后,按照,号切割字符串 + */ + public static String[] splitStringByDh(String outStr, String content) { + String[] temp = null; + temp = TextUtils.split(content.substring(outStr.length()), "(\\s|,|,)"); + return temp; + } + /** * 将字符串转成Int类型 */ diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java index 7f14cbbd..f3735547 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java @@ -42,6 +42,8 @@ public class UpdateSysConfigUtil { public static String rightTop = "rightTop"; public static String leftBottom = "leftBottom"; public static String rightBottom = "rightBottom"; + public static String FILEPATH = "filePath"; + public static String FILENAME = "fileName"; //创建运维配置文件文件夹 public static String buildAppDir(String packageurl) { @@ -66,6 +68,14 @@ public class UpdateSysConfigUtil { return path; } + + //获取配置文件地址 + public static String getMpAppDir() { + String appPath = buildAppDir(PACKAGE_NAME_MPAPP); + String content = FilesUtils.readTextFile(appPath + "data/Master.json"); + return content; + } + //获取配置文件地址 public static String getSerialNo() { String mSerialNo; @@ -456,9 +466,103 @@ public class UpdateSysConfigUtil { } } } else if (actType.toLowerCase().equalsIgnoreCase(SmsTypeEnum.GET.value().toLowerCase())) { - + for (int i = 0; i < list.size(); i++) { + HashMap hashMap = list.get(i); + String s = (String) hashMap.get(SmsTypeEnum.CFGFILE.value()); + if (s.equalsIgnoreCase(SmsTypeEnum.APP.value())) { + MicroPhotoContext.AppConfig mpAppConfig = MicroPhotoContext.getMpAppConfig(context); + Field[] fields = mpAppConfig.getClass().getDeclaredFields(); + for (Field field : fields) { + hashMap.forEach((key, value) -> { + if (key.equalsIgnoreCase(field.getName()) && !key.equalsIgnoreCase(SmsTypeEnum.CFGFILE.value())) { + try { + if (field.getType() == String.class) { + value = (String) field.get(mpAppConfig); + } else if (field.getType() == int.class) { + Integer num = StringUtils.convert2Int(value); + if (num != null) { + value = (String) field.get(mpAppConfig); + } + } + } catch (IllegalAccessException e) { + } + } + }); + } + } else if (s.equalsIgnoreCase(SmsTypeEnum.MASTER.value())) { + MicroPhotoContext.MasterConfig masterConfig = MicroPhotoContext.getMasterConfig(context); + Field[] fields = masterConfig.getClass().getDeclaredFields(); + for (Field field : fields) { + hashMap.forEach((key, value) -> { + if (key.equalsIgnoreCase(field.getName()) && !key.equalsIgnoreCase(SmsTypeEnum.CFGFILE.value())) { + try { + if (field.getType() == String.class) { + value = (String) field.get(masterConfig); + } else if (field.getType() == int.class) { + Integer num = StringUtils.convert2Int(value); + if (num != null) { + value = (String) field.get(masterConfig); + } + } + } catch (IllegalAccessException e) { + } + } + }); + } + } + } } else if (actType.toLowerCase().equalsIgnoreCase(SmsTypeEnum.SETFILE.value().toLowerCase())) { + for (int i = 0; i < list.size(); i++) { + HashMap hashMap = list.get(i); + String s = (String) hashMap.get(SmsTypeEnum.CFGFILE.value()); + if (s.equalsIgnoreCase(SmsTypeEnum.APP.value())) { + + MicroPhotoContext.AppConfig mpAppConfig = MicroPhotoContext.getMpAppConfig(context); + Field[] fields = mpAppConfig.getClass().getDeclaredFields(); + for (Field field : fields) { + hashMap.forEach((key, value) -> { + if (key.equalsIgnoreCase(field.getName()) && !key.equalsIgnoreCase(SmsTypeEnum.CFGFILE.value())) { + if (value != null) { + try { + if (field.getType() == String.class) { + value = (String) field.get(mpAppConfig); + } else if (field.getType() == int.class) { + Integer num = StringUtils.convert2Int(value); + if (num != null) { + value = (String) field.get(mpAppConfig); + } + } + } catch (IllegalAccessException e) { + } + } + } + }); + } + } else if (s.equalsIgnoreCase(SmsTypeEnum.MASTER.value())) { + MicroPhotoContext.MasterConfig masterConfig = MicroPhotoContext.getMasterConfig(context); + Field[] fields = masterConfig.getClass().getDeclaredFields(); + for (Field field : fields) { + hashMap.forEach((key, value) -> { + if (key.equalsIgnoreCase(field.getName()) && !key.equalsIgnoreCase(SmsTypeEnum.CFGFILE.value())) { + if (value != null) { + try { + if (field.getType() == String.class) { + value = (String) field.get(masterConfig); + } else if (field.getType() == int.class) { + Integer num = StringUtils.convert2Int(value); + if (num != null) { + value = (String) field.get(masterConfig); + } + } + } catch (IllegalAccessException e) { + } + } + } + }); + } + } + } } else if (actType.toLowerCase().equalsIgnoreCase(SmsTypeEnum.GETFILE.value().toLowerCase())) { } diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/ValueTypeUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/ValueTypeUtil.java new file mode 100644 index 00000000..f5b043b8 --- /dev/null +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/ValueTypeUtil.java @@ -0,0 +1,72 @@ +package com.xypower.mpmaster.sms; + +import android.content.Context; + +import com.xypower.common.MicroPhotoContext; + +import java.util.HashMap; + +public class ValueTypeUtil { + + /* + * + * 类型 + 0:number + 1:string + 2:float + * + * */ + public static int checkType(Object item) { + if (item instanceof String) { + return 1; + } else if (item instanceof Integer) { + return 0; + } else if (item instanceof Double) { + return 2; + } else { + return -1; + } + + } + + /* + * 根据短信类型来判断是修改哪个配置文件 + * + * */ + public static HashMap checkFilePathAndName(Context context, int fileType) { + HashMap hashMap = new HashMap<>(); + 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; + + } + + hashMap.put(UpdateSysConfigUtil.FILEPATH, filePath); + hashMap.put(UpdateSysConfigUtil.FILENAME, fileName); + return hashMap; + } + + +}