‘liusuyi’
2023-08-09 b00dd7882fa19c346d086363c67c76b01051e594
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
package com.ruoyi.inspect.service.impl;
 
import java.util.*;
import com.ruoyi.alarmpoints.well.domain.ArdAlarmpointsWell;
import com.ruoyi.alarmpoints.well.mapper.ArdAlarmpointsWellMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.device.camera.domain.ArdCameras;
import com.ruoyi.device.camera.domain.CameraCmd;
import com.ruoyi.device.camera.mapper.ArdCamerasMapper;
import com.ruoyi.device.hiksdk.service.IHikClientService;
import com.ruoyi.inspect.domain.ArdVideoInspectRecord;
import com.ruoyi.inspect.mapper.ArdVideoInspectRecordMapper;
import com.ruoyi.inspect.mapper.ArdVideoInspectTaskStepMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.inspect.domain.ArdVideoInspectTaskStep;
import com.ruoyi.inspect.mapper.ArdVideoInspectTaskMapper;
import com.ruoyi.inspect.domain.ArdVideoInspectTask;
import com.ruoyi.inspect.service.IArdVideoInspectTaskService;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
 
/**
 * 视频巡检任务Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-30
 */
@Service
@Slf4j(topic = "PatrolInspectionTask")
public class ArdVideoInspectTaskServiceImpl implements IArdVideoInspectTaskService {
    @Resource
    private ArdVideoInspectTaskMapper ardVideoInspectTaskMapper;
    @Resource
    private ArdVideoInspectTaskStepMapper ardVideoInspectTaskStepMapper;
    @Resource
    private ArdAlarmpointsWellMapper ardAlarmpointsWellMapper;
    @Resource
    private ArdVideoInspectRecordMapper ardVideoInspectRecordMapper;
    @Resource
    private ArdCamerasMapper ardCamerasMapper;
    @Resource
    private IHikClientService hikClientService;
    @Resource
    private ArdCamerasMapper camerasMapper;
 
    @PostConstruct
    public void initTask() {
        List<ArdVideoInspectTask> ardVideoInspectTasks = ardVideoInspectTaskMapper.selectArdVideoInspectTaskList(new ArdVideoInspectTask());
        for (ArdVideoInspectTask ardVideoInspectTask : ardVideoInspectTasks) {
            /*启动后将所有手动任务置为关闭,所有任务当前步骤信息清空*/
            if (ardVideoInspectTask.getInspectMode().equals("手动")) {
                ardVideoInspectTask.setMenualSwitch("关");
            }
            ardVideoInspectTask.setCurrentStepId("");
            ardVideoInspectTask.setCurrentStepStartTime("");
            ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(ardVideoInspectTask);
        }
    }
 
    /**
     * 查询视频巡检任务
     *
     * @param id 视频巡检任务主键
     * @return 视频巡检任务
     */
    @Override
    public ArdVideoInspectTask selectArdVideoInspectTaskById(String id) {
        return ardVideoInspectTaskMapper.selectArdVideoInspectTaskById(id);
    }
 
    /**
     * 查询视频巡检任务列表
     *
     * @param ardVideoInspectTask 视频巡检任务
     * @return 视频巡检任务
     */
    @Override
    public List<ArdVideoInspectTask> selectArdVideoInspectTaskList(ArdVideoInspectTask ardVideoInspectTask) {
        return ardVideoInspectTaskMapper.selectArdVideoInspectTaskList(ardVideoInspectTask);
    }
 
    /**
     * 新增视频巡检任务
     *
     * @param ardVideoInspectTask 视频巡检任务
     * @return 结果
     */
    @Transactional
    @Override
    public int insertArdVideoInspectTask(ArdVideoInspectTask ardVideoInspectTask) {
        ardVideoInspectTask.setId(IdUtils.simpleUUID());
        ardVideoInspectTask.setCreateBy(SecurityUtils.getUsername());
        ardVideoInspectTask.setCreateTime(DateUtils.getNowDate());
        ardVideoInspectTask.setUserId(SecurityUtils.getUserId());
        int rows = ardVideoInspectTaskMapper.insertArdVideoInspectTask(ardVideoInspectTask);
        insertArdVideoInspectTaskStep(ardVideoInspectTask);
        return rows;
    }
 
