zhaowenyu
2025-12-08 b20c288d812ff40acd5d3d9cc961aad6332f3f31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package com.ard.sample.manage.service.impl;
 
import com.ard.sample.manage.model.enums.DeviceDomainEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ard.sample.common.model.Pagination;
import com.ard.sample.common.model.PaginationData;
import com.ard.sample.component.mqtt.model.ChannelName;
import com.ard.sample.component.mqtt.model.CommonTopicReceiver;
import com.ard.sample.component.mqtt.model.MapKeyConst;
import com.ard.sample.component.mqtt.model.TopicConst;
import com.ard.common.core.redis.RedisConst;
import com.ard.common.core.redis.RedisOpsUtils;
import com.ard.sample.component.websocket.service.impl.SendMessageServiceImpl;
import com.ard.sample.component.websocket.service.impl.WebSocketManageServiceImpl;
import com.ard.sample.manage.mapper.IDeviceHmsMapper;
import com.ard.sample.manage.model.common.HmsJsonUtil;
import com.ard.sample.manage.model.common.HmsMessage;
import com.ard.sample.manage.model.dto.DeviceDTO;
import com.ard.sample.manage.model.dto.DeviceHmsDTO;
import com.ard.sample.manage.model.entity.DeviceHmsEntity;
import com.ard.sample.manage.model.enums.HmsEnum;
import com.ard.sample.manage.model.param.DeviceHmsQueryParam;
import com.ard.sample.manage.model.receiver.DeviceHmsReceiver;
import com.ard.sample.manage.model.receiver.HmsArgsReceiver;
import com.ard.sample.manage.service.IDeviceHmsService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
/**
 * @author sean
 * @version 1.1
 * @date 2022/7/6
 */
@Slf4j
@Service
@Transactional
public class DeviceHmsServiceImpl implements IDeviceHmsService {
 
    @Autowired
    private IDeviceHmsMapper mapper;
 
    @Autowired
    private ObjectMapper objectMapper;
 
    @Autowired
    private SendMessageServiceImpl sendMessageService;
 
    @Autowired
    private WebSocketManageServiceImpl webSocketManageService;
 
    private static final Pattern PATTERN_KEY = Pattern.compile(
            HmsEnum.FormatKeyEnum.KEY_START +
                    "(" +
                    Arrays.stream(HmsEnum.FormatKeyEnum.values())
                            .map(HmsEnum.FormatKeyEnum::getKey)
                            .collect(Collectors.joining("|")) +
                    ")");
 
    @Override
    @ServiceActivator(inputChannel = ChannelName.INBOUND_EVENTS_HMS)
    public void handleHms(CommonTopicReceiver receiver, MessageHeaders headers) {
        String topic = headers.get(MqttHeaders.RECEIVED_TOPIC).toString();
        String sn  = topic.substring((TopicConst.THING_MODEL_PRE + TopicConst.PRODUCT).length(),
                topic.indexOf(TopicConst.EVENTS_SUF));
 
        DeviceHmsEntity entity = DeviceHmsEntity.builder()
                .bid(receiver.getBid())
                .tid(receiver.getTid())
                .createTime(receiver.getTimestamp())
                .updateTime(0L)
                .sn(sn)
                .build();
        String key = RedisConst.HMS_PREFIX + sn;
        // Query all unread hms messages of the device in redis.
        Set<String> hmsMap = RedisOpsUtils.listGetAll(key).stream().map(String::valueOf).collect(Collectors.toSet());
 
        DeviceDTO device = (DeviceDTO) RedisOpsUtils.get(RedisConst.DEVICE_ONLINE_PREFIX + sn);
 
        List<DeviceHmsDTO> unReadList = new ArrayList<>();
        objectMapper.convertValue(((Map) (receiver.getData())).get(MapKeyConst.LIST),
                new TypeReference<List<DeviceHmsReceiver>>() {})
                .forEach(hmsReceiver -> {
                    final DeviceHmsEntity hms = entity.clone();
                    this.fillEntity(hms, hmsReceiver, device);
                    // The same unread hms are no longer incremented.
                    if (hmsMap.contains(hms.getHmsKey())) {
                        return;
                    }
                    this.fillMessage(hms, hmsReceiver.getArgs());
                    unReadList.add(entity2Dto(hms));
                    mapper.insert(hms);
                });
 
        if (unReadList.isEmpty()) {
            return;
        }
        RedisOpsUtils.listRPush(key, (Object) unReadList.stream().map(DeviceHmsDTO::getKey).toArray(String[]::new));
        // push to the web
//        Collection<ConcurrentWebSocketSession> sessions = webSocketManageService.getValueWithWorkspaceAndUserType(
//                device.getWorkspaceId(), UserTypeEnum.WEB.getVal());
//        sendMessageService.sendBatch(sessions, CustomWebSocketMessage.builder()
//                .bizCode(BizCodeEnum.DEVICE_HMS.getCode())
//                .data(TelemetryDTO.<List<DeviceHmsDTO>>builder().sn(sn).host(unReadList).build())
//                .timestamp(System.currentTimeMillis())
//                .build());
    }
 
