|
@@ -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;
|