|  |  |  | 
|---|
|  |  |  | import com.dayu.recharge.utils.TipUtil; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.net.InetAddress; | 
|---|
|  |  |  | import java.util.Arrays; | 
|---|
|  |  |  | import java.util.Timer; | 
|---|
|  |  |  | import java.util.TimerTask; | 
|---|
|  |  |  | 
|---|
|  |  |  | super.handleMessage(msg); | 
|---|
|  |  |  | switch (msg.what) { | 
|---|
|  |  |  | case 1: | 
|---|
|  |  |  | createSocket(msg.obj + "", ipBean.getPort()); | 
|---|
|  |  |  | 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()); | 
|---|
|  |  |  | createSocket(ipBean.getIp(), ipBean.getPort(), "0", 88); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | if (EasySocket.getInstance().getDefconnection() != null) { | 
|---|
|  |  |  | SocketAddress address = EasySocket.getInstance().getDefconnection().getOptions().getSocketAddress(); | 
|---|
|  |  |  | 
|---|
|  |  |  | && !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(), 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(), 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; | 
|---|
|  |  |  | 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("确认收到注册报文校验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("充值管理注册报文校验CRC错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } else {//校验CRC错误 | 
|---|
|  |  |  | ToastUtil.show("充值管理注册报文校验CRC错误"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @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() | 
|---|
|  |  |  | // 主机地址,请填写自己的IP地址,以getString的方式是为了隐藏作者自己的IP地址 | 
|---|
|  |  |  | 
|---|
|  |  |  | .setMaxWriteBytes(150) | 
|---|
|  |  |  | .setMaxReadBytes(150) | 
|---|
|  |  |  | .setRequestTimeout(5 * 1000) | 
|---|
|  |  |  | .setBackupAddress(new SocketAddress(backUpIp, backUpPort)) | 
|---|
|  |  |  | .build(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 初始化 | 
|---|