瀏覽代碼

IoT_2021_

hasan 3 年之前
父節點
當前提交
2662aac684

+ 36 - 0
.gitignore

@@ -0,0 +1,36 @@
1
+HELP.md
2
+target/
3
+!.mvn/wrapper/maven-wrapper.jar
4
+!**/src/main/**/target/
5
+!**/src/test/**/target/
6
+
7
+### STS ###
8
+.apt_generated
9
+.classpath
10
+.factorypath
11
+.project
12
+.settings
13
+.springBeans
14
+.sts4-cache
15
+
16
+### IntelliJ IDEA ###
17
+.idea
18
+*.iws
19
+*.iml
20
+*.ipr
21
+.mvn
22
+mvnw
23
+mvnw.cmd
24
+Nstthh.iml
25
+### NetBeans ###
26
+/nbproject/private/
27
+/nbbuild/
28
+/dist/
29
+/nbdist/
30
+/.nb-gradle/
31
+build/
32
+!**/src/main/**/build/
33
+!**/src/test/**/build/
34
+
35
+### VS Code ###
36
+.vscode/

+ 6 - 0
package.json

@@ -0,0 +1,6 @@
1
+{
2
+  "name": "Nstthh",
3
+  "version": "1.0.0",
4
+  "dependencies": {
5
+  }
6
+}

+ 90 - 0
pom.xml

@@ -0,0 +1,90 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4
+    <modelVersion>4.0.0</modelVersion>
5
+    <parent>
6
+        <groupId>org.springframework.boot</groupId>
7
+        <artifactId>spring-boot-starter-parent</artifactId>
8
+        <version>2.6.1</version>
9
+        <relativePath/> <!-- lookup parent from repository -->
10
+    </parent>
11
+    <groupId>com.sjls</groupId>
12
+    <artifactId>Nstthh</artifactId>
13
+    <version>0.0.1-SNAPSHOT</version>
14
+    <name>Nstthh</name>
15
+    <description>Nstthh</description>
16
+    <properties>
17
+        <java.version>1.8</java.version>
18
+    </properties>
19
+    <dependencies>
20
+        <dependency>
21
+            <groupId>org.projectlombok</groupId>
22
+            <artifactId>lombok</artifactId>
23
+            <optional>true</optional>
24
+        </dependency>
25
+        <dependency>
26
+            <groupId>org.springframework.boot</groupId>
27
+            <artifactId>spring-boot-starter-test</artifactId>
28
+            <scope>test</scope>
29
+        </dependency>
30
+        <dependency>
31
+            <groupId>org.springframework.boot</groupId>
32
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
33
+        </dependency>
34
+        <dependency>
35
+            <groupId>com.h2database</groupId>
36
+            <artifactId>h2</artifactId>
37
+            <scope>runtime</scope>
38
+        </dependency>
39
+        <dependency>
40
+            <groupId>com.neuronrobotics</groupId>
41
+            <artifactId>nrjavaserial</artifactId>
42
+            <version>5.1.1</version>
43
+        </dependency>
44
+
45
+        <dependency>
46
+            <groupId>cn.hutool</groupId>
47
+            <artifactId>hutool-all</artifactId>
48
+            <version>5.7.17</version>
49
+        </dependency>
50
+
51
+        <dependency>
52
+            <groupId>junit</groupId>
53
+            <artifactId>junit</artifactId>
54
+            <scope>test</scope>
55
+        </dependency>
56
+        <dependency>
57
+            <groupId>org.springframework.boot</groupId>
58
+            <artifactId>spring-boot-starter-actuator</artifactId>
59
+        </dependency>
60
+        <dependency>
61
+            <groupId>org.springframework.boot</groupId>
62
+            <artifactId>spring-boot-starter-web</artifactId>
63
+        </dependency>
64
+        <dependency>
65
+            <groupId>org.testng</groupId>
66
+            <artifactId>testng</artifactId>
67
+            <version>RELEASE</version>
68
+            <scope>compile</scope>
69
+        </dependency>
70
+    </dependencies>
71
+
72
+    <build>
73
+        <plugins>
74
+            <plugin>
75
+                <groupId>org.springframework.boot</groupId>
76
+                <artifactId>spring-boot-maven-plugin</artifactId>
77
+                <configuration>
78
+                    <excludes>
79
+                        <exclude>
80
+                            <groupId>org.projectlombok</groupId>
81
+                            <artifactId>lombok</artifactId>
82
+                        </exclude>
83
+                    </excludes>
84
+                </configuration>
85
+            </plugin>
86
+
87
+        </plugins>
88
+    </build>
89
+
90
+</project>

+ 31 - 0
src/main/java/com/sjls/nstthh/NstthhApplication.java

@@ -0,0 +1,31 @@
1
+package com.sjls.nstthh;
2
+
3
+
4
+import com.sjls.nstthh.usb.NRJavaSerialService;
5
+import org.springframework.boot.SpringApplication;
6
+import org.springframework.boot.autoconfigure.SpringBootApplication;
7
+
8
+
9
+@SpringBootApplication
10
+
11
+public class NstthhApplication {
12
+
13
+    public static void main(String[] args) {
14
+        SpringApplication.run(NstthhApplication.class, args);
15
+
16
+//        System.out.println(" Hello world java pi ......" );
17
+//        for (String s: NRSerialPort.getAvailableSerialPorts()){
18
+//            System.out.println("Availible port = " + s);
19
+//        }
20
+
21
+        try {
22
+            NRJavaSerialService nRJavaSerialService = new NRJavaSerialService();
23
+            nRJavaSerialService.connect("/dev/ttyUSB0");// /dev/ttyUSB0
24
+
25
+        } catch (Exception e) {
26
+            e.printStackTrace();
27
+        }
28
+
29
+    }
30
+
31
+}

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

@@ -0,0 +1,19 @@
1
+package com.sjls.nstthh.controller;
2
+
3
+import org.springframework.web.bind.annotation.RequestMapping;
4
+import org.springframework.web.bind.annotation.RestController;
5
+
6
+/**
7
+ * smp admin
8
+ */
9
+
10
+@RestController
11
+public class HardwareAdmin {
12
+
13
+    @RequestMapping("/hello")
14
+    public String index() {
15
+        return "Hello World";
16
+    }
17
+
18
+}
19
+

+ 52 - 0
src/main/java/com/sjls/nstthh/entity/CommunicationData.java

@@ -0,0 +1,52 @@
1
+package com.sjls.nstthh.entity;
2
+
3
+
4
+import lombok.Data;
5
+
6
+import javax.persistence.Entity;
7
+import javax.persistence.GeneratedValue;
8
+import javax.persistence.GenerationType;
9
+import javax.persistence.Id;
10
+import java.util.Date;
11
+
12
+
13
+/**
14
+ * 通讯数据
15
+ */
16
+
17
+@Data
18
+@Entity
19
+public class CommunicationData {
20
+    @Id //主键
21
+    @GeneratedValue(strategy = GenerationType.IDENTITY)//自增
22
+
23
+    private Long id;
24
+
25
+
26
+    private long sendId;
27
+    private long receiveId;
28
+
29
+
30
+    private String sendAKey; //12345678;029B029B
31
+    private String receiveKey; //12345678;029B029B
32
+
33
+    // private String heartbeatData;
34
+
35
+    private String deviceid; //12345678
36
+    private String distance; //00000989
37
+//    private String anglez;//截取角度 029B
38
+//    private String angley;//截取角度 029B
39
+
40
+    private String groupVal; //组
41
+    private Date creatDate;
42
+
43
+
44
+    public static void main(String[] args) {
45
+
46
+        //68220185000000E08812345678031101B50102029B029B0000048600FF7B123456781C7016
47
+        String str = "681C0105000000CD9612345678031101A5010203E803E80012345678149B16";  //38 , 46
48
+        System.out.println("args = " + str.getBytes().length);
49
+
50
+        System.out.println("args = " + str.length());
51
+    }
52
+}

