From 1b4835badb3ca32402b6544c668a700c5334d90f Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期二, 26 三月 2024 16:48:31 +0800
Subject: [PATCH] 添加中安身份证识别模块 把IP设置移动到参数设置界面

---
 app/src/main/java/com/dayu/recharge/MyApplication.java |  241 +++++++++++++++++++++++++----------------------
 1 files changed, 129 insertions(+), 112 deletions(-)

diff --git a/app/src/main/java/com/dayu/recharge/MyApplication.java b/app/src/main/java/com/dayu/recharge/MyApplication.java
index 41f25f0..7fe9e91 100644
--- a/app/src/main/java/com/dayu/recharge/MyApplication.java
+++ b/app/src/main/java/com/dayu/recharge/MyApplication.java
@@ -1,21 +1,14 @@
 package com.dayu.recharge;
 
 import android.app.Application;
-import android.nfc.Tag;
 import android.os.Handler;
 import android.os.Message;
 import android.text.TextUtils;
 import android.util.Log;
 
 import com.dayu.recharge.dao.BaseDaoSingleton;
-import com.dayu.recharge.dbBean.DeviceNumber;
 import com.dayu.recharge.dbBean.IpBean;
-import com.dayu.recharge.utils.DeviceNumberUtils;
-import com.dayu.recharge.utils.ToastUtil;
-import com.easysocket.EasySocket;
-import com.easysocket.config.EasySocketOptions;
-import com.easysocket.entity.SocketAddress;
-import com.tencent.bugly.crashreport.CrashReport;
+import com.dayu.recharge.net.CallbackIDFactoryImpl;
 import com.dayu.recharge.net.ScoketMessageProtocol;
 import com.dayu.recharge.net.SocketCallBack;
 import com.dayu.recharge.net.SocketData;
@@ -27,25 +20,30 @@
 import com.dayu.recharge.tools.HexUtil;
 import com.dayu.recharge.utils.AidlUtil;
 import com.dayu.recharge.utils.CRC8;
+import com.dayu.recharge.utils.DeviceNumberUtils;
 import com.dayu.recharge.utils.SocketUtil;
-import com.dayu.recharge.utils.TipUtil;
+import com.dayu.recharge.utils.ToastUtil;
+import com.easysocket.EasySocket;
+import com.easysocket.config.EasySocketOptions;
+import com.easysocket.entity.SocketAddress;
+import com.tencent.bugly.crashreport.CrashReport;
 
-
+import java.net.InetAddress;
 import java.util.Arrays;
 import java.util.Timer;
