admin
2024-05-27 816ba1e56476f0e23f51711aae4b34507a04a59c
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
package com.ruoyi.aibrain.service.impl;
 
import java.util.ArrayList;
import java.util.List;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.activeMQ.vo.RuleAreaVO;
import com.ruoyi.aibrain.domain.AiCameraRule;
import com.ruoyi.aibrain.mapper.AiCameraRuleMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.AiRuleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.aibrain.mapper.AiCameraMapper;
import com.ruoyi.aibrain.domain.AiCamera;
import com.ruoyi.aibrain.service.IAiCameraService;
 
/**
 * 设备管理Service业务层处理
 *
 * @author wf
 * @date 2023-03-08
 */
@Service
public class AiCameraServiceImpl implements IAiCameraService
{
    @Autowired
    private AiCameraMapper aiCameraMapper;
 
    @Autowired
    private AiCameraRuleMapper aiCameraRuleMapper;
 
    @Autowired
    private AiRuleMapper aiRuleMapper;
 
    /**
     * 查询设备管理
     *
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public AiCamera selectAiCameraById(Long id)
    {
        AiCamera aiCamera = aiCameraMapper.selectAiCameraById(id);
        List<AiCameraRule> rules = aiCameraRuleMapper.selectAiCameraRuleListByCameraId(id);
        for (AiCameraRule rule : rules){
            String left = "[";
            String center = "[";
            String right = "[";
            JSONArray array = JSONArray.parseArray(rule.getRuleArea());
            List<RuleAreaVO> areas = new ArrayList<>();
            if (array != null){
                for (int i = 0; i < array.size(); i++) {
                    if(rule.getRuleId() == 1){
                        if(i >= 0 && i < 4){
                            left = left + array.getJSONObject(i) + ",";
                        }
                        if(i >= 4 && i < 8){
                            center = center + array.getJSONObject(i)+ ",";
                        }
                        if(i >= 8){
                            right = right + array.getJSONObject(i)+ ",";
                        }
                    }
                    JSONObject json = array.getJSONObject(i);
                    RuleAreaVO area = new RuleAreaVO();
//                    area.setX(Float.parseFloat(json.get("x").toString()));
//                    area.setY(Float.parseFloat(json.get("y").toString()));
                    areas.add(area);
                }
            }
//            if(rule.getRuleId() == 1){
//                rule.setLeftArea(left.substring(0, left.lastIndexOf(','))+"]");
//                rule.setCenterArea(center.substring(0, center.lastIndexOf(','))+"]");
//                rule.setRightArea(right.substring(0, right.lastIndexOf(','))+"]");
//            }
            rule.setAreas(areas);
        }
        aiCamera.setRules(rules);
        return aiCamera;
    }
 
    /**
     * 查询设备管理列表
     *
     * @param aiCamera 设备管理
     * @return 设备管理
     */
    @Override
    public List<AiCamera> selectAiCameraList(AiCamera aiCamera)
    {
        List<AiCamera> aiCameras = aiCameraMapper.selectAiCameraList(aiCamera);
        for (AiCamera camera : aiCameras) {
            List<AiCameraRule> rules = aiCameraRuleMapper.selectAiCameraRuleListByCameraId(camera.getId());
            for (AiCameraRule rule : rules){
                String left = "[";
                String center = "[";
                String right = "[";
                JSONArray array = JSONArray.parseArray(rule.getRuleArea());
                List<RuleAreaVO> areas = new ArrayList<>();
                if (array != null){
                    for (int i = 0; i < array.size(); i++) {
                        if(rule.getRuleId() == 1){
                            if(i >= 0 && i < 5){
                                left = left + array.getJSONObject(i);
                            }
                            if(i >= 5 && i < 9){
                                center = center + array.getJSONObject(i);
                            }
                            if(i >= 9){
                                right = right + array.getJSONObject(i);
                            }
                        }
                        JSONObject json = array.getJSONObject(i);
                        RuleAreaVO area = new RuleAreaVO();
//                        area.setX(Float.parseFloat(json.get("x").toString()));
//                        area.setY(Float.parseFloat(json.get("y").toString()));
                        areas.add(area);
                    }
                }
                rule.setLeftArea(left+"]");
                rule.setCenterArea(center+"]");
                rule.setRightArea(right+"]");
                rule.setAreas(areas);
            }
            camera.setRules(rules);
        }
        return aiCameras;
    }
 
    /**
     * 新增设备管理
     *
     * @param aiCamera 设备管理
     * @return 结果
     */
    @Override
    public int insertAiCamera(AiCamera aiCamera)
    {
        aiCamera.setCreateTime(DateUtils.getNowDate());
        aiCamera.setCreateBy(SecurityUtils.getUsername());
 
        aiCamera.setRtspUrl("rtsp://"+aiCamera.getCameraUser()+":"+aiCamera.getCameraPassword()+
                "@"+aiCamera.getCameraIp()+":554/h264/1/main/av_stream");
 
        aiCameraMapper.insertAiCamera(aiCamera);
        insertCameraRule(aiCamera);
        aiCamera.setCameraNum("AIC0" + aiCamera.getRegion() + String.format("%03d", aiCamera.getId()));
        return aiCameraMapper.updateAiCamera(aiCamera);
    }
 