    @Override
    public PaginationData<DeviceHmsDTO> getDeviceHmsByParam(DeviceHmsQueryParam param) {
// 1. 核心兜底:参数为null时初始化空对象(结合实体默认值,避免所有NPE)
        if (param == null) {
            param = new DeviceHmsQueryParam();
        }
 
        LambdaQueryWrapper<DeviceHmsEntity> queryWrapper = new LambdaQueryWrapper<DeviceHmsEntity>();
 
        // 2. 设备SN条件:仅当集合非空时添加IN条件(实体已兜底空集合,无需判null)
        if (!param.getDeviceSn().isEmpty()) {
            queryWrapper.in(DeviceHmsEntity::getSn, param.getDeviceSn());
        }
 
        // 3. 创建时间区间:① 两个时间都非null ② 开始时间≤结束时间(避免无效条件)
        if (param.getBeginTime() != null && param.getEndTime() != null) {
            //if (param.getBeginTime() <= param.getEndTime()) {
                queryWrapper.between(DeviceHmsEntity::getCreateTime, param.getBeginTime(), param.getEndTime());
//            } else {
//                // 可选:抛异常/日志提醒/交换时间顺序
//                // log.warn("创建时间区间不合法:beginTime({}) > endTime({})", param.getBeginTime(), param.getEndTime());
//            }
        }
 
        // 4. 更新时间:参数非null时添加
        queryWrapper.eq(param.getUpdateTime() != null, DeviceHmsEntity::getUpdateTime, param.getUpdateTime());
 
        // 5. 级别:参数非null时添加
        queryWrapper.eq(param.getLevel() != null, DeviceHmsEntity::getLevel, param.getLevel());
 
        // 6. 消息模糊查询:按语言匹配(语言为null/空时自动忽略)
        String language = param.getLanguage();
        String message = param.getMessage();
        if (StringUtils.hasText(message)) {
            if (HmsEnum.MessageLanguage.ZH.getLanguage().equals(language)) {
                queryWrapper.like(DeviceHmsEntity::getMessageZh, message);
            } else if (HmsEnum.MessageLanguage.EN.getLanguage().equals(language)) {
                queryWrapper.like(DeviceHmsEntity::getMessageEn, message);
            }
            // 语言不匹配/为null时,不添加任何消息条件
        }
 
        // 7. 固定排序:创建时间降序
        queryWrapper.orderByDesc(DeviceHmsEntity::getCreateTime);
 
        // 8. 分页参数校验(避免非法值:page<1 / pageSize≤0)
        Long page = param.getPage() == null ? 1L : Math.max(param.getPage(), 1L); // 页码最小为1
        Long pageSize = param.getPage() == null ? 1L : param.getPageSize() <= 0 ? 10L : param.getPageSize(); // 页大小默认10
 
        // 9. 「传空返回所有」的分页处理:pageSize为null/0时,查询全部数据
        if (param.getPageSize() == null || param.getPageSize() <= 0) {
            pageSize = (long) mapper.selectCount(queryWrapper); // 总条数作为页大小
        }
 
        // 10. 执行分页查询
        Page<DeviceHmsEntity> pagination = mapper.selectPage(new Page<>(page, pageSize), queryWrapper);
 
        // 11. 实体转DTO
        List<DeviceHmsDTO> deviceHmsList = pagination.getRecords().stream()
                .map(this::entity2Dto)
                .collect(Collectors.toList());
 
        return new PaginationData<DeviceHmsDTO>(deviceHmsList, new Pagination(pagination));
 
 
    }
 
