‘liusuyi’
2023-06-15 4aa86f1386d20c9ad811e2d18b7e56f00b06e25c
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
package com.ruoyi.alarm.globalAlarm.service.impl;
 
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.alarm.globalAlarm.domain.GlobalAlarmCondition;
import com.ruoyi.alarm.globalAlarm.domain.GlobalAlarmData;
import com.ruoyi.alarm.globalAlarm.service.IGlobalAlarmService;
import com.ruoyi.alarm.stealAlarm.domain.ArdAlarmStealelec;
import com.ruoyi.alarm.stealAlarm.mapper.ArdAlarmStealelecMapper;
import com.ruoyi.alarm.tubeAlarm.domain.ArdAlarmTube;
import com.ruoyi.alarm.tubeAlarm.mapper.ArdAlarmTubeMapper;
import com.ruoyi.alarmpoints.tube.domain.ArdTubes;
import com.ruoyi.alarmpoints.tube.domain.ArdTubesDetails;
import com.ruoyi.alarmpoints.tube.mapper.ArdTubesDetailsMapper;
import com.ruoyi.alarmpoints.tube.mapper.ArdTubesMapper;
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.StringUtils;
import com.ruoyi.utils.tools.ArdTool;
import com.ruoyi.utils.tools.GisTool;
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.common.GlobalVariable;
import com.ruoyi.device.hiksdk.service.IHikClientService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.utils.tube.GeoPoint;
import com.ruoyi.utils.tube.TubeTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
 
/**
 * @ClassName: globalAlarmServiceImpl
 * @Description:
 * @Author: Administrator
 * @Date: 2023年03月10日 11:03
 * @Version: 1.0
 **/
