|
@@ -62,14 +62,9 @@ public class MainActivity extends SerialPortActivity {
|
|
|
ConnectivityManager connectivityManager;
|
|
|
private SmdtManager smdtManager;
|
|
|
|
|
|
- private final ExecutorService send433ThreadPool = Executors.newSingleThreadExecutor();
|
|
|
- private Send433Thread send433Thread;
|
|
|
- private static List<String> s433Data = new ArrayList<>();
|
|
|
-
|
|
|
- //缓存本机收到的消息和时间
|
|
|
- Map<String,Long> receivedCache = new HashMap<>();
|
|
|
- //调度延迟任务
|
|
|
- Timer timer = new Timer();
|
|
|
+ //private final ExecutorService send433ThreadPool = Executors.newSingleThreadExecutor();
|
|
|
+ //private Send433Thread send433Thread;
|
|
|
+ private Map<String,String> s433Data = new HashMap<>();
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
@@ -212,7 +207,7 @@ public class MainActivity extends SerialPortActivity {
|
|
|
@Override
|
|
|
protected void onDataReceivedString(String data) {
|
|
|
//$8C:FC:A0:F1:01:A7{"action":"ACK","type":"S433"}#
|
|
|
- Log.e(TAG, "receiveData: " + data);
|
|
|
+ Log.e(TAG, "receive 433 Data: " + data);
|
|
|
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
runOnUiThread(new Runnable() {
|
|
@@ -226,23 +221,36 @@ public class MainActivity extends SerialPortActivity {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- //收到433回复后转发给服务器
|
|
|
- if (data.startsWith("$") && data.endsWith("#")) {
|
|
|
- //只有收到的是ACK才转发服务器
|
|
|
- String tempData = data.substring(1, data.length()-1);
|
|
|
- int sepIndex = tempData.indexOf("{");
|
|
|
- String mac = tempData.substring(0, sepIndex);
|
|
|
- String tcpData = tempData.substring(sepIndex);
|
|
|
- //Log.e("Application", "tempData: " + tempData + ", mac: " + mac + ", tcpData: " + tcpData);
|
|
|
- TcpModel tcpModel = TcpModel.getModelByJson(tcpData);
|
|
|
- if (tcpModel != null) {
|
|
|
- if (TcpAction.S433Action.ACK == tcpModel.getAction()) {
|
|
|
- JSONObject jsonData = new JSONObject();
|
|
|
- jsonData.put("receiverMac", mac);
|
|
|
- tcpModel.setData(jsonData);
|
|
|
- TcpClient.getInstance().sendMsg(tcpModel.toJson());
|
|
|
+ try {
|
|
|
+ if (data.startsWith("$") && data.endsWith("#")) {
|
|
|
+ //只有收到的是ACK才转发服务器
|
|
|
+ String tempData = data.substring(1, data.length() - 1);
|
|
|
+ int sepIndex = tempData.indexOf("{");
|
|
|
+ String mac = tempData.substring(0, sepIndex);
|
|
|
+ String tcpData = tempData.substring(sepIndex);
|
|
|
+ //Log.e("Application", "tempData: " + tempData + ", mac: " + mac + ", tcpData: " + tcpData);
|
|
|
+ TcpModel tcpModel = TcpModel.getModelByJson(tcpData);
|
|
|
+ if (tcpModel != null) {
|
|
|
+ if (TcpAction.S433Action.ACK == tcpModel.getAction()) {
|
|
|
+ //收到433回复后转发给服务器
|
|
|
+ JSONObject jsonData = new JSONObject();
|
|
|
+ jsonData.put("receiverMac", mac);
|
|
|
+ tcpModel.setData(jsonData);
|
|
|
+ TcpClient.getInstance().sendMsg(tcpModel.toJson());
|
|
|
+
|
|
|
+ //收到ack回复同时清除重发缓存
|
|
|
+ Iterator<Map.Entry<String, String>> iterator = s433Data.entrySet().iterator();
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ if (iterator.next().getKey().equals(mac)) {
|
|
|
+ Log.e(TAG, "清除缓存mac: " + mac);
|
|
|
+ iterator.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -405,19 +413,15 @@ public class MainActivity extends SerialPortActivity {
|
|
|
data.put("title", title);
|
|
|
s433Send.put("data", data);
|
|
|
for (int i = 0; i < macs.size(); i++) {
|
|
|
- s433Data.add(macs.get(i) + s433Send.toJSONString());
|
|
|
+ send433DataByMac((String) macs.get(i), s433Send.toJSONString());
|
|
|
}
|
|
|
-
|
|
|
- send433Data();
|
|
|
- }
|
|
|
- if (tcpData.getString("receiverMac") != null) {
|
|
|
+ } else if (tcpData.getString("receiverMac") != null) {
|
|
|
//给单个点阵屏发送
|
|
|
String mac = tcpData.getString("receiverMac");
|
|
|
data.put("title", tcpData.getString("title"));
|
|
|
s433Send.put("data", data);
|
|
|
- s433Data.add(mac + s433Send.toJSONString());
|
|
|
|
|
|
- send433Data();
|
|
|
+ send433DataByMac(mac, s433Send.toJSONString());
|
|
|
}
|
|
|
}
|
|
|
} else if (tcpModel.getAction() == TcpAction.S433Action.DATA) {
|
|
@@ -431,9 +435,8 @@ public class MainActivity extends SerialPortActivity {
|
|
|
//给单个点阵屏发送
|
|
|
String mac = tcpData.getString("receiverMac");
|
|
|
s433Send.put("data", data);
|
|
|
- s433Data.add(mac + s433Send.toJSONString());
|
|
|
|
|
|
- send433Data();
|
|
|
+ send433DataByMac(mac, s433Send.toJSONString());
|
|
|
}
|
|
|
}
|
|
|
} else if (tcpModel.getAction() == TcpAction.S433Action.SYNC_TIME) {
|
|
@@ -446,9 +449,8 @@ public class MainActivity extends SerialPortActivity {
|
|
|
//给单个点阵屏发送
|
|
|
String mac = tcpData.getString("receiverMac");
|
|
|
s433Send.put("data", data);
|
|
|
- s433Data.add(mac + s433Send.toJSONString());
|
|
|
|
|
|
- send433Data();
|
|
|
+ send433DataByMac(mac, s433Send.toJSONString());
|
|
|
}
|
|
|
}
|
|
|
} else if (tcpModel.getAction() == TcpAction.S433Action.EXIST) {
|
|
@@ -456,9 +458,8 @@ public class MainActivity extends SerialPortActivity {
|
|
|
if (tcpData.getString("receiverMac") != null) {
|
|
|
//给单个点阵屏发送
|
|
|
String mac = tcpData.getString("receiverMac");
|
|
|
- s433Data.add(mac + s433Send.toJSONString());
|
|
|
|
|
|
- send433Data();
|
|
|
+ send433DataByMac(mac, s433Send.toJSONString());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -470,7 +471,40 @@ public class MainActivity extends SerialPortActivity {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void send433Data() {
|
|
|
+ private void send433DataByMac(final String mac, final String data) {
|
|
|
+ new Thread(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ //拼接头尾
|
|
|
+ String msg = "$" + mac + data + "#";
|
|
|
+ Log.e(TAG, "send 433 msg ==>" + msg);
|
|
|
+ //433发送
|
|
|
+ sendMsg(msg, mOutputStream, 120);
|
|
|
+ s433Data.put(mac, data);
|
|
|
+
|
|
|
+ //若没有收到ack回复则再次发送
|
|
|
+ for (int i = 0; i < 2; i++) {
|
|
|
+ Thread.sleep(2000);
|
|
|
+ if (s433Data.containsKey(mac)) {
|
|
|
+ //再次发送
|
|
|
+ sendMsg(msg, mOutputStream, 120);
|
|
|
+ } else {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Log.e(TAG, "send 2 times end: " + mac);
|
|
|
+ //两次发送完成后清除该缓存
|
|
|
+ s433Data.remove(mac);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).start();
|
|
|
+ }
|
|
|
+
|
|
|
+ /*private void send433Data() {
|
|
|
if (send433Thread == null) {
|
|
|
send433Thread = new Send433Thread();
|
|
|
}
|
|
@@ -498,7 +532,7 @@ public class MainActivity extends SerialPortActivity {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
/**
|
|
|
* 消息分包发送,包格式如下:每个包可传输的数据内容是发送缓冲区的大小减去21个字节。21个字节为包控制数据
|
|
@@ -511,7 +545,7 @@ public class MainActivity extends SerialPortActivity {
|
|
|
* @param outputStream 输出
|
|
|
* @param packageLength 芯片发射分包长度(60,修改后为120)
|
|
|
*/
|
|
|
- private void sendMsg(String msg, OutputStream outputStream, int packageLength) throws IOException {
|
|
|
+ private synchronized void sendMsg(String msg, OutputStream outputStream, int packageLength) throws IOException {
|
|
|
|
|
|
char[] alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_".toCharArray();
|
|
|
|