    /**
     * 修改视频巡检任务
     *
     * @param ardVideoInspectTask 视频巡检任务
     * @return 结果
     */
    @Transactional
    @Override
    public int updateArdVideoInspectTask(ArdVideoInspectTask ardVideoInspectTask) {
        ardVideoInspectTask.setUpdateBy(SecurityUtils.getUsername());
        ardVideoInspectTask.setUpdateTime(DateUtils.getNowDate());
        //  ardVideoInspectTaskMapper.deleteArdVideoInspectTaskStepByTaskId(ardVideoInspectTask.getId());
        insertArdVideoInspectTaskStep(ardVideoInspectTask);
        return ardVideoInspectTaskMapper.updateArdVideoInspectTask(ardVideoInspectTask);
    }
 
    /**
     * 修改视频巡检任务,bu更新步骤
     *
     * @param ardVideoInspectTask 视频巡检任务
     * @return 结果
     */
    @Transactional
    @Override
    public int updateArdVideoInspectTaskSingle(ArdVideoInspectTask ardVideoInspectTask) {
        ardVideoInspectTask.setUpdateBy(SecurityUtils.getUsername());
        ardVideoInspectTask.setUpdateTime(DateUtils.getNowDate());
        return ardVideoInspectTaskMapper.updateArdVideoInspectTask(ardVideoInspectTask);
    }
 
    @Override
    public int updateArdVideoInspectTaskNoUpdater(ArdVideoInspectTask ardVideoInspectTask) {
        return ardVideoInspectTaskMapper.updateArdVideoInspectTask(ardVideoInspectTask);
    }
 
    /**
     * 批量删除视频巡检任务
     *
     * @param ids 需要删除的视频巡检任务主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteArdVideoInspectTaskByIds(String[] ids) {
        ardVideoInspectTaskMapper.deleteArdVideoInspectTaskStepByTaskIds(ids);
        return ardVideoInspectTaskMapper.deleteArdVideoInspectTaskByIds(ids);
    }
 
    /**
     * 删除视频巡检任务信息
     *
     * @param id 视频巡检任务主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteArdVideoInspectTaskById(String id) {
        ardVideoInspectTaskMapper.deleteArdVideoInspectTaskStepByTaskId(id);
        return ardVideoInspectTaskMapper.deleteArdVideoInspectTaskById(id);
    }
 
    /**
     * 新增视频巡检步骤信息
     *
     * @param ardVideoInspectTask 视频巡检任务对象
     */
    public void insertArdVideoInspectTaskStep(ArdVideoInspectTask ardVideoInspectTask) {
        List<ArdVideoInspectTaskStep> ardVideoInspectTaskStepList = ardVideoInspectTask.getArdVideoInspectTaskStepList();
        //获取相机
        ArdCameras camera = camerasMapper.selectArdCamerasById(ardVideoInspectTask.getCameraId());
        Collections.sort(ardVideoInspectTaskStepList, new ArdVideoInspectTaskStepComparator(this.ardAlarmpointsWellMapper, camera.getLongitude(), camera.getLatitude()));
        String taskId = ardVideoInspectTask.getId();
        for (int i = 0; i < ardVideoInspectTaskStepList.size(); i++) {
            ArdVideoInspectTaskStep step = ardVideoInspectTaskStepList.get(i);
            step.setTaskId(taskId);
            step.setOrderNumber(new Integer(i + 1));
        }
        if (StringUtils.isNotNull(ardVideoInspectTaskStepList)) {
            for (ArdVideoInspectTaskStep ardVideoInspectTaskStep : ardVideoInspectTaskStepList) {
                if (ardVideoInspectTaskStep.getId() == null) {
                    //新的步骤,添加
                    ardVideoInspectTaskStepMapper.insertArdVideoInspectTaskStep(ardVideoInspectTaskStep);
                } else {
                    //已有步骤,更新
                    ardVideoInspectTaskStepMapper.updateArdVideoInspectTaskStep(ardVideoInspectTaskStep);
                }
            }
        }
    }
 