+ 22 - 0
src/main/java/com/sjls/nstthh/entity/Equipment.java

@@ -0,0 +1,22 @@
1
+package com.sjls.nstthh.entity;
2
+
3
+import lombok.Data;
4
+
5
+import javax.persistence.Entity;
6
+import javax.persistence.GeneratedValue;
7
+import javax.persistence.GenerationType;
8
+import javax.persistence.Id;
9
+
10
+/**
11
+ * dpj
12
+ */
13
+@Data
14
+@Entity
15
+public class Equipment {
16
+    @Id //主键
17
+    @GeneratedValue(strategy = GenerationType.IDENTITY)//自增
18
+    private Long id;
19
+
20
+    private String emTitle;
21
+
22
+}

+ 19 - 0
src/main/java/com/sjls/nstthh/entity/ReceiveData.java

@@ -0,0 +1,19 @@
1
+package com.sjls.nstthh.entity;
2
+
3
+import lombok.Data;
4
+
5
+import javax.persistence.Entity;
6
+import javax.persistence.GeneratedValue;
7
+import javax.persistence.GenerationType;
8
+import javax.persistence.Id;
9
+
10
+@Data
11
+@Entity
12
+public class ReceiveData {
13
+    @Id //主键
14
+    @GeneratedValue(strategy = GenerationType.IDENTITY)//自增
15
+    private Long id;
16
+
17
+
18
+    private String receiveStr;
19
+}

+ 19 - 0
src/main/java/com/sjls/nstthh/entity/SendData.java

@@ -0,0 +1,19 @@
1
+package com.sjls.nstthh.entity;
2
+
3
+import lombok.Data;
4
+
5
+import javax.persistence.Entity;
6
+import javax.persistence.GeneratedValue;
7
+import javax.persistence.GenerationType;
8
+import javax.persistence.Id;
9
+
10
+@Data
11
+@Entity
12
+public class SendData {
13
+    @Id //主键
14
+    @GeneratedValue(strategy = GenerationType.IDENTITY)//自增
15
+    private Long id;
16
+
17
+    private String sendStr;
18
+
19
+}

+ 11 - 0
src/main/java/com/sjls/nstthh/repository/CommunicationDataRepository.java

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

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

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

+ 9 - 0
src/main/java/com/sjls/nstthh/repository/SendDataRepository.java

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

+ 244 - 0
src/main/java/com/sjls/nstthh/usb/NRJavaSerialService.java

