From 8ff90d87687b6d38158809aae776b6c709d7b60a Mon Sep 17 00:00:00 2001
From: weimingfei <fei_gaming@sina.com>
Date: 星期四, 04 十二月 2025 12:01:41 +0800
Subject: [PATCH] 指纹钥匙柜

---
 keyCabinet-android/app/src/main/java/com/doumee/keyCabinet/ui/main/MainActivity.java |  690 +++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 469 insertions(+), 221 deletions(-)

diff --git a/keyCabinet-android/app/src/main/java/com/doumee/keyCabinet/ui/main/MainActivity.java b/keyCabinet-android/app/src/main/java/com/doumee/keyCabinet/ui/main/MainActivity.java
index 13b914e..52a2407 100644
--- a/keyCabinet-android/app/src/main/java/com/doumee/keyCabinet/ui/main/MainActivity.java
+++ b/keyCabinet-android/app/src/main/java/com/doumee/keyCabinet/ui/main/MainActivity.java
@@ -6,12 +6,15 @@
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
+import android.graphics.PixelFormat;
 import android.graphics.drawable.BitmapDrawable;
 import android.hardware.Camera;
 import android.opengl.Visibility;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
+import android.serialport.SerialPortFinder;
 import android.text.SpannableString;
 import android.text.Spanned;
 import android.text.TextUtils;
@@ -20,6 +23,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.WindowManager;
 import android.widget.PopupWindow;
 import android.widget.RelativeLayout;
 import android.widget.TextView;
@@ -60,11 +64,13 @@
 import com.doumee.keyCabinet.event.KeyResultEvent;
 import com.doumee.keyCabinet.event.ManageOpenGridResultEvent;
 import com.doumee.keyCabinet.event.OpenAllGridEvent;
+import com.doumee.keyCabinet.event.OpenErrEvent;
 import com.doumee.keyCabinet.event.OpenGridEvent;
 import com.doumee.keyCabinet.event.OpenGridListEvent;
 import com.doumee.keyCabinet.event.OpenGridOneResultEvent;
 import com.doumee.keyCabinet.event.TimeClockEvent;
 import com.doumee.keyCabinet.ui.face.ActivationActivity;
+import com.doumee.keyCabinet.ui.face.ChoseActivationActivity;
 import com.doumee.keyCabinet.ui.face.FaceActivity;
 import com.doumee.keyCabinet.ui.keyCabinet.ChangeUrlActivity;
 import com.doumee.keyCabinet.ui.keyCabinet.KeyCabinetActivity;
@@ -80,6 +86,7 @@
 import com.doumee.keyCabinet.utils.CircularQueue;
 import com.doumee.keyCabinet.utils.CrashHandler;
 import com.doumee.keyCabinet.utils.LMobileInfo;
+import com.doumee.keyCabinet.utils.MCUtils;
 import com.doumee.keyCabinet.utils.TimeUtils;
 import com.doumee.keyCabinet.utils.face.FaceSDKManager;
 import com.doumee.keyCabinet.utils.face.FaceUtils;
@@ -103,12 +110,16 @@
 import com.doumee.lib_coremodel.util.StringUtil;
 import com.doumee.lib_coremodel.view.ToastView;
 import com.example.datalibrary.api.FaceApi;
+import com.example.datalibrary.db.DBManager;
 import com.example.datalibrary.listener.DBLoadListener;
 import com.example.datalibrary.listener.SdkInitListener;
 import com.example.datalibrary.model.ImportFeatureResult;
 import com.example.datalibrary.model.User;
 import com.example.datalibrary.utils.ToastUtils;
 import com.example.datalibrary.view.PreviewTexture;
+import com.mc.enjoysdk.McHome;
+import com.mc.enjoysdk.McSystemUi;
+import com.mc.enjoysdk.transform.McSystemUiFlag;
 import com.yanzhenjie.permission.runtime.Permission;
 
 import org.greenrobot.eventbus.EventBus;
@@ -155,6 +166,8 @@
     private int downCount = 0;
     private Long lastDownTime;
     private Long showTime;
+    private McSystemUi mcSystemUi;
+    private McHome mcHome;
 
     private Handler handler = new Handler(){
         @Override
@@ -171,8 +184,21 @@
                         getVM().getFaceLD();
                     }
                     getVM().upBraceletLog();*/
+                    case 1:
+                        if(isShowing) {
+                            EventBus.getDefault().post(new OpenGridEvent((String) msg.obj));
+                            handler.sendEmptyMessageDelayed(1, 1000);
+                        }
+                        break;
                     case 2:
-
+                        if(isShowing){
+                            //閲嶆柊鍙戦�侀挜鍖欐煡璇�
+                            //鍙戦�佹煡璇㈤挜鍖欏彿
+                            EventBus.getDefault().post(new GetKeyStatusEvent());
+                            handler.sendEmptyMessageDelayed(2,1200);
+                        }
+                        break;
+                    default:
                         break;
                 }
             }
@@ -193,14 +219,16 @@
         getDB().setModel(getVM());
         initRGBCheck();
         handler.sendEmptyMessageDelayed(0, 1000);
-        //鍚姩蹇冭烦
+        /*//鍚姩蹇冭烦
         Intent intent = new Intent(MainActivity.this, HeartbeatService.class);
         startService(intent);
         //鍚姩鏇存柊浜鸿劯
         Intent intent2 = new Intent(MainActivity.this, FaceUpdateService.class);
-        startService(intent2);
+        startService(intent2);*/
         getPermission();
         Timber.tag("====>").d(LMobileInfo.getDeviceUniqueId());