@Service
@Slf4j(topic = "mqtt")
public class GlobalAlarmServiceImpl implements IGlobalAlarmService {
    @Resource
    ArdAlarmStealelecMapper ardAlarmStealelecMapper;
    @Resource
    ArdAlarmTubeMapper ardAlarmTubeMapper;
    @Resource
    ArdTubesMapper ardTubesMapper;
    @Resource
    ArdAlarmpointsWellMapper ardAlarmpointsWellMapper;
    @Resource
    ArdTubesDetailsMapper ardTubesDetailsMapper;
    @Resource
    private ISysConfigService configService;
    @Resource
    private ArdCamerasMapper ardCamerasMapper;
    @Resource
    private IHikClientService hikClientService;
    /**
     * @描述 按条件查询报警
     * @参数 [condition]
     * @返回值 java.util.List<com.ruoyi.alarm.globalAlarm.domain.GlobalAlarmData>
     * @创建人 刘苏义
     * @创建时间 2023/6/15 15:48
     * @修改人和其它信息
     */
    @Override
    public List<GlobalAlarmData> selectAlarmLogs(GlobalAlarmCondition condition) {
        String refreshTime = configService.selectConfigByKey("refreshTime");
        switch (condition.getCommand()) {
            case 1001:
                List<ArdAlarmStealelec> ardAlarmStealelecs = ardAlarmStealelecMapper.selectListAllByCommand(refreshTime);
                return ardAlarmStealelecs.stream()
                        .map(ardAlarmStealelec -> {
                            GlobalAlarmData globalAlarmData = new GlobalAlarmData()
                                    .setId(ardAlarmStealelec.getId())
                                    .setName(ardAlarmStealelec.getDescribe())
                                    .setAlarmTime(ardAlarmStealelec.getStartTime())
                                    .setLongitude(ardAlarmStealelec.getLongitude())
                                    .setLatitude(ardAlarmStealelec.getLatitude())
                                    .setAltitude(ardAlarmStealelec.getAltitude())
                                    .setCount(ardAlarmStealelec.getCount())
                                    .setTotal(ardAlarmStealelec.getTotal());
                            return globalAlarmData;
                        })
                        .collect(Collectors.toList());
            case 1014:
                List<ArdAlarmTube> ardAlarmTubes = ardAlarmTubeMapper.selectListAllByCommand(refreshTime);
                return ardAlarmTubes.stream()
                        .map(ardAlarmTube -> {
                            GlobalAlarmData globalAlarmData = new GlobalAlarmData()
                                    .setId(ardAlarmTube.getId())
                                    .setName(ardAlarmTube.getTubeName())
                                    .setAlarmTime(ardAlarmTube.getAlarmTime())
                                    .setLongitude(ardAlarmTube.getLongitude())
                                    .setLatitude(ardAlarmTube.getLatitude())
                                    .setAltitude(ardAlarmTube.getAltitude())
                                    .setCount(ardAlarmTube.getCount())
                                    .setTotal(ardAlarmTube.getTotal());
                            return globalAlarmData;
                        })
                        .collect(Collectors.toList());
            default:
                return null;
        }
    }
    /**
     * @描述 按条件更新查看时间
     * @参数 [condition]
     * @返回值 java.lang.Object
     * @创建人 刘苏义
     * @创建时间 2023/6/15 15:48
     * @修改人和其它信息
     */
    @Override
    public Object updateAlarmViewTime(GlobalAlarmCondition condition) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        switch (condition.getCommand()) {
            case 1001:
                ArdAlarmStealelec ardAlarmStealelec = ardAlarmStealelecMapper.selectByPrimaryKey(condition.getId());
                if (StringUtils.isNotNull(ardAlarmStealelec)) {
                    String describe = ardAlarmStealelec.getDescribe();
                    String startTime = fmt.format(ardAlarmStealelec.getStartTime());
                    int i = ardAlarmStealelecMapper.updateViewTimeByDescribe(describe, startTime, DateUtils.getTime());
                    return ardAlarmStealelec;
                }
            case 1014:
                ArdAlarmTube ardAlarmTube = ardAlarmTubeMapper.selectArdAlarmTubeById(condition.getId());
                if (StringUtils.isNotNull(ardAlarmTube)) {
                    String tubeId = ardAlarmTube.getTubeId();
                    String alarmTime = fmt.format(ardAlarmTube.getAlarmTime());
                    int i = ardAlarmTubeMapper.updateViewTimeByTubeId(tubeId, alarmTime, DateUtils.getTime());
                    return ardAlarmTube;
                }
            default:
                return null;
        }
    }
    /**
     * @描述 异步处理接收的报警
     * @参数 [topic, message]
     * @返回值 void
     * @创建人 刘苏义
     * @创建时间 2023/6/15 15:46
     * @修改人和其它信息
     */
    @Override
    @Async("alarmExecutor")
    public void receiveAlarm(String topic, String message) {
        try {
            String simpleUUID = IdUtils.simpleUUID();
            switch (topic) {
                case "stealelec":
                    //region 处理盗电报警
                    ArdAlarmStealelec ardAlarmStealelec = JSONObject.parseObject(message, ArdAlarmStealelec.class);
                    ArdAlarmpointsWell well = ardAlarmpointsWellMapper.selectArdAlarmpointsWellByWellId(ardAlarmStealelec.getDescribe());
                    if (well != null) {
                        ardAlarmStealelec.setLongitude(well.getLongitude());
                        ardAlarmStealelec.setLatitude(well.getLatitude());
                        ardAlarmStealelec.setAltitude(well.getAltitude());
                    }
                    ArdAlarmStealelec existe = ardAlarmStealelecMapper.selectByPrimaryKey(ardAlarmStealelec.getId());
                    if (StringUtils.isNotNull(existe)) {
                        return;
                    }
                    int aas = ardAlarmStealelecMapper.insertArdAlarmStealelec(ardAlarmStealelec);
                    if (aas > 0) {
                        log.info("stealelec入库成功:" + ardAlarmStealelec);
                        //引导录像
                        CameraCmd cmd = new CameraCmd();
                        cmd.setRecordBucketName("record");
                        cmd.setRecordObjectName("stealelec");
                        cmd.setOperator("sys_steal_elec");
                        cmd.setExpired(30);
                        cmd.setTargetPosition(new double[]{well.getLongitude(), well.getLatitude(), well.getAltitude()});
                        String url = guideCamera(cmd);
                        if (StringUtils.isNotEmpty(url)) {
                            //更新录像
                            ardAlarmStealelec.setRecordUrl(url);
                            ardAlarmStealelecMapper.updateArdAlarmStealelec(ardAlarmStealelec);
                        }
                    }
                    //endregion
                    break;
                case "tube":
                    //region 处理管线泄露报警
                    ArdAlarmTube ardAlarmTube = JSONObject.parseObject(message, ArdAlarmTube.class);
                    ardAlarmTube.setId(simpleUUID);
                    ArdTubesDetails atd = new ArdTubesDetails();
                    atd.setReelNumber(ardAlarmTube.getTubeId());
                    List<ArdTubesDetails> ardTubesDetails = ardTubesDetailsMapper.selectArdTubesDetailsList(atd);
                    if (ardTubesDetails.size() > 0) {
                        String tubeId = ardTubesDetails.get(0).getTubeId();
                        ArdTubes ardTubes = ardTubesMapper.selectArdTubesById(tubeId);
                        ardAlarmTube.setTubeName(ardTubes.getName());
                        ardAlarmTube.setColor(ardTubes.getColor());
                        ardAlarmTube.setPipeDiameter(ardTubes.getPipeDiameter());
                        ardAlarmTube.setTubeType(ardTubes.getType());
                        GeoPoint geoPoint = TubeTools.CalculateCoordinates(ardTubesDetails, ardAlarmTube.getPosition());
                        if (StringUtils.isNotNull(geoPoint)) {
                            ardAlarmTube.setLongitude(geoPoint.getLongitude());
                            ardAlarmTube.setLatitude(geoPoint.getLatitude());
                            ardAlarmTube.setAltitude(geoPoint.getAltitude());
                        }
                    }
                    int aat = ardAlarmTubeMapper.insertArdAlarmTube(ardAlarmTube);
                    if (aat > 0) {
                        log.debug("tube入库成功:" + ardAlarmTube);
                        //引导录像
                        CameraCmd cmd = new CameraCmd();
                        cmd.setRecordBucketName("record");
                        cmd.setRecordObjectName("tube");
                        cmd.setOperator("sys_tube_leak");
                        cmd.setExpired(30);
                        cmd.setTargetPosition(new double[]{ardAlarmTube.getLongitude(), ardAlarmTube.getLatitude(), ardAlarmTube.getAltitude()});
                        String url = guideCamera(cmd);
                        //更新录像
                        if (StringUtils.isNotEmpty(url)) {
                            ardAlarmTube.setRecordUrl(url);
                            ardAlarmTubeMapper.updateArdAlarmTube(ardAlarmTube);
                        }
                    }
                    //endregion
                    break;
            }
        } catch (Exception ex) {
            log.error("接收报警异常:" + ex.getMessage());
        }
    }
 
    /**
     * @描述 引导最近的大光电指向目标
     * @参数 [targetPosition]
     * @返回值 java.lang.String
     * @创建人 刘苏义
     * @创建时间 2023/6/15 11:55
     * @修改人和其它信息
     */
    private String guideCamera(CameraCmd cmd) {
        String url = "";
        try {
            //获取所有大光电
            List<ArdCameras> ardCamerasList = ardCamerasMapper.selectArdCamerasList(new ArdCameras("1"));
            //统计所有大光电可视范围内与报警点的距离
            Map<String, Double> distanceMap = new HashMap<>();
            for (ArdCameras camera : ardCamerasList) {
                double[] camPosition = new double[]{camera.getLongitude(), camera.getLatitude()};
                double distance = GisTool.getDistance(cmd.getTargetPosition(), camPosition);
                if (distance != 0.0 && distance <= camera.getCamMaxVisibleDistance()) {
                    distanceMap.put(camera.getId(), distance);
                }
            }
            if (distanceMap.size() > 0) {
                log.debug("尝试查找最近光电");
                //获取距离字典中最近的一个相机ID
                String minDistanceCameraId = ArdTool.getKeyByMinValue(distanceMap);
                log.debug("查找到最近光电:"+minDistanceCameraId+",尝试引导");
                //引导光电
                cmd.setCameraId(minDistanceCameraId);
                cmd.setChannelNum(1);
                boolean guideRes = hikClientService.guideTargetPosition(cmd);
                if (guideRes) {
                    log.debug("引导成功,尝试录像");
                    hikClientService.controlLock(cmd);//上锁
                    cmd.setEnable(true);//开始录像
                    hikClientService.recordToMinio(cmd);//开始录像
                    GlobalVariable.threadMap.put(minDistanceCameraId, Thread.currentThread().getName());//将相机id与当前处理线程名称绑定
                    Thread.sleep(cmd.getExpired() * 1000);//录像时长
                    String thread = GlobalVariable.threadMap.get(minDistanceCameraId);
                    String currentThread = Thread.currentThread().getName();
                    //判断相机绑定线程是否是当前线程,如果是,停止录像,如果不是,说明相机被其他线程抢占,不停止录像
                    if (thread.equals(currentThread)) {
                        cmd.setEnable(false);//停止录像
                        cmd.setUploadMinio(true);//上传minio
                        url = hikClientService.recordToMinio(cmd);//停止录像返回url
                    }
                }
            } else {
                log.debug("未查找到最近光电");
            }
        } catch (Exception ex) {
            log.error("引导异常:" + ex.getMessage());
        }
        return url;
    }
 
    /**
     * @描述 查询所有报警的当前数量
     * @参数 []
     * @返回值 java.util.Map<java.lang.String, java.lang.Integer>
     * @创建人 刘苏义
     * @创建时间 2023/6/13 16:30
     * @修改人和其它信息
     */
    @Override
    public Map<String, Integer> selectAlarmLogsCount() {
        Map<String, Integer> countMap = new HashMap<>();
        String refreshTime = configService.selectConfigByKey("refreshTime");
        int count1001 = ardAlarmStealelecMapper.selectCountByStartTime(refreshTime);
        countMap.put("1001", count1001);
        int count1014 = ardAlarmTubeMapper.selectCountByStartTime(refreshTime);
        countMap.put("1014 ", count1014);
        return countMap;
    }
}