@@ -0,0 +1,244 @@
1
+package com.sjls.nstthh.usb;
2
+
3
+import com.sjls.nstthh.util.StrSplicingAssistant;
4
+import gnu.io.NRSerialPort;
5
+import org.slf4j.Logger;
6
+import org.slf4j.LoggerFactory;
7
+import org.springframework.stereotype.Component;
8
+
9
+import java.io.DataInputStream;
10
+import java.io.DataOutputStream;
11
+import java.io.IOException;
12
+import java.util.ArrayList;
13
+import java.util.Iterator;
14
+
15
+/**
16
+ * x64 usb NRJavaSerial
17
+ * <p>
18
+ * SATA;COM;uart;Serial;usb
19
+ */
20
+
21
+@Component
22
+public class NRJavaSerialService {
23
+    private static final Logger logger = LoggerFactory.getLogger(NRJavaSerialService.class);
24
+    static ArrayList<String> sendStr = new ArrayList<String>();
25
+    private volatile static boolean isHeartbeat = false;
26
+    private volatile static boolean isReceive = false;
27
+
28
+    static {
29
+        sendStr.add("681C0105000000CD9612345678031101A50102087708230012345678843C16");
30
+        sendStr.add("681C0105000000CD9612345678031101A501020831082E0012345678013B16");
31
+        sendStr.add("681C0105000000CD9612345678031101A501020877083A0012345678863516");
32
+        sendStr.add("681C0105000000CD9612345678031101A501020387038A00123456785AB116");
33
+        sendStr.add("681C0105000000CD9612345678031101A50102087708170012345678804816");
34
+        sendStr.add("681C0105000000CD9612345678031101A501020827081D001234567885DE16");
35
+        sendStr.add("681C0105000000CD9612345678031101A5010207EE082B00123456780D8216");
36
+        sendStr.add("681C0105000000CD9612345678031101A5010207F8083D00123456788E5216");
37
+        sendStr.add("681C0105000000CD9612345678031101A501020877084600123456788DC916");
38
+        sendStr.add("681C0105000000CD9612345678031101A5010203C0037B00123456780A4616");
39
+        sendStr.add("681C0105000000CD9612345678031101A5010203CA038D00123456789EAF16");
40
+        sendStr.add("681C0105000000CD9612345678031101A501020391039B0012345678D8D616");
41
+        sendStr.add("681C0105000000CD9612345678031101A501020877080C001234567883A316");
42
+        sendStr.add("681C0105000000CD9612345678031101A50102082D080F001234567806D316");
43
+        sendStr.add("681C0105000000CD9612345678031101A5010207E90818001234567849A716");
44
+        sendStr.add("681C0105000000CD9612345678031101A5010207B20826001234567809F616");
45
+        sendStr.add("681C0105000000CD9612345678031101A50102079808380012345678882F16");
46
+        sendStr.add("681C0105000000CD9612345678031101A5010207C6084A0012345678064D16");
47
+        sendStr.add("681C0105000000CD9612345678031101A501020877085200123456788EDD16");
48
+        sendStr.add("681C0105000000CD9612345678031101A5010203F2036E00123456788A4E16");
49
+        sendStr.add("681C0105000000CD9612345678031101A5010204200380001234567837B516");
50
+        sendStr.add("681C0105000000CD9612345678031101A50102040603920012345678B6F516");
51
+        sendStr.add("681C0105000000CD9612345678031101A5010203CF03A0001234567858ED16");
52
+        sendStr.add("681C0105000000CD9612345678031101A50102038B03A900123456785DB716");
53
+        sendStr.add("681C0105000000CD9612345678031101A5010208770801001234567882BE16");
54
+        sendStr.add("681C0105000000CD9612345678031101A5010208360803001234567846A016");
55
+        sendStr.add("681C0105000000CD9612345678031101A5010207F6080800123456780A7B16");
56
+        sendStr.add("681C0105000000CD9612345678031101A5010207B9081200123456784C3116");
57
+        sendStr.add("681C0105000000CD9612345678031101A501020782081F00123456780F8B16");
58
+        sendStr.add("681C0105000000CD9612345678031101A501020755082F001234567846C116");
59
+        sendStr.add("681C0105000000CD9612345678031101A501020741084100123456784EB016");
60
+        sendStr.add("681C0105000000CD9612345678031101A50102076B08530012345678CFA516");
61
+        sendStr.add("681C0105000000CD9612345678031101A50102083C085E0012345678CB9216");
62
+        sendStr.add("681C0105000000CD9612345678031101A50102041B03600012345678637216");
63
+        sendStr.add("681C0105000000CD9612345678031101A50102047203700012345678A7A016");
64
+        sendStr.add("681C0105000000CD9612345678031101A50102046E03830012345678B23A16");
65
+        sendStr.add("681C0105000000CD9612345678031101A50102044803930012345678329816");
66
+        sendStr.add("681C0105000000CD9612345678031101A50102041403A2001234567833D016");
67
+        sendStr.add("681C0105000000CD9612345678031101A5010203D903AC0012345678D90716");
68
+        sendStr.add("681C0105000000CD9612345678031101A50102039A03B400123456789EFA16");
69
+        sendStr.add("681C0105000000CD9612345678031101A50102087707F5001234567868CA16");
70
+        sendStr.add("681C0105000000CD9612345678031101A50102083A07F70012345678AC8116");
71
+        sendStr.add("681C0105000000CD9612345678031101A5010207FE07FB0012345678E01E16");
72
+        sendStr.add("681C0105000000CD9612345678031101A5010207C20803001234567809E716");
73
+        sendStr.add("681C0105000000CD9612345678031101A501020788080D00123456788C8616");
74
+        sendStr.add("681C0105000000CD9612345678031101A50102075008190012345678836816");
75
+        sendStr.add("681C0105000000CD9612345678031101A50102071B08290012345678C31B16");
76
+        sendStr.add("681C0105000000CD9612345678031101A5010206EF083A0012345678C29F16");
77
+        sendStr.add("681C0105000000CD9612345678031101A5010206D5084C00123456784AA216");
78
+        sendStr.add("681C0105000000CD9612345678031101A5010206FA085F00123456780A5916");
79
+        sendStr.add("681C0105000000CD9612345678031101A501020877086B00123456788BB416");
80
+        sendStr.add("681C0105000000CD9612345678031101A5010204BE03590012345678AC5C16");
81
+        sendStr.add("681C0105000000CD9612345678031101A5010204E3036C00123456786D5C16");
82
+        sendStr.add("681C0105000000CD9612345678031101A5010204C9037E0012345678EC4916");
83
+        sendStr.add("681C0105000000CD9612345678031101A50102049D038F0012345678FDA716");
84
+        sendStr.add("681C0105000000CD9612345678031101A501020468039F0012345678304C16");
85
+        sendStr.add("681C0105000000CD9612345678031101A50102043003AB001234567830A216");
86
+        sendStr.add("681C0105000000CD9612345678031101A5010203F603B50012345678995616");
87
+        sendStr.add("681C0105000000CD9612345678031101A5010203BA03BD00123456789C7B16");
88
+        sendStr.add("681C0105000000CD9612345678031101A50102037E03C100123456789A2416");
89
+        sendStr.add("681C0105000000CD9612345678031101A50102087707EA00123456786AA516");
90
+        sendStr.add("681C0105000000CD9612345678031101A50102083F07EC00123456786F5516");
91
+        sendStr.add("681C0105000000CD9612345678031101A50102080807EF00123456782D9416");
92
+        sendStr.add("681C0105000000CD9612345678031101A5010207D007F5001234567862A416");
93
+        sendStr.add("681C0105000000CD9612345678031101A50102079807FC001234567867AB16");
94
+        sendStr.add("681C0105000000CD9612345678031101A5010207610806001234567843DF16");
95
+        sendStr.add("681C0105000000CD9612345678031101A50102072908120012345678455D16");
96
+        sendStr.add("681C0105000000CD9612345678031101A5010206F2081F0012345678053F16");
97
+        sendStr.add("681C0105000000CD9612345678031101A5010206BA082F0012345678055916");
98
+        sendStr.add("681C0105000000CD9612345678031101A501020683083F0012345678C4B716");
99
+        sendStr.add("681C0105000000CD9612345678031101A50102064B08510012345678C1CF16");
100
+        sendStr.add("681C0105000000CD9612345678031101A50102061208640012345678013C16");
101
+        sendStr.add("681C0105000000CD9612345678031101A5010205DC05DC00123456785FA816");
102
+        sendStr.add("681C0105000000CD9612345678031101A5010205A603540012345678A07B16");
103
+        sendStr.add("681C0105000000CD9612345678031101A50102056D03670012345678E85B16");
104
+        sendStr.add("681C0105000000CD9612345678031101A50102053503790012345678EF7F16");
105
+        sendStr.add("681C0105000000CD9612345678031101A5010204FE03890012345678BBFC16");
106
+        sendStr.add("681C0105000000CD9612345678031101A5010204C603990012345678BBDE16");
107
+        sendStr.add("681C0105000000CD9612345678031101A50102048F03A600123456787A8B16");
108
+        sendStr.add("681C0105000000CD9612345678031101A50102045703B20012345678756516");
109
+        sendStr.add("681C0105000000CD9612345678031101A50102042003BC0012345678328916");
110
+        sendStr.add("681C0105000000CD9612345678031101A5010203E803C30012345678128016");
111
+        sendStr.add("681C0105000000CD9612345678031101A5010203B003C9001234567816B016");
112
+        sendStr.add("681C0105000000CD9612345678031101A50102037903CC0012345678DADF16");
113
+        sendStr.add("681C0105000000CD9612345678031101A501020877082F001234567884F016");
114
+
115
+    }
116
+
117
+    public static boolean wait10Seconds() {
118
+        try {
119
+            Thread.sleep(6000);
120
+        } catch (InterruptedException e) {
121
+        }
122
+        return true;
123
+    }
124
+
125
+    public static void main(String[] args) {
126
+        try {
127
+            NRJavaSerialService nRJavaSerialService = new NRJavaSerialService();
128
+            nRJavaSerialService.connect("/dev/tty.usbserial-14130");// /dev/ttyUSB0
129
+
130
+        } catch (Exception e) {
131
+            e.printStackTrace();
132
+        }
133
+    }
134
+
135
+    public void connect(String port) {
136
+
137
+
138
+        NRSerialPort serialPort = new NRSerialPort(port, 9600);
139
+        boolean b = serialPort.connect();
140
+        logger.info("serialPort------" + b);
141
+        DataInputStream inStream = new DataInputStream(serialPort.getInputStream());
142
+        DataOutputStream outStream = new DataOutputStream(serialPort.getOutputStream());
143
+
144
+        new Thread(new SerialWriter(outStream), "SerialWriter").start();
145
+        new Thread(new SerialReader(inStream), "SerialReader").start();
146
+    }
147
+
148
+    public static class SerialReader implements Runnable {
149
+        private final DataInputStream inStream;
150
+
151
+        public SerialReader(DataInputStream inStream) {
152
+            this.inStream = inStream;
153
+        }
154
+
155
+
156
+        public void run() {
157
+            try {
158
+                while (!Thread.interrupted()) {// read all bytes
159
+                    int byteLen = inStream.available();
160
+                    if (byteLen > 0) {
161
+                        byte[] buffer = new byte[byteLen];
162
+                        inStream.read(buffer);
163
+
164
+                        String str = StrSplicingAssistant.bytesToHexString(buffer);
165
+                        if (str.endsWith("16")) {
166
+
167
+                            //心跳 设备在线
168
+                            if (str.equals("68140186000000E55A1234567803510112345678B1E216")) {
169
+                                isHeartbeat = true;
170
+                            } else if (str.startsWith("68220185000000E088")) {
171
+                                isReceive = true;
172
+                                //异常 测距失败
173
+                                if (!str.contains("EEEEEEEE")) {
174
+                                    logger.info("EEEEEEEE------" + str);
175
+                                }
176
+
177
+                                // TODO: 2021/12/28 打点有值,但需要crc验证。
178
+
179
+                                //  等待10秒
180
+                                String deviceid = str.substring(18, 26);//截取设备id
181
+                                String anglez = str.substring(38, 42);//截取角度
182
+                                String angley = str.substring(42, 46);//截取角度
183
+                                String anglezy = str.substring(38, 46);//角度
184
+                                String distance = str.substring(46, 54);//截取距离
185
+                                logger.info("receive = " + anglezy + " | " + distance);
186
+
187
+                            } else if (wait10Seconds()) {
188
+                                isReceive = true;
189
+                                logger.info("wait10Seconds = " + str);
190
+
191
+                            }
192
+                        }
193
+                    }
194
+                    Thread.sleep(600);
195
+                }
196
+
197
+            } catch (IOException e) {
198
+                e.printStackTrace();
199
+            } catch (Exception e) {
200
+                e.printStackTrace();
201
+            }
202
+        }
203
+    }
204
+
205
+    public static class SerialWriter implements Runnable {
206
+        private final DataOutputStream outStream;
207
+        boolean onlyOnce = true;
208
+
209
+        public SerialWriter(DataOutputStream outStream) {
210
+            this.outStream = outStream;
211
+        }
212
+
213
+        public void run() {
214
+            try {
215
+                while (!Thread.interrupted()) {
216
+                    if (isHeartbeat && onlyOnce) {
217
+                        outStream.write(StrSplicingAssistant.hexStrToByteArrs("681C0105000000CD9612345678031101A5010205DC05DC00123456785FA816"));
218
+                        onlyOnce = false;
219
+                        logger.info("init = 1500,1500:05DC05DC");
220
+                    }
221
+                    if (isHeartbeat && isReceive) {
222
+                        if (sendStr.size() > 0) {
223
+                            Iterator iterator = sendStr.iterator();
224
+                            if (iterator.hasNext()) {
225
+                                String s = (String) iterator.next();
226
+                                outStream.write(StrSplicingAssistant.hexStrToByteArrs(s));
227
+                                iterator.remove();
228
+                                isReceive = false;
229
+                                logger.info(sendStr.size() + "_send_str = " + s.substring(38, 46));
230
+
231
+                            }
232
+                        }
233
+                    }
234
+                    Thread.sleep(600);
235
+                }
236
+
237
+            } catch (InterruptedException e) {
238
+                e.printStackTrace();
239
+            } catch (IOException e) {
240
+                e.printStackTrace();
241
+            }
242
+        }
243
+    }
244
+}

