From ebc06a3ae3a42bd6ac1359a59c55d5e6f1a0e2f0 Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期二, 26 三月 2024 16:18:08 +0800
Subject: [PATCH] 优化通讯 写卡界面添加二次确认 优化用户写卡逻辑,提高写卡速度。
---
app/src/main/java/com/dayu/recharge/MyApplication.java | 165 +++++++++++++++++++++++++++++++------------------------
1 files changed, 93 insertions(+), 72 deletions(-)
diff --git a/app/src/main/java/com/dayu/recharge/MyApplication.java b/app/src/main/java/com/dayu/recharge/MyApplication.java
index 5a9cafc..7fe9e91 100644
--- a/app/src/main/java/com/dayu/recharge/MyApplication.java
+++ b/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;
//鏄惁姝e湪璇锋眰鍒濆鍖栨帴鍙�
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;
+
/**
* 鍒濆鍖朎asySocket
*/
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鍚庝慨鏀筰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 {
+ 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鍚庝慨鏀筰p涓嶇敓鏁堥棶棰�
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瑙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();
}
@@ -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()
// 涓绘満鍦板潃锛岃濉啓鑷繁鐨処P鍦板潃锛屼互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();
// 鍒濆鍖�
--
Gitblit v1.8.0