+        mcSystemUi = McSystemUi.getInstance(this);
+        mcHome = McHome.getInstance(this);
     }
 
     private void initM(){
@@ -212,10 +240,10 @@
         long availableMemory = memoryInfo.availMem/(1024*1024);
         long usedMemory = totalMemory - availableMemory;
         //getDB().tvM.setText("鎬诲唴瀛橈細"+totalMemory+",浣跨敤鍐呭瓨锛�"+usedMemory+",鍙敤鍐呭瓨锛�"+availableMemory);
-        if((availableMemory<400&&showTime!=null&&(System.currentTimeMillis()-showTime>5000))||
+        if((availableMemory<100&&showTime!=null&&(System.currentTimeMillis()-showTime>5000))||
                 ("02:00:00".equals(StringUtil.getHM()))){
             //鍙敤鍐呭瓨灏忎簬400M,鎴栬�呮瘡澶╁噷鏅�2鐐癸紝閲嶅惎app
-            //restartApp();
+            restartApp();
         }
     }
 
@@ -235,6 +263,7 @@
             @Override
             public void onChanged(List<FaceUserBean> beans) {
                 //鏇存柊浜鸿劯鏁版嵁
+                getVM().addInfo("鏇存柊浜鸿劯鏁版嵁======銆媌eans = "+beans);
                 if(beans!=null&&beans.size()>0){
                     initModel(beans);
                 }
@@ -270,7 +299,7 @@
                 lastDownTime1 = System.currentTimeMillis();
                 downCount1++;
                 System.out.println();
-                Timber.tag("==>").d(""+downCount1);
+                //Timber.tag("==>").d(""+downCount1);
                 if(downCount1>4) {
                     downCount1=0;
                     lastDownTime1=null;
@@ -296,24 +325,20 @@
                 }
             }
         });
-        getDB().tvVersion.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                //todo 涓存椂浠g爜
-                //setJiuConfig();
-                SpUtil.saveString("rbgCameraId","0");
-            }
-        });
+
         getDB().imgLeft.setOnClickListener(new View.OnClickListener() {
             @Override
-            public void onClick(View v) {
+            public void onClick(View v1) {
                 //todo 涓存椂浠g爜
                 if (getDB().nsv.getVisibility()== View.VISIBLE) {
                     getDB().nsv.setVisibility(View.GONE);
                 }else {
                     getDB().nsv.setVisibility(View.VISIBLE);
                 }
-
+                //鏄剧ず鐘舵�佹爮
+                mcSystemUi.temporarilySwitchStatusBar(true);
+                //鏄剧ず瀵艰埅鏍�
+                mcSystemUi.temporarilySwitchNavigation(true);
             }
         });
         upErrInfo();
@@ -328,6 +353,7 @@
                     }
                     setJiuConfig();
                     initPort();
+                    EventBus.getDefault().post(new GetFacesEvent());
                 }
             }
         });
