123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- package com.emato.ich;
- import android.content.pm.ActivityInfo;
- import android.os.Build;
- import android.os.Bundle;
- import com.cherry.sdk.controller.BuildConfig;
- import com.cherry.sdk.controller.callback.CmdCallback;
- import com.cherry.sdk.controller.callback.OnScanListen;
- import com.cherry.sdk.controller.utils.ScanGunKeyEventHelper;
- import com.emato.ich.api.ICSPClient;
- import com.emato.ich.api.ICSPResponseCodeEnum;
- import com.emato.ich.api.SystemConfigConstant;
- import com.emato.ich.crash.CrashApplication;
- import com.emato.ich.crash.UncaughtExceptionHandlerImpl;
- import com.emato.ich.device.DeviceControl;
- import com.emato.ich.entity.Cabinet;
- import com.emato.ich.entity.Message;
- import com.emato.ich.entity.vo.ResponseData;
- import com.emato.ich.fragment.InputInfoFragment;
- 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.Md5Utils;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.core.type.TypeReference;
- import com.fasterxml.jackson.databind.JavaType;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.util.BeanUtil;
- import com.google.android.material.snackbar.Snackbar;
- import androidx.appcompat.app.AppCompatActivity;
- import android.text.InputType;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.View;
- import androidx.fragment.app.Fragment;
- import androidx.fragment.app.FragmentManager;
- import androidx.navigation.NavController;
- import androidx.navigation.Navigation;
- import androidx.navigation.ui.AppBarConfiguration;
- import androidx.navigation.ui.NavigationUI;
- import com.emato.ich.databinding.ActivityMainBinding;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.WindowManager;
- import android.widget.ArrayAdapter;
- import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
- import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
- import org.eclipse.paho.client.mqttv3.MqttMessage;
- import org.jetbrains.annotations.NotNull;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.concurrent.CountDownLatch;
- import okhttp3.Call;
- import okhttp3.Callback;
- import okhttp3.Response;
- public class MainActivity extends AppCompatActivity {
- private AppBarConfiguration appBarConfiguration;
- private ActivityMainBinding binding;
- public ScanGunKeyEventHelper scanGunKeyEventHelper;
- private final Map<String, Bundle> bundleMap = new ConcurrentHashMap<>();
- private final Map<String, String> configMap = new ConcurrentHashMap<>();
- private static final String TAG = MainActivity.class.getName();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- 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);
- // 自动重启
- UncaughtExceptionHandlerImpl.getInstance().init(getApplication(), BuildConfig.DEBUG, true, 1000, MainActivity.class);
- // DeviceControl.unlockLocker(4, 2, new CmdCallback() {
- // @Override
- // public void onMessage(int i, String... strings) {
- // Log.i(TAG, "onMessage: ===============================>" + i + Arrays.asList(strings));
- // }
- // });
- // 获取系统配置
- ICSPClient.getSystemConfig("", BaseUtils.getClientId(), new Callback() {
- @Override
- public void onFailure(@NotNull Call call, @NotNull IOException e) {
- Log.e(TAG, "onFailure: 获取系统配置错误! 网络错误! ", e);
- }
- @Override
- public void onResponse
- (@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 = new ObjectMapper();
- ResponseData<Map<String, String>> readValue = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<Map<String, String>>>() {
- });
- if (null != readValue && readValue.getCode().equals(ICSPResponseCodeEnum.OK.getCode())) {
- configMap.putAll(readValue.getData());
- 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: 获取系统配置成功! ");
- } else {
- Log.w(TAG, "onResponse: code==>" + readValue.getCode() + ", msg==>" + readValue.getMsg());
- }
- } catch (RuntimeException e) {
- Log.e(TAG, "onResponse: 获取系统配置错误! 未知错误! ", e);
- }
- }
- });
- ICHPublishClient ichPublishClient = ICHPublishClient.getInstance();
- String clientId = BaseUtils.getClientId();
- ICHTopic.CLIENT_ID = clientId;
- // 订阅主题
- // ICHSubscribeClient ichSubscribeClient = ICHSubscribeClient.getInstance();
- ichPublishClient.subscribe(ICHTopic.LOCK + clientId, (s, msg) -> {
- String payload = new String(msg.getPayload());
- ObjectMapper objectMapper = new ObjectMapper();
- Message message = objectMapper.readValue(payload, Message.class);
- Log.i(TAG, "onCreate: message id: " + s + "--------------消息体: " + message);
- DeviceControl.unlockLocker(message.getSection(), message.getPort(), (var1, var2) -> {
- Log.i(TAG, "onCreate: 开锁返回码: " + var1 + "--------------返回消息: " + Arrays.asList(var2));
- MqttMessage mqttMessage;
- if (var1 != 0) {
- try {
- mqttMessage = wrapMessage(message, objectMapper);
- ichPublishClient.publish(String.format(ICHTopic.CALLBACK_FAILED, BaseUtils.getClientId()), mqttMessage);
- } catch (JsonProcessingException e) {
- Log.e(TAG, "onCreate: ---------------------序列化开锁错误消息失败! ", e);
- } catch (Exception e) {
- Log.e(TAG, "onCreate: ---------------------未知错误! ", e);
- }
- } else {
- // TODO 暂时不做 成功需要不断去请求查询锁是否关闭, 关闭后推送成功消息
- Log.i(TAG, "onCreate: -------------------------开锁成功");
- // AtomicBoolean atomicBoolean = new AtomicBoolean(true);
- // AtomicInteger atomicInteger = new AtomicInteger(5);
- // do {
- // try {
- // TimeUnit.SECONDS.sleep(30);
- // } catch (InterruptedException e) {
- // Log.e(TAG, "onCreate: 等待查询锁状态时线程中断! ", e);
- // }
- // DeviceControl.queryLocker(message.getSection(), message.getPort(), (var3, var4) -> {
- // List<String> strings = Arrays.asList(var4);
- // if (var3 == 0 && strings.size() > 0 && !strings.get(0).equals("locked")) {
- // atomicBoolean.set(false);
- // }
- // atomicInteger.decrementAndGet();
- // });
- // if (atomicInteger.get() <= 0) {
- // atomicBoolean.set(false);
- // }
- // } while(atomicBoolean.get());
- try {
- mqttMessage = wrapMessage(message, objectMapper);
- // if (atomicInteger.get() <= 0) {
- // message.setCause("长时间未关闭柜门!");
- // mqttMessage.setPayload(objectMapper.writeValueAsBytes(message));
- // ichPublishClient.publish(String.format(ICHTopic.CALLBACK_FAILED, BaseUtils.getClientId()), mqttMessage);
- // }
- ichPublishClient.publish(String.format(ICHTopic.CALLBACK_SUCCESS, BaseUtils.getClientId()), mqttMessage);
- } catch (JsonProcessingException e) {
- Log.e(TAG, "onCreate: ---------------------序列化开锁成功消息失败! ", e);
- } catch (RuntimeException e) {
- Log.e(TAG, "onCreate: ---------------------未知错误! ", e);
- }
- }
- });
- });
- // 注册柜子信息
- String appVersion = BaseUtils.getAppVersion(getApplicationContext());
- String androidVersion = BaseUtils.getVersionName();
- CountDownLatch countDownLatch = new CountDownLatch(10);
- Cabinet cabinet = DeviceControl.queryCabinetInfo(countDownLatch);
- try {
- countDownLatch.await();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- cabinet.setClientId(clientId);
- cabinet.setSoftwareVersion(appVersion);
- cabinet.setAndroidVersion(androidVersion);
- cabinet.setSoftwareType("android");
- cabinet.setImei(BaseUtils.getIMEI(getApplicationContext()));
- cabinet.setMacIpv4("111");
- cabinet.setMacIpv6("unknown");
- cabinet.setNetType("111");
- cabinet.setPlatType("S905");
- cabinet.setSim("111");
- cabinet.setWifiSSid("111");
- ObjectMapper objectMapper = new ObjectMapper();
- try {
- String jsonData = objectMapper.writeValueAsString(cabinet);
- Log.i(TAG, "onCreate: jsonData===============>" + jsonData);
- MqttMessage message = new MqttMessage();
- message.setQos(1);
- message.setPayload(jsonData.getBytes());
- ichPublishClient.publish(String.format(ICHTopic.CABINET_INFO_REPORT, clientId), message);
- } catch (JsonProcessingException e) {
- Log.e(TAG, "onCreate: 解析成JSON失败!", e);
- }
- // TODO 更新版本监听
- ichPublishClient.subscribe(String.format(ICHTopic.APK_UPDATE_PATH, BaseUtils.getClientId()), (msgId, msg) -> {
- String path = new String(msg.getPayload());
- // 发起请求下载APK
- APKUpdateDownload.getInstance().downloadAPK(MainActivity.this, getApplication(), path);
- });
- }
- @Override
- public boolean dispatchKeyEvent(KeyEvent event) {
- if (null != scanGunKeyEventHelper) {
- scanGunKeyEventHelper.analysisKeyEvent(event);
- }
- return true;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- // getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- // int id = item.getItemId();
- // //noinspection SimplifiableIfStatement
- // if (id == R.id.action_settings) {
- // return true;
- // }
- return super.onOptionsItemSelected(item);
- }
- @Override
- public boolean onSupportNavigateUp() {
- NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
- return NavigationUI.navigateUp(navController, appBarConfiguration)
- || super.onSupportNavigateUp();
- }
- private MqttMessage wrapMessage(Message message, ObjectMapper objectMapper) throws JsonProcessingException {
- MqttMessage mqttMessage = new MqttMessage();
- Message failedMsg = new Message();
- // TODO 失败原因枚举
- failedMsg.setCause("失败原因枚举");
- failedMsg.setMessageId(message.getMessageId());
- failedMsg.setClientId(message.getClientId());
- failedMsg.setCmd(message.getCmd());
- failedMsg.setDatetime(message.getDatetime());
- failedMsg.setPort(message.getPort());
- failedMsg.setScene(message.getScene());
- failedMsg.setSection(message.getSection());
- mqttMessage.setQos(1);
- mqttMessage.setPayload(objectMapper.writeValueAsBytes(failedMsg));
- return mqttMessage;
- }
- public Map<String, Bundle> getBundleMap() {
- return bundleMap;
- }
- public Map<String, String> getConfigMap() {
- return configMap;
- }
- }
|