2021与蓝度共同重构项目,服务端
liuhaonan
2022-05-23 28f1086fd40ef730837690e1346cc968b880cccd
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
package com.sandu.ximon.admin.service;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.dto.LightTaskDto;
import com.sandu.ximon.admin.dto.SingleLightOrderDto;
import com.sandu.ximon.admin.manager.iot.frame.FrameBuilder;
import com.sandu.ximon.admin.manager.iot.frame.IRequestFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.A5LightTimerReqInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.response.A5LightTimerRespInnerFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.WrapResponseCommonFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5OrderEnum;
import com.sandu.ximon.admin.manager.iot.rrpc.mainboard.MainBoardInvokeSyncService;
import com.sandu.ximon.admin.manager.iot.rrpc.util.SupplementUtils;
import com.sandu.ximon.admin.param.LightTaskIssueParam;
import com.sandu.ximon.admin.param.LightTaskParam;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.admin.utils.TaskOrderUtil;
import com.sandu.ximon.dao.bo.LightTaskStatusAndPole;
import com.sandu.ximon.dao.domain.LightEnergyData;
import com.sandu.ximon.dao.domain.LightTask;
import com.sandu.ximon.dao.domain.LightTaskPoleRelation;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.enums.DeviceRespStatusEnums;
import com.sandu.ximon.dao.mapper.LightTaskMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author chenjiantian
 * @date 2021/12/15 16:33
 * 路灯任务操作
 */
@Service
@Slf4j
@AllArgsConstructor
public class LightTaskService extends BaseServiceImpl<LightTaskMapper, LightTask> {
 
    private final PoleService poleService;
    private final LightTaskPoleRelationService lightTaskPoleRelationService;
    private final LightTaskMapper lightTaskMapper;
    private final ClientService clientService;
    private final LightEnergyDataService lightEnergyDataService;
 
