‘liusuyi’
2023-06-14 6d44233b19774063d82303d6fb725feea6601a56
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
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.alarm.tubeAlarm.service.IArdAlarmTubeService;
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.common.utils.tools.ArdTool;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.utils.uuid.UUID;
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.config.MinioClientSingleton;
import com.ruoyi.device.hiksdk.service.IHikClientService;
import com.ruoyi.device.hiksdk.util.hikSdkUtil.GisUtil;
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.apache.poi.ss.formula.functions.T;
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;
 
import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
 
/**
 * @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;
    @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;
        }
    }
 
    @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;
        }
    }
 
    @Override
    @Async("alarmExecutor")
    public void receiveAlarm(String topic, String message) {
        try {
            String simpleUUID = IdUtils.simpleUUID();
            switch (topic) {
                case "stealelec":
                    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.setDeptId(well.getDeptId());
                    }
                    ArdAlarmStealelec existe = ardAlarmStealelecMapper.selectByPrimaryKey(ardAlarmStealelec.getId());
                    if (StringUtils.isNotNull(existe)) {
                        return;
                    }
                    int aas = ardAlarmStealelecMapper.insert(ardAlarmStealelec);
                    if (aas > 0) {
                        log.info("stealelec入库成功:" + ardAlarmStealelec);
                    }
                    break;
                case "tube":
                    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.info("tube入库成功:" + ardAlarmTube);
                        //查询所有大光电
                        ArdCameras ardCamera = new ArdCameras();
                        ardCamera.setGdtype("1");
                        List<ArdCameras> ardCamerasList = ardCamerasMapper.selectArdCamerasList(ardCamera);
                        //统计所有大光电可视范围内与报警点的距离
                        Map<String, Double> distanceMap = new HashMap<>();
                        for (ArdCameras camera : ardCamerasList) {
                            double distance = GisUtil.getDistance(ardAlarmTube.getLongitude(), ardAlarmTube.getLatitude(), camera.getLongitude(), camera.getLatitude());
                            if (distance <= camera.getCamMaxVisibleDistance()) {
                                distanceMap.put(camera.getId(), distance);
                            }
                        }
                        if(distanceMap.size()<0)
                        {
                            return;
                        }
                        //获取距离字典中最近的一个相机ID
                        String minDistanceCameraId = ArdTool.getKeyByMinValue(distanceMap);
                        //引导光电
                        CameraCmd cmd = new CameraCmd();
                        cmd.setOperator("sys_tube_leak");
                        cmd.setCameraId(minDistanceCameraId);
                        cmd.setChannelNum(1);
                        cmd.setTargetPosition(new double[]{ardAlarmTube.getLongitude(), ardAlarmTube.getLatitude()});
                        boolean guideRes = hikClientService.setTargetPosition(cmd);
                        if(guideRes) {
                            cmd.setEnable(true);
                            cmd.setRecordBucketName("record");
                            String time = new SimpleDateFormat("yyyyMMdd").format(new Date());
                            String recordName = minDistanceCameraId + "/" + time + "/" + simpleUUID + ".mp4";
                            cmd.setRecordObjectName(recordName);
                            hikClientService.recordToMinio(cmd);
                            Thread.sleep(30000);
                            cmd.setEnable(false);
                            cmd.setUploadMinio(true);
                            hikClientService.recordToMinio(cmd);
                            //更新录像
                            String url = MinioClientSingleton.domainUrl + "/" + cmd.getRecordBucketName() + "/" + recordName;
                            ardAlarmTube.setRecordUrl(url);
                            ardAlarmTubeMapper.updateArdAlarmTube(ardAlarmTube);
                        }
                    }
                    break;
            }
        }
        catch (Exception ex)
        {
            log.error("接收报警异常:"+ex.getMessage());
        }
    }
    /**
     * @描述 查询所有报警的当前数量
     * @参数 []
     * @返回值 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;
    }
}