|
@@ -15,11 +15,14 @@ import com.emato.ich.entity.vo.ResponseData;
|
|
|
import com.emato.ich.entity.vo.ShellVo;
|
|
|
import com.emato.ich.local.LocalStorage;
|
|
|
import com.emato.ich.message.ICHPublishClient;
|
|
|
+import com.emato.ich.message.ICHSubscribeClient;
|
|
|
import com.emato.ich.message.ICHTopic;
|
|
|
import com.emato.ich.update.APKUpdateDownload;
|
|
|
import com.emato.ich.update.OnDownloadListener;
|
|
|
import com.emato.ich.utils.BaseUtils;
|
|
|
import com.emato.ich.utils.JacksonUtils;
|
|
|
+import com.emato.ich.utils.LoggingUtils;
|
|
|
+import com.emato.ich.utils.ResourceUtils;
|
|
|
import com.emato.ich.utils.StringUtils;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
@@ -27,6 +30,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
|
|
+import android.provider.SyncStateContract;
|
|
|
import android.util.Log;
|
|
|
import android.view.KeyEvent;
|
|
|
|
|
@@ -37,6 +41,12 @@ import androidx.navigation.ui.NavigationUI;
|
|
|
|
|
|
import com.emato.ich.databinding.ActivityMainBinding;
|
|
|
import com.xuexiang.xupdate.XUpdate;
|
|
|
+import com.xuexiang.xupdate.aria.AriaDownloadServiceProxyImpl;
|
|
|
+import com.xuexiang.xupdate.aria.AriaDownloader;
|
|
|
+import com.xuexiang.xupdate.easy.EasyUpdate;
|
|
|
+import com.xuexiang.xupdate.easy.config.UpdateConfig;
|
|
|
+import com.xuexiang.xupdate.entity.UpdateEntity;
|
|
|
+import com.xuexiang.xupdate.proxy.impl.DefaultUpdateParser;
|
|
|
|
|
|
import android.view.Menu;
|
|
|
import android.view.MenuItem;
|
|
@@ -85,20 +95,21 @@ public class MainActivity extends AppCompatActivity {
|
|
|
getSystemConfig();
|
|
|
|
|
|
ICHPublishClient ichPublishClient = ICHPublishClient.getInstance();
|
|
|
+ ICHSubscribeClient ichSubscribeClient = ICHSubscribeClient.getInstance();
|
|
|
|
|
|
ICHTopic.CLIENT_ID = BaseUtils.getClientId();
|
|
|
// 订阅主题
|
|
|
// ICHSubscribeClient ichSubscribeClient = ICHSubscribeClient.getInstance();
|
|
|
- openLocker(ichPublishClient);
|
|
|
+ openLocker(ichSubscribeClient, ichPublishClient);
|
|
|
|
|
|
// 注册柜子信息
|
|
|
cabinetInfoReport(ichPublishClient);
|
|
|
|
|
|
// TODO 更新版本监听
|
|
|
- autoUpdateVersion(ichPublishClient);
|
|
|
+ autoUpdateVersion(ichSubscribeClient);
|
|
|
|
|
|
// TODO 监听shell命令脚本
|
|
|
- executeShell(ichPublishClient);
|
|
|
+ executeShell(ichSubscribeClient, ichPublishClient);
|
|
|
}
|
|
|
|
|
|
private void getSystemConfig(){
|
|
@@ -106,6 +117,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
@Override
|
|
|
public void onFailure(@NotNull Call call, @NotNull IOException e) {
|
|
|
Log.e(TAG, "onFailure: 获取系统配置错误! 网络错误! ", e);
|
|
|
+ LoggingUtils.sendErrorLog("业务异常: 获取系统配置错误! 网络错误! ", e);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -113,23 +125,6 @@ public class MainActivity extends AppCompatActivity {
|
|
|
(@NotNull Call call, @NotNull Response response) throws IOException {
|
|
|
|
|
|
try {
|
|
|
- OnDownloadListener listener = new OnDownloadListener() {
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onDownloadSuccess(File file) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onDownloading(int progress) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onDownloadFailed(Exception e) {
|
|
|
-
|
|
|
- }
|
|
|
- };
|
|
|
String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
|
|
|
ObjectMapper objectMapper = JacksonUtils.objectmapper;
|
|
|
ResponseData<Map<String, String>> readValue = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<Map<String, String>>>() {
|
|
@@ -141,68 +136,8 @@ public class MainActivity extends AppCompatActivity {
|
|
|
if (!StringUtils.isNullOrEmpty(time)) {
|
|
|
LocalStorage.getInstance().getSession().setTime(Long.parseLong(time));
|
|
|
}
|
|
|
- String qrcode_url = configMap.get(SystemConfigConstant.cabinet_take_object_qrcode_url);
|
|
|
-
|
|
|
- try {
|
|
|
- ICSPClient.download(qrcode_url, new Callback() {
|
|
|
- @Override
|
|
|
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
|
|
|
- InputStream is = null;
|
|
|
- byte[] buf = new byte[2048];
|
|
|
- int len = 0;
|
|
|
- FileOutputStream fos = null;
|
|
|
- final String destFileDir = "res/drawable/";
|
|
|
- String destFileName = "e_mp_qrcode_8x8.jpg";
|
|
|
- //储存下载文件的目录
|
|
|
- File dir = new File(getApplication().getFilesDir().getAbsolutePath() + "/" +destFileDir);
|
|
|
- if (!dir.exists()) {
|
|
|
- dir.mkdirs();
|
|
|
- }
|
|
|
-// destFileName = apk_url.substring(apk_url.lastIndexOf('/'), apk_url.length() - 1);
|
|
|
- File file = new File(dir, destFileName);
|
|
|
-
|
|
|
- try {
|
|
|
-
|
|
|
- is = response.body().byteStream();
|
|
|
- long total = response.body().contentLength();
|
|
|
- fos = new FileOutputStream(file);
|
|
|
- long sum = 0;
|
|
|
- while ((len = is.read(buf)) != -1) {
|
|
|
- fos.write(buf, 0, len);
|
|
|
- sum += len;
|
|
|
- int progress = (int) (sum * 1.0f / total * 100);
|
|
|
- //下载中更新进度条
|
|
|
- listener.onDownloading(progress);
|
|
|
- }
|
|
|
- fos.flush();
|
|
|
- //下载完成
|
|
|
- listener.onDownloadSuccess(file);
|
|
|
- } catch (Exception e) {
|
|
|
- listener.onDownloadFailed(e);
|
|
|
- }finally {
|
|
|
- try {
|
|
|
- if (is != null) {
|
|
|
- is.close();
|
|
|
- }
|
|
|
- if (fos != null) {
|
|
|
- fos.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
- }, listener);
|
|
|
- } catch (Exception e) {
|
|
|
|
|
|
- }
|
|
|
- Log.i(TAG, "onResponse: 获取系统配置成功! ");
|
|
|
+ Log.i(TAG, "onResponse: =====================>获取系统配置成功! ");
|
|
|
} else {
|
|
|
Log.w(TAG, "onResponse: code==>" + readValue.getCode() + ", msg==>" + readValue.getMsg());
|
|
|
}
|
|
@@ -213,32 +148,51 @@ public class MainActivity extends AppCompatActivity {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private void autoUpdateVersion(ICHPublishClient ichPublishClient) {
|
|
|
- ichPublishClient.subscribe(String.format(ICHTopic.APK_UPDATE_PATH, BaseUtils.getClientId()), (msgId, msg) -> {
|
|
|
+ private void autoUpdateVersion(ICHSubscribeClient ichSubscribeClient) {
|
|
|
+ ichSubscribeClient.subscribe(String.format(ICHTopic.APK_UPDATE_PATH, BaseUtils.getClientId()), (msgId, msg) -> {
|
|
|
try {
|
|
|
- // String path = new String(msg.getPayload());
|
|
|
+// String path = new String(msg.getPayload());
|
|
|
// 发起请求下载APK
|
|
|
- XUpdate.newBuild(this)
|
|
|
- .updateUrl(ICSPApi.GET_UPDATE_PATH.getUrl())
|
|
|
- .isAutoMode(true) // 如果需要完全无人干预,自动更新,需要root权限【静默安装需要】
|
|
|
+ Log.i(TAG, "autoUpdateVersion: =====================>接收到服务器发来更新指令! 开始更新! ");
|
|
|
+ UpdateConfig updateConfig = EasyUpdate.getUpdateConfig(getApplication());
|
|
|
+ EasyUpdate.create(getBaseContext(), ICSPApi.GET_UPDATE_PATH.getUrl())
|
|
|
+ .updateHttpService(AriaDownloader.getUpdateHttpService(getApplication()))
|
|
|
+ .build()
|
|
|
.update();
|
|
|
- Log.i(TAG, "autoUpdateVersion: ====================================>更新完成! ");
|
|
|
+
|
|
|
+// XUpdate.newBuild(this)
|
|
|
+// .updateUrl(ICSPApi.GET_UPDATE_PATH.getUrl())
|
|
|
+// .isAutoMode(true) // 如果需要完全无人干预,自动更新,需要root权限【静默安装需要】
|
|
|
+// .update();
|
|
|
+ Log.i(TAG, "autoUpdateVersion: ====================================>更新中! ");
|
|
|
// APKUpdateDownload.getInstance().downloadAPK(MainActivity.this, getApplication(), path);
|
|
|
} catch (Exception e) {
|
|
|
- Log.e(TAG, "onCreate: 更新版本监听失败! ", e);
|
|
|
+ Log.e(TAG, "onCreate: =====================>更新版本监听失败! ", e);
|
|
|
+ LoggingUtils.sendErrorLog("业务异常: 更新版本监听失败! ", e);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private void executeShell(ICHPublishClient ichPublishClient){
|
|
|
- ichPublishClient.subscribe(String.format(ICHTopic.EXECUTE_SHELL_SCRIPT, BaseUtils.getClientId()), (msgId, msg) -> {
|
|
|
+ private UpdateEntity getUpdateEntityFromAssets() {
|
|
|
+// UpdateEntity updateEntity = new UpdateEntity();
|
|
|
+// updateEntity.setVersionCode(3);
|
|
|
+//
|
|
|
+//
|
|
|
+
|
|
|
+ return new DefaultUpdateParser().parseJson(ResourceUtils.readStringFromAssert("update_test.json", getAssets()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void executeShell(ICHSubscribeClient ichSubscribeClient, ICHPublishClient ichPublishClient){
|
|
|
+ ichSubscribeClient.subscribe(String.format(ICHTopic.EXECUTE_SHELL_SCRIPT, BaseUtils.getClientId()), (msgId, msg) -> {
|
|
|
if (msg != null) {
|
|
|
try {
|
|
|
ShellVo shellVo = JacksonUtils.objectmapper.readValue(msg.getPayload(), ShellVo.class);
|
|
|
ShellVo result = shellVo;
|
|
|
- if (shellVo != null && shellVo.getScript() != null && "".equals(shellVo.getScript()) && shellVo.getScript().length() > 0) {
|
|
|
+ if (shellVo != null && shellVo.getScript() != null && !"".equals(shellVo.getScript()) && shellVo.getScript().length() > 0) {
|
|
|
+ Log.i(TAG, "executeShell: =====================>接到服务端传来命令: " + shellVo.getScript() + ", 参数: " + shellVo.getArgs());
|
|
|
BaseUtils.executeShell(shellVo);
|
|
|
} else {
|
|
|
+ Log.i(TAG, "executeShell: =====================>接到服务端传来空命令, 不执行! ");
|
|
|
result = new ShellVo();
|
|
|
result.setResult("命令为空, 不可执行!");
|
|
|
}
|
|
@@ -246,19 +200,22 @@ public class MainActivity extends AppCompatActivity {
|
|
|
mqttMessage.setQos(1);
|
|
|
mqttMessage.setPayload(JacksonUtils.objectmapper.writeValueAsString(result).getBytes());
|
|
|
ichPublishClient.publish(String.format(ICHTopic.EXECUTE_SHELL_SCRIPT_RESPONSE, BaseUtils.getClientId()), mqttMessage);
|
|
|
+ Log.i(TAG, "executeShell: =====================>发送命令执行结果成功! ");
|
|
|
} catch (Exception e) {
|
|
|
Log.e(TAG, "onCreate: 发送执行脚本结果失败! ", e);
|
|
|
+ LoggingUtils.sendErrorLog("业务异常: 发送执行脚本结果失败! ", e);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private void openLocker(ICHPublishClient ichPublishClient) {
|
|
|
- ichPublishClient.subscribe(ICHTopic.LOCK + BaseUtils.getClientId(), (s, msg) -> {
|
|
|
+ private void openLocker(ICHSubscribeClient ichSubscribeClient, ICHPublishClient ichPublishClient) {
|
|
|
+ ichSubscribeClient.subscribe(ICHTopic.LOCK + BaseUtils.getClientId(), (s, msg) -> {
|
|
|
String payload = new String(msg.getPayload());
|
|
|
ObjectMapper objectMapper = JacksonUtils.objectmapper;
|
|
|
Message message = objectMapper.readValue(payload, Message.class);
|
|
|
Log.i(TAG, "onCreate: message id: " + s + "--------------消息体: " + message);
|
|
|
+ Log.i(TAG, "openLocker: ==================>接到开锁消息: " + message);
|
|
|
|
|
|
DeviceControl.unlockLocker(message.getSection(), message.getPort(), (var1, var2) -> {
|
|
|
Log.i(TAG, "onCreate: 开锁返回码: " + var1 + "--------------返回消息: " + Arrays.asList(var2));
|
|
@@ -267,14 +224,16 @@ public class MainActivity extends AppCompatActivity {
|
|
|
try {
|
|
|
mqttMessage = wrapMessage(message, objectMapper);
|
|
|
ichPublishClient.publish(String.format(ICHTopic.CALLBACK_FAILED, BaseUtils.getClientId()), mqttMessage);
|
|
|
+ Log.i(TAG, "openLocker: ==================>发送开锁失败消息成功! 锁板id: " + message.getSection() + ", 端口: " + message.getPort());
|
|
|
} catch (JsonProcessingException e) {
|
|
|
Log.e(TAG, "onCreate: ---------------------序列化开锁错误消息失败! ", e);
|
|
|
+ LoggingUtils.sendErrorLog("业务异常: 序列化开锁错误消息失败! ", e);
|
|
|
} catch (Exception e) {
|
|
|
Log.e(TAG, "onCreate: ---------------------未知错误! ", e);
|
|
|
+ LoggingUtils.sendErrorLog("业务异常: 序列化开锁未知错误! ", e);
|
|
|
}
|
|
|
} else {
|
|
|
// TODO 暂时不做 成功需要不断去请求查询锁是否关闭, 关闭后推送成功消息
|
|
|
- Log.i(TAG, "onCreate: -------------------------开锁成功");
|
|
|
// AtomicBoolean atomicBoolean = new AtomicBoolean(true);
|
|
|
// AtomicInteger atomicInteger = new AtomicInteger(5);
|
|
|
// do {
|
|
@@ -295,6 +254,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
// }
|
|
|
// } while(atomicBoolean.get());
|
|
|
try {
|
|
|
+ Log.i(TAG, "onCreate: =====================>开锁成功! 锁板id: " + message.getSection() + ", 端口: " + message.getPort());
|
|
|
mqttMessage = wrapMessage(message, objectMapper);
|
|
|
// if (atomicInteger.get() <= 0) {
|
|
|
// message.setCause("长时间未关闭柜门!");
|
|
@@ -305,8 +265,10 @@ public class MainActivity extends AppCompatActivity {
|
|
|
ichPublishClient.publish(String.format(ICHTopic.CALLBACK_SUCCESS, BaseUtils.getClientId()), mqttMessage);
|
|
|
} catch (JsonProcessingException e) {
|
|
|
Log.e(TAG, "onCreate: ---------------------序列化开锁成功消息失败! ", e);
|
|
|
+ LoggingUtils.sendErrorLog("业务异常: 序列化开锁成功消息失败! ", e);
|
|
|
} catch (RuntimeException e) {
|
|
|
- Log.e(TAG, "onCreate: ---------------------未知错误! ", e);
|
|
|
+ Log.e(TAG, "onCreate: ---------------------序列化开锁成功消息未知错误! ", e);
|
|
|
+ LoggingUtils.sendErrorLog("业务异常: 开锁未知错误! ", e);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
@@ -332,10 +294,10 @@ public class MainActivity extends AppCompatActivity {
|
|
|
cabinet.setImei(BaseUtils.getIMEI(getApplicationContext()));
|
|
|
cabinet.setMacIpv4(BaseUtils.getIp());
|
|
|
cabinet.setMacIpv6("unknown");
|
|
|
- cabinet.setNetType("111");
|
|
|
+ cabinet.setNetType("unknown");
|
|
|
cabinet.setPlatType("S905");
|
|
|
- cabinet.setSim("111");
|
|
|
- cabinet.setWifiSSid("111");
|
|
|
+ cabinet.setSim("unknown");
|
|
|
+ cabinet.setWifiSSid("unknown");
|
|
|
|
|
|
ObjectMapper objectMapper = JacksonUtils.objectmapper;
|
|
|
|
|
@@ -347,8 +309,10 @@ public class MainActivity extends AppCompatActivity {
|
|
|
message.setQos(1);
|
|
|
message.setPayload(jsonData.getBytes());
|
|
|
ichPublishClient.publish(String.format(ICHTopic.CABINET_INFO_REPORT, BaseUtils.getClientId()), message);
|
|
|
+ Log.i(TAG, "cabinetInfoReport: ===============>上报柜子信息成功! ");
|
|
|
} catch (JsonProcessingException e) {
|
|
|
Log.e(TAG, "onCreate: 解析成JSON失败!", e);
|
|
|
+ LoggingUtils.sendErrorLog("业务异常: 上报柜子信息时解析成JSON失败! ", e);
|
|
|
}
|
|
|
}
|
|
|
|