    /**
     * 新增路灯任务
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean addLightTask(LightTaskParam param) {
        if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) {
            throw new BusinessException("灯控命令格式不正确");
        }
        int week = 0;
        for (Integer w : param.getWeekList()) {
            week |= w;
        }
        LightTask lightTask = new LightTask();
        if (SecurityUtils.getClientId() != null) {
            lightTask.setClientId(clientService.getClientId());
            lightTask.setUserId(SecurityUtils.getUserId());
        }
        lightTask.setTaskName(param.getTaskName());
        lightTask.setWeek(week);
        lightTask.setLightAdress(param.getLightAddress());
        lightTask.setCloseOrder(param.getCloseOrder());
        lightTask.setOpenOrder(param.getOpenOrder());
        lightTask.setControlOrder(param.getControlOrder());
        lightTask.setCreateUser(SecurityUtils.getUsername());
        lightTask.setFramePayload(buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week));
        if (!save(lightTask)) {
            throw new BusinessException("保存路灯任务失败");
        }
 
 
        /**
         * 新增路灯任务日志记录开始
         */
        List<String> poleCodeList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(param.getPoleIdList())) {
            List<Pole> poleList = SpringContextHolder.getBean(PoleService.class).listByIds(param.getPoleIdList());
            if (CollectionUtil.isNotEmpty(poleList)) {
                poleCodeList = poleList.stream().map(Pole::getDeviceCode).collect(Collectors.toList());
            }
        }
        String content = "{任务ID:" + lightTask.getTaskId() + ", 任务名:" + lightTask.getTaskName() + "},{内帧指令" + lightTask.getFramePayload() + ", 灯杆ID:" + param.getPoleIdList().toString() + ", 控制的灯头地址:" + param.getLightAddress() + " }";
        StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "新增路灯任务", content);
        /**
         * 新增路灯任务日志记录结束
         */
 
        List<Long> poleIdList = param.getPoleIdList();
        if (CollectionUtil.isNotEmpty(poleIdList)) {
 
            List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, poleIdList, lightTask.getFramePayload(), param.getLightAddress());
 
            // 添加绑定灯杆
            if (!lightTaskPoleRelationService.saveBatch(lightTaskPoleRelationList)) {
                throw new BusinessException("绑定灯杆失败");
            }
 
            // 一个灯杆只能使用一个任务,新任务要覆盖旧任务
            lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getPoleId, poleIdList).ne(LightTaskPoleRelation::getTaskId, lightTask.getTaskId()));
 
            /**
             * 下发路灯任务日志记录开始
             */
 
            String content1 = "{任务ID:" + lightTask.getTaskId() + ", 任务名:" + lightTask.getTaskName() + "}," + " 灯杆ID:" + param.getPoleIdList().toString() + " }";
            StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "下发路灯任务", content1);
            /**
             * 下发路灯任务日志记录结束
             */
        }
        return true;
    }
 
    @Transactional(rollbackFor = Exception.class)
    public int updateLightTask(Long taskId, LightTaskParam param) {
        if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) {
            throw new BusinessException("灯控命令格式不正确");
        }
        LightTask lightTask = getById(taskId);
        if (lightTask == null) {
            throw new BusinessException("找不到路灯任务");
        }
 
 
        int week = 0;
        for (Integer w : param.getWeekList()) {
            week |= w;
        }
 
        LightTask newLightTask = new LightTask();
        newLightTask.setTaskName(param.getTaskName());
        if (SecurityUtils.getClientId() != null) {
            lightTask.setClientId(clientService.getClientId());
            lightTask.setUserId(SecurityUtils.getUserId());
        }
        newLightTask.setWeek(week);
        newLightTask.setCreateUser(SecurityUtils.getUsername());
        newLightTask.setControlOrder(param.getControlOrder());
        newLightTask.setOpenOrder(param.getOpenOrder());
        newLightTask.setCloseOrder(param.getCloseOrder());
        newLightTask.setLightAdress(param.getLightAddress());
        newLightTask.setUpdateTime(LocalDateTime.now());
        newLightTask.setFramePayload(buildControlFramePayload(param.getOpenOrder(), param.getCloseOrder(), param.getControlOrder(), week));
 
 
        //编辑后灯杆ID集合
        List<Long> poleIdList = param.getPoleIdList();
        //记录任务编辑器前灯杆ID集合
        List<LightTaskStatusAndPole> oldLightTaskStatusAndPoles = lightTaskPoleRelationService.listPoleAndStatusIdByTaskId(taskId);
        List<Long> oldList = oldLightTaskStatusAndPoles.stream().map(LightTaskStatusAndPole::getId).collect(Collectors.toList());
 
        //判断param.getPoleIdList()中是否有旧的灯杆ID    (直接下发)
        List<Long> newPoleIdList = param.getPoleIdList().stream().filter(poleId -> !oldList.contains(poleId)).collect(Collectors.toList());
        //判断param.getPoleIdList()中是否有新的灯杆ID    (覆盖操作)
        List<Long> oldPoleIdList = param.getPoleIdList().stream().filter(poleId -> oldList.contains(poleId)).collect(Collectors.toList());
        //oldList中有的灯杆ID,但是param.getPoleIdList()中没有 (关灯操作)
        List<Long> closeLight = oldList.stream().filter(poleId -> !param.getPoleIdList().contains(poleId)).collect(Collectors.toList());
 
 
        List<LightTaskPoleRelation> newPoleAll = new ArrayList<>();
        List<LightTaskPoleRelation> newPoleSuccess = new ArrayList<>();
        List<LightTaskPoleRelation> newPoleFail = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(newPoleIdList)) {
            //新灯杆下发新任务
            Map<String, List<LightTaskPoleRelation>> newPoleMap = sendControllerFrame(newPoleIdList, lightTask.getFramePayload(), param.getLightAddress());
 
            //newPoleAll集合后面用于存储关系表
            newPoleAll = newPoleMap.getOrDefault("all", new ArrayList<>());
            newPoleSuccess = newPoleMap.getOrDefault("success", new ArrayList<>());
            newPoleFail = newPoleMap.getOrDefault("fail", new ArrayList<>());
        }
 
        List<LightTaskPoleRelation> oldPoleFail = new ArrayList<>();
        List<LightTaskPoleRelation> oldPoleSuccess = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(oldPoleIdList)) {
            //覆盖操作灯杆
            Map<String, List<LightTaskPoleRelation>> oldPoleMap = sendControllerFrame(oldPoleIdList, lightTask.getFramePayload(), param.getLightAddress());
            oldPoleFail = oldPoleMap.getOrDefault("fail", new ArrayList<>());
            oldPoleSuccess = oldPoleMap.getOrDefault("success", new ArrayList<>());
        }
 
        List<LightTaskPoleRelation> closePoleFail = new ArrayList<>();
        List<LightTaskPoleRelation> closePoleSuccess = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(closeLight)) {
            //执行关灯内帧
            String framePayloadClose = "7f0000007f173b00";
            //覆盖操作灯杆
            Map<String, List<LightTaskPoleRelation>> closePoleMap = sendControllerFrame(closeLight, framePayloadClose, param.getLightAddress());
            closePoleFail = closePoleMap.getOrDefault("fail", new ArrayList<>());
            closePoleSuccess = closePoleMap.getOrDefault("success", new ArrayList<>());
        }
 
        //有失敗的需要保存旧的任務
        if (closePoleFail.size() == 0 && oldPoleFail.size() == 0) {
            newLightTask.setTaskId(lightTask.getTaskId());
            updateById(newLightTask);
        } else {
            save(newLightTask);
        }
 
        /**
         * 编辑路灯任务日志记录开始
         */
        List<String> poleCodeList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(param.getPoleIdList())) {
            List<Pole> poleList = SpringContextHolder.getBean(PoleService.class).listByIds(param.getPoleIdList());
            if (CollectionUtil.isNotEmpty(poleList)) {
                poleCodeList = poleList.stream().map(Pole::getDeviceCode).collect(Collectors.toList());
            }
        }
        String content = "{任务ID:" + newLightTask.getTaskId() + ", 任务名:" + newLightTask.getTaskName() + "},{内帧指令" + newLightTask.getFramePayload() + ", 灯杆ID:" + param.getPoleIdList().toString() + ", 控制的灯头地址:" + param.getLightAddress() + " }";
        StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "编辑路灯任务", content);
        /**
         * 编辑路灯任务日志记录结束
         */
 
        for (LightTaskPoleRelation bean : newPoleAll) {
            bean.setTaskId(newLightTask.getTaskId());
        }
        //成功用新的任务ID
        for (LightTaskPoleRelation bean : oldPoleSuccess) {
            bean.setTaskId(newLightTask.getTaskId());
        }
        //失败用旧的任务ID
        for (LightTaskPoleRelation bean : oldPoleFail) {
            bean.setTaskId(lightTask.getTaskId());
        }
 
        //失败用旧的任务ID
        for (LightTaskPoleRelation bean : closePoleFail) {
            bean.setTaskId(lightTask.getTaskId());
        }
 
        List<LightTaskPoleRelation> all = new ArrayList<>();
        all.addAll(newPoleAll);
        all.addAll(oldPoleSuccess);
        all.addAll(oldPoleFail);
        all.addAll(closePoleFail);
 
 
 
 
 
        List<Long> allPoleId = new ArrayList<>();
        allPoleId.addAll(poleIdList);
        allPoleId.addAll(oldList);
        lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getPoleId, allPoleId));
        lightTaskPoleRelationService.saveBatch(all);
 
 
        /**
         * 下发路灯任务日志记录开始
         */
        String content1 = "{任务ID:" + newLightTask.getTaskId() + ", 任务名:" + newLightTask.getTaskName() + "}," + " 灯杆ID:" + param.getPoleIdList().toString() + " }";
        StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "下发路灯任务", content1);
        /**
         * 下发路灯任务日志记录结束
         */
 