@@ -446,6 +472,12 @@
 
     private SpannableString getErrPhoneText(){
         String phone = MApplication.getConfigBean().getLinkPhone();
+        if(phone==null){
+            String text = "濡傛湁闂璇疯仈绯荤鐞嗗憳";
+            SpannableString styledText = new SpannableString(text);
+            styledText.setSpan(new TextAppearanceSpan(this, R.style.style_tip3), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+            return styledText;
+        }
         String text = "濡傛湁闂璇疯仈绯荤鐞嗗憳"+phone;
         SpannableString styledText = new SpannableString(text);
         styledText.setSpan(new TextAppearanceSpan(this, R.style.style_tip3), 0, text.length()-phone.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
@@ -458,6 +490,9 @@
     protected void onResume() {
         super.onResume();
         isShowing = true;
+        getVM().setSubmit(true);
+        MCUtils.hind(mcSystemUi);
+        mcHome.setHomePackage("com.doumee.keyCabinet");
         showTime = System.currentTimeMillis();
         if(!isFaceOk) {
             initLicense();
@@ -497,7 +532,10 @@
         if (future != null && !future.isDone()) {
             future.cancel(true);
         }
-        FaceApi.getInstance().cleanRecords();
+        if(isFaceOk) {
+            //婵�娲讳簡鎵嶅鐞�
+            FaceApi.getInstance().cleanRecords();
+        }
         handler.removeCallbacksAndMessages(null);
         handler = null;
         faceHandler.removeCallbacksAndMessages(null);
@@ -528,7 +566,9 @@
 
     @Subscribe
     public void GetFacesEvent(GetFacesEvent event){
+        getVM().addInfo("鏌ヨ浜鸿劯鏁版嵁======銆�");
         if(!isFinishing()&&!isUpdatingFace){
+            getVM().addInfo("鏌ヨ浜鸿劯鏁版嵁======銆媔sDBLoad = "+isDBLoad);
             if(isDBLoad){
                 isNeedUpdateFace = false;
                 //鏌ヨ浜鸿劯
@@ -562,7 +602,7 @@
                         /*
                          *瑕佹墽琛岀殑鎿嶄綔*/
 
-                        startActivity(new Intent(mContext, ActivationActivity.class));
+                        startActivity(new Intent(mContext, ChoseActivationActivity.class));
                     }
                 };
                 Timer timer = new Timer();
@@ -624,10 +664,10 @@
                             @Override
                             public void run() {
                                 FaceApi.getInstance().setUsers(users);
+                                isDBLoad = true;
                                 initFaceSDKManager(null);
                                 if (successCount > 5000 || successCount == 0) {
                                     getDB().progressGroup.setVisibility(View.GONE);
-                                    isDBLoad = true;
                                     isFaceOk = true;
                                 }
                             }
@@ -673,8 +713,10 @@
 
     private void initModel(List<FaceUserBean> userBeans) {
         if (com.example.datalibrary.manager.FaceSDKManager.initStatus == com.example.datalibrary.manager.FaceSDKManager.SDK_MODEL_LOAD_SUCCESS) {
+            getVM().addInfo("鏇存柊浜鸿劯===銆嬪紑濮嬫洿鏂�");
             waitUpdate(userBeans);
         }else {
+            getVM().addInfo("鏇存柊浜鸿劯===銆嬪垵濮嬪寲");
             initFaceSDKManager(userBeans);
         }
     }
@@ -730,6 +772,7 @@
         super.onPause();
         showTime = null;
         isShowing = false;
+        getVM().setSubmit(false);
     }
 
     private int finishCount;
@@ -763,23 +806,119 @@
         }
     };
 
-    private void waitUpdate(List<FaceUserBean> userBeans){
-        if(isUpdatingFace){
-            //姝e湪鏇存柊
-            waitBeans.addAll(userBeans);
-        }else {
+    private UserInfoManager.UserInfoListener mUserListListener = new UserInfoManager.UserInfoListener(){
+        @Override
+        public void userListQuerySuccess(String userName, List<User> listUserInfo) {
             selectBeans.clear();
             selectBeans.addAll(waitBeans);
             waitBeans.clear();
             selectBeans.addAll(userBeans);
             finishCount=0;
-            faceHandler.sendEmptyMessage(0);
+            /*//鍒犻櫎鍏ㄩ儴
+            FaceApi.getInstance().userClean();
+            faceHandler.sendEmptyMessage(0);*/
+
+            HashMap<String,String> uMap = new HashMap<>();
+            for(FaceUserBean d:selectBeans){
+                String key = d.getMemberId().toString()+"_"+d.getGroupId();
+                uMap.put(key,"");
+            }
+            //鍒犻櫎涓嶅瓨鍦ㄧ敤鎴�
+            List<User> users = FaceApi.getInstance().getAllUserList();
+            List<User> dels = new ArrayList<>();
+            StringBuilder sb = new StringBuilder();
+            for(User u:users){
+                String key = u.getUserId()+"_"+u.getGroupId();
+                if(!uMap.containsKey(key)){
+                    u.setChecked(true);
+                    dels.add(u);
+                    if(sb.length()>0){
+                        sb.append(",");
+                    }
+                    sb.append(u.getUserName());
+                }
+            }
+            getVM().addInfo("鏇存柊浜鸿劯銆嬨�嬨�嬨�嬨�嬨�嬨�嬨�嬨�嬨�嬨�嬨�嬨�嬨�嬨�嬨�嬨�嬨��");
+            if(dels.size()>0){
+                getVM().addInfo("鍑嗗鍒犻櫎鐢ㄦ埛锛�"+sb.toString());
+                UserInfoManager.getInstance().deleteUserListInfo(users,
+                        "1", new UserInfoManager.UserInfoListener() {
+                            @Override
+                            public void userListDeleteSuccess() {
+                                // 鐢ㄦ埛鍒楄〃鍒犻櫎鎴愬姛
+                                faceHandler.sendEmptyMessage(0);
+                                getVM().addInfo("鍒犻櫎鐢ㄦ埛鎴愬姛锛�");
+                            }
+
+                            @Override
+                            public void userListDeleteFailure(String message) {
+                                // 鐢ㄦ埛鍒楄〃鍒犻櫎澶辫触
+                                faceHandler.sendEmptyMessage(0);
+                                getVM().addInfo("鍒犻櫎鐢ㄦ埛澶辫触锛�"+message);
+
+                                getVM().addInfo("鍒犻櫎鐢ㄦ埛鍒楄〃锛�"+sb.toString());
+                            }
+                        },
+                        new DBLoadListener() {
+                            @Override
+                            public void onStart(int successCount) {
+
+                            }
+
+                            @Override
+                            public void onLoad(int finishCount, int successCount, float progress) {
+
+                            }
+
+                            @Override
+                            public void onComplete(List<User> features, int successCount) {
+
+                            }
+
+                            @Override
+                            public void onFail(int finishCount, int successCount, List<User> features) {
+
+                            }
+                        });
+            }else {
+                faceHandler.sendEmptyMessage(0);
+            }
+        }
+
+        @Override
+        public void userListQueryFailure(String message) {
+
+        }
+
+        @Override
+        public void userListDeleteSuccess() {
+
+        }
+
+        @Override
+        public void userListDeleteFailure(String message) {
+
+        }
+    };
+
+    private List<FaceUserBean> userBeans = new ArrayList<>();
+    private void waitUpdate(List<FaceUserBean> userBeans){
+        if(isUpdatingFace){
+            //姝e湪鏇存柊
+            waitBeans.addAll(userBeans);
+            getVM().addInfo("姝e湪鏇存柊銆佺瓑寰� 銆娿�娿�娿�娿�娿�娿�娿��");
+        }else {
+            getVM().addInfo("鍒濆鍖栦汉鑴稿簱 銆娿�娿�娿�娿�娿�娿�娿��");
+            com.example.datalibrary.manager.FaceSDKManager.getInstance().initDataBases(MainActivity.this);
+            this.userBeans.clear();
+            this.userBeans.addAll(userBeans);
+            UserInfoManager.getInstance().getUserListInfo(null, mUserListListener);
         }
     }
 
     private int repeatCount = 0;
     private void updateFace(FaceUserBean bean){
-        Timber.d("==UpdatingFace==>瀹屾垚鏁伴噺 "+finishCount);
+        //getVM().addInfo("==UpdatingFace==>瀹屾垚鏁伴噺 " + finishCount);
         if (TextUtils.isEmpty(bean.getFaceImg())||bean.getFaceImg().endsWith("null")) {
             finishCount++;
             faceHandler.sendEmptyMessage(0);
@@ -888,12 +1027,14 @@
             isSave = FaceApi.getInstance().registerUserIntoDBmanager(newDo.getGroupId(),
                     newDo.getUserId(), newDo.getUserName(), newDo.getImageName(), newDo.getUserInfo(), newDo.getFeature());
             //System.out.println("==isOk==>淇濆瓨鎴愬姛");
+            //getVM().addInfo("鏂板浜鸿劯==銆�"+newDo.getUserName()+"   缁撴灉锛�"+isSave);
         } else {
             //鏇存柊
             user.setUserName(newDo.getUserName());
             user.setImageName(newDo.getImageName());
             user.setFeature(newDo.getFeature());
             isSave = FaceApi.getInstance().userUpdateOnly(user);
+            //getVM().addInfo("鏇存柊浜鸿劯==銆�"+newDo.getUserName()+"   缁撴灉锛�"+isSave);
             //System.out.println("==isOk==>鏇存柊鎴愬姛");
         }
 
@@ -1017,7 +1158,7 @@
         SingleBaseConfig.getBaseConfig().setRBGCameraId(index);
         SingleBaseConfig.getBaseConfig().setRgbRevert(true);
         SingleBaseConfig.getBaseConfig().setRgbDetectDirection(90);
-        SingleBaseConfig.getBaseConfig().setRgbVideoDirection(270);
+        SingleBaseConfig.getBaseConfig().setRgbVideoDirection(90);
         GateConfigUtils.modityJson();
         RegisterConfigUtils.modityJson();
     }
@@ -1041,14 +1182,21 @@
         }
     }
 
+    private UpdateUtil updateUtil;
     private void checkUpdate(){
         //鐗堟湰鏇存柊
-        new UpdateUtil(this).setShowToast(false).getServerVerCode(new UpdateUtil.VersionCallBack() {
-            @Override
-            public void isNeedUpdate(boolean isUpdate) {
-                isUpdateVersion = isUpdate;
-            }
-        });
+        if(updateUtil==null){
+            updateUtil = new UpdateUtil(this);
+            updateUtil.setShowToast(false);
+        }
+        if(!updateUtil.isShowing()){
+            updateUtil.getServerVerCode(new UpdateUtil.VersionCallBack() {
+                @Override
+                public void isNeedUpdate(boolean isUpdate) {
+                    isUpdateVersion = isUpdate;
+                }
+            });
+        }
     }
 
     private PermissiondDialog permissiondDialog;
@@ -1080,7 +1228,7 @@
     }
 
     private int tipDownCount = 10;
-    private int loopDownCount = 30;
+    private int loopDownCount = 60;
     private boolean isShowTip;
     @Subscribe
     public void TimeClockEvent(TimeClockEvent e){
@@ -1095,9 +1243,11 @@
                 }
             }
             if(loopDownCount==0){
-                loopDownCount = SpUtil.getInt("loop_time",30);
+                loopDownCount = SpUtil.getInt("loop_time",60);
                 //瀹氭椂鎷夊彇鍩烘湰淇℃伅
                 getVM().devLogin();
+                getPermission();
+                getVM().devHeart();
             }else {
                 loopDownCount--;
             }
@@ -1112,8 +1262,12 @@
     public void OpenGridEvent(OpenGridEvent e){
         if(!isFinishing()){
             //寮�鍚煖闂�
-            String code = "8A"+SportUtils.intToHex(Integer.parseInt(e.getBoardCode()))
-                    +SportUtils.intToHex(Integer.parseInt(e.getChannelCode()))+"11";
+            String key = e.getKey();
+            if(TextUtils.isEmpty(key)) {
+                key = SportUtils.intToHex(Integer.parseInt(e.getBoardCode()))
+                        + SportUtils.intToHex(Integer.parseInt(e.getChannelCode()));
+            }
+            String code = "8A" + key + "11";
             code += SportUtils.getBCC(code);
             send485(0,code);
         }
@@ -1176,38 +1330,6 @@
         }
     }
 