    /**
     * 手动巡检任务
     */
    @Override
    public void manualTaskRun(String TaskId) {
        try {
            ArdVideoInspectTask videoInspectTask = ardVideoInspectTaskMapper.selectArdVideoInspectTaskById(TaskId);
            if (!videoInspectTask.getMenualSwitch().equals("开")) {
                return;
            }
            if (videoInspectTask.getArdVideoInspectTaskStepList().size() == 0) {
                return;
            }
            String currentStepId = videoInspectTask.getCurrentStepId();
            String currentStepStartTime = videoInspectTask.getCurrentStepStartTime();
            if (StringUtils.isNull(currentStepId) || StringUtils.isEmpty(currentStepId)) {
                //从1号步骤开始
                videoInspectTask.setCurrentStepId(videoInspectTask.getArdVideoInspectTaskStepList().get(0).getId());
                if (InspectionTaskManager.taskMap.containsKey(TaskId)) {
                    startRunStep(videoInspectTask);
                }
            } else /*当前任务已经执行,判断是否到期*/ {
                if (StringUtils.isNull(currentStepStartTime) || StringUtils.isEmpty(currentStepStartTime)) {
                    //当前任务启动时间为null,则说明当前步骤被中断,直接启动当前步骤
                    startRunStep(videoInspectTask);
                } else {
                    boolean expird = isExpirdStep(videoInspectTask); /*判断当前步骤时间是否过期*/
                    if (expird) {
                        //停止录像
                        stopRunStep(videoInspectTask);
                        //切换步骤
                        String nextStepId = changeNextStep(videoInspectTask);
                        //开始下一步骤
                        videoInspectTask.setCurrentStepId(nextStepId);
                        startRunStep(videoInspectTask);
                    } else {
                        //未过期尝试引导,引导失败清空当前步骤开始时间,标记中断
                        tryGuide(videoInspectTask);
                    }
                }
            }
        } catch (Exception ex) {
            log.error("手动巡检异常:" + ex.getMessage());
        }
    }
 
    /**
     * 判断当前手动任务能否开启
     */
    @Override
    public boolean isEnablemanualTask(String TaskId) {
        /*扫描当前手动开的任务,获取手动控制的相机idList*/
        List<String> menualCameraId = new ArrayList<>();
        ArdVideoInspectTask menualTask = new ArdVideoInspectTask();
        menualTask.setInspectMode("手动");
        menualTask.setMenualSwitch("开");
        List<ArdVideoInspectTask> menualTasks = ardVideoInspectTaskMapper.selectArdVideoInspectTaskList(menualTask);
        if (menualTasks.size() > 0) {
            for (ArdVideoInspectTask task : menualTasks) {
                if (StringUtils.isNull(task.getCameraId()) || StringUtils.isEmpty(task.getCameraId())) {
                    continue;
                }
                if (!menualCameraId.contains(task.getCameraId())) {
                    menualCameraId.add(task.getCameraId());
                }
            }
        }
        /*判断相机是否已经被人手动控制*/
        ArdVideoInspectTask videoInspectTask = ardVideoInspectTaskMapper.selectArdVideoInspectTaskById(TaskId);
        if (StringUtils.isNull(videoInspectTask)) {
            return false;
        }
        String cameraId = videoInspectTask.getCameraId();
        if (StringUtils.isEmpty(cameraId)) {
            return false;
        }
        if (menualCameraId.contains(cameraId)) {
            /*如果当前任务关联的相机已经有人手动控制,则不允许控制*/
            return false;
        }
        return true;
    }
 
