Browse Source

the new service . manager
Change thread to monitor

add other

hasan 3 years ago
parent
commit
82a39bae98

File diff suppressed because it is too large
+ 586 - 0
hs_err_pid3274.log


+ 20 - 2
src/main/java/com/sjls/nstthh/controller/EquipmentAdmin.java

@@ -1,8 +1,26 @@
1 1
 package com.sjls.nstthh.controller;
2 2
 
3 3
 
4
-import org.springframework.stereotype.Controller;
4
+import com.sjls.nstthh.entity.Equipment;
5
+import com.sjls.nstthh.service.CommunicationService;
6
+import org.springframework.beans.factory.annotation.Autowired;
7
+import org.springframework.web.bind.annotation.GetMapping;
8
+import org.springframework.web.bind.annotation.ResponseBody;
9
+import org.springframework.web.bind.annotation.RestController;
5 10
 
6
-@Controller
11
+@RestController
7 12
 public class EquipmentAdmin {
13
+
14
+    private CommunicationService communicationService;
15
+
16
+    @Autowired
17
+    public void setCommunicationService(CommunicationService communicationService) {
18
+        this.communicationService = communicationService;
19
+    }
20
+
21
+    @GetMapping("/seq")
22
+    @ResponseBody
23
+    public void seq(Equipment equipment) {
24
+    }
25
+
8 26
 }

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

@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
17 17
 import org.springframework.web.bind.annotation.ResponseBody;
18 18
 
19 19
 import java.io.IOException;
20
+import java.io.UnsupportedEncodingException;
20 21
 import java.util.Set;
21 22
 import java.util.TooManyListenersException;
22 23
 