-
-    @Subscribe
-    public void CloseGridOneResultEvent(CloseGridOneResultEvent e){
-        if(!isFinishing()&&isShowing){
-            //鍦ㄩ椤碉紝鏍¢獙鍏抽棬鐨勯挜鍖欐槸鍚﹀尮閰�
-            if (e.getIsOpen() == 0) {
-                //鍏抽棬鎴愬姛
-                //鏌ヨ閽ュ寵鍙�
-                CabinetGridDo gridDo = DaoManager.getCabinetGridDao().getGridByKey(e.getKey());
-                if (gridDo != null) {
-                    getVM().addInfo("姣旇緝閽ュ寵锛氬綋鍓嶏細"+gridDo.getCurKeyCode()+", 鍏宠仈閽ュ寵锛�"+gridDo.getKeyCode());
-                    if(!TextUtils.isEmpty(gridDo.getCurKeyCode())&&
-                            !TextUtils.isEmpty(gridDo.getKeyCode())&&
-                            !gridDo.getCurKeyCode().equals(gridDo.getKeyCode())){
-                        //閽ュ寵涓嶄竴鏍凤紝寮�闂�
-                        ToastView.show(MApplication.mContext,gridDo.getCabinetName()+"閽ュ寵瀛樻斁浣嶇疆閿欒");
-                        EventBus.getDefault().post(new OpenGridEvent(Integer.parseInt(e.getKey().substring(2,4), 16)+"",
-                                Integer.parseInt(e.getKey().substring(0,2), 16)+""));
-                    }else {
-                        if(isShowTip){
-                            doRegister(5,null);
-                        }
-                        if(isShowing){
-                            //鍦ㄩ椤靛叧闂ㄦ垚鍔燂紝闇�璋冪敤鍏抽棬鏂规硶
-                            getVM().closeGrid(gridDo);
-                        }
-                    }
-                }
-            }
-        }
-    }
-
     private SerialPortModel gridPort;
     private SerialPortReadObserver gridReadObserver = new SerialPortReadObserver() {
         @Override
@@ -1267,30 +1389,18 @@
                     if(gridDo!=null){
                         gridDo.setIsOpen("00".equals(open)?1:0);
                         gridDo.setUpdateTime(StringUtil.DateToStr(new Date()));
-                        //todo 鍘绘帀
-                        EventBus.getDefault().post(new HttpEvent(StringUtil.DateToStrSS(new Date())+"=================>鏇存柊鏁版嵁褰撳墠閽ュ寵3锛�"+gridDo.getCurKeyCode()));
 
                         DaoManager.getCabinetGridDao().update(gridDo);
                         EventBus.getDefault().post(new ManageOpenGridResultEvent());
                         if(gridDo.getIsOpen()==0) {
                             //寮�闂ㄥけ璐�
-                            if (isShowing) {
-                                //棣栭〉寮�闂ㄥけ璐ワ紝閽ュ寵涓嶅
-                                if (!TextUtils.isEmpty(gridDo.getCurKeyCode()) &&
-                                        !TextUtils.isEmpty(gridDo.getKeyCode()) &&
-                                        !gridDo.getCurKeyCode().equals(gridDo.getKeyCode())) {
-                                    //閽ュ寵涓嶄竴鏍凤紝寮�闂�
-                                    ToastView.show(MApplication.mContext, gridDo.getCabinetName() + "閽ュ寵瀛樻斁浣嶇疆閿欒");
-                                    EventBus.getDefault().post(new OpenGridEvent(Integer.parseInt(gridDo.getGridKey().substring(2, 4), 16) + "",
-                                            Integer.parseInt(gridDo.getGridKey().substring(0, 2), 16) + ""));
-                                }
-                            }
+                            EventBus.getDefault().post(new OpenErrEvent(key));
                         }else {
-                            //寮�闂ㄦ垚鍔�
+                            //寮�闂ㄨ繑鍥�
+                            EventBus.getDefault().post(new OpenGridOneResultEvent(key,"00".equals(open)?1:0));
                         }
                     }
-                    //寮�闂ㄨ繑鍥�
-                    EventBus.getDefault().post(new OpenGridOneResultEvent(key,"00".equals(open)?1:0));
+
                 }else if(data.length()==16){
                     //鎵归噺
                     //8A 02 00 00 00 00 11 99
@@ -1314,14 +1424,6 @@
                                 updateList.add(gridDo);
                             }
                         }