    /**
     * 自动巡检任务
     */
    @Override
    public void autoTaskRun() {
        try {
            /*扫描当前手动开的任务,获取手动控制的相机idList*/
            List<String> menualCameraId = new ArrayList<>();
            ArdVideoInspectTask menualTask = new ArdVideoInspectTask();
            menualTask.setInspectMode("手动");
            menualTask.setMenualSwitch("开");
            List<ArdVideoInspectTask> menualTasks = ardVideoInspectTaskMapper.selectArdVideoInspectTaskList(menualTask);
            if (menualTasks.size() > 0) {
                for (ArdVideoInspectTask task : menualTasks) {
                    if (StringUtils.isNull(task.getCameraId()) || StringUtils.isEmpty(task.getCameraId())) {
                        continue;
                    }
                    if (!menualCameraId.contains(task.getCameraId())) {
                        menualCameraId.add(task.getCameraId());
                    }
                }
            }
 
            /*扫描所有自动任务*/
            ArdVideoInspectTask autoTask = new ArdVideoInspectTask();
            autoTask.setInspectMode("自动");
            List<ArdVideoInspectTask> ardVideoInspectTasks = ardVideoInspectTaskMapper.selectArdVideoInspectTaskList(autoTask);
            for (ArdVideoInspectTask videoInspectTask : ardVideoInspectTasks) {
                /*遍历所有时间满足的自动任务*/
                boolean timeCompare = DateUtils.TimeCompare(videoInspectTask.getStartTime(), videoInspectTask.getEndTime());
                if (timeCompare) {
                    if (menualCameraId.contains(videoInspectTask.getCameraId())) {
                        /*若当前相机正在有任务手动执行,则不执行*/
                        continue;
                    }
                    /*获取当前任务的所有巡检步骤*/
                    List<ArdVideoInspectTaskStep> ardVideoInspectTaskStepList = ardVideoInspectTaskMapper.selectArdVideoInspectTaskById(videoInspectTask.getId()).getArdVideoInspectTaskStepList();
                    if (ardVideoInspectTaskStepList.size() == 0) {
                        continue;
                    }
                    videoInspectTask.setArdVideoInspectTaskStepList(ardVideoInspectTaskStepList);
 
                    String currentStepId = videoInspectTask.getCurrentStepId();
                    String currentStepStartTime = videoInspectTask.getCurrentStepStartTime();
                    if (StringUtils.isNull(currentStepId) || StringUtils.isEmpty(currentStepId)) {
                        //开始当前任务的第一个步骤
                        videoInspectTask.setCurrentStepId(ardVideoInspectTaskStepList.get(0).getId());
                        startRunStep(videoInspectTask);
                    } else /*当前任务已经执行,判断是否到期*/ {
                        if (StringUtils.isNull(currentStepStartTime) || StringUtils.isEmpty(currentStepStartTime)) {
                            //当前任务启动时间为空,则说明当前步骤被中断,直接启动当前步骤
                            startRunStep(videoInspectTask);
                        } else {
                            //判断当前步骤时间是否过期
                            boolean expird = isExpirdStep(videoInspectTask);
                            if (expird) {
                                //停止录像
                                stopRunStep(videoInspectTask);
                                //切换步骤
                                String nextStepId = changeNextStep(videoInspectTask);
                                //开始下一步骤
                                videoInspectTask.setCurrentStepId(nextStepId);
                                startRunStep(videoInspectTask);
                            } else {
                                tryGuide(videoInspectTask);
                            }
                        }
                    }
                } else {
                    if (StringUtils.isNotEmpty(videoInspectTask.getCurrentStepId())) {
                        videoInspectTask.setCurrentStepId("");
                        ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(videoInspectTask);
                    }
                    if (StringUtils.isNotEmpty(videoInspectTask.getCurrentStepStartTime())) {
                        videoInspectTask.setCurrentStepId("");
                        ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(videoInspectTask);
                    }
                }
            }
        } catch (Exception ex) {
            log.error("自动巡检任务异常:" + ex.getMessage());
        }
    }
 