+ 113 - 0
src/main/java/com/sjls/nstthh/util/AlgAlgorithm.java

@@ -0,0 +1,113 @@
1
+package com.sjls.nstthh.util;
2
+
3
+
4
+public class AlgAlgorithm {
5
+
6
+    /**
7
+     * 打点
8
+     *
9
+     * @param
10
+     */
11
+
12
+    public static String getAveragePosition() {
13
+
14
+
15
+        StringBuffer sb = new StringBuffer();
16
+
17
+        //圆筒仓的直径
18
+        double d = 12;
19
+        //AC 是设备到地面的高
20
+        double AC = 35;
21
+        double x1 = 0;
22
+        double y1 = 0;
23
+        //打出来三角形的底边AB 实际是平面上点的斜边
24
+        double AB = 0; //理想状态
25
+        double x = 0;
26
+        double y = 0;
27
+        double BC = 0;
28
+        double vertical_angle = 0;//垂直的角度
29
+        double level_angle = 0;//水平的角度
30
+        double vertical_step = 0;//垂直的步长
31
+        double level_step = 0;//水平的步长
32
+
33
+        //根据直径判断半径是整数还是非整数 如果是非整数 则向下取整
34
+        double r = Math.floor(d / 2);  //最大的半径 即一共有多少个圆圈
35
+
36
+        //一共有多少个圆圈
37
+        int circleNumber = (int) r;
38
+
39
+        for (int i = 1; i <= circleNumber; i++) {
40
+            //每一个圆一共有多少个的点   此时的半径长就是i
41
+            int pointCount = (int) ((2 * Math.PI * i) / 1.57);
42
+
43
+            for (int j = 0; j < pointCount; j++) {
44
+
45
+                /**
46
+                 * 以其他点为圆心的圆  r是i degrees是以圆心为中心转动的角度 其坐标x1、y1则是
47
+                 */
48
+                double degrees = (360 / pointCount) * j;
49
+                //System.out.println(degrees+"度数");
50
+                double radians = Math.toRadians(degrees);//转化为弧度
51
+                String strx = String.format("%.2f%n", Math.cos(radians) * i);//计算横坐标的值
52
+                x1 = new Double(strx);
53
+                String stry = String.format("%.2f%n", Math.sin(radians) * i);//计算纵坐标的值
54
+                y1 = new Double(stry);
55
+
56
+                /**
57
+                 * 以设备为中心,把圆上的点分为上下两半,
58
+                 * 新的圆 是旧的圆向左平移了(d/2-i)+i 个长度 得到的
59
+                 * (设备在新的圆的最左边上,设备是坐标系的原点)
60
+                 * 所以 横坐标加上这个长度  纵坐标不变
61
+                 */
62
+
63
+                //纵坐标 无论是处于上半部分还是下半部分 其纵坐标是不变的
64
+                x = x1 + (d / 2 - i) + i;
65
+                y = y1;
66
+                //System.out.print("["+x+","+y+"]"+",");
67
+                //底面三角形的斜边
68
+                AB = Math.sqrt(x * x + y * y);
69
+                //垂直三角形的斜边
70
+                BC = Math.sqrt(AB * AB + AC * AC);
71
+                if (x == 0 && y == 0) {//圆 点
72
+                    level_angle = 0;
73
+                    vertical_angle = 0;
74
+
75
+                } else if (y < 0) {//反向转
76
+                    level_angle = -Math.toDegrees(Math.acos(Math.abs(y) / AB));
77
+                    vertical_angle = -(90 - Math.toDegrees(Math.acos(AC / BC)));
78
+
79
+                } else {//正向转
80
+                    level_angle = Math.toDegrees(Math.acos(y / AB));
81
+                    vertical_angle = 90 - Math.toDegrees(Math.acos(AC / BC));
82
+
83
+                }
84
+                //转为步长
85
+                level_step = 1500 + level_angle / 0.135;
86
+                vertical_step = 1500 + vertical_angle / 0.135;
87
+
88
+                sb.append(Math.round(level_step));
89
+                sb.append(",");
90
+                sb.append(Math.round(vertical_step));
91
+                sb.append(";");
92
+                // sb.append("\n");
93
+
94
+            }
95
+        }
96
+
97
+        /**
98
+         * 追加一个圆筒仓中心的点,其坐标是(d/2,0) 水平转动90度 步长为2167
99
+         */
100
+        AB = d / 2;//垂直三角形的底边
101
+        BC = Math.sqrt(AB * AB + AC * AC);//垂直三角形的斜边
102
+        vertical_angle = 90 - Math.toDegrees(Math.acos(AC / BC));
103
+        vertical_step = 1500 + vertical_angle / 0.135;
104
+        sb.append(2167);
105
+        sb.append(",");
106
+        sb.append(Math.round(vertical_step));
107
+        // sb.append("\n");
108
+        //System.out.println(Math.round(level_step)+","+Math.round(vertical_step)+"");
109
+        //System.out.println(sb);
110
+
111
+        return sb.toString();
112
+    }
113
+}

+ 78 - 0
src/main/java/com/sjls/nstthh/util/LaserRangingServer.java