    // region 原代码注释备份一份
//        LambdaQueryWrapper<DeviceHmsEntity> queryWrapper = new LambdaQueryWrapper<DeviceHmsEntity>()
//                .and(wrapper -> param.getDeviceSn().forEach(sn -> wrapper.eq(DeviceHmsEntity::getSn, sn).or()))
//                .between(param.getBeginTime() != null && param.getEndTime() != null,
//                        DeviceHmsEntity::getCreateTime, param.getBeginTime(), param.getEndTime())
//                .eq(param.getUpdateTime() != null, DeviceHmsEntity::getUpdateTime, param.getUpdateTime())
//                .eq(param.getLevel() != null, DeviceHmsEntity::getLevel, param.getLevel())
//                .like(StringUtils.hasText(param.getMessage()) &&
//                                HmsEnum.MessageLanguage.ZH.getLanguage().equals(param.getLanguage()),
//                        DeviceHmsEntity::getMessageZh, param.getMessage())
//                .like(StringUtils.hasText(param.getMessage()) &&
//                                HmsEnum.MessageLanguage.EN.getLanguage().equals(param.getLanguage()),
//                        DeviceHmsEntity::getMessageEn, param.getMessage())
//                .orderByDesc(DeviceHmsEntity::getCreateTime);
//        if (param.getPage() == null || param.getPageSize() == null) {
//            param.setPage(1L);
//            param.setPageSize(Long.valueOf(mapper.selectCount(queryWrapper)));
//        }
//
//        Page<DeviceHmsEntity> pagination = mapper.selectPage(new Page<>(param.getPage(), param.getPageSize()), queryWrapper);
//
//        List<DeviceHmsDTO> deviceHmsList = pagination.getRecords().stream().map(this::entity2Dto).collect(Collectors.toList());
//
//        return new PaginationData<DeviceHmsDTO>(deviceHmsList, new Pagination(pagination));
    // endregion
 
    @Override
    public void updateUnreadHms(String deviceSn) {
        mapper.update(DeviceHmsEntity.builder().updateTime(System.currentTimeMillis()).build(),
                new LambdaUpdateWrapper<DeviceHmsEntity>()
                        .eq(DeviceHmsEntity::getSn, deviceSn)
                        .eq(DeviceHmsEntity::getUpdateTime, 0L));
        // 删除Redis中缓存的未读消息。
        RedisOpsUtils.del(RedisConst.HMS_PREFIX + deviceSn);
    }
 