    /**
     * 自动手动合并,扫库方式,目前不使用
     */
    @Override
    public void taskRun() {
        ArdVideoInspectTask ardVideoInspectTask = new ArdVideoInspectTask();
        List<ArdVideoInspectTask> ardVideoInspectTasks = ardVideoInspectTaskMapper.selectArdVideoInspectTaskList(ardVideoInspectTask);
        for (ArdVideoInspectTask videoInspectTask : ardVideoInspectTasks) {
            if (videoInspectTask.getInspectMode().equals("自动")) {
                boolean timeCompare = DateUtils.TimeCompare(videoInspectTask.getStartTime(), videoInspectTask.getEndTime());
                if (!timeCompare) {
                    continue;
                }
            } else {
                if (videoInspectTask.getMenualSwitch().equals("关")) {
                    continue;
                }
            }
            /*获取当前任务的所有巡检步骤*/
            List<ArdVideoInspectTaskStep> ardVideoInspectTaskStepList = ardVideoInspectTaskMapper.selectArdVideoInspectTaskById(videoInspectTask.getId()).getArdVideoInspectTaskStepList();
            if (ardVideoInspectTaskStepList.size() == 0) {
                continue;
            }
            videoInspectTask.setArdVideoInspectTaskStepList(ardVideoInspectTaskStepList);
            String currentStepId = videoInspectTask.getCurrentStepId();//当前步骤id
            String currentStepStartTime = videoInspectTask.getCurrentStepStartTime();//当前步骤启动时间
            if (StringUtils.isNull(currentStepId) || StringUtils.isEmpty(currentStepId)) {
                //开始第一个步骤
                videoInspectTask.setCurrentStepId(ardVideoInspectTaskStepList.get(0).getId());
                startRunStep(videoInspectTask);
            } else {
                if (StringUtils.isNull(currentStepStartTime) || StringUtils.isEmpty(currentStepStartTime)) {
                    //当前任务启动时间为空,则说明当前步骤被中断,直接启动当前步骤
                    startRunStep(videoInspectTask);
                } else {
                    boolean expird = isExpirdStep(videoInspectTask); /*判断当前步骤时间是否过期*/
                    if (expird) {
                        //停止录像
                        stopRunStep(videoInspectTask);
                        //切换步骤
                        String nextStepId = changeNextStep(videoInspectTask);
                        //开始下一步骤
                        videoInspectTask.setCurrentStepId(nextStepId);
                        startRunStep(videoInspectTask);
                    }
                }
            }
        }
    }
 