@@ -33,6 +34,13 @@ public class HardwareAdmin {
33 34
     private CommunicationService communicationService;
34 35
     private ScantaskService scantaskService;
35 36
 
37
+    private NRJavaSerialManager nRJavaSerialManager;
38
+
39
+    @Autowired
40
+    public void setnRJavaSerialManager(NRJavaSerialManager nRJavaSerialManager) {
41
+        this.nRJavaSerialManager = nRJavaSerialManager;
42
+    }
43
+
36 44
     @Autowired
37 45
     public void setCommunicationService(CommunicationService communicationService) {
38 46
         this.communicationService = communicationService;
@@ -101,7 +109,7 @@ public class HardwareAdmin {
101 109
     @ResponseBody
102 110
     public boolean scom(String port) {
103 111
         boolean b = false;
104
-        if (!StrUtil.isEmpty(port)) {
112
+        if (!StrUtil.isEmpty(port) && CommConfig.serialPort == null) {
105 113
             CommConfig.serialPort = new NRSerialPort(port, 9600);
106 114
             serialPort = CommConfig.serialPort;
107 115
             b = serialPort.connect();
@@ -118,10 +126,11 @@ public class HardwareAdmin {
118 126
     public void ccom() {
119 127
         try {
120 128
             if (serialPort != null) {
121
-                //serialPort.disconnect();
122
-                CommConfig.serialPort.disconnect();
129
+                serialPort.disconnect();
130
+                //CommConfig.serialPort.disconnect();
131
+                CommConfig.serialPort = null;
123 132
                 serialPort = null;
124
-                logger.info("close serialPort {}", serialPort);
133
+                logger.info("close serialPort {} , {}", CommConfig.serialPort, serialPort);
125 134
             }
126 135
         } catch (Exception e) {
127 136
             //e.printStackTrace();
@@ -133,8 +142,12 @@ public class HardwareAdmin {
133 142
 
134 143
     @GetMapping("/sync-v2xyy")
135 144
     @ResponseBody
136
-    public void v2xyy() {
137
-        NRJavaSerialManager.synctoCloudData();
145
+    public void v2xyy(Long id) throws UnsupportedEncodingException {
146
+        Scantask scantask = new Scantask();
147
+        scantask.setId(5L);
148
+        //nRJavaSerialManager.setCommunicationService(communicationService);
149
+        nRJavaSerialManager.setScantask(scantask);
150
+        nRJavaSerialManager.synctoCloudData();
138 151
     }
139 152
 }
140 153
 

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

@@ -12,6 +12,8 @@ public class Grain {
12 12
     @GeneratedValue(strategy = GenerationType.IDENTITY)//自增
13 13
     private Long id;
14 14
 
15
+    /*2次之后*/
16
+    private double grainVolume_t;
15 17
 
16 18
     private double grainVolume;
17 19
     private double grainDensity;

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

@@ -0,0 +1,12 @@
1
+package com.sjls.nstthh.entity;
2
+
3
+import lombok.Data;
4
+
5
+import java.util.Date;
6
+
7
+@Data
8
+public class SyncV2xyz {
9
+    private double volume;
10
+    private String xyz;
11
+    private Date startTime;
12
+}

+ 12 - 0
src/main/java/com/sjls/nstthh/repository/EquipmentRepository.java

@@ -0,0 +1,12 @@
1
+package com.sjls.nstthh.repository;
2
+
3
+
4
+import com.sjls.nstthh.entity.Equipment;
5
+import org.springframework.data.jpa.repository.JpaRepository;
6
+import org.springframework.stereotype.Repository;
7
+
8
+@Repository
9
+public interface EquipmentRepository extends JpaRepository<Equipment, Long> {
10
+
11
+
12
+}

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

@@ -5,8 +5,11 @@ import com.sjls.nstthh.entity.ReceiveData;
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 ReceiveDataRepository extends JpaRepository<ReceiveData, Long> {
12
+    List<ReceiveData> findByCantask_id(long cantask_id);
10 13
 
11 14
 
12 15
 }

+ 12 - 3
src/main/java/com/sjls/nstthh/service/CommunicationService.java

@@ -7,6 +7,7 @@ import com.sjls.nstthh.entity.Grain;
7 7
 import com.sjls.nstthh.entity.ReceiveData;
8 8
 import com.sjls.nstthh.entity.Scantask;
9 9
 import com.sjls.nstthh.entity.SendData;
10
+import com.sjls.nstthh.repository.EquipmentRepository;
10 11
 import com.sjls.nstthh.repository.GrainRepository;
11 12
 import com.sjls.nstthh.repository.ReceiveDataRepository;
12 13
 import com.sjls.nstthh.repository.SendDataRepository;
@@ -17,7 +18,6 @@ import org.slf4j.LoggerFactory;
17 18
 import org.springframework.beans.factory.annotation.Autowired;
18 19
 import org.springframework.stereotype.Service;
19 20
 
20
-import java.beans.Transient;
21 21
 import java.util.ArrayList;
22 22
 import java.util.Collections;
23 23
 import java.util.List;
@@ -36,6 +36,7 @@ public class CommunicationService {
36 36
     private SendDataRepository sendDataRepository;
37 37
     private ReceiveDataRepository receiveDataRepository;
38 38
     private GrainRepository grainRepository;
39
+    private EquipmentRepository equipmentRepository;
39 40
 
40 41
     /**
41 42
      * 16进制 str
@@ -80,6 +81,11 @@ public class CommunicationService {
80 81
         this.grainRepository = grainRepository;
81 82
     }
82 83
 
84
+    @Autowired
85
+    public void setEquipmentRepository(EquipmentRepository equipmentRepository) {
86
+        this.equipmentRepository = equipmentRepository;
87
+    }
88
+
83 89
     /**
84 90
      * 发送的
85 91
      *
@@ -101,6 +107,10 @@ public class CommunicationService {
101 107
         return receiveDataRepository.findAll();
102 108
     }
103 109
 
110
+    public List<ReceiveData> getReceiveData(Scantask scantask) {
111
+        return receiveDataRepository.findByCantask_id(scantask.getId());
112
+    }
113
+
104 114
     /**
105 115
      * 粮食信息
106 116
      *
@@ -178,7 +188,6 @@ public class CommunicationService {
178 188
             }
179 189
         } else {
180 190
             logger.info(": _send_str_anglezy _receive_str_anglezy, val false {},{} ", currentSendStr_zy, anglezy);
181
-
182 191
         }
183 192
         return true;
184 193
     }
@@ -219,7 +228,7 @@ public class CommunicationService {
219 228
      * @param diameter
220 229
      * @param high
221 230
      */
222
-    @Transient
231
+    //@Transient
223 232
     public List<SendData> creatToBeTested(Scantask scantask, double diameter, double high) {
224 233
 
225 234
         //sendDataRepository.deleteAll();

+ 49 - 18
src/main/java/com/sjls/nstthh/service/NRJavaSerialManager.java

@@ -1,8 +1,6 @@
1 1
 package com.sjls.nstthh.service;
2 2
 
3
-import com.sjls.nstthh.entity.Grain;
4
-import com.sjls.nstthh.entity.Scantask;
5
-import com.sjls.nstthh.entity.SendData;
3
+import com.sjls.nstthh.entity.*;
6 4
 import com.sjls.nstthh.util.StrSplicingAssistant;
7 5
 import gnu.io.NRSerialPort;
8 6
 import gnu.io.SerialPortEvent;
@@ -16,6 +14,8 @@ import org.springframework.web.client.RestTemplate;
16 14
 import java.io.DataInputStream;
17 15
 import java.io.DataOutputStream;
18 16
 import java.io.IOException;
17
+import java.io.UnsupportedEncodingException;
18
+import java.net.URLEncoder;
19 19
 import java.util.*;
20 20
 
21 21
 @Component
@@ -29,7 +29,7 @@ public class NRJavaSerialManager {
29 29
     volatile static double diameter = 12;
30 30
     volatile static double h1 = 6;
31 31
     volatile static double h2 = 3;
32
-    static String cloud_url = "http://112.51.248.191:9025/n-basic-edge/getEdgeData/{volume}/{xyz}/{startTime}";
32
+    static String cloud_url = "http://112.51.248.191:9025/api/n-basic-edge/getEdgeData";
33 33
     private volatile static String currentSendStr = str_init;
34 34
     private volatile static List<SendData> sendData = Collections.synchronizedList(new ArrayList<SendData>());
35 35
     private volatile static boolean isHeartbeat = false;
@@ -38,13 +38,6 @@ public class NRJavaSerialManager {
38 38
     private static Scantask scantask;
39 39
     private static CommunicationService communicationService;
40 40
 
41
-    public NRJavaSerialManager() {
42
-    }
43
-
44
-    public NRJavaSerialManager(Scantask st) {
45
-        this.scantask = st;
46
-    }
47
-
48 41
     public static double getHigh() {
49 42
         return high;
50 43
     }
@@ -77,7 +70,7 @@ public class NRJavaSerialManager {
77 70
         NRJavaSerialManager.h2 = h2;
78 71
     }
79 72
 
80
-    public final synchronized static boolean wait10Seconds(long m) {
73
+    public final static boolean wait10Seconds(long m) {
81 74
         try {
82 75
             Thread.sleep(m);
83 76
         } catch (InterruptedException e) {
@@ -85,17 +78,51 @@ public class NRJavaSerialManager {
85 78
         return true;
86 79
     }
87 80
 
88
-    public static void synctoCloudData() {
81
+    public static void synctoCloudData() throws UnsupportedEncodingException {
82
+
89 83
         List<Grain> grainList = communicationService.getGrainData(scantask);
84
+
90 85
         double v = 0;
91 86
         String str = "";
92 87
         for (Grain grain : grainList) {
93 88
             v += grain.getGrainVolume();
94 89
             str += grain.getXyz();
95 90
         }
91
+
92
+        SyncV2xyz syncV2xyz = new SyncV2xyz();
93
+        syncV2xyz.setXyz(URLEncoder.encode(str, "UTF-8"));
94
+        syncV2xyz.setVolume(v / 2);
95
+
96
+        syncV2xyz.setStartTime(scantask.getCreatTime());
97
+
98
+        logger.info("Grain -  {}", syncV2xyz);
99
+
96 100
         RestTemplate restTemplate = new RestTemplate();
97
-        restTemplate.getForObject(cloud_url, String.class, v / 2, str, scantask.getCreatTime());
98
-        logger.info("Grain - v,str {} {} ", cloud_url, v / 2);
101
+        restTemplate.postForObject(cloud_url, syncV2xyz, String.class);
102
+    }
103
+
104
+    /**
105
+     * 两次打点数据 平均
106
+     *
107
+     * @throws UnsupportedEncodingException
108
+     */
109
+    public static double getVolumeAvg() throws UnsupportedEncodingException {
110
+        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;
120
+
121
+        return realHypotenuse_avg;
122
+    }
123
+
124
+    public void setScantask(Scantask scantask) {
125
+        NRJavaSerialManager.scantask = scantask;
99 126
     }
100 127
 
101 128
     @Autowired
@@ -128,7 +155,11 @@ public class NRJavaSerialManager {
128 155
         t2.start();
129 156
         t2.join();
130 157
         Thread t3 = new Thread(() -> {
131
-            synctoCloudData();
158
+            try {
159
+                synctoCloudData();
160
+            } catch (UnsupportedEncodingException e) {
161
+                e.printStackTrace();
162
+            }
132 163
         });
133 164
         t3.start();
134 165
     }
@@ -158,7 +189,7 @@ public class NRJavaSerialManager {
158 189
                     if (str_receive.length() == 10) {
159 190
                         str_receive_at += str_receive;
160 191
                     }
161
-
192
+                    //wait10Seconds(600);
162 193
                     while (str_receive_at.length() == 74 && !isReceive) {
163 194
                         //synSet.add(str_receive_at);
164 195
                         if (!str_receive_at.contains("EEEEEEEE")) {
@@ -200,8 +231,8 @@ public class NRJavaSerialManager {
200 231
 //                    logger.info("init dot  ......{} {} " ,rs1,communicationService.receiveList.size());
201 232
 
202 233
                     if (isHeartbeat && onlyOnce) {
203
-                        outStream.write(StrSplicingAssistant.hexStrToByteArrs(str_init));
204 234
                         wait10Seconds(2000);
235
+                        outStream.write(StrSplicingAssistant.hexStrToByteArrs(str_init));
205 236
                         onlyOnce = false;
206 237
                         logger.info("init_send_str =" + str_init.substring(38, 46));
207 238
                     }

+ 10 - 1
src/main/java/com/sjls/nstthh/service/SaticScheduleTask.java

@@ -1,5 +1,6 @@
1 1
 package com.sjls.nstthh.service;
2 2
 
3
+import cn.hutool.core.date.DateUtil;
3 4
 import org.slf4j.Logger;
4 5
 import org.slf4j.LoggerFactory;
5 6
 import org.springframework.beans.factory.annotation.Autowired;
@@ -41,8 +42,16 @@ public class SaticScheduleTask {
41 42
         //}
42 43
 
43 44
         // /dev/ttyUSB0     /dev/tty.usbserial-14130
44
-        if (CommConfig.serialPort != null && CommConfig.serialPort.isConnected())
45
+        //todo 手动连接串口
46
+
47
+        if (CommConfig.serialPort != null && CommConfig.serialPort.isConnected()) {
48
+            logger.info("{} :定时扫描任务开启......", DateUtil.now());
45 49
             scantaskService.creatScanTask(CommConfig.serialPort);
50
+            logger.info("{} :定时扫描任务结束......", DateUtil.now());
51
+
52
+        } else {
53
+            logger.info("{} :定时扫描任务未开启,手动链接串口......", DateUtil.now());
54
+        }
46 55
 
47 56
         //System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
48 57
     }

+ 9 - 4
src/main/java/com/sjls/nstthh/service/ScantaskService.java

@@ -20,12 +20,18 @@ public class ScantaskService {
20 20
 
21 21
 
22 22
     private ScantaskRepository scantaskRepository;
23
+    private NRJavaSerialManager nRJavaSerialManager;
23 24
 
24 25
     @Autowired
25 26
     public void setScantaskRepository(ScantaskRepository scantaskRepository) {
26 27
         this.scantaskRepository = scantaskRepository;
27 28
     }
28 29
 
30
+    @Autowired
31
+    public void setnRJavaSerialManager(NRJavaSerialManager nRJavaSerialManager) {
32
+        this.nRJavaSerialManager = nRJavaSerialManager;
33
+    }
34
+
29 35
     /**
30 36
      * @return
31 37
      */
@@ -43,13 +49,12 @@ public class ScantaskService {
43 49
         //String key = id + "a1001" + DateTime.now().getTime();
44 50
 
45 51
         Scantask scantask = new Scantask();
46
-        scantask.setStkey(String.valueOf(id));
47 52
 
53
+        scantask.setStkey(String.valueOf(id));
48 54
         scantask.setCreatTime(DateTime.now());
49 55
         Scantask scantaskdb = scantaskRepository.save(scantask);
50
-
51
-        new NRJavaSerialManager(scantaskdb).operation(serialPort);
52
-
56
+        nRJavaSerialManager.setScantask(scantaskdb);
57
+        nRJavaSerialManager.operation(serialPort);
53 58
         return scantaskdb;
54 59
     }
55 60
 }

+ 11 - 2
src/main/java/com/sjls/nstthh/util/AlgAlgorithm.java

@@ -148,7 +148,8 @@ public class AlgAlgorithm {
148 148
         double x = 0; //实际点的坐标的x轴
149 149
         double y = 0; //实际点的坐标的y轴
150 150
         double z = 0; //实际点的坐标的z轴
151
-
151
+        int yy = 0;
152
+        int xx = 0;
152 153
         StringBuffer sb = new StringBuffer();
153 154
         sb.append("[");
154 155
 
@@ -204,7 +205,15 @@ public class AlgAlgorithm {
204 205
                 }
205 206
                 y = y + diameter / 2;
206 207
 
207
-                sb.append("[" + x + "," + y + "," + z + "],");
208
+
209
+                BigDecimal big_z = new BigDecimal(z);
210
+                z = big_z.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
211
+
212
+                yy = (int) Math.round(y);
213
+                xx = (int) Math.round(x);
214
+
215
+                sb.append("[" + xx + "," + yy + "," + z + "],");
216
+                //sb.append("[" + x + "," + y + "," + z + "],");
208 217
             }
209 218
 
210 219
         }

+ 2 - 2
src/main/resources/application.yml

@@ -4,9 +4,9 @@ spring:
4 4
   datasource:
5 5
     driver-class-name: com.mysql.cj.jdbc.Driver
6 6
     username: root
7
-    password: 123456
7
+    password: 1qaz@WSX
8 8
     ## Edge-dongtian
9
-    url: jdbc:mysql://192.168.40.74:3306/nstthh?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
9
+    url: jdbc:mysql://101.36.160.140:21029/nstthh?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
10 10
   jpa:
11 11
     show-sql: false
12 12
     open-in-view: false

+ 1 - 3
src/main/resources/templates/index.html

@@ -24,13 +24,11 @@
24 24
             <td th:text="${scantask.id}"></td>
25 25
             <td th:text="${scantask.stkey}"></td>
26 26
             <td th:text="${scantask.creatTime}"></td>
27
-            <td th:text="${scantask.warehouse.id}"></td>
28 27
         </tr>
29 28
     </table>
30 29
     <div class="panel panel-default">
31 30
         <div class="panel-body">
32
-            port:8888
33
-            /admin /sync-v2xyy
31
+            /admin /sync-v2xyy/id
34 32
         </div>
35 33
     </div>
36 34
 </div>

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

@@ -8,7 +8,6 @@
8 8
     <script type="text/javascript">
9 9
         $(document).ready(function () {
10 10
             $("#start_scanning").attr("disabled", true);
11
-
12 11
             $("#connect_equipment").click(function () {
13 12
                 $("#start_scanning").attr("disabled", false);
14 13
                 $("#connect_equipment").attr("disabled", true);
@@ -23,8 +22,11 @@
23 22
                         $('#connect_equipment').text('正在链接,请稍等!');
24 23
                     },
25 24
                     success: function (data, status) {
25
+                        //localStorage.setItem("serialIsConnected", data);
26 26
                         if (data) {
27 27
                             $("#connect_equipment").text("设备连接成功");
28
+                        } else {
29
+                            alert('未成功 或 重复连接');
28 30
                         }
29 31
                     }
30 32
                 });
@@ -86,6 +88,7 @@
86 88
                     <input class="form-control" id="h2" name="h2" placeholder="h2" type="text">
87 89
                 </div>
88 90
             </div>
91
+            <br/>
89 92
             <div class="row">
90 93
                 <div class="col-xs-12">
91 94
                     <input class="form-control" id="high" name="port" placeholder="高" type="text">