//        if (oldPoleFail.isEmpty() && closePoleFail.isEmpty()) {
//            //全部成功
//            return 0;
//        } else if (!closeLight.isEmpty() || !oldPoleFail.isEmpty()) {
//            //关灯失败  或者覆盖任务失败
//            return 1;
//        } else if (newPoleSuccess.isEmpty() && oldPoleSuccess.isEmpty() && closeSuccess.isEmpty()) {
//            //编辑全部失败
//            return 2;
//        } else {
//            //其他情况
//            return -1;
//        }
 
//        if (newPoleFail.isEmpty() && oldPoleFail.isEmpty() && closePoleFail.isEmpty()) {
//            //编辑成功
//            return 0;
//        } else if (newPoleSuccess.isEmpty() && oldPoleSuccess.isEmpty() && closePoleSuccess.isEmpty()) {
//            //编辑失败, 新旧任务都有
//            return 1;
//        } else if ((newPoleSuccess.isEmpty() && newPoleFail.isEmpty())) {
//            if (oldPoleSuccess.isEmpty() && closePoleSuccess.isEmpty()) {
//                //编辑失败,保留原先任务没有改变,只有旧任务
//                return 2;
//            } else {
//                //编辑部分成功部分失败,新旧任务同时存在
//                return 3;
//            }
//        } else {
//            return -1;
//        }
 
        return 0;
    }
 
 
    /**
     * 发送灯控请求
     *
     * @param framePayload 灯控参数
     * @param deviceCode   设备吗
     * @return 返回帧
     */
    public A5LightTimerRespInnerFrame sendTimeRRpc(String framePayload, String deviceCode, String lightAddress) {
        IRequestFrame requestFrame = FrameBuilder.builderA5().innerFrame(new A5LightTimerReqInnerFrame(framePayload, lightAddress)).orderType(A5OrderEnum.REQUEST_LIGHT_DATA.getCode()).build();
        WrapResponseCommonFrame<A5LightTimerRespInnerFrame> responseCommonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(deviceCode, requestFrame, A5LightTimerRespInnerFrame.class);
 
        StoreOperationRecordsUtils.storeInnerFrameData(deviceCode, "单灯帧-控灯", requestFrame, responseCommonFrame);
 
        return Optional.ofNullable(responseCommonFrame).map(WrapResponseCommonFrame::getResponseInnerFrame).orElse(null);
    }
 
    public List<LightTaskDto> listLightTask(BaseConditionVO conditionVO, String keyword) {
        LambdaQueryWrapper<LightTask> wrapper = Wrappers.lambdaQuery(LightTask.class);
        if (StrUtil.isNotBlank(keyword)) {
            wrapper.like(LightTask::getTaskName, keyword);
        }
        //不是超管需要筛选
        if (SecurityUtils.getClientId() != null) {
            wrapper.eq(LightTask::getClientId, SecurityUtils.getUserId()).or(w -> {
                w.eq(LightTask::getUserId, SecurityUtils.getUserId());
            });
        }
 
        if (conditionVO != null) {
            PageHelper.startPage(conditionVO.getPageNo(), conditionVO.getPageSize());
        }
        List<LightTask> list = list(wrapper);
 
        Page<LightTaskDto> page = new Page<>();
        BeanUtils.copyProperties(list, page);
        for (LightTask lightTask : list) {
            LightTaskDto lightTaskDto = new LightTaskDto();
            BeanUtils.copyProperties(lightTask, lightTaskDto);
            lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(lightTask.getWeek()));
            page.add(lightTaskDto);
        }
        return page;
    }
 
    /**
     * 执行中的路灯任务
     *
     * @return
     */
    public List<LightTaskDto> listTask() {
        Long clientId = SecurityUtils.getClientId();
        List<LightTask> lightTasks = lightTaskMapper.listLightTask(clientId);
        Page<LightTaskDto> page = new Page<>();
        BeanUtils.copyProperties(lightTasks, page);
        for (LightTask lightTask : lightTasks) {
            LightTaskDto lightTaskDto = new LightTaskDto();
            BeanUtils.copyProperties(lightTask, lightTaskDto);
            lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(lightTask.getWeek()));
            page.add(lightTaskDto);
        }
        return page;
    }
 
    /**
     * 用于任务新增,下发
     *
     * @param lightTask
     * @param poleIdList
     * @param framePayload
     * @param lightAddress
     * @return
     */
    private List<LightTaskPoleRelation> sendControllerFrame(LightTask lightTask, List<Long> poleIdList, String framePayload, String lightAddress) {
        List<LightTaskPoleRelation> lightTaskPoleRelationList = new ArrayList<>();
        //成功
        List<LightTaskPoleRelation> success = new ArrayList<>();
        //失败
        List<LightTaskPoleRelation> fail = new ArrayList<>();
 
        Map map = new HashMap();
 
        List<Pole> poles = poleService.listByIds(poleIdList);
        if (CollectionUtil.isEmpty(poles)) {
            return null;
        }
 
        for (Pole pole : poles) {
            LightTaskPoleRelation lightTaskPoleRelation = new LightTaskPoleRelation();
            lightTaskPoleRelation.setPoleId(pole.getId());
            lightTaskPoleRelation.setTaskId(lightTask.getTaskId());
            // rrpc 发生定时命令
            try {
                A5LightTimerRespInnerFrame a5LightTimerRespInnerFrame = sendTimeRRpc(framePayload, pole.getDeviceCode(), lightAddress);
                if (a5LightTimerRespInnerFrame == null) {
                    lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode());
                } else {
                    lightTaskPoleRelation.setIssueStatus(HexUtil.hexToInt(a5LightTimerRespInnerFrame.getResponseStatus()));
                }
            } catch (BusinessException e) {
                lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode());
            }
 
            lightTaskPoleRelationList.add(lightTaskPoleRelation);
        }
        return lightTaskPoleRelationList;
    }
 
    /**
     * 用于任务编辑
     *
     * @param poleIdList
     * @param framePayload
     * @param lightAddress
     * @return
     */
    private Map<String, List<LightTaskPoleRelation>> sendControllerFrame(List<Long> poleIdList, String framePayload, String lightAddress) {
        List<LightTaskPoleRelation> lightTaskPoleRelationList = new ArrayList<>();
        //成功
        List<LightTaskPoleRelation> success = new ArrayList<>();
        //失败
        List<LightTaskPoleRelation> fail = new ArrayList<>();
 
        Map<String, List<LightTaskPoleRelation>> map = new HashMap();
 
        List<Pole> poles = poleService.listByIds(poleIdList);
        if (CollectionUtil.isEmpty(poles)) {
            return null;
        }
 
        for (Pole pole : poles) {
            LightTaskPoleRelation lightTaskPoleRelation = new LightTaskPoleRelation();
            lightTaskPoleRelation.setPoleId(pole.getId());
//            lightTaskPoleRelation.setTaskId(lightTask.getTaskId());
            // rrpc 发生定时命令
            try {
                A5LightTimerRespInnerFrame a5LightTimerRespInnerFrame = sendTimeRRpc(framePayload, pole.getDeviceCode(), lightAddress);
                if (a5LightTimerRespInnerFrame == null) {
                    lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode());
                    fail.add(lightTaskPoleRelation);
                } else {
                    lightTaskPoleRelation.setIssueStatus(HexUtil.hexToInt(a5LightTimerRespInnerFrame.getResponseStatus()));
                    success.add(lightTaskPoleRelation);
                }
            } catch (BusinessException e) {
                lightTaskPoleRelation.setIssueStatus(DeviceRespStatusEnums.OTHER_ERROR.getCode());
                fail.add(lightTaskPoleRelation);
            }
 
            lightTaskPoleRelationList.add(lightTaskPoleRelation);
        }
        map.put("success", success);
        map.put("fail", fail);
        map.put("all", lightTaskPoleRelationList);
        return map;
    }
 
 
    @Transactional(rollbackFor = Exception.class)
    public boolean delLightTask(List<Long> taskIdList) {
        List<LightTask> lightTaskList = listByIds(taskIdList);
        if (CollectionUtil.isEmpty(lightTaskList)) {
            throw new BusinessException("找不到任务信息");
        }
 
        List<LightTaskPoleRelation> list = lightTaskPoleRelationService.list(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getTaskId, taskIdList));
        if (list.size() != 0) {
            throw new BusinessException("删除任务失败,删除的任务有灯杆正在使用");
        }
 
        // 删除任务
        if (!removeByIds(taskIdList)) {
            throw new BusinessException("删除任务失败");
        }
 
        boolean del;
        // 删除绑定灯杆