    //步骤开始
    private void startRunStep(ArdVideoInspectTask ardVideoInspectTask) {
        try {
            String currentStepId = ardVideoInspectTask.getCurrentStepId();
            if (StringUtils.isNull(currentStepId)) {
                log.debug("当前开始巡检步骤id为空");
                return;
            }
            log.debug("巡检步骤:" + currentStepId + "尝试开始");
            String cameraId = ardVideoInspectTask.getCameraId();
            Integer channel = ardVideoInspectTask.getChannel();
            Optional<ArdVideoInspectTaskStep> objectOptional = ardVideoInspectTask.getArdVideoInspectTaskStepList().stream()
                    .filter(obj -> obj.getId().equals(currentStepId))
                    .findFirst();
            if (objectOptional.isPresent()) {
                ArdVideoInspectTaskStep step = objectOptional.get();
                /*更新任务当前步骤id和步骤启动时间*/
                ardVideoInspectTask.setCurrentStepId(step.getId());
                ardVideoInspectTask.setCurrentStepStartTime(DateUtils.getTime());
                ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(ardVideoInspectTask);
                String wellId = step.getWellId();
                if (!StringUtils.isNull(wellId)) {
                    /*获取井坐标*/
                    ArdAlarmpointsWell ardAlarmpointsWell = ardAlarmpointsWellMapper.selectArdAlarmpointsWellById(wellId);
                    double[] targetPositon = new double[3];
                    targetPositon[0] = ardAlarmpointsWell.getLongitude();
                    targetPositon[1] = ardAlarmpointsWell.getLatitude();
                    targetPositon[2] = ardAlarmpointsWell.getAltitude();
                    /*获取相机坐标*/
                    ArdCameras cameras = ardCamerasMapper.selectArdCamerasById(cameraId);
                    if(StringUtils.isNull(cameras))
                    {
                        log.debug("找不到相机:"+cameraId);
                        return;
                    }
                    double[] cameraPositon = new double[3];
                    cameraPositon[0] = cameras.getLongitude();
                    cameraPositon[1] = cameras.getLatitude();
                    cameraPositon[2] = cameras.getAltitude();
                    /*控制相机巡检*/
                    CameraCmd cmd = new CameraCmd();
                    cmd.setCameraId(cameraId);
                    cmd.setChannelNum(channel);
                    cmd.setTargetPosition(targetPositon);
                    cmd.setOperator("sys_patrol_inspect");
                    cmd.setExpired(step.getRecordingTime() * 60);
                    boolean setTargetPosition = hikClientService.guideTargetPosition(cmd);
                    if (setTargetPosition) {
                        /*控制相机巡检成功,开始录像*/
                        hikClientService.recordStart(cmd);
                    } else {
                        /*控制失败,当前步骤启动时间置null*/
                        ardVideoInspectTask.setCurrentStepStartTime("");
                        ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(ardVideoInspectTask);
                    }
                }
            }
        } catch (Exception ex) {
            log.error("巡检开始异常:" + ex.getMessage());
        }
    }
 
    //尝试控制引导
    private void tryGuide(ArdVideoInspectTask ardVideoInspectTask) {
        try {
            String currentStepId = ardVideoInspectTask.getCurrentStepId();
            if (StringUtils.isNull(currentStepId)) {
                log.debug("当前开始巡检步骤id为空");
                return;
            }
            log.debug("巡检正常,尝试引导:" + currentStepId);
            String cameraId = ardVideoInspectTask.getCameraId();
            Integer channel = ardVideoInspectTask.getChannel();
            Optional<ArdVideoInspectTaskStep> objectOptional = ardVideoInspectTask.getArdVideoInspectTaskStepList().stream()
                    .filter(obj -> obj.getId().equals(currentStepId))
                    .findFirst();
            if (objectOptional.isPresent()) {
                ArdVideoInspectTaskStep step = objectOptional.get();
                String wellId = step.getWellId();
                if (!StringUtils.isNull(wellId)) {
                    /*获取井坐标*/
                    ArdAlarmpointsWell ardAlarmpointsWell = ardAlarmpointsWellMapper.selectArdAlarmpointsWellById(wellId);
                    double[] targetPositon = new double[3];
                    targetPositon[0] = ardAlarmpointsWell.getLongitude();
                    targetPositon[1] = ardAlarmpointsWell.getLatitude();
                    targetPositon[2] = ardAlarmpointsWell.getAltitude();
                    /*获取相机坐标*/
                    ArdCameras cameras = ardCamerasMapper.selectArdCamerasById(cameraId);
                    if(StringUtils.isNull(cameras))
                    {
                        log.debug("找不到相机:"+cameraId);
                        return;
                    }
                    double[] cameraPositon = new double[3];
                    cameraPositon[0] = cameras.getLongitude();
                    cameraPositon[1] = cameras.getLatitude();
                    cameraPositon[2] = cameras.getAltitude();
                    /*控制相机巡检*/
                    CameraCmd cmd = new CameraCmd();
                    cmd.setCameraId(cameraId);
                    cmd.setChannelNum(channel);
                    cmd.setTargetPosition(targetPositon);
                    cmd.setOperator("sys_patrol_inspect");
                    cmd.setExpired(step.getRecordingTime() * 60);
                    boolean setTargetPosition = hikClientService.guideTargetPosition(cmd);
                    if (!setTargetPosition) {
                        /*控制失败,当前步骤启动时间置null*/
                        ardVideoInspectTask.setCurrentStepStartTime("");
                        ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(ardVideoInspectTask);
                    } else {
                        log.debug("引导成功!");
                    }
                }
            }
        } catch (Exception ex) {
            log.error("巡检尝试引导异常:" + ex.getMessage());
        }
    }
 
