|
@@ -14,12 +14,14 @@ import com.emato.ich.entity.Cabinet;
|
|
|
import com.emato.ich.entity.Message;
|
|
|
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.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.StringUtils;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
@@ -74,24 +76,33 @@ public class MainActivity extends AppCompatActivity {
|
|
|
binding = ActivityMainBinding.inflate(getLayoutInflater());
|
|
|
// 强制关闭输入法
|
|
|
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
|
|
|
-// setSupportActionBar(binding.toolbar);
|
|
|
+
|
|
|
setContentView(binding.getRoot());
|
|
|
-// NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
|
|
|
-// appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
|
|
|
-// NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
|
|
|
|
|
|
// 自动重启
|
|
|
((CrashApplication) getApplication()).addActivity(MainActivity.this);
|
|
|
|
|
|
-// DeviceControl.unlockLocker(4, 2, new CmdCallback() {
|
|
|
-// @Override
|
|
|
-// public void onMessage(int i, String... strings) {
|
|
|
-// Log.i(TAG, "onMessage: ===============================>" + i + Arrays.asList(strings));
|
|
|
-// }
|
|
|
-// });
|
|
|
+ // 获取系统配置
|
|
|
+ getSystemConfig();
|
|
|
|
|
|
+ ICHPublishClient ichPublishClient = ICHPublishClient.getInstance();
|
|
|
|
|
|
- // 获取系统配置
|
|
|
+ ICHTopic.CLIENT_ID = BaseUtils.getClientId();
|
|
|
+ // 订阅主题
|
|
|
+// ICHSubscribeClient ichSubscribeClient = ICHSubscribeClient.getInstance();
|
|
|
+ openLocker(ichPublishClient);
|
|
|
+
|
|
|
+ // 注册柜子信息
|
|
|
+ cabinetInfoReport(ichPublishClient);
|
|
|
+
|
|
|
+ // TODO 更新版本监听
|
|
|
+ autoUpdateVersion(ichPublishClient);
|
|
|
+
|
|
|
+ // TODO 监听shell命令脚本
|
|
|
+ executeShell(ichPublishClient);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getSystemConfig(){
|
|
|
ICSPClient.getSystemConfig("", BaseUtils.getClientId(), new Callback() {
|
|
|
@Override
|
|
|
public void onFailure(@NotNull Call call, @NotNull IOException e) {
|
|
@@ -126,6 +137,11 @@ public class MainActivity extends AppCompatActivity {
|
|
|
});
|
|
|
if (null != readValue && readValue.getCode().equals(ICSPResponseCodeEnum.OK.getCode())) {
|
|
|
configMap.putAll(readValue.getData());
|
|
|
+ // 系统配置的session过期时间
|
|
|
+ String time = configMap.get(SystemConfigConstant.deliverer_token_expire_seconds);
|
|
|
+ if (!StringUtils.isNullOrEmpty(time)) {
|
|
|
+ LocalStorage.getInstance().getSession().setTime(Long.parseLong(time));
|
|
|
+ }
|
|
|
String qrcode_url = configMap.get(SystemConfigConstant.cabinet_take_object_qrcode_url);
|
|
|
|
|
|
try {
|
|
@@ -196,14 +212,49 @@ public class MainActivity extends AppCompatActivity {
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
+ }
|
|
|
|
|
|
- ICHPublishClient ichPublishClient = ICHPublishClient.getInstance();
|
|
|
+ private void autoUpdateVersion(ICHPublishClient ichPublishClient) {
|
|
|
+ ichPublishClient.subscribe(String.format(ICHTopic.APK_UPDATE_PATH, BaseUtils.getClientId()), (msgId, msg) -> {
|
|
|
+ try {
|
|
|
+ // String path = new String(msg.getPayload());
|
|
|
+ // 发起请求下载APK
|
|
|
+ XUpdate.newBuild(this)
|
|
|
+ .updateUrl(ICSPApi.GET_UPDATE_PATH.getUrl())
|
|
|
+ .isAutoMode(true) // 如果需要完全无人干预,自动更新,需要root权限【静默安装需要】
|
|
|
+ .update();
|
|
|
+// APKUpdateDownload.getInstance().downloadAPK(MainActivity.this, getApplication(), path);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.e(TAG, "onCreate: 更新版本监听失败! ", e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
- String clientId = BaseUtils.getClientId();
|
|
|
- ICHTopic.CLIENT_ID = clientId;
|
|
|
- // 订阅主题
|
|
|
-// ICHSubscribeClient ichSubscribeClient = ICHSubscribeClient.getInstance();
|
|
|
- ichPublishClient.subscribe(ICHTopic.LOCK + clientId, (s, msg) -> {
|
|
|
+ private void executeShell(ICHPublishClient ichPublishClient){
|
|
|
+ ichPublishClient.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) {
|
|
|
+ BaseUtils.executeShell(shellVo);
|
|
|
+ } else {
|
|
|
+ result = new ShellVo();
|
|
|
+ result.setResult("命令为空, 不可执行!");
|
|
|
+ }
|
|
|
+ MqttMessage mqttMessage = new MqttMessage();
|
|
|
+ mqttMessage.setQos(1);
|
|
|
+ mqttMessage.setPayload(JacksonUtils.objectmapper.writeValueAsString(result).getBytes());
|
|
|
+ ichPublishClient.publish(String.format(ICHTopic.EXECUTE_SHELL_SCRIPT_RESPONSE, BaseUtils.getClientId()), mqttMessage);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log.e(TAG, "onCreate: 发送执行脚本结果失败! ", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void openLocker(ICHPublishClient ichPublishClient) {
|
|
|
+ ichPublishClient.subscribe(ICHTopic.LOCK + BaseUtils.getClientId(), (s, msg) -> {
|
|
|
String payload = new String(msg.getPayload());
|
|
|
ObjectMapper objectMapper = JacksonUtils.objectmapper;
|
|
|
Message message = objectMapper.readValue(payload, Message.class);
|
|
@@ -261,9 +312,9 @@ public class MainActivity extends AppCompatActivity {
|
|
|
});
|
|
|
|
|
|
});
|
|
|
+ }
|
|
|
|
|
|
- // 注册柜子信息
|
|
|
-
|
|
|
+ private void cabinetInfoReport(ICHPublishClient ichPublishClient){
|
|
|
String appVersion = BaseUtils.getAppVersion(getApplicationContext());
|
|
|
String androidVersion = BaseUtils.getVersionName();
|
|
|
CountDownLatch countDownLatch = new CountDownLatch(10);
|
|
@@ -274,12 +325,12 @@ public class MainActivity extends AppCompatActivity {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
- cabinet.setClientId(clientId);
|
|
|
+ cabinet.setClientId(BaseUtils.getClientId());
|
|
|
cabinet.setSoftwareVersion(appVersion);
|
|
|
cabinet.setAndroidVersion(androidVersion);
|
|
|
cabinet.setSoftwareType("android");
|
|
|
cabinet.setImei(BaseUtils.getIMEI(getApplicationContext()));
|
|
|
- cabinet.setMacIpv4("111");
|
|
|
+ cabinet.setMacIpv4(BaseUtils.getIp());
|
|
|
cabinet.setMacIpv6("unknown");
|
|
|
cabinet.setNetType("111");
|
|
|
cabinet.setPlatType("S905");
|
|
@@ -295,47 +346,10 @@ public class MainActivity extends AppCompatActivity {
|
|
|
MqttMessage message = new MqttMessage();
|
|
|
message.setQos(1);
|
|
|
message.setPayload(jsonData.getBytes());
|
|
|
- ichPublishClient.publish(String.format(ICHTopic.CABINET_INFO_REPORT, clientId), message);
|
|
|
+ ichPublishClient.publish(String.format(ICHTopic.CABINET_INFO_REPORT, BaseUtils.getClientId()), message);
|
|
|
} catch (JsonProcessingException e) {
|
|
|
Log.e(TAG, "onCreate: 解析成JSON失败!", e);
|
|
|
}
|
|
|
-
|
|
|
- // TODO 更新版本监听
|
|
|
- ichPublishClient.subscribe(String.format(ICHTopic.APK_UPDATE_PATH, BaseUtils.getClientId()), (msgId, msg) -> {
|
|
|
- try {
|
|
|
- // String path = new String(msg.getPayload());
|
|
|
- // 发起请求下载APK
|
|
|
- XUpdate.newBuild(this)
|
|
|
- .updateUrl(ICSPApi.GET_UPDATE_PATH.getUrl())
|
|
|
- .isAutoMode(true) // 如果需要完全无人干预,自动更新,需要root权限【静默安装需要】
|
|
|
- .update();
|
|
|
-// APKUpdateDownload.getInstance().downloadAPK(MainActivity.this, getApplication(), path);
|
|
|
- } catch (Exception e) {
|
|
|
- Log.e(TAG, "onCreate: 更新版本监听失败! ", e);
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- // TODO 监听shell命令脚本
|
|
|
- ichPublishClient.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) {
|
|
|
- BaseUtils.executeShell(shellVo);
|
|
|
- } else {
|
|
|
- result = new ShellVo();
|
|
|
- result.setResult("命令为空, 不可执行!");
|
|
|
- }
|
|
|
- MqttMessage mqttMessage = new MqttMessage();
|
|
|
- mqttMessage.setQos(1);
|
|
|
- mqttMessage.setPayload(JacksonUtils.objectmapper.writeValueAsString(result).getBytes());
|
|
|
- ichPublishClient.publish(String.format(ICHTopic.EXECUTE_SHELL_SCRIPT_RESPONSE, BaseUtils.getClientId()), mqttMessage);
|
|
|
- } catch (Exception e) {
|
|
|
- Log.e(TAG, "onCreate: 发送执行脚本结果失败! ", e);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
}
|
|
|
|
|
|
@Override
|