|
@@ -1,24 +1,31 @@
|
1
|
1
|
package com.njyunzhi.nanyang.service.impl;
|
2
|
2
|
|
|
3
|
+import com.alibaba.fastjson.JSON;
|
3
|
4
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
4
|
5
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
6
|
+import com.hikvision.ae.hatc.enums.SubscribableMessageTypeEnum;
|
5
|
7
|
import com.hikvision.ae.hatc.message.cms2sdk.DeviceChannel;
|
6
|
8
|
import com.hikvision.ae.hatc.message.cms2sdk.DeviceInfo;
|
7
|
9
|
import com.hikvision.ae.hatc.message.cms2sdk.DeviceModelInfo;
|
|
10
|
+import com.hikvision.ae.hatc.message.device2dsg.gpsdata.GpsData;
|
|
11
|
+import com.hikvision.ae.hatc.sdk.callback.Callback;
|
8
|
12
|
import com.hikvision.ae.hatc.sdk.response.Result;
|
9
|
13
|
import com.hikvision.ae.hatc.sdk.service.SDKService;
|
10
|
14
|
import com.njyunzhi.nanyang.common.HatcUtil;
|
|
15
|
+import com.njyunzhi.nanyang.entity.TaMachineryGps;
|
11
|
16
|
import com.njyunzhi.nanyang.entity.TdHatcDevice;
|
|
17
|
+import com.njyunzhi.nanyang.mapper.TaMachineryGpsMapper;
|
12
|
18
|
import com.njyunzhi.nanyang.mapper.TdHatcDeviceMapper;
|
13
|
19
|
import com.njyunzhi.nanyang.service.ITdHatcDeviceService;
|
14
|
|
-import io.netty.util.internal.StringUtil;
|
15
|
20
|
import lombok.extern.slf4j.Slf4j;
|
16
|
21
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
22
|
import org.springframework.scheduling.annotation.Async;
|
18
|
23
|
import org.springframework.stereotype.Service;
|
19
|
24
|
|
|
25
|
+import java.time.LocalDateTime;
|
20
|
26
|
import java.util.ArrayList;
|
21
|
27
|
import java.util.List;
|
|
28
|
+import java.util.Map;
|
22
|
29
|
|
23
|
30
|
/**
|
24
|
31
|
* <p>
|
|
@@ -35,6 +42,9 @@ public class TdHatcDeviceServiceImpl extends ServiceImpl<TdHatcDeviceMapper, TdH
|
35
|
42
|
@Autowired
|
36
|
43
|
HatcUtil hatcUtil;
|
37
|
44
|
|
|
45
|
+ @Autowired
|
|
46
|
+ TaMachineryGpsMapper taMachineryGpsMapper;
|
|
47
|
+
|
38
|
48
|
@Async
|
39
|
49
|
@Override
|
40
|
50
|
public void syncDevice() throws Exception {
|
|
@@ -98,6 +108,68 @@ public class TdHatcDeviceServiceImpl extends ServiceImpl<TdHatcDeviceMapper, TdH
|
98
|
108
|
}
|
99
|
109
|
}
|
100
|
110
|
|
|
111
|
+ @Override
|
|
112
|
+ public void subscribeDevice() throws Exception {
|
|
113
|
+ SDKService sdkService = hatcUtil.getSdkService();
|
|
114
|
+ if (sdkService == null) {
|
|
115
|
+ throw new Exception("海康 SDK 未成功初始化");
|
|
116
|
+ }
|
|
117
|
+
|
|
118
|
+ // 先查询自己账号范围内的设备型号
|
|
119
|
+ Result<List<DeviceModelInfo>> getModelResult = sdkService.getDeviceModelList();
|
|
120
|
+
|
|
121
|
+ if(getModelResult.isSucceed() && getModelResult.getData() != null){
|
|
122
|
+ // 把需要订阅的设备型号的productKey装入列表
|
|
123
|
+ List<String> productKeyList = new ArrayList<>();
|
|
124
|
+ for(DeviceModelInfo model:getModelResult.getData()){
|
|
125
|
+ productKeyList.add(model.getProductKey());
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ // 订阅设备上下线消息
|
|
129
|
+ Result<Map<String, String>> subscribeOnlineStatusResult = sdkService.subscribeByProductKeys(productKeyList,
|
|
130
|
+ SubscribableMessageTypeEnum.DEVICE_STATUS,
|
|
131
|
+ new Callback<Integer>() {
|
|
132
|
+ @Override
|
|
133
|
+ public void callback(String terminalID, Integer status) {
|
|
134
|
+ // 设备上下线消息回调函数
|
|
135
|
+ log.info("设备 " + terminalID + " 在线状态:" + ((status == 1) ? "在线" : "离线"));
|
|
136
|
+
|
|
137
|
+ // 更新设备状态
|
|
138
|
+ baseMapper.updateOnlineStatus(terminalID, status);
|
|
139
|
+ }
|
|
140
|
+ });
|
|
141
|
+ System.out.println("根据ProductKey订阅设备上下线消息结果:" + JSON.toJSONString(subscribeOnlineStatusResult));
|
|
142
|
+
|
|
143
|
+ // 订阅位置信息
|
|
144
|
+ Result<Map<String, String>> subscribeGPSResult = sdkService.subscribeByProductKeys(productKeyList,
|
|
145
|
+ SubscribableMessageTypeEnum.GPS_DATA,
|
|
146
|
+ new Callback<GpsData>() {
|
|
147
|
+ @Override
|
|
148
|
+ public void callback(String terminalID, GpsData gpsData) {
|
|
149
|
+ // 设备GPS消息回调函数
|
|
150
|
+ String gpsJsonData = JSON.toJSONString(gpsData);
|
|
151
|
+ System.out.println("收到设备位置信息,终端ID:" + terminalID + ", 位置信息:" + gpsData);
|
|
152
|
+
|
|
153
|
+ // 查询设备
|
|
154
|
+ TdHatcDevice device = getDevice(terminalID, null);
|
|
155
|
+ if (null == device) return;
|
|
156
|
+
|
|
157
|
+ // 插入GPS信息
|
|
158
|
+ TaMachineryGps taMachineryGps = new TaMachineryGps();
|
|
159
|
+ taMachineryGps.setDeviceId(device.getDeviceId());
|
|
160
|
+ taMachineryGps.setLat(String.valueOf(gpsData.getLatitude()));
|
|
161
|
+ taMachineryGps.setLng(String.valueOf(gpsData.getLongitude()));
|
|
162
|
+ taMachineryGps.setGpsData(gpsJsonData);
|
|
163
|
+ taMachineryGps.setCreateDate(LocalDateTime.now());
|
|
164
|
+ taMachineryGpsMapper.insert(taMachineryGps);
|
|
165
|
+ }
|
|
166
|
+ });
|
|
167
|
+ System.out.println("根据ProductKey订阅GPS结果:" + JSON.toJSONString(subscribeGPSResult));
|
|
168
|
+ } else {
|
|
169
|
+ throw new Exception(String.format("[%s] %s", getModelResult.getErrorCode(), getModelResult.getErrorDesc()));
|
|
170
|
+ }
|
|
171
|
+ }
|
|
172
|
+
|
101
|
173
|
private String getChannelList(List<DeviceChannel> channelList) {
|
102
|
174
|
if (null == channelList || channelList.size() < 1) return null;
|
103
|
175
|
|
|
@@ -113,7 +185,7 @@ public class TdHatcDeviceServiceImpl extends ServiceImpl<TdHatcDeviceMapper, TdH
|
113
|
185
|
public TdHatcDevice getDevice(String terminalID, String productKey) {
|
114
|
186
|
QueryWrapper<TdHatcDevice> queryWrapper = new QueryWrapper<>();
|
115
|
187
|
queryWrapper.eq("terminal_id", terminalID);
|
116
|
|
- queryWrapper.eq("product_key", productKey);
|
|
188
|
+ queryWrapper.eq(null != productKey && !"".equals(productKey), "product_key", productKey);
|
117
|
189
|
|
118
|
190
|
return getOne(queryWrapper);
|
119
|
191
|
}
|