|  |  | 
 |  |  | package com.dayu.recharge; | 
 |  |  |  | 
 |  |  | import android.app.Application; | 
 |  |  | 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.easysocket.EasySocket; | 
 |  |  | import com.easysocket.config.EasySocketOptions; | 
 |  |  | import com.easysocket.connection.action.SocketStatus; | 
 |  |  | import com.easysocket.entity.SocketAddress; | 
 |  |  | import com.dayu.baselibrary.BaseApplication; | 
 |  |  | import com.dayu.baselibrary.dao.BaseDaoSingleton; | 
 |  |  | import com.dayu.baselibrary.dbbean.LibraryBean; | 
 |  |  | import com.dayu.baselibrary.utils.BaseCommon; | 
 |  |  | import com.dayu.henanlibrary.HeNanApplication; | 
 |  |  | import com.dayu.henanlibrary.dao.HNBaseDaoSingleton; | 
 |  |  | import com.dayu.henanlibrary.dbBean.AdminDataBean; | 
 |  |  | import com.dayu.qihealonelibrary.QHAloneApplication; | 
 |  |  | import com.tencent.bugly.crashreport.CrashReport; | 
 |  |  | import com.dayu.recharge.net.ScoketMessageProtocol; | 
 |  |  | import com.dayu.recharge.net.SocketCallBack; | 
 |  |  | import com.dayu.recharge.net.SocketData; | 
 |  |  | import com.dayu.recharge.net.SocketNet; | 
 |  |  | import com.dayu.recharge.socketBean.ConfirmMachineRequestBean; | 
 |  |  | import com.dayu.recharge.socketBean.InitMachineBackBean; | 
 |  |  | import com.dayu.recharge.socketBean.InitMachineRequestBean; | 
 |  |  | import com.dayu.recharge.tools.BcdUtil; | 
 |  |  | import com.dayu.recharge.tools.HexUtil; | 
 |  |  | import com.dayu.recharge.utils.AidlUtil; | 
 |  |  | import com.dayu.recharge.utils.CRC8; | 
 |  |  | import com.dayu.recharge.utils.SocketUtil; | 
 |  |  | import com.dayu.recharge.utils.ToastUtil; | 
 |  |  | import com.dayu.recharge.utils.WSHelper; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | import java.util.Arrays; | 
 |  |  | import java.util.Timer; | 
 |  |  | import java.util.TimerTask; | 
 |  |  | import java.io.File; | 
 |  |  |  | 
 |  |  | public class MyApplication extends Application { | 
 |  |  |     public static String TAG = "MyApplication"; | 
 |  |  |  | 
 |  |  |     //当前加载的library模块   //0:河南版本,1齐河单机版 | 
 |  |  |     public int libraryType = BaseCommon.NoLibrary; | 
 |  |  |     public static MyApplication myApplication; | 
 |  |  |     private String address; | 
 |  |  |  | 
 |  |  |     SocketNet.CreateBack createBack; | 
 |  |  |     //是否正在请求初始化接口 | 
 |  |  |     public boolean isDoInitMachine = false; | 
 |  |  |     //是否连接服务器成功 | 
 |  |  |     public boolean isConncet = false; | 
 |  |  |     private final Timer timer = new Timer(); | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void onCreate() { | 
 |  |  |         super.onCreate(); | 
 |  |  |         myApplication = this; | 
 |  |  |         isAidl = true; | 
 |  |  |         AidlUtil.getInstance().connectPrinterService(this); | 
 |  |  |         initEasySocket(false, null); | 
 |  |  |         socketConnect(); | 
 |  |  |         try { | 
 |  |  |             File file = new File(HNBaseDaoSingleton.SqlitePath); | 
 |  |  |             if (!file.exists()) { | 
 |  |  |                 file.mkdirs(); | 
 |  |  |             } | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         } | 
 |  |  |         initApplication(); | 
 |  |  |         BaseApplication.getInstance().onCreat(this); | 
 |  |  |         CrashReport.initCrashReport(getApplicationContext(), "45551598b8", true); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private boolean isAidl; | 
 |  |  |  | 
 |  |  |     public boolean isAidl() { | 
 |  |  |         return isAidl; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void setAidl(boolean aidl) { | 
 |  |  |         isAidl = aidl; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 初始化EasySocket | 
 |  |  |      */ | 
 |  |  |     public void initEasySocket(boolean isCreate, SocketNet.CreateBack createBack) { | 
 |  |  |     public void initApplication() { | 
 |  |  |         //兼容老版的,认为有addressCode并且LibraryBean为null的为河南版本的 | 
 |  |  |         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(MyApplication.this, "DNS解析失败"); | 
 |  |  |                                     break; | 
 |  |  |                             } | 
 |  |  |  | 
 |  |  |                         } | 
 |  |  |                     }); | 
 |  |  |  | 
 |  |  |                 } else { | 
 |  |  |                     //ip方式 | 
 |  |  |                     if (EasySocket.getInstance().getContext() == null || isCreate) { | 
 |  |  |                         createSocket(ipBean.getIp(), ipBean.getPort()); | 
 |  |  |                     } 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(); | 
 |  |  |                                 } | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |             AdminDataBean adminDataBean = HNBaseDaoSingleton.getInstance(this).adminDao().findFirst(); | 
 |  |  |             LibraryBean libraryBean = BaseDaoSingleton.getInstance(this).libraryDao().findFirst(); | 
 |  |  |             if (adminDataBean != null && libraryBean == null && !TextUtils.isEmpty(adminDataBean.addressCode)) { | 
 |  |  |                 libraryType = BaseCommon.HeNanLibrary; | 
 |  |  |                 initHeNan(); | 
 |  |  |             } else if (libraryBean != null) { | 
 |  |  |                 libraryType = libraryBean.getType(); | 
 |  |  |                 switch (libraryType) { | 
 |  |  |                     case BaseCommon.HeNanLibrary: | 
 |  |  |                         initHeNan(); | 
 |  |  |                         break; | 
 |  |  |                     case BaseCommon.QHAloneLibrary: | 
 |  |  |                         QHAloneApplication.getInstance().application = this; | 
 |  |  |                         break; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |             CrashReport.postCatchedException(e); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 充值管理机注册 | 
 |  |  |      * 初始化河南 | 
 |  |  |      */ | 
 |  |  |     public void initMachineRequestBean(String ip, int port, String addressCode) { | 
 |  |  |         if (!TextUtils.isEmpty(ip) | 
 |  |  |                 && !TextUtils.isEmpty(String.valueOf(port)) | 
 |  |  |                 && !TextUtils.isEmpty(addressCode)) { | 
 |  |  |             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"); | 
 |  |  | // | 
 |  |  |                 Log.i("Socket", "注册报文"); | 
 |  |  |                 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() { | 
 |  |  |  | 
 |  |  |                                 @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")) { | 
 |  |  |                                             //保存设备注册号 | 
 |  |  |                                             DeviceNumber deviceNumber = BaseDaoSingleton.getInstance(MyApplication.myApplication).deviceNumberDao().findFirst(); | 
 |  |  |                                             if (deviceNumber != null) { | 
 |  |  |                                                 deviceNumber.setDeviceRegistrationNumber(initCode); | 
 |  |  |                                             } else { | 
 |  |  |                                                 deviceNumber = new DeviceNumber(); | 
 |  |  |                                                 deviceNumber.setDeviceRegistrationNumber(initCode); | 
 |  |  |                                             } | 
 |  |  |                                             BaseDaoSingleton.getInstance(MyApplication.myApplication).deviceNumberDao().insert(deviceNumber); | 
 |  |  |  | 
 |  |  | //                                            WSHelper.getInstance(MyApplication.this).put("initCode", initCode); | 
 |  |  |                                             ToastUtil.show(MyApplication.this, "充值管理注册报文获取成功"); | 
 |  |  |                                         } else { | 
 |  |  |                                             //校验CRC错误 | 
 |  |  |                                             ToastUtil.show(MyApplication.this, "确认收到注册报文状态失败"); | 
 |  |  |                                         } | 
 |  |  |                                     } else { | 
 |  |  |                                         //校验CRC错误 | 
 |  |  |                                         ToastUtil.show(MyApplication.this, "确认收到注册报文校验CRC错误"); | 
 |  |  |                                     } | 
 |  |  |                                 } | 
 |  |  |  | 
 |  |  |                                 @Override | 
 |  |  |                                 public void onSocketError(int code, String msg) { | 
 |  |  |  | 
 |  |  |                                 } | 
 |  |  |                             }); | 
 |  |  |                         } else {//校验CRC错误 | 
 |  |  |                             ToastUtil.show(MyApplication.this, "充值管理注册报文校验CRC错误"); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                     @Override | 
 |  |  |                     public void onSocketError(int code, String msg) { | 
 |  |  |  | 
 |  |  |                     } | 
 |  |  |                 }); | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |     private void initHeNan() { | 
 |  |  |         HeNanApplication.getInstance().application = this; | 
 |  |  |         HeNanApplication.getInstance().initEasySocket(false, null); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void createSocket(String ip, int port) { | 
 |  |  |         // 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) | 
 |  |  |                 .build(); | 
 |  |  |  | 
 |  |  |         // 初始化 | 
 |  |  |         EasySocket.getInstance().createConnection(options, this);// 创建一个socket连接 | 
 |  |  | //        EasySocket.getInstance().subscribeSocketAction(socketActionListener); | 
 |  |  |         EasySocket.getInstance().subscribeSocketAction(SocketNet.getInstance().socketNet.socketActionListener); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 定时重连服务器 | 
 |  |  |      */ | 
 |  |  |     private void socketConnect() { | 
 |  |  |  | 
 |  |  |         try { | 
 |  |  |             TimerTask task = new TimerTask() { | 
 |  |  |                 @Override | 
 |  |  |                 public void run() { | 
 |  |  |                     // TODO Auto-generated method stub | 
 |  |  |                     Message message = new Message(); | 
 |  |  |                     message.what = 1; | 
 |  |  |                     handler.sendMessage(message); | 
 |  |  |                 } | 
 |  |  |             }; | 
 |  |  | //            timer.schedule(task, 5000, 1000 * 60 * 3); | 
 |  |  |             timer.schedule(task, 5000, 1000 * 60 * 3); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     Handler handler = new Handler() { | 
 |  |  |         @Override | 
 |  |  |         public void handleMessage(Message msg) { | 
 |  |  |             // TODO Auto-generated method stub | 
 |  |  |             // 要做的事情 | 
 |  |  |             super.handleMessage(msg); | 
 |  |  |             try { | 
 |  |  |                 SocketNet.getInstance().isConnect(MyApplication.this); | 
 |  |  |             } catch (Exception e) { | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     }; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |