Просмотр исходного кода

the new service . manager
Change thread to monitor

add other

hasan лет назад: 3
Родитель
Сommit
0f47cf7c0b

+ 9 - 2
pom.xml

@@ -9,8 +9,8 @@
9 9
         <relativePath/> <!-- lookup parent from repository -->
10 10
     </parent>
11 11
     <groupId>com.sjls</groupId>
12
-    <artifactId>Nstthh</artifactId>
13
-    <version>0.0.1-SNAPSHOT</version>
12
+    <artifactId>nstthh</artifactId>
13
+    <version>0.1</version>
14 14
     <name>Nstthh</name>
15 15
     <description>Nstthh</description>
16 16
     <properties>
@@ -74,6 +74,13 @@
74 74
             <artifactId>hutool-all</artifactId>
75 75
             <version>5.7.17</version>
76 76
         </dependency>
77
+        <!--
78
+        <dependency>
79
+            <groupId>com.github.pagehelper</groupId>
80
+            <artifactId>pagehelper-spring-boot-starter</artifactId>
81
+            <version>1.4.1</version>
82
+        </dependency>
83
+        -->
77 84
         <dependency>
78 85
             <groupId>junit</groupId>
79 86
             <artifactId>junit</artifactId>

+ 1 - 1
src/main/java/com/sjls/nstthh/NstthhApplication.java