//        if (!lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).notIn(LightTaskPoleRelation::getTaskId, taskIdList))) {
//            throw new BusinessException("未找到任务/灯杆绑定关系");
//        }
//        lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getTaskId, taskIdList));
        taskIdList.forEach(taskId -> {
            lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getTaskId, taskId));
        });
 
        /**
         * 删除控灯任务日志记录开始
         */
        String content = "{控灯任务id:" + taskIdList + " }";
        StoreOperationRecordsUtils.storeOperationData(null, null, "删除控灯任务", content);
        /**
         * 删除控灯任务日志记录结束
         */
        return true;
    }
 
    /**
     * 任务详情
     */
    public Map<Object, Object> detailLightTask(Long taskId) {
        LightTask lightTask = getById(taskId);
        if (lightTask == null) {
            throw new BusinessException("找不到任务");
        }
        List<LightTaskStatusAndPole> lightTaskStatusAndPoles = lightTaskPoleRelationService.listPoleAndStatusIdByTaskId(taskId);
        return MapUtil.builder().put("task", lightTask).put("poles", lightTaskStatusAndPoles).build();
    }
 
    /**
     * 根据任务信息转成帧负荷
     *
     * @return 路灯定时器列表
     */
    private String buildControlFramePayload(String openOrder, String closeOrder, String controlOrder, Integer week) {
 
        // 添加开灯定时器
        SingleLightOrderDto openLightOrder = TaskOrderUtil.parseLightOrder(openOrder);
        SingleLightOrderDto closeLightOrder = TaskOrderUtil.parseLightOrder(closeOrder);
        String[] controlOrderArray = StrUtil.split(controlOrder, LightTaskParam.REQUEST_ORDER_LENGTH);
        Integer[] weekArrays = TaskOrderUtil.parseLightCronWeek2List(week).toArray(new Integer[0]);
        if (openLightOrder == null || closeLightOrder == null) {
            throw new BusinessException("解析单灯命令失败");
        }
        if (ArrayUtil.isEmpty(weekArrays)) {
            throw new BusinessException("请设置星期");
        }
 
        StringBuilder sb = new StringBuilder();
        String weekHex = SupplementUtils.suppleZero(HexUtil.toHex(week), 2);
 
        // 拼接开灯命令
        sb.append(weekHex);
        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(openLightOrder.getHour()), 2));
        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(openLightOrder.getMinute()), 2));
        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(openLightOrder.getBrightness()), 2));
 
        // 拼接关灯命令
        sb.append(weekHex);
        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(closeLightOrder.getHour()), 2));
        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(closeLightOrder.getMinute()), 2));
        sb.append(SupplementUtils.suppleZero(HexUtil.toHex(closeLightOrder.getBrightness()), 2));
 
        // 拼接亮度控灯命令
        for (String controlOrderStr : controlOrderArray) {
            SingleLightOrderDto controlLightOrder = TaskOrderUtil.parseLightOrder(controlOrderStr);
            if (controlLightOrder != null) {
                sb.append(weekHex);
                sb.append(SupplementUtils.suppleZero(HexUtil.toHex(controlLightOrder.getHour()), 2));
                sb.append(SupplementUtils.suppleZero(HexUtil.toHex(controlLightOrder.getMinute()), 2));
                sb.append(SupplementUtils.suppleZero(HexUtil.toHex(controlLightOrder.getBrightness()), 2));
            }
        }
 
        return sb.toString();
    }
 
    /**
     * 下发单个灯杆的任务
     */
    public boolean issueLightTask(LightTaskIssueParam param) {
        LightTask lightTask = getById(param.getTaskId());
        if (lightTask == null) {
            throw new BusinessException("找不到任务");
        }
        String framePayload = buildControlFramePayload(lightTask.getOpenOrder(), lightTask.getCloseOrder(), lightTask.getControlOrder(), lightTask.getWeek());
        List<LightTaskPoleRelation> lightTaskPoleRelationList = sendControllerFrame(lightTask, ListUtil.toList(param.getPoleId()), framePayload, lightTask.getLightAdress());
 
        /**
         * 下发路灯任务日志记录开始
         */
        List<String> poleCodeList = new ArrayList<>();
        List<Pole> list = SpringContextHolder.getBean(PoleService.class).list(Wrappers.lambdaQuery(Pole.class).eq(Pole::getId, param.getPoleId()));
        if (CollectionUtil.isNotEmpty(list)) {
            poleCodeList = list.stream().map(Pole::getDeviceCode).collect(Collectors.toList());
        }
        String content = "{任务ID:" + lightTask.getTaskId() + ", 任务名:" + lightTask.getTaskName() + "}," + " 灯杆ID:" + param.getPoleId() + " }";
        StoreOperationRecordsUtils.storeOperationData(poleCodeList, null, "下发路灯任务", content);
        /**
         * 下发路灯任务日志记录结束
         */
 
 
        if (CollectionUtil.isNotEmpty(lightTaskPoleRelationList)) {
            return lightTaskPoleRelationService.update(lightTaskPoleRelationList.get(0), Wrappers.lambdaUpdate(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getPoleId, param.getPoleId()).eq(LightTaskPoleRelation::getTaskId, param.getTaskId()));
        }
 
 
        return true;
    }
 
 
    /**
     * 计算功率及能耗
     */
    public void energy() {
        List<LightTask> lightTasks = list(Wrappers.lambdaQuery(LightTask.class));
        for (LightTask lightTask : lightTasks) {
            List<LightTaskPoleRelation> list = SpringContextHolder.getBean(LightTaskPoleRelationService.class).list(Wrappers.lambdaQuery(LightTaskPoleRelation.class).eq(LightTaskPoleRelation::getTaskId, lightTask.getTaskId()));
 
            LightTaskDto lightTaskDto = new LightTaskDto();
            BeanUtils.copyProperties(lightTask, lightTaskDto);
            lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(lightTask.getWeek()));
 
            BigDecimal energySaving = SpringContextHolder.getBean(LightService.class).jisuan(lightTaskDto);
            BigDecimal energy = SpringContextHolder.getBean(LightService.class).jisuanEnergy(lightTaskDto);
 
            //获取昨天的星期数
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime yesterday = now.minusDays(1);
 
 
            LightEnergyData lightEnergyData = new LightEnergyData();
            lightEnergyData.setTaskId(lightTask.getTaskId());
            lightEnergyData.setYtdTime(yesterday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            lightEnergyData.setEnergySaving(energySaving);
            lightEnergyData.setEnergy(energy);
            lightEnergyData.setUserId(lightTask.getUserId());
            lightEnergyData.setClientId(lightTask.getClientId());
 
            //保存能耗数据到数据库
            lightEnergyDataService.save(lightEnergyData);
 
            System.out.println(lightEnergyData + "---------------");
            //ID   任务id
        }
    }
 
}