@@ -0,0 +1,78 @@
1
+package com.sjls.nstthh.util;
2
+
3
+import cn.hutool.core.io.FileUtil;
4
+import cn.hutool.core.io.file.FileReader;
5
+
6
+import java.io.IOException;
7
+import java.io.OutputStream;
8
+import java.net.ServerSocket;
9
+import java.net.Socket;
10
+import java.util.List;
11
+
12
+/**
13
+ * node java
14
+ */
15
+public class LaserRangingServer implements Runnable {
16
+
17
+    private static final int PORT = 8089;
18
+    static String start = "68 07 FF FF FF FF FF E6 F1 16";
19
+    static String end = "68 07 00 00 00 00 00 F3 71 16";
20
+    private final Socket socket;
21
+
22
+    LaserRangingServer(Socket socket) {
23
+        this.socket = socket;
24
+    }
25
+
26
+    public static void main(String[] args) throws IOException {
27
+        ServerSocket serverSocket = new ServerSocket(PORT);
28
+        while (true) {
29
+            Socket socket = serverSocket.accept();
30
+            new Thread(new LaserRangingServer(socket)).start();
31
+        }
32
+    }
33
+
34
+    @Override
35
+    public void run() {
36
+        try {
37
+            System.out.println("new:" + socket.getInetAddress() + ":" + socket.getPort());
38
+
39
+            OutputStream out = socket.getOutputStream();
40
+            while (true) {
41
+                if (FileUtil.exist("send_data.dat")) {
42
+                    FileReader fileReader = new FileReader("send_data.dat");
43
+                    List<String> str = fileReader.readLines();
44
+                    if (!str.isEmpty()) {
45
+                        out.write(StrSplicingAssistant.hexStrToByteArrs(start));
46
+                        System.out.println(start + "-----------strar");
47
+                        for (String s : str) {
48
+                            Thread.sleep(3600);
49
+                            out.write(StrSplicingAssistant.hexStrToByteArrs(s));
50
+                            System.out.println(s);
51
+                        }
52
+                        Thread.sleep(1200);
53
+                        out.write(StrSplicingAssistant.hexStrToByteArrs(end));
54
+                        System.out.println(end + "-----------end");
55
+                        //out.flush();
56
+                        FileUtil.del("send_data.dat");
57
+                    }
58
+                    Thread.sleep(3600);
59
+                    //out.close();
60
+                }
61
+            }
62
+        } catch (IOException e) {
63
+            e.printStackTrace();
64
+        } catch (InterruptedException e) {
65
+            e.printStackTrace();
66
+        } finally {
67
+            if (socket != null) {
68
+                try {
69
+                    socket.close();
70
+                } catch (IOException e) {
71
+                    e.printStackTrace();
72
+                }
73
+            }
74
+
75
+        }
76
+
77
+    }
78
+}

+ 203 - 0
src/main/java/com/sjls/nstthh/util/StrSplicingAssistant.java

@@ -0,0 +1,203 @@
1
+package com.sjls.nstthh.util;
2
+
3
+import java.io.ByteArrayOutputStream;
4
+
5
+public class StrSplicingAssistant {
6
+
7
+    private static final String hexString = "0123456789ABCDEF";
8
+    /**
9
+     * 步长率
10
+     */
11
+    static double Step = 0.135;
12
+
13
+    /**
14
+     * 计算CRC16校验码
15
+     *
16
+     * @param bytes
17
+     * @return
18
+     */
19
+    public static String getCRC(byte[] bytes) {
20
+        int CRC = 0x0000ffff;
21
+        int POLYNOMIAL = 0x0000a001;
22
+
23
+        int i, j;
24
+        for (i = 0; i < bytes.length; i++) {
25
+            CRC ^= ((int) bytes[i] & 0x000000ff);
26
+            for (j = 0; j < 8; j++) {
27
+                if ((CRC & 0x00000001) != 0) {
28
+                    CRC >>= 1;
29
+                    CRC ^= POLYNOMIAL;
30
+                } else {
31
+                    CRC >>= 1;
32
+                }
33
+            }
34
+        }
35
+        return Integer.toHexString(CRC);
36
+    }
37
+
38
+    /**
39
+     * 逆序校验
40
+     *
41
+     * @param dataHexStr
42
+     * @return
43
+     */
44
+    public static String CRC16Right(String dataHexStr) {
45
+        //字符串类型的16进制字符串转换为字节数组
46
+        byte[] data = hexStrToByteArrs(dataHexStr);
47
+        //初始化寄存器 我们用数据的低八位进行处理,已经可以不用限制寄存器的个数了
48
+        //我测试了int long 都能达到预期的效果 但是short 反而数据错误 没想明白为什么。
49
+        int crci = 0xffff;
50
+        int i, j;
51
+        for (j = 0; j < data.length; j++) {
52
+            //此处&0x00ff的目的是为了将数据放入寄存器的低8位并与寄存器异或
53
+            crci ^= data[j] & 0x00ff;
54
+            for (i = 0; i < 8; i++) {
55
+                //判断即将右移的是不是1 如果是1则与多项式进行异或
56
+                if ((crci & 0X0001) == 0X0001) {
57
+                    crci >>= 1;
58
+                    //将数据与约定的多项式做(模二除/异或)  数据流向变了此处的异或值实际是8005的逆序反转a001
59
+                    crci ^= 0xa001;
60
+                } else {
61
+                    crci >>= 1;
62
+                }
63
+            }
64
+        }
65
+        //结果值与0x0000异或 其实值不会改变,不异或对结果没有影响
66
+        String res = Long.toHexString(crci ^ 0x0000);
67
+        if (res.length() < 4) {
68
+            res = "0" + res;
69
+        }
70
+
71
+        return res;
72
+    }
73
+
74
+    public static String strEncode10_16(String str10) {
75
+        byte[] bytes = str10.getBytes();
76
+        StringBuilder sb = new StringBuilder(bytes.length * 2);
77
+
78
+        for (int i = 0; i < bytes.length; i++) {
79
+            sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
80
+            sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
81
+        }
82
+        return sb.toString();
83
+    }
84
+
85
+    public static String strDecode16_10(String str16) {
86
+
87
+        ByteArrayOutputStream baos = new ByteArrayOutputStream(str16.length() / 2);
88
+
89
+        for (int i = 0; i < str16.length(); i += 2)
90
+            baos.write((hexString.indexOf(str16.charAt(i)) << 4 | hexString.indexOf(str16.charAt(i + 1))));
91
+        return baos.toString();
92
+    }
93
+
94
+    /**
95
+     * 将十六进制的字符串转换成字节数组
96
+     *
97
+     * @param hexString
98
+     * @return byte[]
99
+     */
100
+    public static byte[] hexStrToByteArrs(String hexString) {
101
+        hexString = hexString.replaceAll(" ", "");
102
+        int len = hexString.length();
103
+        int index = 0;
104
+
105
+        byte[] bytes = new byte[len / 2];
106
+
107
+        while (index < len) {
108
+            String sub = hexString.substring(index, index + 2);
109
+            bytes[index / 2] = (byte) Integer.parseInt(sub, 16);
110
+            index += 2;
111
+        }
112
+
113
+        return bytes;
114
+    }
115
+
116
+    public static String byteToHex(byte b) {
117
+        String hex = Integer.toHexString(b & 0xFF);
118
+        if (hex.length() < 2) {
119
+            hex = "0" + hex;
120
+        }
121
+        return hex;
122
+    }
123
+
124
+
125
+    /**
126
+     * 数组转换成十六进制字符串
127
+     *
128
+     * @param bArray
129
+     * @return HexString
130
+     */
131
+    public static final String bytesToHexString(byte[] bArray) {
132
+
133
+        StringBuffer sb = new StringBuffer(bArray.length);
134
+        String sTemp;
135
+        for (int i = 0; i < bArray.length; i++) {
136
+            sTemp = Integer.toHexString(0xFF & bArray[i]);
137
+            if (sTemp.length() < 2)
138
+                sb.append(0);
139
+            sb.append(sTemp.toUpperCase());
140
+        }
141
+        return sb.toString();
142
+    }
143
+
144
+    /**
145
+     * 10进制 转 16进制
146
+     *
147
+     * @param i
148
+     * @return 16_i
149
+     */
150
+    public static String hex10To16(long i) {
151
+        return String.format("%04X", i);
152
+    }
153
+
154
+    /**
155
+     * 角度转步长
156
+     * 500?2500? 1500
157
+     *
158
+     * @param angle
159
+     * @return int
160
+     */
161
+    public static long angleTransformationStep(double angle) {
162
+        return Math.round(angle / Step);
163
+    }
164
+
165
+
166
+    /**
167
+     * 字符串 每隔2位加空格,最后也有空格
168
+     * 如 E803E800 转 E8 03 E8 00
169
+     *
170
+     * @param str
171
+     * @return String
172
+     */
173
+    public static String spaceEveryTwoStrings(String str) {
174
+        return str.replaceAll("(.{2})", "$1 ");
175
+    }
176
+
177
+
178
+    /**
179
+     * 高低位转换 大写
180
+     * 如“1A2B”转“2B1A”。
181
+     *
182
+     * @param hex
183
+     * @return String
184
+     */
185
+    public static String displacementHighLow(String hex) {
186
+        char[] charArray = hex.toCharArray();
187
+        int length = charArray.length;
188
+        int times = length / 2;
189
+        for (int c1i = 0; c1i < times; c1i += 2) {
190
+            int c2i = c1i + 1;
191
+            char c1 = charArray[c1i];
192
+            char c2 = charArray[c2i];
193
+            int c3i = length - c1i - 2;
194
+            int c4i = length - c1i - 1;
195
+            charArray[c1i] = charArray[c3i];
196
+            charArray[c2i] = charArray[c4i];
197
+            charArray[c3i] = c1;
198
+            charArray[c4i] = c2;
199
+        }
200
+        return new String(charArray).toUpperCase();
201
+
202
+    }
203
+}

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

