‘liusuyi’
2023-05-31 f36efaba44f2108f551336336e437be58c270313
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
package com.ruoyi.quartz.task;
 
import com.ruoyi.alarmpoints.well.domain.ArdAlarmpointsWell;
import com.ruoyi.alarmpoints.well.service.IArdAlarmpointsWellService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.device.camera.domain.ArdCameras;
import com.ruoyi.device.camera.domain.CameraCmd;
import com.ruoyi.device.camera.service.IArdCamerasService;
import com.ruoyi.device.hiksdk.config.MinioClientSingleton;
import com.ruoyi.device.hiksdk.service.IHikClientService;
import com.ruoyi.inspect.domain.ArdVideoInspectRecord;
import com.ruoyi.inspect.domain.ArdVideoInspectTask;
import com.ruoyi.inspect.domain.ArdVideoInspectTaskStep;
import com.ruoyi.inspect.service.IArdVideoInspectRecordService;
import com.ruoyi.inspect.service.IArdVideoInspectTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * @Description: 巡检任务
 * @ClassName: PatrolInspectionTask
 * @Author: 刘苏义
 * @Date: 2023年05月29日17:18
 * @Version: 1.0
 **/
@Component("PatrolInspectionTask")
@Slf4j(topic = "PatrolInspectionTask")
public class PatrolInspectionTask {
 
    @Resource
    IArdVideoInspectTaskService ardVideoInspectTaskService;
    @Resource
    IArdAlarmpointsWellService ardAlarmpointsWellService;
    @Resource
    IArdVideoInspectRecordService ardVideoInspectRecordService;
    @Resource
    IArdCamerasService ardCamerasService;
    @Resource
    IHikClientService hikClientService;
 
    public void scanRun() {
        /*扫描所有可执行任务1-时间满足2-自动*/
        ArdVideoInspectTask ardVideoInspectTask = new ArdVideoInspectTask();
        ardVideoInspectTask.setInspectMode("自动");
        List<ArdVideoInspectTask> ardVideoInspectTasks = ardVideoInspectTaskService.selectArdVideoInspectTaskList(ardVideoInspectTask);
        for (ArdVideoInspectTask videoInspectTask : ardVideoInspectTasks) {
            /*遍历所有时间满足的自动任务*/
            boolean timeCompare = DateUtils.TimeCompare(videoInspectTask.getStartTime(), videoInspectTask.getEndTime());
            if (timeCompare) {
                /*获取当前任务的所有巡检步骤*/
                List<ArdVideoInspectTaskStep> ardVideoInspectTaskStepList = ardVideoInspectTaskService.selectArdVideoInspectTaskById(videoInspectTask.getId()).getArdVideoInspectTaskStepList();
                if (ardVideoInspectTaskStepList.size() == 0) {
                    continue;
                }
                videoInspectTask.setArdVideoInspectTaskStepList(ardVideoInspectTaskStepList);
                String currentStepId = videoInspectTask.getCurrentStepId();
                if (StringUtils.isNull(currentStepId)) {
                    videoInspectTask.setCurrentStepId(ardVideoInspectTaskStepList.get(0).getId());
                    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) {
        String currentStepId = ardVideoInspectTask.getCurrentStepId();
        if(StringUtils.isNull(currentStepId))
        {
            log.info("当前开始巡检步骤id为空");
            return;
        }
        log.info("步骤:" + 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 = ardAlarmpointsWellService.selectArdAlarmpointsWellById(wellId);
                double[] targetPositon = new double[3];
                targetPositon[0] = ardAlarmpointsWell.getLongitude();
                targetPositon[1] = ardAlarmpointsWell.getLatitude();
                targetPositon[2] = ardAlarmpointsWell.getAltitude();
                /*获取相机坐标*/
                ArdCameras cameras = ardCamerasService.selectArdCamerasById(cameraId);
                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.setCamPosition(cameraPositon);
                cmd.setTargetPosition(targetPositon);
                cmd.setOperator("sys_patrol_inspect");
                cmd.setExpired(step.getRecordingTime());
                boolean setTargetPosition = hikClientService.setTargetPosition(cmd);
                if (setTargetPosition) {
                    /*控制相机巡检成功,开始录像*/
                    cmd.setEnable(true);//启动录像
                    hikClientService.recordToMinio(cmd);
                    /*更新任务当前步骤id和步骤启动时间*/
                    ArdVideoInspectTask avit = new ArdVideoInspectTask();
                    avit.setId(ardVideoInspectTask.getId());
                    avit.setCurrentStepId(step.getId());
                    avit.setCurrentStepStartTime(DateUtils.getTime());
                    ardVideoInspectTaskService.updateArdVideoInspectTaskNoUpdater(avit);
                }
            }
        }
    }
 
    //步骤停止
    private void stopRunStep(ArdVideoInspectTask ardVideoInspectTask) {
        String currentStepId = ardVideoInspectTask.getCurrentStepId();
        log.info("步骤:" + 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.setEnable(false);//停止录像
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
            String recordName = cameraId + "/" + time + "/" + uuid + ".mp4";
            cmd.setRecordBucketName("record");
            cmd.setRecordObjectName(recordName);
            hikClientService.recordToMinio(cmd);
            /*插入巡检记录*/
            ArdVideoInspectRecord ardVideoInspectRecord = new ArdVideoInspectRecord();
            ardVideoInspectRecord.setStepId(step.getId());
            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));
            String url = MinioClientSingleton.domainUrl + "/" + cmd.getRecordBucketName() + "/" + recordName;
            ardVideoInspectRecord.setRecordFilePath(url);
            ardVideoInspectRecordService.insertArdVideoInspectRecord(ardVideoInspectRecord);
        }
    }
 
    //步骤判断是否过期
    private boolean isExpirdStep(ArdVideoInspectTask ardVideoInspectTask) {
        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;
        }
 
    }
 
    //步骤切换
    private String changeNextStep(ArdVideoInspectTask ardVideoInspectTask) {
        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);
            ardVideoInspectTaskService.updateArdVideoInspectTaskNoUpdater(avit);
            log.info("步骤:" + currentStepId + "切换为"+nextStepId);
            return nextStepId;
        }
        return "";
    }
}