|
@@ -0,0 +1,273 @@
|
|
|
|
+package com.fd.controller;
|
|
|
|
+
|
|
|
|
+import com.fd.entity.Classroom;
|
|
|
|
+import com.fd.socket.*;
|
|
|
|
+import com.fd.util.DataFormatUtils;
|
|
|
|
+import com.fd.util.PythonUtils;
|
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
+import org.springframework.web.bind.annotation.CrossOrigin;
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
+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;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Created by Owen on 2019/10/22 0022 10:55
|
|
|
|
+ */
|
|
|
|
+@Log4j2
|
|
|
|
+@CrossOrigin(maxAge = 3600)
|
|
|
|
+@RestController
|
|
|
|
+@RequestMapping("api")
|
|
|
|
+public class DataController {
|
|
|
|
+
|
|
|
|
+ @Value("${python.exePath}")
|
|
|
|
+ private String exePath;
|
|
|
|
+
|
|
|
|
+ @Value("${python.command}")
|
|
|
|
+ private String command;
|
|
|
|
+
|
|
|
|
+ @Value("${socket.ipAddress}")
|
|
|
|
+ private String ipAddress;
|
|
|
|
+
|
|
|
|
+ @Value("${socket.port}")
|
|
|
|
+ private Integer port;
|
|
|
|
+
|
|
|
|
+ // 监控时间
|
|
|
|
+ @Value("${socket.listenTime}")
|
|
|
|
+ private Integer listenTime;
|
|
|
|
+
|
|
|
|
+ // 定时拍照时间
|
|
|
|
+ @Value("${timedTask}")
|
|
|
|
+ private Integer timedTask;
|
|
|
|
+
|
|
|
|
+ private Socket client;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @PostConstruct
|
|
|
|
+ private Socket init() {
|
|
|
|
+ // 1 建立连接
|
|
|
|
+ Socket client = null;
|
|
|
|
+ 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);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return this.client = client;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 返回前端总入口
|
|
|
|
+ @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);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Thread.sleep(9000);
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return new ResponseEntity(list, HttpStatus.OK);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 测试算法
|
|
|
|
+ */
|
|
|
|
+ @GetMapping("p")
|
|
|
|
+ @SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
|
+ private ResponseEntity getData1() {
|
|
|
|
+ log.info("run testPython");
|
|
|
|
+ List connect = PythonUtils.connect(exePath, command);
|
|
|
|
+ return new ResponseEntity(connect, HttpStatus.OK);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 重启socket客户端
|
|
|
|
+ */
|
|
|
|
+ @GetMapping("restart")
|
|
|
|
+ @SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
|
+ private ResponseEntity restartSocketClient() {
|
|
|
|
+ log.info("run restartSocketClient");
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ // 判断socket是否关闭连接,如果关闭,重启连接
|
|
|
|
+ boolean closed = client.isClosed();
|
|
|
|
+ log.info("socket is closed: {}", closed);
|
|
|
|
+ if (closed) {
|
|
|
|
+ init();
|
|
|
|
+ log.info("restart socket ....");
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return new ResponseEntity("success", HttpStatus.OK);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 测试联调
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ @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();
|
|
|
|
+
|
|
|
|
+ // 5 返回结果集给前端
|
|
|
|
+ result = DataFormatUtils.frontendFormat(result);
|
|
|
|
+
|
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
|
+ log.info("Total time: {}s", (end - start) / 1000);
|
|
|
|
+ return new ResponseEntity(result, HttpStatus.OK);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // test cpp
|
|
|
|
+ @GetMapping("6")
|
|
|
|
+ @SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
|
+ private ResponseEntity testListen6() {
|
|
|
|
+ log.info("run testListen6");
|
|
|
|
+
|
|
|
|
+ // 1 通知cpp拍照 #P
|
|
|
|
+ new Thread(new SendMsgThread(client, "#P")).start();
|
|
|
|
+
|
|
|
|
+ List<Object> list = new ArrayList<>();
|
|
|
|
+ for (int i = 0; i < 5; i++) {
|
|
|
|
+ String ip = "127.0.0." + i;
|
|
|
|
+ String s = ip + ":" + i;
|
|
|
|
+ list.add(s);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ Thread.sleep(2000);
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 2 告诉cpp ip+人数
|
|
|
|
+ new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(list))).start();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return new ResponseEntity("success", HttpStatus.OK);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 循环测试服务器版本
|
|
|
|
+ */
|
|
|
|
+ @GetMapping("load")
|
|
|
|
+ @SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
|
+ private void load() {
|
|
|
|
+
|
|
|
|
+ log.info("run load");
|
|
|
|
+ boolean isRunning = true;
|
|
|
|
+
|
|
|
|
+ // 1 通知cpp拍照 #P
|
|
|
|
+ int count = 1;
|
|
|
|
+ while (isRunning) {
|
|
|
|
+ try {
|
|
|
|
+ Thread.sleep(timedTask);
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
+
|
|
|
|
+ new Thread(new SendMsgThread(client, "#P")).start();
|
|
|
|
+
|
|
|
|
+ // 2 调用算法计算人数
|
|
|
|
+ List result = PythonUtils.connect(exePath, command);
|
|
|
|
+
|
|
|
|
+ // 3 告诉cpp ip+人数
|
|
|
|
+ new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(result))).start();
|
|
|
|
+
|
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
|
+ log.info("Total count: {} time: {}s", count++, (end - start) / 1000);
|
|
|
|
+
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ isRunning = false;
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 循环测试本地版本
|
|
|
|
+ */
|
|
|
|
+ @GetMapping("1")
|
|
|
|
+ @SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
|
+ private void load1() {
|
|
|
|
+
|
|
|
|
+ boolean isRunning = true;
|
|
|
|
+
|
|
|
|
+ // 1 通知cpp拍照 #P
|
|
|
|
+ int count = 1;
|
|
|
|
+ while (isRunning) {
|
|
|
|
+ try {
|
|
|
|
+ Thread.sleep(timedTask);
|
|
|
|
+ log.info("run timedTask");
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
+ // 1 通知cpp拍照 #P
|
|
|
|
+ new Thread(new SendMsgThread(client, "#P")).start();
|
|
|
|
+
|
|
|
|
+ List<Object> list = new ArrayList<>();
|
|
|
|
+ for (int i = 0; i < 5; i++) {
|
|
|
|
+ String ip = "127.0.0." + i;
|
|
|
|
+ String s = ip + ":" + i;
|
|
|
|
+ list.add(s);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Thread.sleep(2000);
|
|
|
|
+
|
|
|
|
+ // 2 告诉cpp ip+人数
|
|
|
|
+ new Thread(new SendMsgThread(client, DataFormatUtils.ipFormat(list))).start();
|
|
|
|
+
|
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
|
+
|
|
|
|
+ log.info("Total count: {} time: {}s", count++, (end - start) / 1000);
|
|
|
|
+
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ isRunning = false;
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|