@@ -0,0 +1,26 @@
1
+server:
2
+  port: 8080
3
+management:
4
+  endpoint:
5
+    shutdown:
6
+      enabled: true
7
+  endpoints:
8
+    web:
9
+      exposure:
10
+        include: shutdown
11
+# DataSource Config
12
+spring:
13
+  datasource:
14
+    driver-class-name: org.h2.Driver
15
+    url: jdbc:h2:~/nstthh;AUTO_SERVER=TRUE
16
+    username: sa
17
+    password: 123456
18
+
19
+  jpa:
20
+    show-sql: true
21
+    hibernate:
22
+      ddl-auto: update
23
+  h2:
24
+    console:
25
+      path: /h2-console
26
+      enabled: true

+ 387 - 0
src/main/resources/static/data.js

@@ -0,0 +1,387 @@
1
+const fs = require('fs');
2
+const readline = require('readline');
3
+const liner = readline.createInterface({
4
+    input: fs.createReadStream("/home/pi/config.txt")
5
+});
6
+var lineIndex = 0;
7
+var line;
8
+var ServerIP;
9
+var ServerPort;
10
+var ComName;
11
+var HttpUrl;
12
+var chunk;
13
+
14
+liner.on('line', function (line) { //事件监听
15
+
16
+    if (lineIndex === 0)
17
+        ServerIP = line;
18
+    else if (lineIndex === 1)
19
+        ServerPort = line;
20
+    else if (lineIndex === 2)
21
+        ComName = line;
22
+    else if (lineIndex === 3) {
23
+        HttpUrl = line;
24
+        console.log('ServerIP:' + ServerIP + "," + "ServerPort:" + ServerPort + "," + "ComName:" + ComName + "," + "HttpUrl:" + HttpUrl);
25
+    }
26
+
27
+    lineIndex = lineIndex + 1;
28
+})
29
+var SocketDataIndex = 0;
30
+var SocketDataList = [];
31
+var isSocketStarted = false;//socket开始接受数据
32
+var isSocketEnded = false;//socket结束接受数据
33
+var isComStart = false;//串口是否有回复
34
+var net = require('net');
35
+var socket;
36
+var isConnected = false;
37
+
38
+setInterval(function () {
39
+    if (isConnected == false) {
40
+        TCPFunction();
41
+    }
42
+}, 3000);//定时3秒检查tcp连接状态
43
+
44
+
45
+const iconv = require("iconv-lite");
46
+const encodeconvert = require("encoding");
47
+
48
+function TCPFunction() {
49
+
50
+    socket = new net.Socket();
51
+    socket.setEncoding('hex');
52
+
53
+    socket.connect(ServerPort, ServerIP, function () {
54
+        socket.write('hello');
55
+        isConnected = true;
56
+    });
57
+
58
+    socket.on('data', function (data) {
59
+        var data1 = data.slice(0, data.legth);
60
+
61
+        console.log("value:" + data1);
62
+        if (isSocketStarted == false) {
63
+            if (data1 == "6807ffffffffffe6f116") {//开始命令
64
+
65
+                isSuccessHttp = false;
66
+                StartHttpNum = 0;
67
+                clearInterval(IntervalHttp);//释放http的定时器
68
+                isSocketEnded = false;
69
+                isComStart = false;
70
+
71
+                post_data = [];
72
+                isSocketStarted = true;
73
+                SocketDataIndex = 0;
74
+                SocketDataList = [];
75
+                fs.writeFile('/home/pi/DataTest/data1.txt', "", (err, data) => {
76
+                    if (err) throw err;
77
+                });
78
+                fs.writeFile('/home/pi/DataTest/data2.txt', "", (err, data) => {
79
+                    if (err) throw err;
80
+                });
81
+            } else {
82
+                if ((data1 != 0xff) && (data1 != "68140186000000e55a12345678b1e216")) {
83
+                    var bt = Hexstring2btye(data1);
84
+                    serialPort.write(bt, function (err, results) {
85
+                        //console.log('err'+err);
86
+                        //console.log('results'+results);
87
+                    });
88
+                }
89
+            }
90
+        } else {
91
+            if (data1 == "68070000000000f37116") {//结束命令
92
+                isSocketEnded = true;
93
+                isSocketStarted = false;
94
+                isComStart = true;
95
+                console.log('SocketDataList:' + SocketDataList[0]);
96
+                var bt = Hexstring2btye(SocketDataList[0]);
97
+                serialPort.write(bt, function (err, results) {
98
+                    //console.log('err'+err);
99
+                    //console.log('results'+results);
100
+                    /*
101
+                    var bt = Hexstring2btye(tst);
102
+                    console.log(bt);
103
+                    var st = Bytes2HexString(bt);
104
+                    console.log(st);
105
+                    */
106
+                });
107
+                SocketDataIndex = 0;
108
+            } else {
109
+                if (isSocketEnded == false) {
110
+                    SocketDataList[SocketDataIndex] = data1;
111
+                    SocketDataIndex = SocketDataIndex + 1;
112
+                    fs.appendFile('/home/pi/DataTest/data1.txt', data1 + '\r\n', (err, data) => {
113
+                        if (err) throw err;
114
+                    });
115
+                }
116
+            }
117
+        }
118
+    });
119
+
120
+    socket.on('end', function () {
121
+        console.log("disconnect");
122
+        isConnected = false;
123
+        socket.destroy();
124
+    });
125
+
126
+    socket.on('error', function (err) {
127
+        isConnected = false;
128
+        console.log(err);
129
+        socket.destroy();
130
+    });
131
+
132
+}
133
+
134
+var serialPort = require('serialport');
135
+setTimeout(function () {
136
+
137
+    //Opening a Port
138
+    serialPort = new serialPort(ComName, {
139
+        //波特率,可在设备管理器中对应端口的属性中查看
140
+        baudRate: 9600,
141
+        autoOpen: false
142
+    })
143
+    //连接串口后进行写入指令操作
144
+    serialPort.open(function (err) {
145
+        console.log('IsOpen:', serialPort.isOpen)
146
+        //		serialPort.write("123",function(err,results){
147
+        //console.log('err'+err);
148
+        //console.log('results'+results);
149
+        //});
150
+
151
+    })
152
+
153
+    setInterval(function () {
154
+        var btm;
155
+        var i;
156
+        if (isComStart == true) {
157
+            RevComDataCount = RevComDataCount + 1;
158
+            if (RevComDataCount >= 10) {
159
+                RevComDataCount = 0;
160
+                //打印模拟数据
161
+                console.log('time out!!!!!!!!!!!!!!!!!');
162
+                var chunkc = ['68', '22', '01', '85', '00', '00', '00', 'e0', '88', '12', '34', '56', '78', '03', '11', '01', 'b5', '01', '02', 'ff', 'ff', 'ff', 'ff', 'ff', 'ff', 'ff', 'ff', '00', 'ff', 'd8', '12', '34', '56', '78', 'd8', '69', '16'];
163
+                btm = chunkc;
164
+                console.log('chaoshi:' + btm.length + "    " + btm);
165
+                analysisData(btm);
166
+            }
167
+        }//超时反馈假数据
168
+    }, 1000);//定时1秒检查串口是否接受过数据
169
+
170
+    var isRevComData = false;
171
+    var RevComDataCount = 0;
172
+
173
+    var chunk_buf;
174
+    var flag_com;
175
+    var mlenbuf;
176
+    const InterByteTimeout = require('@serialport/parser-inter-byte-timeout');
177
+    const parser = serialPort.pipe(new InterByteTimeout({interval: 25})); // 2000毫秒未接到数据触发
178
+    parser.on('data', chunk => {
179
+        console.log('data received: ' + chunk);
180
+        console.log('data received: ' + chunk.length + "    " + chunk.toString('hex'));
181
+        if (isComStart == true) {
182
+            analysisData(chunk);
183
+        }
184
+
185
+    });
186
+
187
+
188
+    function analysisData(chunk) {
189
+        if (chunk.length < 37) {
190
+            if (chunk[1] == 0x22) {
191
+                chunk_buf = chunk;
192
+                flag_com = 2;
193
+                mlenbuf = chunk.length;
194
+                //console.log('mlenbuf:' + mlenbuf);////
195
+            } else if (((chunk.length + mlenbuf) == 37) && (flag_com == 2)) {
196
+                chunk = chunk_buf.toString('hex') + chunk.toString('hex');
197
+                flag_com = 3;
198
+                //console.log('3333' + chunk);
199
+            } else {
200
+                chunk = "";
201
+            }
202
+            RevComDataCount = 0;//有数据定时器计数清零
203
+        }
204
+        if ((chunk.length == 37) || (flag_com == 3)) {
205
+
206
+            RevComDataCount = 0;//有数据定时器计数清零
207
+            fs.appendFile('/home/pi/DataTest/data2.txt', chunk.toString('hex') + '\r\n', (err, msg) => {
208
+                if (err) throw err;
209
+            });
210
+
211
+            var comData = chunk.toString('hex');
212
+            var jsonArr = {};
213
+            if (comData.length == 110) {
214
+                var idd;
215
+                var comDatabuf;
216
+                for (idd = 0; idd < 110; idd++) {
217
+                    if (comData[idd] == ',') {
218
+                    } else {
219
+                        comDatabuf += comData[idd];
220
+                    }
221
+                }
222
+                comData = comDatabuf.substring(9, 83);
223
+            }
224
+            post_data.deviceid = comData.substring(18, 26);//截取设备id
225
+            jsonArr.anglez = comData.substring(38, 42);//截取角度
226
+            jsonArr.angley = comData.substring(42, 46);//截取角度
227
+            jsonArr.distance = comData.substring(46, 54);//截取距离
228
+
229
+            post_data.push(jsonArr);//json合并
230
+
231
+            if (isComStart == true) {
232
+
233
+                console.log(post_data);
234
+
235
+                SocketDataIndex = SocketDataIndex + 1;
236
+                if (SocketDataIndex >= SocketDataList.length) {
237
+                    isComStart = false;
238
+                    console.log('startHttp');
239
+
240
+                    HttpFun();
241
+                    IntervalHttp = setInterval(HttpFun, 30000);//间隔30秒发送http
242
+
243
+                } else {
244
+                    var bt = Hexstring2btye(SocketDataList[SocketDataIndex]);
245
+                    serialPort.write(bt, function (err, results) {
246
+                        //console.log('err'+err);
247
+                        //console.log('results'+results);
248
+                    });
249
+
250
+                }
251
+
252
+            }
253
+            console.log('SocketDataList:' + SocketDataList.length + "," + SocketDataIndex);
254
+            flag_com = 0;
255
+            mlenbuf = 0;
256
+        } else {
257
+            chunk = "";
258
+        }
259
+    }
260
+
261
+    //指令监听
262
+    /*serialPort.on('data', function (data) {
263
+        console.log('data received: ' + data.toString('hex'));
264
+        fs.appendFile('/home/pi/DataTest/data2.txt', data.toString('hex') + '\r\n', (err, data) => {
265
+            if (err) throw err;
266
+        });
267
+        var comData = data.toString('hex');
268
+        var jsonArr = {};
269
+        post_data.deviceid = comData.substring(18, 26);//截取设备id
270
+        jsonArr.angle = comData.substring(38, 41);//截取角度
271
+        jsonArr.angle2 = comData.substring(42, 45);//截取角度
272
+        jsonArr.distance = comData.substring(46, 54);//截取距离
273
+
274
+        post_data.push(jsonArr);//json合并
275
+
276
+
277
+
278
+        if (isComStart == true) {
279
+
280
+            console.log(post_data);
281
+
282
+            SocketDataIndex = SocketDataIndex + 1;
283
+            if (SocketDataIndex >= SocketDataList.length) {
284
+                isComStart = false;
285
+                console.log('startHttp');
286
+
287
+                HttpFun();
288
+                IntervalHttp = setInterval(HttpFun, 30000);//间隔30秒发送http
289
+
290
+            }
291
+            else {
292
+                var bt = Hexstring2btye(SocketDataList[SocketDataIndex]);
293
+                serialPort.write(bt, function (err, results) {
294
+                    //console.log('err'+err);
295
+                    //console.log('results'+results);
296
+                });
297
+                time.sleep(0.1);
298
+            }
299
+
300
+        }
301
+        console.log('SocketDataList:' + SocketDataList.length + "," + SocketDataIndex);
302
+
303
+    })*/
304
+    //错误监听
305
+    serialPort.on('error', function (error) {
306
+        console.log('error: ' + error)
307
+    })
308
+
309
+}, 1000);
310
+
311
+var StartHttpNum = 0;//发送http的次数
312
+var IntervalHttp;//定时器,发送htto
313
+var isSuccessHttp = false;//http是否成功响应
314
+var post_data = [];//json数组
315
+function HttpFun() {
316
+    if (isSuccessHttp == false) {
317
+        console.log('startHttp');
318
+        StartHttpNum = StartHttpNum + 1;
319
+        if (StartHttpNum > 10) {
320
+            StartHttpNum = 0;
321
+            clearInterval(IntervalHttp);//释放http的定时器
322
+            isSocketStarted = false;
323
+            isSocketEnded = false;
324
+            isComStart = false;
325
+        }
326
+        var request = require("request");
327
+        request({
328
+            url: HttpUrl,
329
+            method: "GET",
330
+            json: true,
331
+            headers: {
332
+                "content-type": "application/json",
333
+            },
334
+            body: JSON.stringify(post_data)
335
+        }, function (error, response, body) {
336
+            if (!error && response.statusCode == 200) {
337
+                console.log(body);
338
+                isSuccessHttp = true;
339
+                clearInterval(IntervalHttp);
340
+
341
+                isSocketStarted = false;
342
+                isSocketEnded = false;
343
+                isComStart = false;
344
+            }
345
+        });
346
+    }
347
+
348
+}
349
+
350
+const Hexstring2btye = (str) => {
351
+    let pos = 0;
352
+    let len = str.length;
353
+    if (len % 2 != 0) {
354
+        return null;
355
+    }
356
+    len /= 2;
357
+    let hexA = [];
358
+    for (let i = 0; i < len; i++) {
359
+        let s = str.substr(pos, 2);
360
+        let v = parseInt(s, 16);
361
+        hexA.push(v);
362
+        pos += 2;
363
+    }
364
+    return hexA;
365
+}
366
+
367
+
368
+const Bytes2HexString = (b) => {
369
+    let hexs = "";
370
+    for (let i = 0; i < b.length; i++) {
371
+        let hex = b[i].toString(16);
372
+        if (hex.length == 1) {
373
+            hex = '0' + hex;
374
+        }
375
+        hexs += hex.toUpperCase();
376
+    }
377
+    return hexs;
378
+}
379
+
380
+
381
+/*
382
+var tst = "ba3697dd768150d1e2162dd9ae643694";
383
+
384
+var bt = Hexstring2btye(tst);
385
+console.log(bt);
386
+var st = Bytes2HexString(bt);
387
+console.log(st);*/

