|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+package com.unissoft.beidou.demo;
|
|
|
2
|
+
|
|
|
3
|
+import com.yang.protocolstack.ProtocolStack;
|
|
|
4
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
5
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
6
|
+import org.springframework.boot.ApplicationArguments;
|
|
|
7
|
+import org.springframework.boot.ApplicationRunner;
|
|
|
8
|
+import org.springframework.core.annotation.Order;
|
|
|
9
|
+import org.springframework.stereotype.Component;
|
|
|
10
|
+import com.yang.protocolstack.protocol.*;
|
|
|
11
|
+
|
|
|
12
|
+import java.net.InetAddress;
|
|
|
13
|
+import java.util.Arrays;
|
|
|
14
|
+import java.util.Date;
|
|
|
15
|
+
|
|
|
16
|
+/**
|
|
|
17
|
+ * @author qingsong.han
|
|
|
18
|
+ * @description: 延迟启动, 加载类
|
|
|
19
|
+ * @create 2022-12-15 17:19
|
|
|
20
|
+ */
|
|
|
21
|
+@Slf4j
|
|
|
22
|
+@Component
|
|
|
23
|
+@Order(1)
|
|
|
24
|
+public class BeiDouConnect implements ApplicationRunner {
|
|
|
25
|
+
|
|
|
26
|
+ //1.获取配置ip port
|
|
|
27
|
+ @Value("${customSocket.beidou.command.ip}")
|
|
|
28
|
+ private String ip;
|
|
|
29
|
+ @Value("${customSocket.beidou.command.port}")
|
|
|
30
|
+ private int port;
|
|
|
31
|
+ @Value("${customSocket.beidou.command.protocolType}")
|
|
|
32
|
+ private int protocolType;
|
|
|
33
|
+
|
|
|
34
|
+ // 全局定义
|
|
|
35
|
+ private SocketClient socketClient;
|
|
|
36
|
+ private ProtocolStack protocolStack;
|
|
|
37
|
+ private int id;
|
|
|
38
|
+ private int SubCount = 0;
|
|
|
39
|
+
|
|
|
40
|
+ private Thread readThread = new Thread(new Runnable() {
|
|
|
41
|
+ @Override
|
|
|
42
|
+ public void run() {
|
|
|
43
|
+ while (true) {
|
|
|
44
|
+ Response response;
|
|
|
45
|
+ if (protocolStack != null) {
|
|
|
46
|
+ response = protocolStack.getResponse();
|
|
|
47
|
+ if (response != null) {
|
|
|
48
|
+ switch (response.getMsgType()) {
|
|
|
49
|
+ case MsgType.BSI_PROTOCOL:
|
|
|
50
|
+ //2.1协议
|
|
|
51
|
+ log.info("波束强度: {}" + Arrays.toString(((BDBSIResponse) response).getPower()));
|
|
|
52
|
+ break;
|
|
|
53
|
+ case MsgType.ZDA_PROTOCOL:
|
|
|
54
|
+ //2.1协议
|
|
|
55
|
+ log.info("时间: {}" + ((BDZDAResponse) response).getTimeBJ());
|
|
|
56
|
+ break;
|
|
|
57
|
+ case MsgType.GLZK_PROTOCOL:
|
|
|
58
|
+ //4.0协议
|
|
|
59
|
+ log.info("波束强度: {}" + Arrays.toString(((BDGLZKResponse) response).getPower()));
|
|
|
60
|
+ break;
|
|
|
61
|
+ case MsgType.ICXX_PROTOCOL:
|
|
|
62
|
+ log.info(
|
|
|
63
|
+ "北斗卡号: {}", ((BDICXXResponse) response).getId(),
|
|
|
64
|
+ "通播地址: {}", ((BDICXXResponse) response).getBroadcastId(),
|
|
|
65
|
+ "用户机特征: {}", ((BDICXXResponse) response).getFeature(),
|
|
|
66
|
+ "服务频度: {}", ((BDICXXResponse) response).getServerFreq(),
|
|
|
67
|
+ "通信长度: {}", ((BDICXXResponse) response).getMaxCommLength()
|
|
|
68
|
+ );
|
|
|
69
|
+ id = ((BDICXXResponse) response).getId();
|
|
|
70
|
+ log.info("Thread id: {}", id);
|
|
|
71
|
+ SubCount = ((BDICXXResponse) response).getNumber();
|
|
|
72
|
+ log.info("Thread SubCount: {}", SubCount);
|
|
|
73
|
+ break;
|
|
|
74
|
+ case MsgType.ICI_PROTOCOL:
|
|
|
75
|
+ log.info(
|
|
|
76
|
+ "北斗卡号: {}", ((BDICIResponse) response).getLocalId(),
|
|
|
77
|
+ "通播地址: {}", ((BDICIResponse) response).getBroadcastId(),
|
|
|
78
|
+ "用户机特征: {}", ((BDICIResponse) response).getFeature(),
|
|
|
79
|
+ "服务频度: {}", ((BDICIResponse) response).getServerFreq(),
|
|
|
80
|
+ "通信长度: {}", ((BDICIResponse) response).getMaxCommLength()
|
|
|
81
|
+ );
|
|
|
82
|
+ id = ((BDICIResponse) response).getLocalId();
|
|
|
83
|
+ log.info("Thread id: {}", id);
|
|
|
84
|
+ break;
|
|
|
85
|
+ case MsgType.SJXX_PROTOCOL:
|
|
|
86
|
+ //4.0协议
|
|
|
87
|
+ log.info("时间: {}" + ((BDSJXXResponse) response).getTime());
|
|
|
88
|
+ break;
|
|
|
89
|
+ case MsgType.DWR_PROTOCOL:
|
|
|
90
|
+ log.info(String.format("定位位置:经度: %.06f 纬度: %.06f %n", ((BDDWRResponse) response).getLongitude(), ((BDDWRResponse) response).getLatitude()));
|
|
|
91
|
+ break;
|
|
|
92
|
+ case MsgType.DWXX_PROTOCOL:
|
|
|
93
|
+ log.info(String.format("定位位置:经度: %s 纬度: %s %n", ((BDDWXXResponse) response).getLongitude(), ((BDDWXXResponse) response).getLatitude()));
|
|
|
94
|
+ break;
|
|
|
95
|
+ case MsgType.FKI_PROTOCOL:
|
|
|
96
|
+ if (((BDFKIResponse) response).isCmdRes()) {
|
|
|
97
|
+ log.info("申请成功,等待反馈!");
|
|
|
98
|
+ } else {
|
|
|
99
|
+ log.info(String.format("申请失败,频度未到。等待时间:%d s%n", ((BDFKIResponse) response).getWaitTime()));
|
|
|
100
|
+ }
|
|
|
101
|
+ break;
|
|
|
102
|
+ case MsgType.FKXX_PROTOCOL:
|
|
|
103
|
+ if (((BDFKXXResponse) response).isCmdResult()) {
|
|
|
104
|
+ log.info("申请成功,等待反馈!");
|
|
|
105
|
+ } else {
|
|
|
106
|
+ log.info(String.format("申请失败,频度未到。等待时间:%d s%n", ((BDFKXXResponse) response).getWaitTime()));
|
|
|
107
|
+ }
|
|
|
108
|
+ break;
|
|
|
109
|
+ case MsgType.WAA_PROTOCOL:
|
|
|
110
|
+ log.info(String.format("收到位置报告:经度: %.06f 纬度: %.06f %n", ((BDWAAResponse) response).getLongitude(), ((BDWAAResponse) response).getLatitude()));
|
|
|
111
|
+ break;
|
|
|
112
|
+ case MsgType.TXR_PROTOCOL:
|
|
|
113
|
+ log.info(String.format("收到通信信息:内容: %s 发送方: %d 通信类型: %d %n", ((BDTXRResponse) response).getCommInfo(), ((BDTXRResponse) response).getSenderID(), ((BDTXRResponse) response).getCodeType()));
|
|
|
114
|
+ break;
|
|
|
115
|
+ case MsgType.TXXX_PROTOCOL:
|
|
|
116
|
+ if (((BDTXXXResponse) response).getType() == 3) {
|
|
|
117
|
+ log.info(
|
|
|
118
|
+ String.format("4.0协议通过通信发送接收位置报告,接收到的位置信息: 时间: %tT,经度: %s,纬度: %s,高程: %d%n", new Date(((BDTXXXResponse) response).getTime()),
|
|
|
119
|
+ ((BDTXXXResponse) response).getLongitude(), ((BDTXXXResponse) response).getLatitude(), ((BDTXXXResponse) response).getAltitude())
|
|
|
120
|
+ );
|
|
|
121
|
+ } else {
|
|
|
122
|
+ log.info(String.format("收到通信信息:内容: %s 发送方: %s 通信类型: %d %n", ((BDTXXXResponse) response).getContent(), ((BDTXXXResponse) response).getSendId(), ((BDTXXXResponse) response).getType()));
|
|
|
123
|
+ }
|
|
|
124
|
+ break;
|
|
|
125
|
+ case MsgType.ZJX_PROTOCOL: {//2.1协议
|
|
|
126
|
+ log.info("自检信息 IC状态: {}", ((BDZJXResponse) response).getCardState());
|
|
|
127
|
+ log.info("自检信息 IC状态: {}", ((BDZJXResponse) response).getCardState());
|
|
|
128
|
+ log.info("自检信息 入站状态: {}", ((BDZJXResponse) response).getInState());
|
|
|
129
|
+ }
|
|
|
130
|
+ break;
|
|
|
131
|
+ }
|
|
|
132
|
+ } else {
|
|
|
133
|
+ try {
|
|
|
134
|
+ Thread.sleep(1);
|
|
|
135
|
+ } catch (InterruptedException e) {
|
|
|
136
|
+ e.printStackTrace();
|
|
|
137
|
+ }
|
|
|
138
|
+ }
|
|
|
139
|
+ } else {
|
|
|
140
|
+ try {
|
|
|
141
|
+ Thread.sleep(1);
|
|
|
142
|
+ } catch (InterruptedException e) {
|
|
|
143
|
+ e.printStackTrace();
|
|
|
144
|
+ }
|
|
|
145
|
+ }
|
|
|
146
|
+ }
|
|
|
147
|
+ }
|
|
|
148
|
+ });
|
|
|
149
|
+
|
|
|
150
|
+
|
|
|
151
|
+ //2.打开网口
|
|
|
152
|
+ private void openSocket() {
|
|
|
153
|
+ try {
|
|
|
154
|
+ socketClient = new SocketClient(ip, port) {
|
|
|
155
|
+ @Override
|
|
|
156
|
+ public void onReceive(InetAddress addr, byte[] bytes) {
|
|
|
157
|
+ protocolStack.setBytes(bytes);
|
|
|
158
|
+ }
|
|
|
159
|
+
|
|
|
160
|
+ @Override
|
|
|
161
|
+ public void onDisconnect(InetAddress addr) {
|
|
|
162
|
+ log.info("openSocket addr: {}", addr);
|
|
|
163
|
+ }
|
|
|
164
|
+ };
|
|
|
165
|
+ protocolStack = new ProtocolStack();
|
|
|
166
|
+ protocolStack.init(protocolType, false);
|
|
|
167
|
+ socketClient.start();
|
|
|
168
|
+ } catch (Exception e1) {
|
|
|
169
|
+ e1.printStackTrace();
|
|
|
170
|
+ }
|
|
|
171
|
+ if (!readThread.isAlive()) {
|
|
|
172
|
+ readThread.start();
|
|
|
173
|
+ }
|
|
|
174
|
+ }
|
|
|
175
|
+
|
|
|
176
|
+ // 关闭网口
|
|
|
177
|
+ private void closeSocket() {
|
|
|
178
|
+ protocolStack.stop();
|
|
|
179
|
+ socketClient.stop();
|
|
|
180
|
+ log.info("网口已关闭");
|
|
|
181
|
+ protocolStack = null;
|
|
|
182
|
+ }
|
|
|
183
|
+
|
|
|
184
|
+ private void actionListener() throws Exception {
|
|
|
185
|
+ // 打开网口
|
|
|
186
|
+ log.info("打开网口中...");
|
|
|
187
|
+ Thread.sleep(1);
|
|
|
188
|
+ this.openSocket();
|
|
|
189
|
+ log.info("网口已打开!");
|
|
|
190
|
+ log.info("读卡申请中...");
|
|
|
191
|
+ if (protocolStack != null) {
|
|
|
192
|
+ //3.读卡
|
|
|
193
|
+ Thread.sleep(1);
|
|
|
194
|
+ socketClient.send(protocolStack.getDKSQ());
|
|
|
195
|
+ Thread.sleep(1000);
|
|
|
196
|
+ if (id == 0) {
|
|
|
197
|
+ log.info("未读卡,在获取到卡号之后才能定位...");
|
|
|
198
|
+ } else {
|
|
|
199
|
+ //4.获取定位
|
|
|
200
|
+ log.info("定位申请中...");
|
|
|
201
|
+ socketClient.send(protocolStack.getDWSQ(String.valueOf(id), false, 0));
|
|
|
202
|
+
|
|
|
203
|
+ //5.读取下属
|
|
|
204
|
+ log.info("读子用户申请中...");
|
|
|
205
|
+ Thread.sleep(1);
|
|
|
206
|
+ byte icNo = 1;
|
|
|
207
|
+ for (int i = 0; i < SubCount / 20; i++)
|
|
|
208
|
+ //默认百用户 千用户时需要设置为true
|
|
|
209
|
+ socketClient.send(protocolStack.getZYHDKSQ(icNo, id + "", false));
|
|
|
210
|
+
|
|
|
211
|
+ //6.下属位置报告
|
|
|
212
|
+ log.info("发送位置报告中...");
|
|
|
213
|
+ Thread.sleep(1);
|
|
|
214
|
+ if (protocolType == 0) {
|
|
|
215
|
+ //仅2.1协议
|
|
|
216
|
+ socketClient.send(protocolStack.getWZBG(String.valueOf(id), 0, null));
|
|
|
217
|
+ } else {
|
|
|
218
|
+ socketClient.send(protocolStack.getWZXX40(String.valueOf(id), new Date().getTime(), 38.123d, 112.456d, 153, String.valueOf(id)));
|
|
|
219
|
+ }
|
|
|
220
|
+ }
|
|
|
221
|
+ }
|
|
|
222
|
+
|
|
|
223
|
+ }
|
|
|
224
|
+
|
|
|
225
|
+ @Override
|
|
|
226
|
+ public void run(ApplicationArguments args) throws Exception {
|
|
|
227
|
+ actionListener();
|
|
|
228
|
+ }
|
|
|
229
|
+
|
|
|
230
|
+}
|