-import java.util.TimerTask;
 
 public class MyApplication extends Application {
     public static String TAG = "MyApplication";
     public static MyApplication myApplication;
-    private String address;
 
     SocketNet.CreateBack createBack;
     //鏄惁姝e湪璇锋眰鍒濆鍖栨帴鍙�
     public boolean isDoInitMachine = false;
     //鏄惁杩炴帴鏈嶅姟鍣ㄦ垚鍔�
     public boolean isConncet = false;
-    private final Timer timer = new Timer();
+
+    //姣忔鍚姩绋嬪簭鍙幓娉ㄥ唽涓�閬嶏紝涓嶅啀閲嶆柊娉ㄥ唽銆�
+    public static boolean isCreate = false;
 
 
     @Override
@@ -78,50 +76,22 @@
     public void initEasySocket(boolean isCreate, SocketNet.CreateBack createBack) {
         try {
             this.createBack = createBack;
-            ipBean = BaseDaoSingleton.getInstance(MyApplication.myApplication).ipDao().findFirst();
+            try {
+                ipBean = BaseDaoSingleton.getInstance(MyApplication.myApplication).ipDao().findFirst();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
             if (ipBean == null) {
                 ipBean = new IpBean();
-                ipBean.setIp("dayuyanjiuyuan.top");
-                ipBean.setPort(8888);
+                ipBean.setIp("newreceive.hnsjgg.com");
+                ipBean.setPort(9999);
+//                ipBean.setIp("dayuyanjiuyuan.top");
+//                ipBean.setPort(8888);
             }
-            if (ipBean != null) {
-                try {
-                    //澶勭悊褰撹繛鎺ヤ笂IP鍚庝慨鏀筰p涓嶇敓鏁堥棶棰�
-                    if (EasySocket.getInstance().getDefconnection() != null) {
-                        SocketAddress address = EasySocket.getInstance().getDefconnection().getOptions().getSocketAddress();
-                        if (address != null) {
-                            if (!address.getIp().equals(ipBean.getIp()) || address.getPort() != ipBean.getPort()) {
-                                EasySocket.getInstance().destroyConnection();
-                            }
-                        }
-                    }
-                } catch (Exception e) {
-                    e.printStackTrace();
-                }
-
-                if (!SocketUtil.isInteger(ipBean.getIp())) {
-                    //鍩熷悕鏂瑰紡
-                    SocketUtil.parseHostGetIPAddress(ipBean.getIp(), new Handler() {
-                        @Override
-                        public void handleMessage(Message msg) {
-                            super.handleMessage(msg);
-                            switch (msg.what) {
-                                case 1:
-                                    createSocket(msg.obj + "", ipBean.getPort());
-                                    break;
-                                case 2:
-                                    ToastUtil.show("DNS瑙f瀽澶辫触");
-                                    break;
-                            }
-
-                        }
-                    });
-
-                } else {
-                    //ip鏂瑰紡
-                    if (EasySocket.getInstance().getContext() == null || isCreate) {
-                        createSocket(ipBean.getIp(), ipBean.getPort());
-                    } else {
+            if (SocketNet.getInstance().isNetworkAvailable(this)){
+                if (ipBean != null) {
+                    try {
+                        //澶勭悊褰撹繛鎺ヤ笂IP鍚庝慨鏀筰p涓嶇敓鏁堥棶棰�
                         if (EasySocket.getInstance().getDefconnection() != null) {
                             SocketAddress address = EasySocket.getInstance().getDefconnection().getOptions().getSocketAddress();
                             if (address != null) {
@@ -130,10 +100,53 @@
                                 }
                             }
                         }
+                    } catch (Exception e) {
+                        e.printStackTrace();
                     }
 
+                    if (!SocketUtil.isInteger(ipBean.getIp())) {
+                        //鍩熷悕鏂瑰紡
+                        SocketUtil.parseHostGetIPAddress(ipBean.getIp(), new Handler() {
+                            @Override
+                            public void handleMessage(Message msg) {
+                                super.handleMessage(msg);
+                                switch (msg.what) {
+                                    case 1:
+                                        createSocket(msg.obj + "", ipBean.getPort(), "0", 88);
+                                        break;
+                                    case 2:
+                                        InetAddress[] data = (InetAddress[]) msg.obj;
+                                        createSocket(data[0].getHostAddress(), ipBean.getPort(), data[1].getHostAddress(), ipBean.getPort());
+                                        break;
+                                    case -1:
+                                        ToastUtil.show("DNS瑙f瀽澶辫触");
+                                        break;
+                                }
+
+                            }
+                        });
+
+                    } else {
+                        //ip鏂瑰紡
+                        if (EasySocket.getInstance().getContext() == null || isCreate) {
+                            createSocket(ipBean.getIp(), ipBean.getPort(), "0", 88);
+                        } else {
+                            if (EasySocket.getInstance().getDefconnection() != null) {
+                                SocketAddress address = EasySocket.getInstance().getDefconnection().getOptions().getSocketAddress();
+                                if (address != null) {
+                                    if (!address.getIp().equals(ipBean.getIp()) || address.getPort() != ipBean.getPort()) {
+                                        EasySocket.getInstance().destroyConnection();
+                                    }
+                                }
+                            }
+                        }
+
+                    }
                 }
+            }else {
+                ToastUtil.show("璇锋鏌ョ綉缁滆繛鎺�");
             }
+
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -149,89 +162,93 @@
                 && !TextUtils.isEmpty(addressCode)) {
 
             Log.i("initMachineRequestBean", "寮�濮嬫敞鍐岃澶�");
-                if (EasySocket.getInstance().getDefconnection() != null) {
-                    isDoInitMachine = true;
-                    InitMachineRequestBean initMachineRequestBean = new InitMachineRequestBean();
-                    initMachineRequestBean.setControl("01");
-                    //130107200008	112	闈掓硥灞呭浼�
-                    initMachineRequestBean.setAddress(addressCode);
-                    initMachineRequestBean.setXuLie(SocketUtil.getXuLie(this));
-                    initMachineRequestBean.setAFN("91");
-                    SocketNet.getInstance().sendTestMessage(initMachineRequestBean.pack(), new SocketCallBack() {
-                        @Override
-                        public void onSocketResponse(SocketData readData) {
+            if (EasySocket.getInstance().getDefconnection() != null) {
+                isDoInitMachine = true;
+                InitMachineRequestBean initMachineRequestBean = new InitMachineRequestBean();
+                initMachineRequestBean.setControl("01");
+                //130107200008	112	闈掓硥灞呭浼�
+                initMachineRequestBean.setAddress(addressCode);
+                initMachineRequestBean.setXuLie(SocketUtil.getXuLie(this));
+                initMachineRequestBean.setAFN("91");
+                SocketNet.getInstance().sendTestMessage(initMachineRequestBean.pack(), "91",new SocketCallBack() {
+                    @Override
+                    public void onSocketResponse(SocketData readData) {
 
-                            if (CRC8.isCRC8(readData.getOriginDataBytes())) {
-                                //鏍¢獙CRC鎴愬姛
-                                InitMachineBackBean initMachineBackBean = new InitMachineBackBean();
-                                String initCode = HexUtil.bytesToHex(Arrays.copyOfRange(readData.getBodyBytes(), 2, 18));
-                                //纭鏀跺埌娉ㄥ唽鍙锋姤鏂�
-                                ConfirmMachineRequestBean confirmMachineRequestBean = new ConfirmMachineRequestBean();
-                                confirmMachineRequestBean.setControl("01");
-                                //130107200008	112	闈掓硥灞呭浼�
-                                confirmMachineRequestBean.setAddress(addressCode);
-                                confirmMachineRequestBean.setXuLie(SocketUtil.getXuLie(MyApplication.this));
-                                confirmMachineRequestBean.setAFN("92");
-                                confirmMachineRequestBean.setInitCode(initCode);
-                                Log.i("Socket", "纭娉ㄥ唽鎶ユ枃");
-                                SocketNet.getInstance().sendTestMessage(confirmMachineRequestBean.pack(), new SocketCallBack() {
+                        if (CRC8.isCRC8(readData.getOriginDataBytes())) {
+                            //鏍¢獙CRC鎴愬姛
+                            InitMachineBackBean initMachineBackBean = new InitMachineBackBean();
+                            String initCode = HexUtil.bytesToHex(Arrays.copyOfRange(readData.getBodyBytes(), 2, 18));
+                            //纭鏀跺埌娉ㄥ唽鍙锋姤鏂�
+                            ConfirmMachineRequestBean confirmMachineRequestBean = new ConfirmMachineRequestBean();
+                            confirmMachineRequestBean.setControl("01");
+                            //130107200008	112	闈掓硥灞呭浼�
+                            confirmMachineRequestBean.setAddress(addressCode);
+                            confirmMachineRequestBean.setXuLie(SocketUtil.getXuLie(MyApplication.this));
+                            confirmMachineRequestBean.setAFN("92");
+                            confirmMachineRequestBean.setInitCode(initCode);
+                            Log.i("Socket", "纭娉ㄥ唽鎶ユ枃");
+                            SocketNet.getInstance().sendTestMessage(confirmMachineRequestBean.pack(), "92",new SocketCallBack() {
 
-                                    @Override
-                                    public void onSocketResponse(SocketData readData) {
-                                        isDoInitMachine = false;
-                                        if (CRC8.isCRC8(readData.getOriginDataBytes())) {
-                                            byte stateByte = readData.getBodyBytes()[2];
-                                            String state = BcdUtil.bcdToStr(stateByte);
-                                            //BCD鐮�00鎴愬姛01澶辫触(娉ㄥ唽鍙蜂笉鏄湇鍔″櫒杩斿洖淇℃伅)
-                                            if (state.equalsIgnoreCase("00")) {
-                                                //淇濆瓨璁惧娉ㄥ唽鍙�
-                                                if (!TextUtils.isEmpty(initCode)){
-                                                    DeviceNumberUtils.setDeviceNumber(initCode);
-                                                }
-                                                Log.i(TAG, "onSuccess: 璁惧娉ㄥ唽鍙凤細" + initCode);
-//                                            WSHelper.getInstance(MyApplication.this).put("initCode", initCode);
-                                                ToastUtil.show("鍏呭�肩鐞嗘敞鍐屾姤鏂囪幏鍙栨垚鍔�");
-                                            } else {
-                                                //鏍¢獙CRC閿欒
-                                                ToastUtil.show("纭鏀跺埌娉ㄥ唽鎶ユ枃鐘舵�佸け璐�");
+                                @Override
+                                public void onSocketResponse(SocketData readData) {
+                                    isDoInitMachine = false;
+                                    MyApplication.myApplication.isCreate = true;
+                                    if (CRC8.isCRC8(readData.getOriginDataBytes())) {
+                                        byte stateByte = readData.getBodyBytes()[2];
+                                        String state = BcdUtil.bcdToStr(stateByte);
+                                        //BCD鐮�00鎴愬姛01澶辫触(娉ㄥ唽鍙蜂笉鏄湇鍔″櫒杩斿洖淇℃伅)
+                                        if (state.equalsIgnoreCase("00")) {
+                                            //淇濆瓨璁惧娉ㄥ唽鍙�
+                                            if (!TextUtils.isEmpty(initCode)) {
+                                                DeviceNumberUtils.setDeviceNumber(initCode);
                                             }
+                                            Log.i(TAG, "onSuccess: 璁惧娉ㄥ唽鍙凤細" + initCode);
+//                                            WSHelper.getInstance(MyApplication.this).put("initCode", initCode);
+                                            ToastUtil.show("鍏呭�肩鐞嗘敞鍐屾姤鏂囪幏鍙栨垚鍔�");
                                         } else {
+                                            Log.i(TAG, "娉ㄥ唽澶辫触--- 鐘舵�佺爜锛�" + state);
                                             //鏍¢獙CRC閿欒
-                                            ToastUtil.show("纭鏀跺埌娉ㄥ唽鎶ユ枃鏍¢獙CRC閿欒");
+//                                            ToastUtil.show("纭鏀跺埌娉ㄥ唽鎶ユ枃鐘舵�佸け璐�");
                                         }
+                                    } else {
+                                        //鏍¢獙CRC閿欒
+                                        ToastUtil.show("纭鏀跺埌娉ㄥ唽鎶ユ枃鏍¢獙CRC閿欒");
                                     }
+                                }
 
-                                    @Override
-                                    public void onSocketError(int code, String msg) {
+                                @Override
+                                public void onSocketError(int code, String msg) {
 
-                                    }
-                                });
-                            } else {//鏍¢獙CRC閿欒
-                                ToastUtil.show("鍏呭�肩鐞嗘敞鍐屾姤鏂囨牎楠孋RC閿欒");
-                            }
+                                }
+                            });
+                        } else {//鏍¢獙CRC閿欒
+                            ToastUtil.show("鍏呭�肩鐞嗘敞鍐屾姤鏂囨牎楠孋RC閿欒");
                         }
+                    }
 
-                        @Override
-                        public void onSocketError(int code, String msg) {
+                    @Override
+                    public void onSocketError(int code, String msg) {
 
-                        }
-                    });
+                    }
+                });
 
-                }
             }
+        }
 
     }
 
-    private void createSocket(String ip, int port) {
+    private void createSocket(String ip, int port, String backUpIp, int backUpPort) {
         // socket閰嶇疆
         EasySocketOptions options = new EasySocketOptions.Builder()
                 // 涓绘満鍦板潃锛岃濉啓鑷繁鐨処P鍦板潃锛屼互getString鐨勬柟寮忔槸涓轰簡闅愯棌浣滆�呰嚜宸辩殑IP鍦板潃
                 .setSocketAddress(new SocketAddress(ip, port))
                 // 瀹氫箟娑堟伅鍗忚锛屾柟渚胯В鍐� socket榛忓寘銆佸垎鍖呯殑闂
                 .setReaderProtocol(new ScoketMessageProtocol())
+                .setCallbackIDFactory(new CallbackIDFactoryImpl())
                 .setMaxWriteBytes(150)
                 .setMaxReadBytes(150)
-                .setRequestTimeout(5 * 1000)
+                .setRequestTimeout(20 * 1000)
+                .setBackupAddress(new SocketAddress(backUpIp, backUpPort))
                 .build();
 
         // 鍒濆鍖�

--
Gitblit v1.8.0