短信通用版本修改

streaming
liuguijing 4 months ago
parent c91c97e5d0
commit c680c83015

@ -107,6 +107,25 @@ public class JSONUtils {
return false; 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) { public static boolean updateConfigFile(String path, String fileName, String name, int fieldType, Object val) {
if (name == null) { if (name == null) {

@ -70,7 +70,6 @@ public class SimUtil {
public static void analysisSMSInfo(final Context context, final Intent intent, final SmsMessage smsMessage) { public static void analysisSMSInfo(final Context context, final Intent intent, final SmsMessage smsMessage) {
SmsMessageModel smsInfo = getSMSInfo(intent, smsMessage); SmsMessageModel smsInfo = getSMSInfo(intent, smsMessage);
if (smsInfo != null) { if (smsInfo != null) {
String packageName = context.getApplicationContext().getPackageName();
String content = smsInfo.getContent(); String content = smsInfo.getContent();
int pos = content.lastIndexOf("##"); int pos = content.lastIndexOf("##");
if (pos != -1) { if (pos != -1) {
@ -85,53 +84,66 @@ public class SimUtil {
if (StringUtils.isEmpty(content)) { if (StringUtils.isEmpty(content)) {
return; 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<HashMap> 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<Object, Object> 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;
}
}
} if (content.toLowerCase().contains(SmsTypeEnum.UPD_CFG_FILE.value().toLowerCase())) { //修改配置文件中参数
UpdateSysConfigUtil.setCommon(context, actType, list, restartType); 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<HashMap> 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<Object, Object> 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())) { // if (content.contains(SmsTypeEnum.REBOOT1.value())) {
@ -798,16 +810,15 @@ public class SimUtil {
return result; return result;
} }
private static void updateConfigFile(Context context, String content) { private static void updateConfigFile(boolean ifmessageCorrect, Context context, String content) {
String result = "";
Map<String, String> fields = new HashMap<>(); Map<String, String> fields = new HashMap<>();
int rebootMpApp = 0; int rebootMpApp = 0;
try { 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 fileType = 0;
int numberOfFields = 0; int numberOfFields = 0;
String filePath = null;
String fileName = null;
if (parts != null) { if (parts != null) {
for (String part : parts) { for (String part : parts) {
if (TextUtils.isEmpty(part)) { if (TextUtils.isEmpty(part)) {
@ -832,33 +843,9 @@ public class SimUtil {
} }
} }
String filePath = null; HashMap<String, String> hashMap = ValueTypeUtil.checkFilePathAndName(context, fileType);
String fileName = null; filePath = hashMap.get(UpdateSysConfigUtil.FILEPATH);
switch (fileType) { fileName = hashMap.get(UpdateSysConfigUtil.FILENAME);
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) { if (!TextUtils.isEmpty(filePath) && !TextUtils.isEmpty(fileName) && numberOfFields > 0) {
for (int idx = 0; idx <= numberOfFields; idx++) { for (int idx = 0; idx <= numberOfFields; idx++) {
String idxStr = Integer.toString(idx); String idxStr = Integer.toString(idx);
@ -877,7 +864,6 @@ public class SimUtil {
JSONUtils.updateConfigFile(filePath, fileName, configName, configType, configValue); JSONUtils.updateConfigFile(filePath, fileName, configName, configType, configValue);
} }
} }
if (rebootMpApp != 0) { if (rebootMpApp != 0) {
MicroPhotoContext.restartMpApp(context, "Config Updated From SMS"); MicroPhotoContext.restartMpApp(context, "Config Updated From SMS");
} else { } else {
@ -888,18 +874,62 @@ public class SimUtil {
} }
} }
} }
} catch (
Exception ex) {
ex.printStackTrace();
}
}
private static void setConfigFile(boolean ifmessageCorrect, Context context, String content) {
Map<String, String> 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<String, String> 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) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
private static String queryConfigFile(Context context, String content) { private static String queryConfigFile(boolean ifmessageCorrect, Context context, String content) {
String result = "ERR"; String result = "ERR";
try { 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; int fileType = 0;
String filePath = null; String filePath = null;
String fileName = null;
if (parts != null) { if (parts != null) {
for (String part : parts) { for (String part : parts) {
@ -921,28 +951,84 @@ public class SimUtil {
} }
} }
switch (fileType) { HashMap<String, String> hashMap = ValueTypeUtil.checkFilePathAndName(context, fileType);
case 1: filePath = hashMap.get(UpdateSysConfigUtil.FILEPATH);
filePath = MicroPhotoContext.buildMpAppDir(context) + "/data/App.json"; fileName = hashMap.get(UpdateSysConfigUtil.FILENAME);
break;
case 2: if (!TextUtils.isEmpty(filePath + fileName)) {
filePath = MicroPhotoContext.buildMasterAppDir(context) + "/data/Master.json"; File file = new File(filePath + fileName);
break; if (file.exists()) {
case 91: try {
case 92: result = Base64.encodeToString(FilesUtils.readAllBytes(file), Base64.NO_WRAP);
case 93: } catch (Exception ex) {
case 94: ex.printStackTrace();
case 95: }
case 96: } else {
case 97: result = "NOT Existed";
case 98: }
case 99:
filePath = MicroPhotoContext.buildMpAppDir(context) + "/data/channels/" + Integer.toString(fileType - 90) + ".json";
break;
default:
break;
} }
}
} 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<String, String> fields = new HashMap<>();
Map<String, String> 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<String, String> 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)) { if (!TextUtils.isEmpty(filePath)) {
File file = new File(filePath); File file = new File(filePath);
if (file.exists()) { if (file.exists()) {
@ -959,10 +1045,10 @@ public class SimUtil {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
return result; return result;
} }
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:

@ -67,6 +67,8 @@ public enum SmsTypeEnum {
SIMCARD("at+str=sim"), SET_AUTO_TIME("at-auto-time"), // act=[on/off] type=[net/gps] 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= 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] 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数据获取 GET_GPS("yw+at+getGPS");//GPS数据获取

@ -1,6 +1,8 @@
package com.xypower.mpmaster.sms; package com.xypower.mpmaster.sms;
import android.text.TextUtils;
import java.util.*; import java.util.*;
/** /**
@ -341,6 +343,15 @@ public class StringUtils {
return temp; 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 * Int
*/ */

@ -42,6 +42,8 @@ public class UpdateSysConfigUtil {
public static String rightTop = "rightTop"; public static String rightTop = "rightTop";
public static String leftBottom = "leftBottom"; public static String leftBottom = "leftBottom";
public static String rightBottom = "rightBottom"; public static String rightBottom = "rightBottom";
public static String FILEPATH = "filePath";
public static String FILENAME = "fileName";
//创建运维配置文件文件夹 //创建运维配置文件文件夹
public static String buildAppDir(String packageurl) { public static String buildAppDir(String packageurl) {
@ -66,6 +68,14 @@ public class UpdateSysConfigUtil {
return path; 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() { public static String getSerialNo() {
String mSerialNo; String mSerialNo;
@ -456,9 +466,103 @@ public class UpdateSysConfigUtil {
} }
} }
} else if (actType.toLowerCase().equalsIgnoreCase(SmsTypeEnum.GET.value().toLowerCase())) { } else if (actType.toLowerCase().equalsIgnoreCase(SmsTypeEnum.GET.value().toLowerCase())) {
for (int i = 0; i < list.size(); i++) {
HashMap<String, String> 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())) { } else if (actType.toLowerCase().equalsIgnoreCase(SmsTypeEnum.SETFILE.value().toLowerCase())) {
for (int i = 0; i < list.size(); i++) {
HashMap<String, String> 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())) { } else if (actType.toLowerCase().equalsIgnoreCase(SmsTypeEnum.GETFILE.value().toLowerCase())) {
} }

@ -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 {
/*
*
*
0number
1string
2float
*
* */
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<String, String> 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;
}
}
Loading…
Cancel
Save