@@ -26,8 +26,8 @@ public class NstthhApplication {
26 26
     public static void main(String[] args) throws TooManyListenersException, IOException, InterruptedException {
27 27
         SpringApplication.run(NstthhApplication.class, args);
28 28
         //start();
29
-    }
30 29
 
30
+    }
31 31
     public static void start() throws TooManyListenersException, IOException, InterruptedException {
32 32
         String port = "/dev/tty.usbserial-14130";//   /dev/ttyUSB0
33 33
 

+ 15 - 6
src/main/java/com/sjls/nstthh/controller/HardwareAdmin.java

@@ -10,6 +10,7 @@ import gnu.io.NRSerialPort;
10 10
 import org.slf4j.Logger;
11 11
 import org.slf4j.LoggerFactory;
12 12
 import org.springframework.beans.factory.annotation.Autowired;
13
+import org.springframework.data.domain.Page;
13 14
 import org.springframework.stereotype.Controller;
14 15
 import org.springframework.ui.Model;
15 16
 import org.springframework.web.bind.annotation.GetMapping;
@@ -53,14 +54,21 @@ public class HardwareAdmin {
53 54
 
54 55
     @RequestMapping("")
55 56
     public String index(Model model) {
56
-        model.addAttribute("scantaskList", scantaskService.getScantask());
57
-
58
-        //model.addAttribute("sendData", communicationService.getSendData(""));
59
-        //model.addAttribute("grain", communicationService.getGrain());
60
-        // model.addAttribute("receiveData", communicationService.getReceiveData());
57
+//        model.addAttribute("scantaskList", scantaskService.getScantask());
58
+//        Page<Scantask> pageS = scantaskService.getScantaskPage(0,10,"creatTime");
59
+//        model.addAttribute("scantaskList", pageS);
60
+//        logger.info("total{}",pageS.getTotalElements());
61
+        model.addAttribute("total", scantaskService.getScantaskCount());
61 62
         return "index";
62 63
     }
63 64
 
65
+    @RequestMapping("/scantask_d")
66
+    @ResponseBody
67
+    public Page<Scantask> scantask_d(int pageIndex) {
68
+        //logger.info("scantask_d {}", pageIndex);
69
+        return scantaskService.getScantaskPage(pageIndex-1, 10);
70
+    }
71
+
64 72
     @RequestMapping("/admin")
65 73
     public String admin(Model model) {
66 74
         Set<String> set = NRSerialPort.getAvailableSerialPorts();
@@ -102,9 +110,10 @@ public class HardwareAdmin {
102 110
     /**
103 111
      * 连串口
104 112
      *
105
-     * @param port
113
+     * @param port /dev/ttyUSB0
106 114
      * @return
107 115
      */
116
+
108 117
     @GetMapping("/scom")
109 118
     @ResponseBody
110 119
     public boolean scom(String port) {

+ 8 - 0
src/main/java/com/sjls/nstthh/controller/WarehouseAdmin.java

@@ -2,7 +2,15 @@ package com.sjls.nstthh.controller;
2 2
 
3 3
 
4 4
 import org.springframework.stereotype.Controller;
5
+import org.springframework.ui.Model;
6
+import org.springframework.web.bind.annotation.RequestMapping;
5 7
 
6 8
 @Controller
7 9
 public class WarehouseAdmin {
10
+
11
+    @RequestMapping("/sync-warehouse")
12
+    public String warehouse(Model model) {
13
+
14
+        return "warehouse";
15
+    }
8 16
 }

+ 2 - 0
src/main/java/com/sjls/nstthh/entity/Scantask.java

@@ -16,6 +16,8 @@ public class Scantask {
16 16
 
17 17
     private Date creatTime;
18 18
 
19
+    //任务是否结束
20
+    private boolean isOver;
19 21
 
20 22
     @ManyToOne
21 23
     @JoinColumn(name = "warehouse_id")

+ 4 - 0
src/main/java/com/sjls/nstthh/entity/SyncV2xyz.java

@@ -6,7 +6,11 @@ import java.util.Date;
6 6
 
7 7
 @Data
8 8
 public class SyncV2xyz {
9
+    //第一次打点平均值 与 第二次打点平均值 再平均。
9 10
     private double volume;
11
+    // 两次扫描,所有点,平均。
12
+    private double volume_2all;
13
+
10 14
     private String xyz;
11 15
     private Date startTime;
12 16
 }

+ 3 - 0
src/main/java/com/sjls/nstthh/repository/ScantaskRepository.java

@@ -5,8 +5,11 @@ import com.sjls.nstthh.entity.Scantask;
5 5
 import org.springframework.data.jpa.repository.JpaRepository;
6 6
 import org.springframework.stereotype.Repository;
7 7
 
8
+import java.util.List;
9
+
8 10
 @Repository
9 11
 public interface ScantaskRepository extends JpaRepository<Scantask, Long> {
12
+    List<Scantask> findByIsOver(boolean isOver);
10 13
 
11 14
 
12 15
 }

+ 12 - 2
src/main/java/com/sjls/nstthh/service/CommConfig.java

@@ -30,7 +30,17 @@ public class CommConfig {
30 30
     @PostConstruct
31 31
     public void init() {
32 32
 
33
+        // TODO: 2022/1/15
34
+        logger.info("...... todo init sPorts and connect ...... ");
35
+//        if (CommConfig.serialPort == null) {
36
+//            Set<String> set =  NRSerialPort.getAvailableSerialPorts();
37
+//            for (String s : set) {
38
+//                if(s.equals("/dev/ttyUSB0")){
39
+//                    CommConfig.serialPort = new NRSerialPort("/dev/ttyUSB0", 9600);
40
+//                    boolean b = serialPort.connect();
41
+//                    logger.info("init connect serialPort {} ", b);
42
+//                }
43
+//            }
44
+//        }
33 45
     }
34
-
35
-
36 46
 }

+ 24 - 7
src/main/java/com/sjls/nstthh/service/CommunicationService.java

@@ -18,8 +18,8 @@ import org.slf4j.LoggerFactory;
18 18
 import org.springframework.beans.factory.annotation.Autowired;
19 19
 import org.springframework.stereotype.Service;
20 20
 
21
+import java.beans.Transient;
21 22
 import java.util.ArrayList;
22
-import java.util.Collections;
23 23
 import java.util.List;
24 24
 
25 25
 
@@ -31,7 +31,8 @@ import java.util.List;
31 31
 public class CommunicationService {
32 32
 
33 33
     private static final Logger logger = LoggerFactory.getLogger(CommunicationService.class);
34
-    public volatile static List receiveList = Collections.synchronizedList(new ArrayList<SendData>());
34
+    //public volatile static List receiveList = Collections.synchronizedList(new ArrayList<SendData>());
35
+    public volatile static List receiveList = new ArrayList<SendData>();
35 36
 
36 37
     private SendDataRepository sendDataRepository;
37 38
     private ReceiveDataRepository receiveDataRepository;
@@ -126,10 +127,24 @@ public class CommunicationService {
126 127
      * @param receiveDataList
127 128
      * @return
128 129
      */
130
+    @Transient
129 131
     public List saveReceiveData(List receiveDataList) {
130
-        return receiveDataRepository.saveAll(receiveDataList);
132
+        return receiveDataRepository.saveAllAndFlush(receiveDataList);
131 133
     }
132 134
 
135
+    public synchronized void Pretreatment(Scantask scantask, String str_receive_head, String currentSendStr, String str_receive) {
136
+
137
+        String currentSendStr_deviceid = currentSendStr.substring(18, 26);
138
+        String currentSendStr_zy = currentSendStr.substring(38, 46);
139
+
140
+        //当前接收的串,截取出角度,设备,距离。
141
+        String deviceid = str_receive.substring(18, 26);//截取设备id
142
+        String anglez = str_receive.substring(38, 42);//截取角度
143
+        String angley = str_receive.substring(42, 46);//截取角度
144
+        String anglezy = str_receive.substring(38, 46);//角度
145
+
146
+        String distance = str_receive.substring(46, 54);//截取距离
147
+    }
133 148
     /**
134 149
      * 验证  打点后的数据 ?是否crc 并组装到list
135 150
      *
@@ -138,7 +153,7 @@ public class CommunicationService {
138 153
      * @param str_receive
139 154
      * @return
140 155
      */
141
-    public synchronized boolean determineRstrPersistence(Scantask scantask, String str_receive_head, String currentSendStr, String str_receive) {
156
+    public  boolean determineRstrPersistence(Scantask scantask, String str_receive_head, String currentSendStr, String str_receive) {
142 157
         //logger.info(" determineRstrPersistence_currentSendStr = " + currentSendStr + " str_receive= " + str_receive);
143 158
         //当前发送的串,截取出角度。
144 159
         String currentSendStr_deviceid = currentSendStr.substring(18, 26);
@@ -153,7 +168,7 @@ public class CommunicationService {
153 168
         String distance = str_receive.substring(46, 54);//截取距离
154 169
 
155 170
         //发送串 角度+设备 与 返回串 角度+设备 比较。
156
-        if (currentSendStr_zy.endsWith(anglezy) && currentSendStr_deviceid.endsWith(deviceid)) {
171
+        if (currentSendStr_zy.equals(anglezy) && currentSendStr_deviceid.equals(deviceid)) {
157 172
             // 68220185000000E08812345678031101B50102029B029B0000048600FF7B123456781C7016
158 173
             String str_all = str_receive.replaceAll(str_receive_head, "");
159 174
             //返回串的待验证串
@@ -164,7 +179,7 @@ public class CommunicationService {
164 179
             //crc+高低位 返回串验证
165 180
             String str_crc = StrSplicingAssistant.displacementHighLow(StrSplicingAssistant.CRC16Right(str_needCrc));
166 181
 
167
-            if (str_crc.endsWith(str_needCrc_receive)) {
182
+            if (str_crc.equals(str_needCrc_receive)) {
168 183
                 // 根据发送与接收角度,得到返回的数据 ,并crc验证通过
169 184
                 ReceiveData receiveData = new ReceiveData();
170 185
                 receiveData.setReceiveStr(str_receive);
@@ -183,13 +198,15 @@ public class CommunicationService {
183 198
                 receiveData.setCantask(scantask);
184 199
                 receiveList.add(receiveData);
185 200
                 logger.info(receiveList.size() + ": _receive_str = " + anglezy + " | " + distance);
201
+
202
+                return true;
186 203
             } else {
187 204
                 logger.info(":  _send_str_anglezy _receive_str_anglezy, crc false {} {} ", str_crc, str_needCrc_receive);
188 205
             }
189 206
         } else {
190 207
             logger.info(": _send_str_anglezy _receive_str_anglezy, val false {},{} ", currentSendStr_zy, anglezy);
191 208
         }
192
-        return true;
209
+        return false;
193 210
     }
194 211
 
195 212
     /**

+ 96 - 55
src/main/java/com/sjls/nstthh/service/NRJavaSerialManager.java

@@ -1,6 +1,8 @@
1 1
 package com.sjls.nstthh.service;
2 2
 
3 3
 import com.sjls.nstthh.entity.*;
4
+import com.sjls.nstthh.repository.ScantaskRepository;
5
+import com.sjls.nstthh.util.AlgAlgorithm;
4 6
 import com.sjls.nstthh.util.StrSplicingAssistant;
5 7
 import gnu.io.NRSerialPort;
6 8
 import gnu.io.SerialPortEvent;
@@ -11,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
11 13
 import org.springframework.stereotype.Component;
12 14
 import org.springframework.web.client.RestTemplate;
13 15
 
16
+import java.beans.Transient;
14 17
 import java.io.DataInputStream;
15 18
 import java.io.DataOutputStream;
16 19
 import java.io.IOException;
@@ -25,18 +28,26 @@ public class NRJavaSerialManager {
25 28
     private static final String str_init = "681C0105000000CD9612345678031101A5010205DC05DC00123456785FA816";
26 29
     private static final String str_heartbeat = "68140186000000E55A1234567803510112345678B1E216";
27 30
     private static final String str_receive_head = "68220185000000E088";
28
-    volatile static double high = 1;
31
+    volatile static double high = 31;
29 32
     volatile static double diameter = 12;
30 33
     volatile static double h1 = 6;
31 34
     volatile static double h2 = 3;
32
-    static String cloud_url = "http://112.51.248.191:9025/api/n-basic-edge/getEdgeData";
35
+    static String cloud_url = "http://192.168.1.216:9025/api/n-basic-edge/getEdgeData";
33 36
     private volatile static String currentSendStr = str_init;
34
-    private volatile static List<SendData> sendData = Collections.synchronizedList(new ArrayList<SendData>());
37
+    //private volatile static List<SendData> sendData = Collections.synchronizedList(new ArrayList<SendData>());
38
+    private volatile static List<SendData> sendData = new ArrayList<SendData>();
39
+
35 40
     private volatile static boolean isHeartbeat = false;
36 41
     private volatile static boolean isReceive = false;
37 42
     private volatile static boolean isSerialPortDisconnect = false;
38 43
     private static Scantask scantask;
39 44
     private static CommunicationService communicationService;
45
+    private static ScantaskRepository scantaskRepository;
46
+
47
+    @Autowired
48
+    public void setScantaskRepository(ScantaskRepository scantaskRepository) {
49
+        this.scantaskRepository = scantaskRepository;
50
+    }
40 51
 
41 52
     public static double getHigh() {
42 53
         return high;
@@ -78,9 +89,11 @@ public class NRJavaSerialManager {
78 89
         return true;
79 90
     }
80 91
 
92
+    @Transient
81 93
     public static void synctoCloudData() throws UnsupportedEncodingException {
82 94
 
83 95
         List<Grain> grainList = communicationService.getGrainData(scantask);
96
+        double realHypotenuse_avg = getVolumeAvg();
84 97
 
85 98
         double v = 0;
86 99
         String str = "";
@@ -92,31 +105,33 @@ public class NRJavaSerialManager {
92 105
         SyncV2xyz syncV2xyz = new SyncV2xyz();
93 106
         syncV2xyz.setXyz(URLEncoder.encode(str, "UTF-8"));
94 107
         syncV2xyz.setVolume(v / 2);
95
-
108
+        syncV2xyz.setVolume_2all(realHypotenuse_avg);
96 109
         syncV2xyz.setStartTime(scantask.getCreatTime());
97 110
 
98
-        logger.info("Grain -  {}", syncV2xyz);
111
+        //任务完成
112
+        scantask.setOver(true);
113
+        Scantask scantaskdb = scantaskRepository.saveAndFlush(scantask);
114
+        logger.info("Grain -  {},{}", scantaskdb, syncV2xyz);
99 115
 
100 116
         RestTemplate restTemplate = new RestTemplate();
101
-        restTemplate.postForObject(cloud_url, syncV2xyz, String.class);
117
+        try {
118
+            restTemplate.postForObject(cloud_url, syncV2xyz, String.class);
119
+        } catch (Exception e) {
120
+        }
102 121
     }
103 122
 
104 123
     /**
105 124
      * 两次打点数据 平均
106 125
      *
107
-     * @throws UnsupportedEncodingException
126
+     * @throws
108 127
      */
109
-    public static double getVolumeAvg() throws UnsupportedEncodingException {
128
+    public static double getVolumeAvg() {
129
+        //查询所有打点记录。
110 130
         List<ReceiveData> receiveDataList = communicationService.getReceiveData(scantask);
111
-        double realHypotenuse = 0;
112
-        double realHypotenuse_count = 0;
113
-        double realHypotenuse_avg = 0;
114
-
115
-        for (ReceiveData receiveData : receiveDataList) {
116
-            realHypotenuse += receiveData.getRealHypotenuse();
117
-            realHypotenuse_count++;
118
-        }
119
-        realHypotenuse_avg = realHypotenuse / realHypotenuse_count;
131
+        // 组装 zlist
132
+        List<Double> zDataList =  AlgAlgorithm.getZList(receiveDataList);
133
+        //算体积
134
+        double realHypotenuse_avg = AlgAlgorithm.calculateVolume(zDataList);
120 135
 
121 136
         return realHypotenuse_avg;
122 137
     }
@@ -138,12 +153,15 @@ public class NRJavaSerialManager {
138 153
      * @throws TooManyListenersException
139 154
      * @throws InterruptedException
140 155
      */
141
-    public void operation(NRSerialPort serialPort) throws IOException, TooManyListenersException, InterruptedException {
156
+    public void operation(NRSerialPort serialPort) throws IOException, InterruptedException {
142 157
 
143 158
         DataOutputStream outStream = null;
144 159
         if (serialPort != null && serialPort.isConnected()) {
145 160
             DataInputStream inStream = new DataInputStream(serialPort.getInputStream());
146
-            serialPort.addEventListener(new SerialReader(inStream));
161
+            try {
162
+                serialPort.addEventListener(new SerialReader(inStream));
163
+            } catch (TooManyListenersException e) {
164
+            }
147 165
             serialPort.notifyOnDataAvailable(true);
148 166
             outStream = new DataOutputStream(serialPort.getOutputStream());
149 167
 
@@ -151,9 +169,11 @@ public class NRJavaSerialManager {
151 169
         Thread t1 = new Thread(new SerialWriter(outStream), "t1_thread");
152 170
         t1.start();
153 171
         t1.join();
172
+        //wait10Seconds(5000);
154 173
         Thread t2 = new Thread(new SerialWriter(outStream), "t2_thread");
155 174
         t2.start();
156 175
         t2.join();
176
+        //wait10Seconds(5000);
157 177
         Thread t3 = new Thread(() -> {
158 178
             try {
159 179
                 synctoCloudData();
@@ -189,18 +209,31 @@ public class NRJavaSerialManager {
189 209
                     if (str_receive.length() == 10) {
190 210
                         str_receive_at += str_receive;
191 211
                     }
192
-                    //wait10Seconds(600);
193
-                    while (str_receive_at.length() == 74 && !isReceive) {
194
-                        //synSet.add(str_receive_at);
195
-                        if (!str_receive_at.contains("EEEEEEEE")) {
196
-                            // 判断返回串 正确与否,并组装 list for add。
197
-                            isReceive = communicationService.determineRstrPersistence(scantask, str_receive_head, currentSendStr, str_receive_at);
198
-                            //logger.info(" str_receive_at = {}, {} " ,str_receive_at,isReceive);
212
+
213
+                    if (str_receive_at.length() == 74 && !isReceive) {
214
+                        //logger.info(" 74 isReceive str_receive_at {} {} {} ", isHeartbeat, isReceive, str_receive_at);
215
+
216
+                        String currentSendStr_deviceid = currentSendStr.substring(18, 26);
217
+                        String currentSendStr_zy = currentSendStr.substring(38, 46);
218
+                        //当前接收的串,截取出角度,设备,距离。
219
+                        String deviceid = str_receive_at.substring(18, 26);//截取设备id
220
+                        String anglezy = str_receive_at.substring(38, 46);//角度
221
+                        //logger.info(" 74 isReceive currentSendStr_zy,anglezy {} {} {} ",currentSendStr_zy ,anglezy ,str_receive_at);
222
+
223
+                        //发送串 角度+设备 与 返回串 角度+设备 比较。
224
+                        if (currentSendStr_zy.equals(anglezy) && currentSendStr_deviceid.equals(deviceid)) {
225
+                            //synSet.add(str_receive_at);
226
+                            if (!str_receive_at.contains("EEEEEEEE")) {
227
+                                // 判断返回串 正确与否,并组装 list for add。
228
+                                isReceive = communicationService.determineRstrPersistence(scantask, str_receive_head, currentSendStr, str_receive_at);
229
+                                // logger.info(" while - str_receive_at = {}, {} ", str_receive_at, isReceive);
230
+                            }
231
+                            //isReceive = true;
232
+                            // break;
199 233
                         }
200
-                        isReceive = true;
201
-                        break;
202 234
                     }
203 235
                 }
236
+
204 237
             } catch (IOException e) {
205 238
                 e.printStackTrace();
206 239
                 return;
@@ -219,57 +252,65 @@ public class NRJavaSerialManager {
219 252
 
220 253
         public void run() {
221 254
             sendData = communicationService.creatToBeTested(scantask, diameter, high);
222
-            //sendData = communicationService.getSendData(scantask);
223 255
             while (sendData.size() > 0) {
224 256
                 break;
225 257
             }
226
-            logger.info(" wait for send point number testing ......" + sendData.size());
227
-
228
-            while (!Thread.interrupted()) {
229
-                try {
230
-//                    int rs1 = communicationService.receiveList.size();
231
-//                    logger.info("init dot  ......{} {} " ,rs1,communicationService.receiveList.size());
258
+            logger.info("1.15 -15 Beta version wait for send point number testing ......" + sendData.size());
232 259
 
233
-                    if (isHeartbeat && onlyOnce) {
234
-                        wait10Seconds(2000);
260
+            //没心跳的话,等待10次。
261
+            int count = 0;
262
+            while (true) {
263
+                if (isHeartbeat && onlyOnce || count == 10) {
264
+                    try {
235 265
                         outStream.write(StrSplicingAssistant.hexStrToByteArrs(str_init));
236
-                        onlyOnce = false;
237
-                        logger.info("init_send_str =" + str_init.substring(38, 46));
266
+                    } catch (IOException e) {
267
+                        e.printStackTrace();
238 268
                     }
239
-                    if (isHeartbeat && isReceive) {
240
-                        if (sendData.size() > 0) {
269
+                    onlyOnce = false;
270
+                    isReceive = false;
271
+                    logger.info("init_send_str =" + str_init.substring(38, 46));
272
+                    break;
273
+                }
274
+                count++;
275
+                wait10Seconds(2000);
276
+            }
277
+
278
+            while (!Thread.interrupted()) {
279
+                try {
280
+//                        if (!isReceive && onlyOnce) {
281
+//                            if (sendData != null && sendData.size() > 0)
282
+//                                outStream.write(StrSplicingAssistant.hexStrToByteArrs(String.valueOf(sendData.get(0))));
283
+//                            wait10Seconds(8000);
284
+//                            isReceive = true;
285
+//                        }
286
+                    if (isHeartbeat && isReceive || wait10Seconds(10000)) {
287
+                        if (sendData != null && sendData.size() > 0) {
241 288
                             Iterator<SendData> iterator = sendData.iterator();
242 289
                             if (iterator.hasNext()) {
243
-                                synchronized (this) {
244
-                                    currentSendStr = iterator.next().getSendStr();
245
-                                    outStream.write(StrSplicingAssistant.hexStrToByteArrs(currentSendStr));
246
-//                                    wait10Seconds(2000);
247
-//                                    if(rs1 == communicationService.receiveList.size()){
248
-//                                        logger.info("Second dot  ......{} {} " ,rs1,communicationService.receiveList.size());
249
-//                                        outStream.write(StrSplicingAssistant.hexStrToByteArrs(currentSendStr));
250
-//                                    }
251
-                                    iterator.remove();
252
-                                    isReceive = false;
253
-
254
-                                }
255
-                                //38,46 = 角度
290
+                                //synchronized (this) {
291
+                                currentSendStr = iterator.next().getSendStr();
292
+                                outStream.write(StrSplicingAssistant.hexStrToByteArrs(currentSendStr));
256 293
                                 logger.info("_send_str = {}", currentSendStr.substring(38, 46));
294
+                                iterator.remove();
295
+                                isReceive = false;
257 296
                             }
258 297
                         } else {
259 298
                             List receiveList_ = CommunicationService.receiveList;
260 299
                             // 组装的list 一次性save。
261 300
                             List receiveListDB_ = communicationService.saveReceiveData(receiveList_);
301
+                            wait10Seconds(2000);
262 302
                             if (receiveListDB_ != null && receiveListDB_.size() > 0) {
263 303
                                 isSerialPortDisconnect = true;
264 304
                                 logger.info(isSerialPortDisconnect + "_success _ :" + Thread.currentThread().getName());
265 305
                                 high = communicationService.againTestbyHighestPoint(scantask, receiveListDB_);
266 306
                                 isReceive = false;
307
+                                isHeartbeat = false;
267 308
                                 Thread.currentThread().interrupt();
268 309
                                 return;
269 310
                             }
270 311
                         }
271 312
                     }
272
-                    wait10Seconds(2000);
313
+                    wait10Seconds(6000);
273 314
                 } catch (IOException e) {
274 315
                     e.printStackTrace();
275 316
                     return;

+ 38 - 13
src/main/java/com/sjls/nstthh/service/ScantaskService.java

@@ -8,16 +8,20 @@ import gnu.io.NRSerialPort;
8 8
 import org.slf4j.Logger;
9 9
 import org.slf4j.LoggerFactory;
10 10
 import org.springframework.beans.factory.annotation.Autowired;
11
+import org.springframework.data.domain.Page;
12
+import org.springframework.data.domain.PageRequest;
13
+import org.springframework.data.domain.Sort;
11 14
 import org.springframework.stereotype.Service;
12 15
 
16
+import java.beans.Transient;
13 17
 import java.io.IOException;
14 18
 import java.util.List;
15
-import java.util.TooManyListenersException;
16 19
 
17 20
 @Service
18 21
 public class ScantaskService {
19 22
     private static final Logger logger = LoggerFactory.getLogger(ScantaskService.class);
20 23
 
24
+    private static long current_scantaskId = 0;
21 25
 
22 26
     private ScantaskRepository scantaskRepository;
23 27
     private NRJavaSerialManager nRJavaSerialManager;
@@ -39,22 +43,43 @@ public class ScantaskService {
39 43
         List<Scantask> list = scantaskRepository.findAll();
40 44
         return list;
41 45
     }
46
+    public long getScantaskCount() {
47
+        return scantaskRepository.count();
48
+    }
49
+
50
+    public Page<Scantask> getScantaskPage(int cpage, int size) {
51
+       // Page<Scantask> page = scantaskRepository.findAll(PageRequest.of(cpage, size, Sort.by(Sort.Order.desc("id"),Sort.Order.desc("creatTime"))));
52
+        Page<Scantask> page = scantaskRepository.findAll(PageRequest.of(cpage, size, Sort.Direction.DESC,"creatTime"));
42 53
 
43
-    public Scantask creatScanTask(NRSerialPort serialPort) throws TooManyListenersException, IOException, InterruptedException {
54
+        //logger.info("......{},{},{},{}",page.getContent(),page.getTotalElements(),page.getTotalPages(),page.getSize());
55
+        return page;
56
+    }
44 57
 
45
-//        Warehouse warehouse = new Warehouse();
46
-//        warehouse.setDiameter(1);
47
-//        warehouse.setHigh(1);
48
-        long id = new SnowflakeIdWorker(0, 0).nextId();
49
-        //String key = id + "a1001" + DateTime.now().getTime();
58
+    @Transient
59
+    public Scantask creatScanTask(NRSerialPort serialPort) throws  IOException, InterruptedException {
60
+        boolean flag = true;
61
+        if (current_scantaskId > 0) {
62
+            Scantask c_scantaskdb = scantaskRepository.getById(current_scantaskId);
63
+            if (c_scantaskdb.isOver()) {
64
+                flag = true;
65
+            } else {
66
+                logger.info("Wait for the last task to complete ......");
67
+                flag = false;
68
+            }
69
+        }
50 70
 
51
-        Scantask scantask = new Scantask();
71
+        Scantask scantaskdb = null;
72
+        if (flag) {
73
+            long id = new SnowflakeIdWorker(0, 0).nextId();
74
+            Scantask scantask = new Scantask();
75
+            scantask.setStkey(String.valueOf(id));
76
+            scantask.setCreatTime(DateTime.now());
77
+            scantaskdb = scantaskRepository.saveAndFlush(scantask);
78
+            current_scantaskId = scantaskdb.getId();
79
+            nRJavaSerialManager.setScantask(scantaskdb);
80
+            nRJavaSerialManager.operation(serialPort);
81
+        }
52 82
 
53
-        scantask.setStkey(String.valueOf(id));
54
-        scantask.setCreatTime(DateTime.now());
55
-        Scantask scantaskdb = scantaskRepository.save(scantask);
56
-        nRJavaSerialManager.setScantask(scantaskdb);
57
-        nRJavaSerialManager.operation(serialPort);
58 83
         return scantaskdb;
59 84
     }
60 85
 }

+ 61 - 1
src/main/java/com/sjls/nstthh/util/AlgAlgorithm.java

@@ -14,7 +14,9 @@ import java.util.List;
14 14
 public class AlgAlgorithm {
15 15
     private static final Logger logger = LoggerFactory.getLogger(AlgAlgorithm.class);
16 16
 
17
-    public volatile static List<Double> zList = Collections.synchronizedList(new ArrayList<Double>()); //粮面高的集合
17
+    //public volatile static List<Double> zList = Collections.synchronizedList(new ArrayList<Double>()); //粮面高的集合
18
+    public volatile static List<Double> zList = new ArrayList<Double>(); //粮面高的集合
19
+
18 20
     //圆筒仓的直径
19 21
     static double diameter = NRJavaSerialManager.getDiameter();
20 22
     //high 是设备到地面的高即AC
@@ -290,6 +292,64 @@ public class AlgAlgorithm {
290 292
     }
291 293
 
292 294
 
295
+    public static List<Double> getZList(List<ReceiveData> list) {
296
+        if (list == null && list.size() == 0) {
297
+            return zList;
298
+        }
299
+        // logger.info("zHeight - list",list);
300
+
301
+        double AC = high;
302
+        double B1C = 0;//设备和粮面点之间的距离
303
+        double A1B1 = 0;//粮面到设备的水平距离
304
+        double z = 0; //实际点的坐标的z轴
305
+        double zHigh = 0;//以粮面为基准面的高
306
+        double zMax = 0;//最高的点
307
+        double verticalAngle = 0;//垂直角度
308
+        double levelAngle = 0;//水平角度
309
+        double realHypotenuse = 0;//返回的距离
310
+        for (ReceiveData angleObject : list) {
311
+            verticalAngle = angleObject.getVerticalAngle();
312
+            levelAngle = angleObject.getLevelAngle();
313
+            realHypotenuse = angleObject.getRealHypotenuse();
314
+            //logger.info("zHeight - realHypotenuse {} ",realHypotenuse);
315
+
316
+            //将垂直的角度转化为弧度
317
+            double verticalAngleRadians = Math.toRadians(verticalAngle);
318
+            String verticalAngleS = String.format("%.2f%n", Math.sin(verticalAngleRadians));
319
+            double sinc = new Double(verticalAngleS);
320
+            //B1C = realHypotenuse;
321
+            A1B1 = realHypotenuse * sinc; //即 平面三角形的斜边
322
+
323
+            //logger.info("zHeight - A1B1 {} ",A1B1);
324
+
325
+            String sinLevel = String.format("%.2f%n", Math.sin(Math.abs(levelAngle)));
326
+            double sinLevelc = new Double(sinLevel);
327
+
328
+            /**
329
+             * 对比实际的A1B1与打到墙上时最大的这个边进行比较 如果A1B1小于这个最大边则可取
330
+             * 如果A1B1大于或等于这个最大的边 则打到了墙上 舍去
331
+             */
332
+            //打到墙上 最大的边
333
+            double maxLength = sinLevelc * diameter;
334
+
335
+            if (A1B1 < maxLength) {
336
+                //实际打在粮面上的点的坐标
337
+                String verticalAngleC = String.format("%.2f%n", Math.cos(verticalAngleRadians));
338
+                double cosc = new Double(verticalAngleC);
339
+                double A1C = realHypotenuse * cosc;
340
+                z = AC - A1C;
341
+
342
+
343
+                // logger.info("zHeight - z√√√√√√z z z z z {} ",z);
344
+
345
+                zList.add(z);
346
+            }
347
+        }
348
+
349
+        return zList;
350
+
351
+    }
352
+
293 353
     /**
294 354
      * 计算体积
295 355
      *

+ 10 - 0
src/main/resources/application.yml

@@ -7,6 +7,16 @@ spring:
7 7
     password: 1qaz@WSX
8 8
     ## Edge-dongtian
9 9
     url: jdbc:mysql://101.36.160.140:21029/nstthh?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
10
+    hikari:
11
+      minimum-idle: 5
12
+      maximum-pool-size: 10
13
+      idle-timeout: 30000
14
+      max-lifetime: 0
15
+      connection-timeout: 60000
16
+      validation-timeout: 3000
17
+      connection-test-query: select 1
18
+      auto-commit: true
19
+
10 20
   jpa:
11 21
     show-sql: false
12 22
     open-in-view: false

Разница между файлами не показана из-за своего большого размера
+ 4 - 0
src/main/resources/static/jquery.jsontotable.min.js


Разница между файлами не показана из-за своего большого размера
+ 1 - 0
src/main/resources/static/jquery.pagination.min.css


Разница между файлами не показана из-за своего большого размера
+ 1 - 0
src/main/resources/static/jquery.pagination.min.js


+ 65 - 20
src/main/resources/templates/index.html

@@ -1,36 +1,81 @@
1 1
 <!DOCTYPE html>
2
-<html xmlns:th="http://www.thymeleaf.org">
2
+<html>
3 3
 <head>
4 4
     <link href="bootstrap.min.css" rel="stylesheet">
5 5
     <link href="bootstrap-theme.min.css" rel="stylesheet">
6
+    <link href="jquery.pagination.min.css" rel="stylesheet">
7
+
6 8
     <script src="jquery.min.js"></script>
7 9
     <script src="bootstrap.min.js"></script>
8
-    <script type="text/javascript">
9
-        $(document).ready(function () {
10
+    <script src="jquery.jsontotable.min.js"></script>
11
+    <script src="jquery.pagination.min.js"></script>
10 12
 
11
-        });
12
-    </script>
13 13
     <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
14
-    <title>admin</title>
14
+    <title>Nstthh_admin</title>
15 15
 </head>
16 16
 <body>
17 17
 <div class="container">
18 18
     <p class="lead">
19
-    <h1>Hi.Nstthh_admin</h1>
19
+    <h1>Hi.Nstthh_index</h1>
20 20
     </p>
21 21
     <hr/>
22
-    <table class="table table-hover">
23
-        <tr th:each="scantask:${scantaskList}">
24
-            <td th:text="${scantask.id}"></td>
25
-            <td th:text="${scantask.stkey}"></td>
26
-            <td th:text="${scantask.creatTime}"></td>
27
-        </tr>
28
-    </table>
29
-    <div class="panel panel-default">
30
-        <div class="panel-body">
31
-            /admin /sync-v2xyy/id
32
-        </div>
33
-    </div>
22
+
23
+
24
+    <div id="jsontotable"></div>
25
+    <div id="wrap"></div>
26
+    <span id="total" hidden th:text="${total}"></span>
27
+
28
+    <!--    <table class="table table-bordered table-hover">-->
29
+    <!--        <thead>-->
30
+    <!--        <tr>-->
31
+    <!--            <th>#</th>-->
32
+    <!--            <th>key</th>-->
33
+    <!--            <th>time</th>-->
34
+    <!--            <th>complete</th>-->
35
+    <!--        </tr>-->
36
+    <!--        </thead>-->
37
+    <!--        <tbody>-->
38
+    <!--        <tr th:each="scantask:${scantaskList}">-->
39
+    <!--            <td th:text="${scantask.id}"></td>-->
40
+    <!--            <td th:text="${scantask.stkey}"></td>-->
41
+    <!--            <td th:text="${scantask.creatTime}"></td>-->
42
+    <!--            <td th:text="${scantask.isOver}"></td>-->
43
+    <!--        </tr>-->
44
+    <!--        </tbody>-->
45
+    <!--    </table>-->
46
+    <button type="button" class="btn btn-info">/sync-v2xyy/id</button>
47
+    <button type="button" class="btn btn-info">/sync-warehouse</button>
34 48
 </div>
35 49
 </body>
36
-</html>
50
+</html>
51
+<script type="text/javascript">
52
+    $(document).ready(function () {
53
+        var totalVal = $("#total").text();
54
+       // $.ajaxSettings.async = false;
55
+        $.get("/scantask_d?pageIndex=1", function (res) {
56
+            //alert(res.content);
57
+             //{ id: 'header', class: 'header-class', _data:['1', '2', '3'] }
58
+            //{"id":26,"stkey":"931981565586571264","creatTime":"2022-01-15T10:42:00.623+00:00","warehouse":null,"over":true}
59
+            $.jsontotable(res.content, {id: '#jsontotable', header: false, className: 'table table-hover'});
60
+        });
61
+        //$.ajaxSettings.async = true;
62
+        $("#wrap").pagination({
63
+            total: totalVal,  // 默认0
64
+            callback: function (no, pagination) {
65
+                $("#jsontotable").empty();
66
+                $.ajax({
67
+                    url: '/scantask_d',
68
+                    data: {
69
+                        pageIndex: no,
70
+                    },
71
+                    success: function (res) {
72
+                        //alert(res.content);
73
+                        $.jsontotable(res.content, {id: '#jsontotable',  header: false, className: 'table table-hover'});
74
+                        // 总数一般是动态的,调用setTotal更新
75
+                        pagination.setTotal(res.totalElements);
76
+                    }
77
+                })
78
+            }
79
+        })
80
+    });
81
+</script>

+ 1 - 1
src/main/resources/templates/nstthh.html

@@ -58,7 +58,7 @@
58 58
         });
59 59
     </script>
60 60
     <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
61
-    <title>admin</title>
61
+    <title>Nstthh_admin</title>
62 62
 </head>
63 63
 <body>
64 64
 <div class="container">

+ 11 - 2
src/test/java/com/sjls/nstthh/TestNstthh.java

@@ -12,8 +12,17 @@ public class TestNstthh {
12 12
 
13 13
     public static void main(String[] args) throws IOException {
14 14
 
15
-        String str10 = send16strUseCom(1500, 1500);
16
-        System.out.println(str10);
15
+        //String str10 = send16strUseCom(1500, 1500);
16
+        //System.out.println(str10);
17
+
18
+//08770729 、072906E1、05DC05DC、048F04D7
19
+
20
+String ajStr = "05DC05DC";
21
+
22
+        int z = StrSplicingAssistant.hex16To10(ajStr.substring(0,4));
23
+        int y = StrSplicingAssistant.hex16To10(ajStr.substring(4,8));
24
+        String str10 = send16strUseCom(z, y);
25
+        System.out.println(ajStr +" = " + str10);
17 26
 
18 27
     }
19 28
 

+ 85 - 0
src/test/java/com/sjls/nstthh/TestThread.java

@@ -0,0 +1,85 @@
1
+package com.sjls.nstthh;
2
+
3
+import java.util.ArrayList;
4
+import java.util.Collections;
5
+import java.util.Iterator;
6
+import java.util.List;
7
+
8
+public class TestThread {
9
+    volatile static boolean isHeartbeat = true,onlyOnce=false, isReceive = false;
10
+    private volatile static List<String> sendData = Collections.synchronizedList(new ArrayList<String>());
11
+
12
+
13
+
14
+    public static void main(String[] args) {
15
+
16
+
17
+        Thread t1 = new Thread() {
18
+
19
+
20
+            public void run() {
21
+
22
+                while (true) {
23
+
24
+                        synchronized (this) {
25
+                            System.out.println("t1 = 1111111" + isReceive);
26
+                            isReceive = true;
27
+
28
+                        }
29
+
30
+
31
+                        wait10Seconds(1000);
32
+
33
+
34
+                }
35
+            }
36
+        };
37
+        t1.start();
38
+
39
+
40
+        Thread t2 = new Thread() {
41
+
42
+            public void run() {
43
+
44
+                while (!Thread.interrupted()) {
45
+
46
+//                    wait10Seconds(2000);
47
+//                    System.out.println("1111 = " + 111111);
48
+
49
+                    if (isHeartbeat && isReceive || wait10Seconds(10000)) {
50
+                        if (sendData.size() > 0) {
51
+                            Iterator<String> iterator = sendData.iterator();
52
+                            if (iterator.hasNext()) {
53
+
54
+                                synchronized (this) {
55
+                                    System.out.println("t2 = 2222" + isReceive);
56
+                                    iterator.remove();
57
+                                    isReceive = false;
58
+                                }
59
+
60
+                            }
61
+                        }
62
+                    } else {
63
+                        Thread.currentThread().interrupt();
64
+
65
+                    }
66
+
67
+                      wait10Seconds(2000);
68
+
69
+                }
70
+            }
71
+        };
72
+        t2.start();
73
+
74
+
75
+    }
76
+
77
+
78
+    public final static boolean wait10Seconds(long m) {
79
+        try {
80
+            Thread.sleep(m);
81
+        } catch (InterruptedException e) {
82
+        }
83
+        return true;
84
+    }
85
+}

+ 138 - 0
src/test/java/com/sjls/nstthh/TestVolatile.java

@@ -0,0 +1,138 @@
1
+package com.sjls.nstthh;
2
+
3
+import java.util.ArrayList;
4
+import java.util.Collections;
5
+import java.util.Iterator;
6
+import java.util.List;
7
+
8
+public class TestVolatile {
9
+
10
+
11
+    static volatile boolean isHeartbeat = false, isReceive = false;
12
+
13
+    private volatile static List<String> sendData = Collections.synchronizedList(new ArrayList<String>());
14
+
15
+    public static void main(String[] args) throws InterruptedException {
16
+        new Thread(new Runnable() {
17
+            public void run() {
18
+                while (true) {
19
+                    isHeartbeat = true;
20
+
21
+                    if (false) {
22
+
23
+
24
+                        isHeartbeat = true;
25
+                        //System.out.println("线程A 心跳");
26
+                        //System.out.println("3333");
27
+                        wait10Seconds(1000);
28
+
29
+                    } else {
30
+                        // System.out.println("3333");
31
+
32
+                        if (true && !isReceive) {
33
+                            System.out.println("线程A");
34
+                            wait10Seconds(1000);
35
+                            System.out.println("线程A111111");
36
+                            //wait10Seconds(6000);
37
+
38
+                            isReceive = true;
39
+                        }
40
+                    }
41
+
42
+
43
+                }
44
+            }
45
+        }).start();
46
+
47
+        Thread t2 = new Thread(new Runnable() {
48
+            boolean onlyOnce = true;
49
+
50
+            public void run() {
51
+                int count =0;
52
+                while (true){
53
+                    if(isHeartbeat&& onlyOnce||count==3){
54
+                        System.out.println("isHeartbeat = " + isHeartbeat);
55
+                        break;
56
+                    }
57
+                    System.out.println("yyyy = " + isHeartbeat);
58
+
59
+                    wait10Seconds(6000);
60
+                    count++;
61
+                }
62
+                System.out.println("zzzzzz = " + isHeartbeat);
63
+
64
+                sendData.add("a");
65
+                sendData.add("b");
66
+                sendData.add("c");
67
+                sendData.add("d");
68
+                sendData.add("e");
69
+                int i = 0;
70
+                while (!Thread.interrupted()) {
71
+                    i++;
72
+                    if (isHeartbeat && onlyOnce) {
73
+                        //wait10Seconds(2000);
74
+                        onlyOnce = false;
75
+                        isReceive = false;
76
+
77
+                        System.out.println(i + "线程B onlyOnce11111 = " + onlyOnce);
78
+                       // wait10Seconds(3000);
79
+
80
+                    } else {
81
+
82
+                       // System.out.println(i + "线程B onlyOnce22222 = " + onlyOnce);
83
+
84
+//                    if(!isReceive && !onlyOnce){
85
+//                        if (sendData != null && sendData.size() > 0) {
86
+//                         System.out.println("线程B1111" + sendData.get(0));
87
+//                        }
88
+//                        wait10Seconds(8000);
89
+//                        isReceive = true;
90
+//                    }
91
+                    if (isHeartbeat && isReceive ||    wait10Seconds(10000)) {
92
+                            if (sendData != null && sendData.size() > 0) {
93
+                                Iterator<String> iterator = sendData.iterator();
94
+                                if (iterator.hasNext()) {
95
+                                    iterator.next();
96
+                                    System.out.println("线程B" + sendData);
97
+                                    iterator.remove();
98
+                                    isReceive = false;
99
+                                }
100
+                            } else {
101
+                                Thread.currentThread().interrupt();
102
+                                isReceive = false;
103
+                                isHeartbeat = false;
104
+                                return;
105
+                            }
106
+
107
+
108
+                        }
109
+                    }
110
+                    wait10Seconds(2000);
111
+
112
+                }
113
+            }
114
+        });
115
+        t2.start();
116
+        t2.join();
117
+        wait10Seconds(10000);
118
+        Thread t3 = new Thread(new Runnable() {
119
+            public void run() {
120
+                for (int i = 0; i < 10; i++) {
121
+                    System.out.println("ttttt3333333 = " + 33333333);
122
+
123
+                }
124
+            }
125
+        });
126
+        t3.start();
127
+
128
+    }
129
+
130
+
131
+    public final static boolean wait10Seconds(long m) {
132
+        try {
133
+            Thread.sleep(m);
134
+        } catch (InterruptedException e) {
135
+        }
136
+        return true;
137
+    }
138
+}