| | |
| | | |
| | | import com.dayu.recharge.dao.BaseDaoSingleton; |
| | | import com.dayu.recharge.dbBean.IpBean; |
| | | import com.dayu.recharge.net.CallbackIDFactoryImpl; |
| | | import com.dayu.recharge.net.ScoketMessageProtocol; |
| | | import com.dayu.recharge.net.SocketCallBack; |
| | | import com.dayu.recharge.net.SocketData; |
| | |
| | | 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 |
| | |
| | | try { |
| | | ipBean = BaseDaoSingleton.getInstance(MyApplication.myApplication).ipDao().findFirst(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | 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后修改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(), "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 (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) { |
| | |
| | | } |
| | | } |
| | | } |
| | | } 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(); |
| | | } |
| | |
| | | initMachineRequestBean.setAddress(addressCode); |
| | | initMachineRequestBean.setXuLie(SocketUtil.getXuLie(this)); |
| | | initMachineRequestBean.setAFN("91"); |
| | | SocketNet.getInstance().sendTestMessage(initMachineRequestBean.pack(), new SocketCallBack() { |
| | | SocketNet.getInstance().sendTestMessage(initMachineRequestBean.pack(), "91",new SocketCallBack() { |
| | | @Override |
| | | public void onSocketResponse(SocketData readData) { |
| | | |
| | |
| | | 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); |
| | |
| | | // WSHelper.getInstance(MyApplication.this).put("initCode", initCode); |
| | | ToastUtil.show("充值管理注册报文获取成功"); |
| | | } else { |
| | | Log.i(TAG, "注册失败--- 状态码:" + state); |
| | | //校验CRC错误 |
| | | ToastUtil.show("确认收到注册报文状态失败"); |
| | | // ToastUtil.show("确认收到注册报文状态失败"); |
| | | } |
| | | } else { |
| | | //校验CRC错误 |
| | |
| | | .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(); |
| | | |