+ 13 - 0
src/test/java/com/sjls/nstthh/NstthhApplicationTests.java

@@ -0,0 +1,13 @@
1
+package com.sjls.nstthh;
2
+
3
+import org.junit.jupiter.api.Test;
4
+import org.springframework.boot.test.context.SpringBootTest;
5
+
6
+@SpringBootTest
7
+class NstthhApplicationTests {
8
+
9
+    @Test
10
+    void contextLoads() {
11
+    }
12
+
13
+}

+ 36 - 0
src/test/java/com/sjls/nstthh/RepositoryTest.java

@@ -0,0 +1,36 @@
1
+package com.sjls.nstthh;
2
+
3
+
4
+import com.sjls.nstthh.repository.CommunicationDataRepository;
5
+import org.junit.Test;
6
+import org.junit.runner.RunWith;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.boot.test.context.SpringBootTest;
9
+import org.springframework.test.context.junit4.SpringRunner;
10
+
11
+@SpringBootTest
12
+@RunWith(SpringRunner.class)
13
+public class RepositoryTest {
14
+
15
+    @Autowired
16
+    private CommunicationDataRepository userRepository;
17
+
18
+    @Test
19
+    public void addUser() {
20
+//        CommunicationData user = new CommunicationData();
21
+//        //user.setId(1L);
22
+//        user.setReceive(true);
23
+//        user.setSend(true);
24
+//        user.setSendData("681C0105000000CD9612345678031101A50102029B029B001234567845F916");
25
+//        user.setReceiveData("68220185000000E08812345678031101B50102029B029B0000098900FF8312341234567803510112345678B1E21600000000");
26
+//        userRepository.save(user);
27
+//
28
+//
29
+//        SendData sendData = new SendData();
30
+//        //user.setId(1L);
31
+//        sendData.setSendStr("681C0105000000CD9612345678031101A50102029B029B001234567845F916");
32
+//        //sendData.setGroupVal();
33
+//        sendData.setSendKey("029B029B");
34
+//        userRepository.save(user);
35
+    }
36
+}