    private DeviceHmsDTO entity2Dto(DeviceHmsEntity entity) {
        if (entity == null) {
            return null;
        }
        return DeviceHmsDTO.builder()
                .bid(entity.getBid())
                .tid(entity.getTid())
                .createTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(entity.getCreateTime()), ZoneId.systemDefault()))
                .updateTime(entity.getUpdateTime().intValue() == 0 ?
                        null : LocalDateTime.ofInstant(Instant.ofEpochMilli(entity.getUpdateTime()), ZoneId.systemDefault()))
                .sn(entity.getSn())
                .hmsId(entity.getHmsId())
                .key(entity.getHmsKey())
                .level(entity.getLevel())
                .module(entity.getModule())
                .messageEn(entity.getMessageEn())
                .messageZh(entity.getMessageZh())
                .inTheSky(entity.getInTheSky())
                .deviceType(entity.getDeviceType())
                .build();
    }
 
    /**
     * 将收到的数据填充到实体中。请参阅文档中的拼接规则。
     * @param dto
     * @param receiver
     */
    private void fillEntity(DeviceHmsEntity dto, DeviceHmsReceiver receiver, DeviceDTO deviceDTO) {
        dto.setLevel(receiver.getLevel());
        dto.setModule(receiver.getModule());
        dto.setHmsId(UUID.randomUUID().toString());
        dto.setInTheSky(receiver.getInTheSky());
        dto.setDeviceType(receiver.getDeviceType());
 
        if(deviceDTO.getDomain() == DeviceDomainEnum.DOCK.getVal()) {
            dto.setHmsKey(HmsEnum.HmsFaqIdEnum.DOCK_TIP.getText() + receiver.getCode());
            log.info("SN:{}的Hms匹配拼接规则值为:{}",deviceDTO.getDeviceSn(), HmsEnum.HmsFaqIdEnum.DOCK_TIP.getText() + receiver.getCode());
            return;
        }
//        if (HmsEnum.DomainType.DRONE_NEST.getDomain().equals(receiver.getDomainType())) {
//            dto.setHmsKey(HmsEnum.HmsFaqIdEnum.DOCK_TIP.getText() + receiver.getCode());
//            return;
//        }
        StringBuilder key = new StringBuilder(HmsEnum.HmsFaqIdEnum.FPV_TIP.getText()).append(receiver.getCode());
 
        if (receiver != null && HmsEnum.IN_THE_SKY.getVal() == (receiver.getInTheSky() != null ? receiver.getInTheSky() : -1)) {
            key.append(HmsEnum.IN_THE_SKY.getText());
        }
        dto.setHmsKey(key.toString());
        log.info("SN:{}的Hms匹配拼接规则值为:{}",deviceDTO.getDeviceSn(), key.toString());
    }
 
    /**
     * Replace wildcards in messages according to the relevant rules.
     * Please refer to the documentation for splicing rules.
     * @param dto
     * @param args
     */
    private void fillMessage(DeviceHmsEntity dto, HmsArgsReceiver args) {
        HmsMessage hmsMessage = HmsJsonUtil.get(dto.getHmsKey());
        String zh = StringUtils.hasText(hmsMessage.getZh()) ? hmsMessage.getZh() : String.format("未知错误(%s)", dto.getHmsKey());
        String en = StringUtils.hasText(hmsMessage.getEn()) ? hmsMessage.getEn() : String.format("Unknown(%s)", dto.getHmsKey());//
 
        dto.setMessageZh(format(Locale.CHINESE.getLanguage(), zh, args));
        dto.setMessageEn(format(Locale.ENGLISH.getLanguage(), en, args));
    }
 
    /**
     * Set the matching parameters for key.
     * @param l     language: zh or en
     * @param hmsArgs
     * @return
     */
    private List<String> fillKeyArgs(String l, HmsArgsReceiver hmsArgs) {
        List<String> args = new ArrayList<>();
        args.add(Objects.nonNull(hmsArgs.getAlarmId()) ? Long.toHexString(hmsArgs.getAlarmId()) : null);
        args.add(Objects.nonNull(hmsArgs.getComponentIndex()) ? String.valueOf(hmsArgs.getComponentIndex() + 1) : null);
        if (Objects.nonNull(hmsArgs.getSensorIndex())) {
            args.add(String.valueOf(hmsArgs.getSensorIndex() + 1));
 
            HmsEnum.HmsBatteryIndexEnum hmsBatteryIndexEnum = HmsEnum.HmsBatteryIndexEnum.find(hmsArgs.getSensorIndex());
            HmsEnum.HmsDockCoverIndexEnum hmsDockCoverIndexEnum = HmsEnum.HmsDockCoverIndexEnum.find(hmsArgs.getSensorIndex());
            HmsEnum.HmsChargingRodIndexEnum hmsChargingRodIndexEnum = HmsEnum.HmsChargingRodIndexEnum.find(hmsArgs.getSensorIndex());
 
            switch (l) {
                case "zh":
                    args.add(hmsBatteryIndexEnum.getZh());
                    args.add(hmsDockCoverIndexEnum.getZh());
                    args.add(hmsChargingRodIndexEnum.getZh());
                    break;
                case "en":
                    args.add(hmsBatteryIndexEnum.getEn());
                    args.add(hmsDockCoverIndexEnum.getEn());
                    args.add(hmsChargingRodIndexEnum.getEn());
                    break;
                default:
                    break;
            }
 
        }
        return args;
    }
 
    /**
     * Returns a formatted string using the specified locale, format string, and arguments.
     * @param l language: zh or en
     * @param format
     * @param hmsArgs
     * @return
     */
    private String format(String l, String format, HmsArgsReceiver hmsArgs) {
        List<String> args = fillKeyArgs(l, hmsArgs);
        List<String> list = parse(format);
        StringBuilder sb = new StringBuilder();
        for (String word : list) {
            if (!StringUtils.hasText(word)) {
                continue;
            }
            HmsEnum.FormatKeyEnum keyEnum = HmsEnum.FormatKeyEnum.find(word.substring(1));
            sb.append(HmsEnum.FormatKeyEnum.KEY_START != word.charAt(0) || HmsEnum.FormatKeyEnum.UNKNOWN == keyEnum ?
                    word : args.get(keyEnum.getIndex()));
        }
        return sb.toString();
    }
 
    /**
     * Finds format specifiers in the format string.
     * @param s
     * @return
     */
    private List<String> parse(String s) {
        List<String> list = new ArrayList<>();
        Matcher matcher = PATTERN_KEY.matcher(s);
        for (int i = 0; i < s.length(); ) {
            if (matcher.find(i)) {
                if (matcher.start() != i) {
                    list.add(s.substring(i, matcher.start()));
                }
                list.add(matcher.group());
                i = matcher.end();
            } else {
                list.add(s.substring(i));
                break;
            }
        }
        return list;
    }
}