|
@@ -1,8 +1,9 @@
|
|
|
package com.fd.controller;
|
|
|
|
|
|
-import com.fd.entity.Classroom;
|
|
|
-import com.fd.socket.*;
|
|
|
+import com.fd.entity.Room;
|
|
|
+import com.fd.socket.demo.SendMsg;
|
|
|
import com.fd.util.DataFormatUtils;
|
|
|
+import com.fd.util.FileUtils;
|
|
|
import com.fd.util.PythonUtils;
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -16,8 +17,8 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.io.IOException;
|
|
|
import java.net.Socket;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
|
|
/**
|
|
|
* Created by Owen on 2019/10/22 0022 10:55
|
|
@@ -25,7 +26,7 @@ import java.util.List;
|
|
|
@Log4j2
|
|
|
@CrossOrigin(maxAge = 3600)
|
|
|
@RestController
|
|
|
-@RequestMapping("api")
|
|
|
+@RequestMapping("/api")
|
|
|
public class DataController {
|
|
|
|
|
|
@Value("${python.exePath}")
|
|
@@ -51,8 +52,22 @@ public class DataController {
|
|
|
private Socket client;
|
|
|
|
|
|
|
|
|
+ // 缓存算法数据
|
|
|
+ private Map<String, List> allmap = new HashMap();
|
|
|
+
|
|
|
+ // 记录调用次数
|
|
|
+ private Integer count = 1;
|
|
|
+
|
|
|
+
|
|
|
+ // 多线程共享变量
|
|
|
+ // false 表示完成
|
|
|
+ private static AtomicBoolean exists = new AtomicBoolean(false);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 初始化
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
@PostConstruct
|
|
@@ -62,11 +77,6 @@ public class DataController {
|
|
|
try {
|
|
|
client = new Socket(ipAddress, port);
|
|
|
log.info("========= run client ===========");
|
|
|
-
|
|
|
- // 监听服务端
|
|
|
- new Thread(new SocketListenThread(client, listenTime)).start();
|
|
|
-// new Thread(new ReceiveThread(client)).start();
|
|
|
-
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
log.error(e);
|
|
@@ -76,15 +86,13 @@ public class DataController {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 重启socket客户端
|
|
|
*/
|
|
|
- @GetMapping("restart")
|
|
|
+ @GetMapping("/restart")
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
private ResponseEntity restartSocketClient() {
|
|
|
log.info("run restartSocketClient");
|
|
|
-
|
|
|
try {
|
|
|
// 判断socket是否关闭连接,如果关闭,重启连接
|
|
|
boolean closed = client.isClosed();
|
|
@@ -104,51 +112,81 @@ public class DataController {
|
|
|
* 测试联调
|
|
|
*/
|
|
|
|
|
|
- @GetMapping("all")
|
|
|
+ @GetMapping("/all")
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
private ResponseEntity all() {
|
|
|
long start = System.currentTimeMillis();
|
|
|
log.info("run all");
|
|
|
|
|
|
- // 1 通知cpp拍照 #P
|
|
|
- new Thread(new SendMsgThread(client, "#P")).start();
|
|
|
|
|
|
- // 2 调用算法计算人数
|
|
|
- List result = PythonUtils.connect(exePath, command);
|
|
|
|
|
|
- // 4 告诉cpp ip+人数
|
|
|
- new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(result))).start();
|
|
|
+ // 重启后第一次是没有数据的,模拟一条给他
|
|
|
+ if (allmap.size() == 0) {
|
|
|
+ ArrayList<Object> list = new ArrayList<>();
|
|
|
+ list.add("192.168.0.249:20");
|
|
|
+ allmap.put("result", list);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从缓存里拿数据
|
|
|
+ List result = allmap.get("result");
|
|
|
|
|
|
// 5 返回结果集给前端
|
|
|
- result = DataFormatUtils.frontendFormat(result);
|
|
|
+ result = DataFormatUtils.frontendIpFormat2(result);
|
|
|
+
|
|
|
+ if (exists.compareAndSet(false, true)) {
|
|
|
+ // 1 通知cpp拍照 #P
|
|
|
+ SendMsg sendMsg = new SendMsg(client, "#P");
|
|
|
+ sendMsg.send();
|
|
|
+
|
|
|
+ // 调用算法计算人数
|
|
|
+ new Thread(new PythonThread()).start();
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
- log.info("Total time: {}s", (end - start) / 1000);
|
|
|
+ log.info("Total count: {} time: {}s", count++, (end - start) / 1000);
|
|
|
return new ResponseEntity(result, HttpStatus.OK);
|
|
|
}
|
|
|
|
|
|
+ // 调用算法
|
|
|
+ public class PythonThread implements Runnable {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ log.info("run PythonThread");
|
|
|
+ List result = PythonUtils.connect(exePath, command);
|
|
|
+ if (result != null) {
|
|
|
+ allmap.put("result", result);
|
|
|
+ } else {
|
|
|
+ log.info("Python get data is null");
|
|
|
+ result = allmap.get("result");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 告诉cpp ip+人数
|
|
|
+ SendMsg sendMsg = new SendMsg(client, DataFormatUtils.ipFormat(result));
|
|
|
+ sendMsg.send();
|
|
|
+ // 表示执行完成
|
|
|
+ exists.set(false);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// ===================================== 测试用 ========================================================
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
// 返回前端总入口
|
|
|
- @GetMapping("data")
|
|
|
+ @GetMapping("/data")
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
private ResponseEntity getData() {
|
|
|
log.info("run getData");
|
|
|
-// List connect = PythonUtils.connect(exePath, command);
|
|
|
- List<Object> list = new ArrayList<>();
|
|
|
- for (int i = 1; i < 99; i++) {
|
|
|
- Classroom classroom = new Classroom();
|
|
|
- classroom.setId(i);
|
|
|
- classroom.setRegion(DataFormatUtils.randomInt(3, 1));
|
|
|
- classroom.setStatus(DataFormatUtils.randomInt(2, 0));
|
|
|
- classroom.setStorey(DataFormatUtils.randomInt(10, 1));
|
|
|
- classroom.setCount(DataFormatUtils.randomInt(50, 20));
|
|
|
- list.add(classroom);
|
|
|
- }
|
|
|
|
|
|
+ String[] str = new String[]{"192.168.1.1:10", "192.168.1.2:20", "192.168.2.1:30", "192.168.2.2:40", "192.168.3.1:22"};
|
|
|
+ List<String> strings = Arrays.asList(str);
|
|
|
+
|
|
|
+ List list = DataFormatUtils.frontendIpFormat2(strings);
|
|
|
try {
|
|
|
- Thread.sleep(3000);
|
|
|
+ Thread.sleep(1000);
|
|
|
} catch (InterruptedException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -159,7 +197,7 @@ public class DataController {
|
|
|
/**
|
|
|
* 测试算法
|
|
|
*/
|
|
|
- @GetMapping("p")
|
|
|
+ @GetMapping("/python")
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
private ResponseEntity getDataP() {
|
|
|
log.info("run testPython");
|
|
@@ -167,14 +205,18 @@ public class DataController {
|
|
|
return new ResponseEntity(connect, HttpStatus.OK);
|
|
|
}
|
|
|
|
|
|
- // test cpp
|
|
|
- @GetMapping("6")
|
|
|
+ // 测试 cpp 通信
|
|
|
+ @GetMapping("/cpp")
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
private ResponseEntity testListen6() {
|
|
|
log.info("run testListen6");
|
|
|
|
|
|
// 1 通知cpp拍照 #P
|
|
|
- new Thread(new SendMsgThread(client, "#P")).start();
|
|
|
+// new Thread(new SendMsgThread(client, "#P")).start();
|
|
|
+
|
|
|
+ // 1 通知cpp拍照 #P
|
|
|
+ SendMsg sendMsg = new SendMsg(client, "#P");
|
|
|
+ sendMsg.send();
|
|
|
|
|
|
List<Object> list = new ArrayList<>();
|
|
|
for (int i = 0; i < 5; i++) {
|
|
@@ -189,8 +231,12 @@ public class DataController {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
- // 2 告诉cpp ip+人数
|
|
|
- new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(list))).start();
|
|
|
+// // 2 告诉cpp ip+人数
|
|
|
+// new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(list))).start();
|
|
|
+
|
|
|
+ // 告诉cpp ip+人数
|
|
|
+ SendMsg sendMsg1 = new SendMsg(client, DataFormatUtils.ipFormat(list));
|
|
|
+ sendMsg1.send();
|
|
|
|
|
|
|
|
|
return new ResponseEntity("success", HttpStatus.OK);
|
|
@@ -200,7 +246,7 @@ public class DataController {
|
|
|
/**
|
|
|
* 循环测试服务器版本
|
|
|
*/
|
|
|
- @GetMapping("load")
|
|
|
+ @GetMapping("/load")
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
private void load() {
|
|
|
|
|
@@ -214,13 +260,18 @@ public class DataController {
|
|
|
Thread.sleep(timedTask);
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
- new Thread(new SendMsgThread(client, "#P")).start();
|
|
|
+// new Thread(new SendMsgThread(client, "#P")).start();
|
|
|
+ // 1 通知cpp拍照 #P
|
|
|
+ SendMsg sendMsg = new SendMsg(client, "#P");
|
|
|
+ sendMsg.send();
|
|
|
|
|
|
// 2 调用算法计算人数
|
|
|
List result = PythonUtils.connect(exePath, command);
|
|
|
|
|
|
// 3 告诉cpp ip+人数
|
|
|
- new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(result))).start();
|
|
|
+// new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(result))).start();
|
|
|
+ SendMsg sendMsg1 = new SendMsg(client, DataFormatUtils.ipFormat(result));
|
|
|
+ sendMsg1.send();
|
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
log.info("Total count: {} time: {}s", count++, (end - start) / 1000);
|
|
@@ -235,9 +286,10 @@ public class DataController {
|
|
|
/**
|
|
|
* 循环测试本地版本
|
|
|
*/
|
|
|
- @GetMapping("1")
|
|
|
+ @GetMapping("/1")
|
|
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
private void load1() {
|
|
|
+ log.info("run load1");
|
|
|
|
|
|
boolean isRunning = true;
|
|
|
|
|
@@ -245,15 +297,16 @@ public class DataController {
|
|
|
int count = 1;
|
|
|
while (isRunning) {
|
|
|
try {
|
|
|
- Thread.sleep(timedTask);
|
|
|
+ Thread.sleep(2000);
|
|
|
log.info("run timedTask");
|
|
|
long start = System.currentTimeMillis();
|
|
|
// 1 通知cpp拍照 #P
|
|
|
- new Thread(new SendMsgThread(client, "#P")).start();
|
|
|
+ SendMsg sendMsg = new SendMsg(client, "#P");
|
|
|
+ sendMsg.send();
|
|
|
|
|
|
List<Object> list = new ArrayList<>();
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
- String ip = "127.0.0." + i;
|
|
|
+ String ip = "127.0.0.1" + i;
|
|
|
String s = ip + ":" + i;
|
|
|
list.add(s);
|
|
|
}
|
|
@@ -261,7 +314,9 @@ public class DataController {
|
|
|
Thread.sleep(2000);
|
|
|
|
|
|
// 2 告诉cpp ip+人数
|
|
|
- new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(list))).start();
|
|
|
+ SendMsg sendMsg1 = new SendMsg(client, DataFormatUtils.ipFormat(list));
|
|
|
+ sendMsg1.send();
|
|
|
+
|
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
|
|
@@ -277,4 +332,16 @@ public class DataController {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @GetMapping("/test")
|
|
|
+ @SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
+ private String test2() {
|
|
|
+ log.info("run test2");
|
|
|
+ int i = new Random().nextInt(100);
|
|
|
+ return "success: " + i;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|