|
|
@@ -1,5 +1,6 @@
|
|
|
package com.qy.agv.receiver;
|
|
|
|
|
|
+import static com.qy.agv.util.Constant.DATETIME_CONTINUOUS_FORMATE;
|
|
|
import static com.qy.agv.util.Constant.TIME_FORMATE;
|
|
|
|
|
|
import android.app.Service;
|
|
|
@@ -27,6 +28,10 @@ import java.net.Socket;
|
|
|
import java.net.SocketException;
|
|
|
import java.net.SocketTimeoutException;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Timer;
|
|
|
import java.util.TimerTask;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
@@ -54,6 +59,8 @@ public class SocketService extends Service {
|
|
|
public static Logger mylog =Logger.getLogger(SocketService.class);
|
|
|
private final Intent intent = new Intent(Constant.RECEIVER_MSG);
|
|
|
private Long lastTime = 0L;
|
|
|
+ private int bleSeq = 0;
|
|
|
+ public volatile Map<String, String[]> checkID = new HashMap<>();
|
|
|
|
|
|
|
|
|
@Override
|
|
|
@@ -115,7 +122,6 @@ public class SocketService extends Service {
|
|
|
e.printStackTrace();
|
|
|
if (e instanceof SocketTimeoutException) {
|
|
|
mylog.error("连接超时,正在重连");
|
|
|
- showMsg("连接失败");
|
|
|
try {
|
|
|
Thread.sleep(1000);
|
|
|
} catch (InterruptedException ex) {
|
|
|
@@ -125,12 +131,10 @@ public class SocketService extends Service {
|
|
|
|
|
|
} else if (e instanceof NoRouteToHostException) {
|
|
|
mylog.error("地址不存在,请检查");
|
|
|
- showMsg("连接失败");
|
|
|
stopSelf();
|
|
|
|
|
|
} else if (e instanceof ConnectException) {
|
|
|
mylog.error("连接异常或被拒绝,请检查");
|
|
|
- showMsg("连接失败");
|
|
|
try {
|
|
|
Thread.sleep(5000);
|
|
|
} catch (InterruptedException ex) {
|
|
|
@@ -146,15 +150,28 @@ public class SocketService extends Service {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void showMsg(String msg){
|
|
|
-// intent.putExtra("msg", msg);
|
|
|
-// intent.putExtra("json","");
|
|
|
-// sendBroadcast(intent);
|
|
|
- }
|
|
|
|
|
|
- private void showCommand(String msg){
|
|
|
- intent.putExtra("json", msg);
|
|
|
- sendBroadcast(intent);
|
|
|
+ private void showCommand(String rec){
|
|
|
+ if(!rec.contains(",")){
|
|
|
+ mylog.error("无效信息");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String recID = rec.split(",")[1];
|
|
|
+ if(checkID.containsKey(recID)){
|
|
|
+ String[] str = checkID.get(rec);
|
|
|
+ if(str == null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(rec.contains("1T")) {
|
|
|
+ toastMsg(str[0]);
|
|
|
+ }else{
|
|
|
+ toastMsg(str[1]);
|
|
|
+ }
|
|
|
+ checkID.remove(rec);
|
|
|
+ }else {
|
|
|
+ intent.putExtra("json", rec);
|
|
|
+ sendBroadcast(intent);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/*因为Toast是要运行在主线程的 所以需要到主线程哪里去显示toast*/
|
|
|
@@ -167,33 +184,25 @@ public class SocketService extends Service {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- public void sendToRobot(String toRobot, String msg){
|
|
|
- msg = toRobot + ":" + msg;
|
|
|
- sendOrder(msg);
|
|
|
- }
|
|
|
-
|
|
|
- public void sendToServer(String msg){
|
|
|
- msg = "execute:" + msg;
|
|
|
- sendOrder(msg);
|
|
|
- }
|
|
|
-
|
|
|
/*发送数据*/
|
|
|
- public void sendOrder(final String json) {
|
|
|
+ public boolean sendData(final String json) {
|
|
|
if (socket != null && socket.isConnected()) {
|
|
|
/*发送指令*/
|
|
|
new Thread(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
try {
|
|
|
- tcpClient(json, 0);
|
|
|
+ tcpClient(json);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}).start();
|
|
|
+ return true;
|
|
|
} else {
|
|
|
toastMsg("机器人未连接,请重试");
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
/*定时发送数据*/
|
|
|
@@ -216,11 +225,9 @@ public class SocketService extends Service {
|
|
|
String msg = "HpingT";
|
|
|
outputStream.write((msg+"\n").getBytes(StandardCharsets.UTF_8));
|
|
|
outputStream.flush();
|
|
|
- showMsg("连接成功");
|
|
|
} catch (Exception e) {
|
|
|
/*发送失败说明socket断开了或者出现了其他错误*/
|
|
|
toastMsg("连接断开,正在重连");
|
|
|
- showMsg("连接中断");
|
|
|
/*重连*/
|
|
|
releaseSocket();
|
|
|
e.printStackTrace();
|
|
|
@@ -231,7 +238,7 @@ public class SocketService extends Service {
|
|
|
timer.schedule(task, 0, 10000);
|
|
|
}
|
|
|
|
|
|
- public void rec(){
|
|
|
+ private void rec(){
|
|
|
try{
|
|
|
while (true) {
|
|
|
if(socket!=null && !socket.isInputShutdown()) {
|
|
|
@@ -244,21 +251,12 @@ public class SocketService extends Service {
|
|
|
}
|
|
|
inMsg = inMsg + System.getProperty("line.separator");
|
|
|
inMsg = inMsg.replace("\n", "");
|
|
|
- if (inMsg.equals("HpongT")
|
|
|
- || inMsg.equals("HokT")) {
|
|
|
- lastTime = System.currentTimeMillis();
|
|
|
- continue;
|
|
|
- }
|
|
|
- if (inMsg.equals("HerrorT")) {
|
|
|
- toastMsg("无效指令");
|
|
|
- continue;
|
|
|
- }
|
|
|
+ lastTime = System.currentTimeMillis();
|
|
|
mylog.info("received: " + inMsg);
|
|
|
if (!StringUtils.isEmpty(inMsg)) {
|
|
|
showCommand(inMsg);
|
|
|
}
|
|
|
}catch (SocketException ex){
|
|
|
- showMsg("指令发送异常");
|
|
|
TimeUnit.SECONDS.sleep(5);
|
|
|
}catch (Exception ex){
|
|
|
mylog.error(ex);
|
|
|
@@ -272,7 +270,7 @@ public class SocketService extends Service {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void tcpClient(String json, int flag){
|
|
|
+ private void tcpClient(String json){
|
|
|
try{
|
|
|
if(socket == null){
|
|
|
throw new SocketException("重新连接");
|
|
|
@@ -337,6 +335,13 @@ public class SocketService extends Service {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ public String getSendID() {
|
|
|
+ bleSeq++;
|
|
|
+ if (bleSeq > 9) {
|
|
|
+ bleSeq = 0;
|
|
|
+ }
|
|
|
+ return DateUtil.getCurrDate(DATETIME_CONTINUOUS_FORMATE) + "" + bleSeq;
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void onDestroy() {
|