    private void insertCameraRule(AiCamera aiCamera) {
        //清除原设备算法
        aiCameraRuleMapper.deleteAiCameraRuleByCameraId(aiCamera.getId());
        for (AiCameraRule rule : aiCamera.getRules()){
            rule.setCameraId(aiCamera.getId());
            rule.setCreateBy(SecurityUtils.getUsername());
            rule.setRuleName(aiRuleMapper.selectAiRuleById(rule.getRuleId()).getRuleName());
//            if(rule.getRuleId() == 1){
//                String left = rule.getLeftArea().replace("]", "");
//                String center = rule.getCenterArea().replace("[", "");
//                String centerArea = center.replace("]", "");
//                String right = rule.getRightArea().replace("[", "");
//                rule.setRuleArea(left +","+ centerArea +","+ right);
//            }
            aiCameraRuleMapper.insertAiCameraRule(rule);
        }
    }
 
    /**
     * 修改设备管理
     *
     * @param aiCamera 设备管理
     * @return 结果
     */
    @Override
    public int updateAiCamera(AiCamera aiCamera)
    {
        aiCamera.setUpdateTime(DateUtils.getNowDate());
        insertCameraRule(aiCamera);
        aiCamera.setRtspUrl("rtsp://"+aiCamera.getCameraUser()+":"+aiCamera.getCameraPassword()+
                "@"+aiCamera.getCameraIp()+":554/h264/1/main/av_stream");
        return aiCameraMapper.updateAiCamera(aiCamera);
    }
 
    /**
     * 批量删除设备管理
     *
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteAiCameraByIds(Long[] ids)
    {
        return aiCameraMapper.deleteAiCameraByIds(ids);
    }
 
    /**
     * 删除设备管理信息
     *
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteAiCameraById(Long id)
    {
        return aiCameraMapper.deleteAiCameraById(id);
    }
 
    /**
     * 查询要启动的AI识别设备
     * @return
     */
    @Override
    public List<AiCamera> queryCameraToStart() {
        List<AiCamera> cameraList = aiCameraMapper.queryCameraToStart();
        for (AiCamera camera : cameraList){
            List<AiCameraRule> rules = aiCameraRuleMapper.selectAiCameraRuleListByCameraId(camera.getId());
            for (AiCameraRule rule : rules){
                JSONArray array = JSONArray.parseArray(rule.getRuleArea());
                List<RuleAreaVO> areas = new ArrayList<>();
                if (array != null){
                    for (int i = 0; i < array.size(); i++) {
                        JSONObject json = array.getJSONObject(i);
                        RuleAreaVO area = new RuleAreaVO();
//                        area.setX(Float.parseFloat(json.get("x").toString()));
//                        area.setY(Float.parseFloat(json.get("y").toString()));
                        areas.add(area);
                    }
                }
                rule.setAreas(areas);
            }
            camera.setRules(rules);
        }
        return cameraList;
    }
 
    /**
     * 变更设备状态
     * @return
     */
    @Override
    public void updateAiCameraStatus(AiCamera aiCamera) {
        aiCameraMapper.updateAiCamera(aiCamera);
    }
 
    /**
     * 查询所有设备
     * @return
     */
    @Override
    public List<AiCamera> selectAllCamera() {
        AiCamera camera = new AiCamera();
        List<AiCamera> aiCameras = aiCameraMapper.selectAiCameraList(camera);
        for (AiCamera aiCamera : aiCameras) {
            List<AiCameraRule> rules = aiCameraRuleMapper.selectAiCameraRuleListByCameraId(aiCamera.getId());
            for (AiCameraRule rule : rules){
                String left = "[";
                String center = "[";
                String right = "[";
                JSONArray array = JSONArray.parseArray(rule.getRuleArea());
                List<RuleAreaVO> areas = new ArrayList<>();
                if (array != null){
                    for (int i = 0; i < array.size(); i++) {
                        if(rule.getRuleId() == 1){
                            if(i >= 0 && i < 4){
                                left = left + array.getJSONObject(i) + ",";
                            }
                            if(i >= 4 && i < 8){
                                center = center + array.getJSONObject(i)+ ",";
                            }
                            if(i >= 8){
                                right = right + array.getJSONObject(i)+ ",";
                            }
                        }
                        JSONObject json = array.getJSONObject(i);
                        RuleAreaVO area = new RuleAreaVO();
//                    area.setX(Float.parseFloat(json.get("x").toString()));
//                    area.setY(Float.parseFloat(json.get("y").toString()));
                        areas.add(area);
                    }
                }
//            if(rule.getRuleId() == 1){
//                rule.setLeftArea(left.substring(0, left.lastIndexOf(','))+"]");
//                rule.setCenterArea(center.substring(0, center.lastIndexOf(','))+"]");
//                rule.setRightArea(right.substring(0, right.lastIndexOf(','))+"]");
//            }
                rule.setAreas(areas);
            }
            aiCamera.setRules(rules);
        }
        return aiCameras;
    }
}