-                        runOnUiThread(() -> {
-                            getVM().addInfo("鎵归噺寮�闂ㄦ洿鏂板簱鏁版嵁:鐗堝彿锛�" +bh+","+updateList.size());
-                        });
-                        //todo 鍘绘帀
-                        CabinetGridDo gridDo = oldMap.get("0101");
-                        if(gridDo!=null) {
-                            EventBus.getDefault().post(new HttpEvent(StringUtil.DateToStrSS(new Date())+"=================>鏇存柊鏁版嵁褰撳墠閽ュ寵6锛�" + gridDo.getCurKeyCode()));
-                        }
 
                         getVM().updateGrids(updateList);
                         EventBus.getDefault().post(new ManageOpenGridResultEvent());
@@ -1341,16 +1443,13 @@
                 if(gridDo!=null){
                     gridDo.setIsOpen("00".equals(open)?1:0);
                     gridDo.setUpdateTime(StringUtil.DateToStr(new Date()));
-                    //todo 鍘绘帀
-                    EventBus.getDefault().post(new HttpEvent(StringUtil.DateToStrSS(new Date())+"=================>鏇存柊鏁版嵁褰撳墠閽ュ寵3锛�"+gridDo.getCurKeyCode()));
-
                     DaoManager.getCabinetGridDao().update(gridDo);
                 }
                 //鍏抽棬杩斿洖
                 EventBus.getDefault().post(new CloseGridOneResultEvent(key,"00".equals(open)?1:0));
                 if(isShowing&&isShowTip){
                     //淇敼鏄剧ず鎻愰啋
-                    doRegister(5,null);
+                    getVM().doAction(5);
                 }
             }else if(data.startsWith("80")){
                 //鏌ヨ鎵�鏈夐棬鐘舵��
@@ -1393,12 +1492,6 @@
                         runOnUiThread(() -> {
                             getVM().addInfo("鏇存柊搴撴暟鎹�:鐗堝彿锛�" +bh+","+ addList.size()+","+updateList.size());
                         });
-                        //todo 鍘绘帀
-                        CabinetGridDo gridDo = oldMap.get("0101");
-                        if(gridDo!=null){
-                            EventBus.getDefault().post(new HttpEvent(StringUtil.DateToStrSS(new Date())+
-                                    "=================>鏇存柊鏁版嵁褰撳墠閽ュ寵5锛�"+gridDo.getCurKeyCode()));
-                        }
 
                         getVM().updateGrids(updateList);
                         if(isShowing){
@@ -1520,15 +1613,9 @@
                 }
             }
         }
-        //todo 鍘绘帀
-        CabinetGridDo gridDo = oldMap.get("0101");
-        if(gridDo!=null) {
-            EventBus.getDefault().post(new HttpEvent(StringUtil.DateToStrSS(new Date())+
-                    "=================>鏇存柊鏁版嵁褰撳墠閽ュ寵7锛�" + gridDo.getCurKeyCode()));
-        }
 
         getVM().updateGrids(updateList);
-        EventBus.getDefault().post(new KeyResultEvent());
+        EventBus.getDefault().post(new KeyResultEvent(bh+""));
     }
 
     private SerialPortModel jiuPort;
@@ -1552,12 +1639,9 @@
             }
             try {
                 runOnUiThread(() -> {
-                    //getVM().addInfo("鑾峰彇鍒伴厭绮炬娴嬫暟鎹�:"+StringUtil.DateToStrSS(new Date())+" " + new String(bytes));
+                    getVM().addInfo("鑾峰彇鍒伴厭绮炬娴嬫暟鎹�:"+StringUtil.DateToStrSS(new Date())+" " + new String(bytes));
                 });
                 String data = new String(bytes);
-                runOnUiThread(() -> {
-                    //getVM().addInfo("鑾峰彇鍒伴厭绮炬娴嬫槸鍚︽娴嬪け璐�:" + (data.contains("blow fail")));
-                });
                 if(data.contains("blow fail")){
                     //妫�娴嬪け璐�
                     EventBus.getDefault().post(new JiujinResultEvent(false,null));
@@ -1595,20 +1679,28 @@
                 return;
             }
             runOnUiThread(() -> {
-                getVM().addInfo("鑾峰彇鍒伴棬鏁版嵁:" + SportUtils.bytesToHexSimple(bytes));
+                getVM().addInfo("鑾峰彇鍒版暟鎹�:" + SportUtils.bytesToHexSimple(bytes));
             });
+            String data = SportUtils.bytesToHexSimple(bytes);
             if(startIndex==1){
-                gridPath = path;
-                getVM().addInfo("璁剧疆闂ㄤ覆鍙h矾寰�:" + path);
-                SpUtil.saveString("port_grid",path);
+                if(data.toLowerCase().startsWith("80")){
+                    gridPath = path;
+                    getVM().addInfo("璁剧疆闂ㄤ覆鍙h矾寰�:=====XXXXX===>" + path);
+                    SpUtil.saveString("port_grid",path);
+                }
             }else if(startIndex==2){
-                keyPath = path;
-                getVM().addInfo("璁剧疆閽ュ寵涓插彛璺緞:" + path);
-                SpUtil.saveString("port_key",path);
+                if(data.toLowerCase().startsWith("cc")){
+                    keyPath = path;
+                    getVM().addInfo("璁剧疆閽ュ寵涓插彛璺緞:====XXXX====>" + path);
+                    SpUtil.saveString("port_key",path);
+                }
             }else if(startIndex==3){
-                jiuPath = path;
-                getVM().addInfo("璁剧疆閰掔簿涓插彛璺緞:" + path);
-                SpUtil.saveString("port_jiu",path);
+                String data2 = new String(bytes);
+                if(data2.toLowerCase().startsWith("mic")) {
+                    jiuPath = path;
+                    getVM().addInfo("璁剧疆閰掔簿涓插彛璺緞:====XXXX====>" + path);
+                    SpUtil.saveString("port_jiu", path);
+                }
             }
         }
     };
