左晓为主开发手持机充值管理机
zuoxiao
2024-04-02 732e4481b8437719468cddfb4971ee54a4b36625
app/src/main/java/com/dayu/recharge/MyApplication.java
@@ -7,13 +7,8 @@
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.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;
@@ -25,25 +20,31 @@
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;
    //是否正在请求初始化接口
    public boolean isDoInitMachine = false;
    //是否连接服务器成功
    public boolean isConncet = false;
    private final Timer timer = new Timer();
    //每次启动程序只去注册一遍,不再重新注册。
    public static boolean isCreate = false;
    @Override
    public void onCreate() {
@@ -51,7 +52,8 @@
        myApplication = this;
        isAidl = true;
        AidlUtil.getInstance().connectPrinterService(this);
        initEasySocket(false, null);
//        initEasySocket(false, null);
        CrashReport.initCrashReport(getApplicationContext(), "45551598b8", true);
    }
@@ -66,51 +68,30 @@
        isAidl = aidl;
    }
    public IpBean ipBean;
    /**
     * 初始化EasySocket
     */
    public void initEasySocket(boolean isCreate, SocketNet.CreateBack createBack) {
        try {
            this.createBack = createBack;
            IpBean ipBean = BaseDaoSingleton.getInstance(MyApplication.myApplication).ipDao().findFirst();
            if (ipBean != null) {
                try {
                    //处理当连接上IP后修改ip不生效问题
                    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解析失败");
                                    break;
                            }
                        }
                    });
                } else {
                    //ip方式
                    if (EasySocket.getInstance().getContext() == null || isCreate) {
                        createSocket(ipBean.getIp(), ipBean.getPort());
                    } else {
            try {
                ipBean = BaseDaoSingleton.getInstance(MyApplication.myApplication).ipDao().findFirst();
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (ipBean == null) {
                ipBean = new IpBean();
                ipBean.setIp("newreceive.hnsjgg.com");
                ipBean.setPort(9999);
//                ipBean.setIp("dayuyanjiuyuan.top");
//                ipBean.setPort(8888);
            }
            if (SocketNet.getInstance().isNetworkAvailable(this)){
                if (ipBean != null) {
                    try {
                        //处理当连接上IP后修改ip不生效问题
                        if (EasySocket.getInstance().getDefconnection() != null) {
                            SocketAddress address = EasySocket.getInstance().getDefconnection().getOptions().getSocketAddress();
                            if (address != null) {
@@ -119,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解析失败");
                                        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();
        }
@@ -136,6 +160,8 @@
        if (!TextUtils.isEmpty(ip)
                && !TextUtils.isEmpty(String.valueOf(port))
                && !TextUtils.isEmpty(addressCode)) {
            Log.i("initMachineRequestBean", "开始注册设备");
            if (EasySocket.getInstance().getDefconnection() != null) {
                isDoInitMachine = true;
                InitMachineRequestBean initMachineRequestBean = new InitMachineRequestBean();
@@ -144,16 +170,13 @@
                initMachineRequestBean.setAddress(addressCode);
                initMachineRequestBean.setXuLie(SocketUtil.getXuLie(this));
                initMachineRequestBean.setAFN("91");
//
                Log.i("Socket", "注册报文");
                SocketNet.getInstance().sendTestMessage(initMachineRequestBean.pack(), new SocketCallBack() {
                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();
@@ -164,31 +187,28 @@
                            confirmMachineRequestBean.setAFN("92");
                            confirmMachineRequestBean.setInitCode(initCode);
                            Log.i("Socket", "确认注册报文");
                            SocketNet.getInstance().sendTestMessage(confirmMachineRequestBean.pack(), new SocketCallBack() {
                            SocketNet.getInstance().sendTestMessage(confirmMachineRequestBean.pack(), "92",new SocketCallBack() {
                                @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")) {
                                            //保存设备注册号
                                            DeviceNumber deviceNumber = BaseDaoSingleton.getInstance(MyApplication.myApplication).deviceNumberDao().findFirst();
                                            if (deviceNumber != null) {
                                                deviceNumber.setDeviceRegistrationNumber(initCode);
                                            } else {
                                                deviceNumber = new DeviceNumber();
                                                deviceNumber.setDeviceRegistrationNumber(initCode);
                                            if (!TextUtils.isEmpty(initCode)) {
                                                DeviceNumberUtils.setDeviceNumber(initCode);
                                            }
                                            BaseDaoSingleton.getInstance(MyApplication.myApplication).deviceNumberDao().insert(deviceNumber);
                                            Log.i(TAG, "onSuccess: 设备注册号:" + initCode);
//                                            WSHelper.getInstance(MyApplication.this).put("initCode", initCode);
                                            ToastUtil.show("充值管理注册报文获取成功");
                                        } else {
                                            Log.i(TAG, "注册失败--- 状态码:" + state);
                                            //校验CRC错误
                                            ToastUtil.show("确认收到注册报文状态失败");
//                                            ToastUtil.show("确认收到注册报文状态失败");
                                        }
                                    } else {
                                        //校验CRC错误
@@ -213,21 +233,22 @@
                });
            }
        }
    }
    private void createSocket(String ip, int port) {
    private void createSocket(String ip, int port, String backUpIp, int backUpPort) {
        // socket配置
        EasySocketOptions options = new EasySocketOptions.Builder()
                // 主机地址,请填写自己的IP地址,以getString的方式是为了隐藏作者自己的IP地址
                .setSocketAddress(new SocketAddress(ip, port))
                // 定义消息协议,方便解决 socket黏包、分包的问题
                .setReaderProtocol(new ScoketMessageProtocol())
                .setMaxWriteBytes(1024)
                .setMaxReadBytes(1024)
                .setRequestTimeout(10 * 1000)
                .setCallbackIDFactory(new CallbackIDFactoryImpl())
                .setMaxWriteBytes(150)
                .setMaxReadBytes(150)
                .setRequestTimeout(20 * 1000)
                .setBackupAddress(new SocketAddress(backUpIp, backUpPort))
                .build();
        // 初始化