    //步骤停止
    private void stopRunStep(ArdVideoInspectTask ardVideoInspectTask) {
        try {
            String currentStepId = ardVideoInspectTask.getCurrentStepId();
            log.debug("巡检步骤:" + currentStepId + "停止");
            if (StringUtils.isNull(currentStepId)) {
                log.info("当前停止巡检步骤id为空");
                return;
            }
            String currentStepStartTime = ardVideoInspectTask.getCurrentStepStartTime();
            String cameraId = ardVideoInspectTask.getCameraId();
            Integer channel = ardVideoInspectTask.getChannel();
            Optional<ArdVideoInspectTaskStep> objectOptional = ardVideoInspectTask.getArdVideoInspectTaskStepList().stream()
                    .filter(obj -> obj.getId().equals(currentStepId))
                    .findFirst();
            if (objectOptional.isPresent()) {
                ArdVideoInspectTaskStep step = objectOptional.get();
                /*停止录像*/
                CameraCmd cmd = new CameraCmd();
                cmd.setCameraId(cameraId);
                cmd.setChannelNum(channel);
                cmd.setOperator("sys_patrol_inspect");
                cmd.setRecordBucketName("record");
                cmd.setRecordObjectName("inspect_" + IdUtils.fastSimpleUUID());
                String url = hikClientService.recordStopToMinio(cmd);
                /*插入巡检记录*/
                ArdVideoInspectRecord ardVideoInspectRecord = new ArdVideoInspectRecord();
                ardVideoInspectRecord.setStepId(step.getId());
                ArdAlarmpointsWell ardAlarmpointsWell = ardAlarmpointsWellMapper.selectArdAlarmpointsWellById(step.getWellId());
                if (StringUtils.isNotNull(ardAlarmpointsWell)) {
                    ardVideoInspectRecord.setWellName(ardAlarmpointsWell.getWellId());
                }
                Date currentStepStartDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, currentStepStartTime);
                Date currentStepStopDate = DateUtils.addMinutes(currentStepStartDate, step.getRecordingTime());
                ardVideoInspectRecord.setStartTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, currentStepStartDate));
                ardVideoInspectRecord.setEndTime(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, currentStepStopDate));
                ardVideoInspectRecord.setRecordFilePath(url);
                ardVideoInspectRecordMapper.insertArdVideoInspectRecord(ardVideoInspectRecord);
            }
        } catch (Exception ex) {
            log.error("巡检停止异常:" + ex.getMessage());
        }
    }
 
    //步骤判断是否过期
    private boolean isExpirdStep(ArdVideoInspectTask ardVideoInspectTask) {
        try {
            List<ArdVideoInspectTaskStep> ardVideoInspectTaskStepList = ardVideoInspectTask.getArdVideoInspectTaskStepList();
            /*获取当前任务正在执行的步骤和当前步骤开始的时间*/
            String currentStepId = ardVideoInspectTask.getCurrentStepId();
            String currentStepStartTime = ardVideoInspectTask.getCurrentStepStartTime();
            Optional<ArdVideoInspectTaskStep> objectOptional = ardVideoInspectTaskStepList.stream()
                    .filter(ardVideoInspectTaskStep -> ardVideoInspectTaskStep.getId().equals(currentStepId))
                    .findFirst();
            if (objectOptional.isPresent()) {
                /*获取当前步骤信息*/
                ArdVideoInspectTaskStep currentStep = objectOptional.get();
                /*获取到当前步骤的开始和结束时间*/
                Date currentStepStartDate = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, currentStepStartTime);
                Date currentStepStopDate = DateUtils.addMinutes(currentStepStartDate, currentStep.getRecordingTime());
                /*判断当前步骤时间是否过期*/
                if (!DateUtils.TimeCompare(currentStepStartDate, currentStepStopDate)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception ex) {
            log.error("判断巡检步骤过期异常:" + ex.getMessage());
            return false;
        }
    }
 
    //步骤切换
    private String changeNextStep(ArdVideoInspectTask ardVideoInspectTask) {
        try {
            String currentStepId = ardVideoInspectTask.getCurrentStepId();
            Optional<ArdVideoInspectTaskStep> objectOptional = ardVideoInspectTask.getArdVideoInspectTaskStepList().stream()
                    .filter(obj -> obj.getId().equals(currentStepId))
                    .findFirst();
            if (objectOptional.isPresent()) {
                ArdVideoInspectTaskStep step = objectOptional.get();
                Integer currentStepOrderNumber = step.getOrderNumber();
                /*判断当前步骤序号是否小于步骤总数*/
                if (currentStepOrderNumber < ardVideoInspectTask.getArdVideoInspectTaskStepList().size()) {
                    /*小于则执行下一步骤*/
                    currentStepOrderNumber++;
                } else {
                    /*否则从1开始执行*/
                    currentStepOrderNumber = 1;
                }
                Integer nextStepOrderNumber = currentStepOrderNumber;
                /*更新当前任务切换新步骤*/
                ArdVideoInspectTask avit = new ArdVideoInspectTask();
                avit.setId(ardVideoInspectTask.getId());
                String nextStepId = ardVideoInspectTask.getArdVideoInspectTaskStepList().stream()
                        .filter(obj -> obj.getOrderNumber() == nextStepOrderNumber)
                        .map(ArdVideoInspectTaskStep::getId)
                        .findFirst()
                        .orElse(null);
                avit.setCurrentStepId(nextStepId);
                ardVideoInspectTaskMapper.updateArdVideoInspectTaskWithCurrentSetpInfo(avit);
                log.info("步骤:" + currentStepId + "切换为" + nextStepId);
                return nextStepId;
            }
        } catch (Exception ex) {
            log.error("巡检步骤切换异常:" + ex.getMessage());
        }
        return "";
    }
 
    public List getTaskUsedCameraPeriods(String cameraId) {
        return ardVideoInspectTaskMapper.getTaskUsedCameraPeriods(cameraId);
    }
 
    /**
     * 获取相机的空闲时段
     * @param cameraId
     * @return
     */
    public List getCameraIdleTimeList(String cameraId) {
        LinkedList<String> timeList = new LinkedList();
        List<Map> usedPeriods = this.getTaskUsedCameraPeriods(cameraId);
        for (Map p :
                usedPeriods) {
            timeList.add((String) p.get("start_time"));
            timeList.add((String) p.get("end_time"));
        }
        //判断第一个起始点
        if (timeList.size() > 0) {
            if ("00:00:00".equals(timeList.getFirst())) {
                timeList.removeFirst();
            } else {
                timeList.addFirst("00:00:00");
            }
            //判断最后一个结束时间
            if ("23:59:59".equals(timeList.getLast())) {
                timeList.removeLast();
            } else {
                timeList.addLast("23:59:59");
            }
            //事件段为空,则全天作为一个时间段
            if (timeList.size() == 0) {
                timeList.add("00:00:00");
                timeList.add("23:59:59");
            }
 
        }
 
        return timeList;
    }
}