@@ -1627,8 +1719,8 @@
             switch (msg.what){
                 case 0:
                     if(index>=1&&!checkEnd()){
-                        path = ports.get(index);
                         index--;
+                        path = ports.get(index);
                         portHandler.sendEmptyMessage(4);
                     }else if(checkEnd()){
                         initPort();
@@ -1691,18 +1783,35 @@
         sendPortHandlerMsg(2,"A002010000000003",time+400);
     }
 
+    private String jiu_blow;
+    private String jiu_mic_ad;
+    private String jiu_alarm;
+    private String jiu_unit;
     private void setJiuConfig(long time){
         CabinetConfigDataBean config = MApplication.getConfigBean().getCabinetConfigDataVO();
-        //璁剧疆鍚规皵鏃堕棿
-        sendPortHandlerMsg(3,"blow:"+config.getBlowTime(),time+100);
-        //鍘嬪姏鍊�
-        sendPortHandlerMsg(3,"mic_ad:"+config.getPressure(),time+300);
-        //娴撳害闃堝��
-        sendPortHandlerMsg(3,"alarm:"+config.getConcentration(),time+600);
-        //娴撳害闃堝�煎崟浣� (1=mg/100ml;2=mg/L;3=%BAC)
-        sendPortHandlerMsg(3,"unit:"+config.getThreshold(),time+900);
-
-        sendPortHandlerMsg(3,"read"+config.getThreshold(),time+1200);
+        if(!config.getBlowTime().equals(jiu_blow)) {
+            //璁剧疆鍚规皵鏃堕棿
+            jiu_blow = config.getBlowTime();
+            sendPortHandlerMsg(3, "blow:" + config.getBlowTime(), time += 100);
+        }
+        if(!config.getPressure().equals(jiu_mic_ad)) {
+            //鍘嬪姏鍊�
+            jiu_mic_ad = config.getPressure();
+            sendPortHandlerMsg(3, "mic_ad:" + config.getPressure(), time += 300);
+        }
+        if(!config.getConcentration().equals(jiu_alarm)) {
+            //娴撳害闃堝��
+            jiu_alarm = config.getConcentration();
+            sendPortHandlerMsg(3, "alarm:" + config.getConcentration(), time += 300);
+        }
+        if("3".equals(config.getThreshold())){
+            config.setThreshold("0");
+        }
+        if(!config.getThreshold().equals(jiu_unit)) {
+            //娴撳害闃堝�煎崟浣� (1=mg/100ml;2=mg/L;3=%BAC)
+            jiu_unit = config.getThreshold();
+            sendPortHandlerMsg(3, "unit:" + config.getThreshold(), time += 300);
+        }
     }
 
     private void sendPortHandlerMsg(int what,String obj,long time){
@@ -1715,90 +1824,97 @@
     private boolean isInitPort;
     //鍒濆鍖栦覆鍙�
     private void initPort(){
-        SpUtil.setString("port_grid","/dev/ttyS7");
-        SpUtil.setString("port_key","/dev/ttyS1");
-        SpUtil.setString("port_jiu","/dev/ttyS2");
-        gridPath = SpUtil.getString("port_grid");
-        //getVM().addInfo("闂ㄤ覆鍙i摼鎺ワ細"+gridPath);
-        keyPath = SpUtil.getString("port_key");
-        //getVM().addInfo("閽ュ寵涓插彛閾炬帴锛�"+keyPath);
-        jiuPath = SpUtil.getString("port_jiu");
-        //getVM().addInfo("閰掔簿涓插彛閾炬帴锛�"+jiuPath);
-        if(!TextUtils.isEmpty(SpUtil.getString("port_grid"))&&
-                !TextUtils.isEmpty(SpUtil.getString("port_key"))&&
-                !TextUtils.isEmpty(SpUtil.getString("port_jiu"))){
-            if(isInitPort){
-                return;
-            }
-            xhCount=0;
-            closePort(chosePort);
-            if(!TextUtils.isEmpty(SpUtil.getString("port_grid"))){
-                String path = SpUtil.getString("port_grid");
-                closePort(gridPort);
-                getVM().addInfo("闂ㄤ覆鍙i摼鎺ワ細"+path);
-                gridPort = new SerialPortModel(path,9600 ,8
-                        ,1 , 0,gridReadObserver);
-                boolean isSucc = gridPort.open();
-                if(isSucc) {
-                    isInitPort = true;
-                    getVM().addInfo("闂ㄤ覆鍙i摼鎺ユ垚鍔燂細"+path);
-                    String msg = isSucc ? "鎴愬姛" : "澶辫触";
-                    //getVM().append("涓插彛 "+ settingBean.getDevicePath() + " -杩炴帴"+msg);
-                    gridPort.startRead();
-                    //鏌ヨ鎵�鏈夋煖鏍奸棬淇℃伅
-                   checkGridStatus(300);
-                }else {
-                    getVM().addInfo("闂ㄤ覆鍙i摼鎺ュけ璐ワ細"+path);
+        try {
+            /*SpUtil.setString("port_grid","/dev/ttyS0");
+            SpUtil.setString("port_key","/dev/ttyS4");
+            SpUtil.setString("port_jiu","/dev/ttyS9");*/
+
+            gridPath = SpUtil.getString("port_grid");
+            getVM().addInfo("闂ㄤ覆鍙i摼鎺ワ細"+gridPath);
+            keyPath = SpUtil.getString("port_key");
+            getVM().addInfo("閽ュ寵涓插彛閾炬帴锛�"+keyPath);
+            jiuPath = SpUtil.getString("port_jiu");
+            getVM().addInfo("閰掔簿涓插彛閾炬帴锛�"+jiuPath);
+            if(!TextUtils.isEmpty(SpUtil.getString("port_grid"))&&
+                    !TextUtils.isEmpty(SpUtil.getString("port_key"))&&
+                    !TextUtils.isEmpty(SpUtil.getString("port_jiu"))){
+                if(isInitPort){
+                    return;
                 }
-            }
-            if(!TextUtils.isEmpty(SpUtil.getString("port_key"))){
-                String path = SpUtil.getString("port_key");
-                closePort(keyPort);
-                keyPort = new SerialPortModel(path,115200 ,8
-                        ,1 , 0,keyReadObserver);
-                boolean isSucc = keyPort.open();
-                if(isSucc) {
-                    isInitPort = true;
-                    getVM().addInfo("閽ュ寵涓插彛閾炬帴鎴愬姛锛�"+path);
-                    String msg = isSucc ? "鎴愬姛" : "澶辫触";
-                    //getVM().append("涓插彛 "+ settingBean.getDevicePath() + " -杩炴帴"+msg);
-                    keyPort.startRead();
-                    checkKeyStatus(1500);
-                }else {
-                    getVM().addInfo("閽ュ寵涓插彛閾炬帴澶辫触锛�"+path);
+                xhCount=0;
+                closePort(chosePort);
+                if(!TextUtils.isEmpty(SpUtil.getString("port_grid"))){
+                    String path = SpUtil.getString("port_grid");
+                    closePort(gridPort);
+                    getVM().addInfo("闂ㄤ覆鍙i摼鎺ワ細"+path);
+                    gridPort = new SerialPortModel(path,9600 ,8
+                            ,1 , 0,gridReadObserver);
+                    boolean isSucc = gridPort.open();
+                    if(isSucc) {
+                        isInitPort = true;
+                        getVM().addInfo("闂ㄤ覆鍙i摼鎺ユ垚鍔燂細"+path);
+                        String msg = isSucc ? "鎴愬姛" : "澶辫触";
+                        //getVM().append("涓插彛 "+ settingBean.getDevicePath() + " -杩炴帴"+msg);
+                        gridPort.startRead();
+                        //鏌ヨ鎵�鏈夋煖鏍奸棬淇℃伅
+                        checkGridStatus(300);
+                    }else {
+                        getVM().addInfo("闂ㄤ覆鍙i摼鎺ュけ璐ワ細"+path);
+                    }
                 }
-            }
-            if(!TextUtils.isEmpty(SpUtil.getString("port_jiu"))){
-                String path = SpUtil.getString("port_jiu");
-                closePort(jiuPort);
-                jiuPort = new SerialPortModel(path,9600 ,8
-                        ,1 , 0,jiuReadObserver);
-                boolean isSucc = jiuPort.open();
-                if(isSucc) {
-                    isInitPort = true;
-                    getVM().addInfo("閰掔簿涓插彛閾炬帴鎴愬姛锛�"+path);
-                    String msg = isSucc ? "鎴愬姛" : "澶辫触";
-                    //getVM().append("涓插彛 "+ settingBean.getDevicePath() + " -杩炴帴"+msg);
-                    jiuPort.startRead();
-                }else {
-                    getVM().addInfo("閰掔簿涓插彛閾炬帴澶辫触锛�"+path);
+                if(!TextUtils.isEmpty(SpUtil.getString("port_key"))){
+                    String path = SpUtil.getString("port_key");
+                    closePort(keyPort);
+                    keyPort = new SerialPortModel(path,115200 ,8
+                            ,1 , 0,keyReadObserver);
+                    boolean isSucc = keyPort.open();
+                    if(isSucc) {
+                        isInitPort = true;
+                        getVM().addInfo("閽ュ寵涓插彛閾炬帴鎴愬姛锛�"+path);
+                        String msg = isSucc ? "鎴愬姛" : "澶辫触";
+                        //getVM().append("涓插彛 "+ settingBean.getDevicePath() + " -杩炴帴"+msg);
+                        keyPort.startRead();
+                        checkKeyStatus(1500);
+                    }else {
+                        getVM().addInfo("閽ュ寵涓插彛閾炬帴澶辫触锛�"+path);
+                    }
                 }
-            }
-        }else {
-            if(xhCount>2){
-                return;
-            }
-            xhCount++;
-            ports = SportUtils.getSerialPortPaths(this);
-            if(ports==null){
-                getVM().addInfo("鏈煡璇㈠埌涓插彛鍒楄〃");
+                if(!TextUtils.isEmpty(SpUtil.getString("port_jiu"))){
+                    String path = SpUtil.getString("port_jiu");
+                    closePort(jiuPort);
+                    jiuPort = new SerialPortModel(path,9600 ,8
+                            ,1 , 0,jiuReadObserver);
+                    boolean isSucc = jiuPort.open();
+                    if(isSucc) {
+                        isInitPort = true;
+                        getVM().addInfo("閰掔簿涓插彛閾炬帴鎴愬姛锛�"+path);
+                        String msg = isSucc ? "鎴愬姛" : "澶辫触";
+                        //getVM().append("涓插彛 "+ settingBean.getDevicePath() + " -杩炴帴"+msg);
+                        jiuPort.startRead();
+                    }else {
+                        getVM().addInfo("閰掔簿涓插彛閾炬帴澶辫触锛�"+path);
+                    }
+                }
             }else {
-                getVM().addInfo("涓插彛鍒楄〃锛�"+ports.size());
+                if(xhCount>2){
+                    return;
+                }
+                xhCount++;
+                ports = SportUtils.getSerialPortPaths(this);
+                if(ports==null){
+                    getVM().addInfo("鏈煡璇㈠埌涓插彛鍒楄〃");
+                }else {
+                    getVM().addInfo("涓插彛鍒楄〃锛�"+ports.size());
+                }
+                if(ports.size()>0){
+                    index = ports.size();
+                    portHandler.sendEmptyMessage(0);
+                }
             }
-            if(ports.size()>0){
-                index = ports.size()-1;
-                portHandler.sendEmptyMessage(0);
-            }
+        }catch (RuntimeException e){
+            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
+        }catch (Exception e){
+            Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
         }
     }
 
@@ -1867,10 +1983,6 @@
         runOnUiThread(new Runnable() {
             @Override
             public void run() {
-                //todo 鍘绘帀
-                if(type==2){
-                    return;
-                }
                 getVM().addInfo("鍙戦�佹寚浠わ細"+StringUtil.DateToStrSS(new Date())+"==>"+code);
             }
         });
@@ -1920,21 +2032,157 @@
 
     private boolean checkEnd(){
         if(TextUtils.isEmpty(gridPath)){
-            getVM().addInfo("闂ㄩ敊璇�");
+            getVM().addInfo("闂ㄩ敊璇�<<<<<<<<<<<<<<<<<<<");
             startIndex=0;
             return false;
         }
         if(TextUtils.isEmpty(keyPath)){
-            getVM().addInfo("閽ュ寵閿欒");
+            getVM().addInfo("閽ュ寵閿欒<<<<<<<<<<<<<<<<<<<");
             startIndex=1;
             return false;
         }
         if(TextUtils.isEmpty(jiuPath)){
-            getVM().addInfo("閰掔簿閿欒");
+            getVM().addInfo("閰掔簿閿欒<<<<<<<<<<<<<<<<<<<");
             startIndex=2;
             return false;
         }
         getVM().addInfo("涓插彛鍏ㄩ儴閾炬帴鎴愬姛锛�");
         return true;
     }
+
+    //寮�闂�
+    @Subscribe
+    public void OpenGridOneResultEvent(OpenGridOneResultEvent e){
+        if(!isFinishing()&&isShowing){
+            //寮�闂ㄦ垚鍔燂紝绉婚櫎寮�闂�
+            if(handler.hasMessages(1)){
+                handler.removeMessages(1);
+                getVM().addInfo("==绉婚櫎寮�闂ㄤ换鍔�");
+            }
+        }
+    }
+
+    private Long keyTime;
+    private HashMap<String,String> closeMap = new HashMap<>();
+    /**
+     * 鍏抽棬
+     * @param e
+     */
+    @Subscribe(threadMode= ThreadMode.MAIN)
+    public void CloseGridOneResultEvent(CloseGridOneResultEvent e){
+        if(!isFinishing()&&isShowing){
+            //鍏抽棬鎴愬姛锛岀Щ闄ゅ紑闂�
+            if(handler.hasMessages(1)){
+                handler.removeMessages(1);
+                getVM().addInfo("==绉婚櫎寮�闂ㄤ换鍔�");
+            }
+            closeMap.put(e.getKey(),"");
+            //鍙戦�佹煡璇㈤挜鍖欏彿
+            EventBus.getDefault().post(new GetKeyStatusEvent());
+            keyTime = new Date().getTime()+500;
+            if(handler.hasMessages(2)){
+                handler.removeMessages(2);
+            }
+            handler.sendEmptyMessageDelayed(2,1200);
+        }
+    }
+
+    @Subscribe(threadMode= ThreadMode.MAIN)
+    public void OpenErrEvent(OpenErrEvent e){
+        //寮�闂ㄥけ璐�
+        if(!isFinishing()&&isShowing){
+            //寮�闂�
+            if(handler.hasMessages(1)){
+                handler.removeMessages(1);
+                getVM().addInfo("==绉婚櫎寮�闂ㄤ换鍔�");
+            }
+            EventBus.getDefault().post(new OpenGridEvent(e.getKey()));
+            Message msg = Message.obtain();
+            msg.what=1;
+            msg.obj = e.getKey();
+            handler.sendMessageDelayed(msg,1000);
+        }
+    }
+
+    @Subscribe
+    public void KeyResultEvent(KeyResultEvent e){
+        if(!isFinishing()&&isShowing){
+            //鏍¢獙鏃堕棿
+            if(keyTime==null){
+                return;
+            }
+            if(keyTime!=null&&keyTime>System.currentTimeMillis()){
+                //鏍¢獙杩斿洖鏃堕棿<鍛戒护鏃堕棿
+                return;
+            }
+            if(handler.hasMessages(2)){
+                handler.removeMessages(2);
+            }
+            HashMap<String,String> copyMap = new HashMap<>();
+            copyMap.putAll(closeMap);
+            closeMap.clear();
+            //鏍¢獙鍗″彿涓嶅鐨勶紝鐩存帴寮瑰嚭
+            List<CabinetGridDo> gridDos = DaoManager.getCabinetGridDao().loadAll();
+            List<String> keys = new ArrayList<>();
+            StringBuilder sb = new StringBuilder();
+            for(CabinetGridDo gridDo:gridDos){
+                //瑕佸湪鑼冨洿鍐�
+                if(copyMap.containsKey(gridDo.getGridKey())){
+                    boolean isFail = false;
+                    if(TextUtils.isEmpty(gridDo.getGridKey())){
+                        if(!TextUtils.isEmpty(gridDo.getCurKeyCode())&&gridDo.getIsOpen()==0){
+                            //鏈粦瀹氱殑瀛樻斁浜嗛挜鍖�
+                            keys.add(gridDo.getGridKey());
+                            if(sb.length()>0){
+                                sb.append("銆�");
+                            }
+                            sb.append(gridDo.getCabinetName());
+                            isFail = true;
+                        }
+                    }else {
+                        if(!TextUtils.isEmpty(gridDo.getCurKeyCode())){
+                            if(!gridDo.getCurKeyCode().equals(gridDo.getKeyCode())&&gridDo.getIsOpen()==0){
+                                //閽ュ寵涓嶅尮閰�
+                                keys.add(gridDo.getGridKey());
+                                if(sb.length()>0){
+                                    sb.append("銆�");
+                                }
+                                sb.append(gridDo.getCabinetName());
+                                isFail = true;
+                            }
+                        }
+                    }
+                    if(!isFail){
+                        //鍏抽棬鎴愬姛
+                        boolean isSend = false;
+                        String bh = gridDo.getGridKey().substring(0,2);
+                        if("1".equals(e.getType())){
+                            //1鍙锋澘
+                            if("01".equals(bh)){
+                                isSend = true;
+                            }
+                        }else if("2".equals(e.getType())){
+                            //2鍙锋澘
+                            if("02".equals(bh)){
+                                isSend = true;
+                            }
+                        }else {
+                            isSend = true;
+                        }
+                        if(isSend) {
+                            EventBus.getDefault().post(new HttpEvent(StringUtil.DateToStrSS(new Date()) + "闂ㄥ叧闂垚鍔�***************************>鐩爣锛�" + gridDo.getKeyCode() + ",褰撳墠锛�" + gridDo.getCurKeyCode()));
+                            getVM().closeGrid(gridDo);
+                        }
+                    }
+                }
+            }
+            if(sb.length()>0){
+                ToastView.show(MApplication.mContext, sb.toString() + "閽ュ寵瀛樻斁浣嶇疆閿欒");
+            }
+            if(keys.size()>0){
+                //寮�闂�
+                EventBus.getDefault().post(new OpenGridListEvent(keys));
+            }
+        }
+    }
 }

--
Gitblit v1.9.3