+ 105 - 0
src/test/java/com/sjls/nstthh/TestNstthh.java

@@ -0,0 +1,105 @@
1
+package com.sjls.nstthh;
2
+
3
+import cn.hutool.core.util.StrUtil;
4
+import com.sjls.nstthh.util.AlgAlgorithm;
5
+import com.sjls.nstthh.util.StrSplicingAssistant;
6
+
7
+import java.io.IOException;
8
+
9
+public class TestNstthh {
10
+
11
+    static String start = "68 07 FF FF FF FF FF E6 F1 16";
12
+    static String end = "68 07 00 00 00 00 00 F3 71 16";
13
+
14
+    public static void main(String[] args) throws IOException {
15
+
16
+        String str = AlgAlgorithm.getAveragePosition();
17
+        String[] strArr_z_y = str.split(";");
18
+
19
+        for (int i = 0; i < strArr_z_y.length; i++) {
20
+
21
+            String[] strArr = strArr_z_y[i].split(",");
22
+
23
+
24
+            //System.out.println(Long.valueOf(strArr[0]));  System.out.println(Long.valueOf(strArr[1]));
25
+            String str10 = send16strUseCom(Long.valueOf(strArr[0]), Long.valueOf(strArr[1]));
26
+            System.out.println("sendStr.add(\"" + str10 + "\");");
27
+        }
28
+
29
+
30
+    }
31
+
32
+    public static String send16strUseCom(long z_val, long y_val) {
33
+
34
+        // 第一个{}为 :z,y 角度转为步长 16进制 每隔两位字符串串加空格
35
+        // 第二个{}为 :sendStr_1_tmp串 crc验证后
36
+        String sendStr_0_tmp = "681C0105000000CD96";
37
+        String sendStr_1_tmp = "12345678031101A50102{}0012345678";
38
+        String sendStr_2_tmp = "{}16";
39
+
40
+
41
+//        String z = StrSplicingAssistant.hex10To16(StrSplicingAssistant.angleTransformationStep(z_val));
42
+//        String y = StrSplicingAssistant.hex10To16(StrSplicingAssistant.angleTransformationStep(y_val));
43
+        String z = StrSplicingAssistant.hex10To16(z_val);
44
+        String y = StrSplicingAssistant.hex10To16(y_val);
45
+        //16进制zy 替换 sendStr_1_tmp{}
46
+        String sendStr_1_v = StrUtil.format(sendStr_1_tmp, z + y);
47
+        //sendStr_1_v比特流CRC验证逆序并高低位置换后加空格格式化 替换 sendStr_2_tmp{}
48
+        String sendStr_2_v = StrUtil.format(sendStr_2_tmp, StrSplicingAssistant.displacementHighLow(StrSplicingAssistant.CRC16Right(sendStr_1_v)));
49
+
50
+        String sendstr_ = sendStr_0_tmp + sendStr_1_v + sendStr_2_v;
51
+        // System.out.print(sendstr_);
52
+
53
+        return sendstr_;
54
+    }
55
+
56
+    public static String send16str(double z_val, double y_val) {
57
+
58
+        // 第一个{}为 :z,y 角度转为步长 16进制 每隔两位字符串串加空格
59
+        // 第二个{}为 :sendStr_1_tmp串 crc验证后
60
+        String sendStr_0_tmp = "68 1C 01 05 00 00 00 CD 96 ";
61
+        String sendStr_1_tmp = "12 34 56 78 03 11 01 A5 01 02 {}00 12 34 56 78 ";
62
+        String sendStr_2_tmp = "{}16";
63
+
64
+
65
+        String z = StrSplicingAssistant.hex10To16(StrSplicingAssistant.angleTransformationStep(z_val));
66
+        String y = StrSplicingAssistant.hex10To16(StrSplicingAssistant.angleTransformationStep(y_val));
67
+
68
+        //16进制zy 替换 sendStr_1_tmp{}
69
+        String sendStr_1_v = StrUtil.format(sendStr_1_tmp, StrSplicingAssistant.spaceEveryTwoStrings(z + y));
70
+        //sendStr_1_v比特流CRC验证逆序并高低位置换后加空格格式化 替换 sendStr_2_tmp{}
71
+        String sendStr_2_v = StrUtil.format(sendStr_2_tmp, StrSplicingAssistant.spaceEveryTwoStrings(StrSplicingAssistant.displacementHighLow(StrSplicingAssistant.CRC16Right(sendStr_1_v))));
72
+
73
+        String sendstr_ = sendStr_0_tmp + sendStr_1_v + sendStr_2_v;
74
+        // System.out.print(sendstr_);
75
+
76
+        return sendstr_;
77
+    }
78
+
79
+    /**
80
+     * z,y;z,y;
81
+     *
82
+     * @param zyStr
83
+     * @return
84
+     */
85
+    public static String[] zyStrToSend16str(String zyStr) {
86
+
87
+
88
+        String[] strArr_zy = zyStr.split("\\;");
89
+        String[] strArr_zy_n = new String[strArr_zy.length];
90
+        for (int i = 0; i < strArr_zy.length; i++) {
91
+
92
+            String[] strArr_z_y = strArr_zy[i].split("\\,");
93
+
94
+            String z = strArr_z_y[0];
95
+            String y = strArr_z_y[1];
96
+
97
+            // strArr_zy_n[i] = send16strUseCom(Double.valueOf(z), Double.valueOf(y));
98
+            //strArr_zy_n[i] = send16str(Double.valueOf(z), Double.valueOf(y));
99
+
100
+        }
101
+
102
+        return strArr_zy_n;
103
+    }
104
+
105
+}