elkers
7 天以前 3ca43c0f02fd9974eb087c79f411a3d55f806613
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
/**
#                                                    __----~~~~~~~~~~~------___
#                                   .  .   ~~//====......          __--~ ~~
#                   -.            \_|//     |||\\  ~~~~~~::::... /~
#                ___-==_       _-~o~  \/    |||  \\            _/~~-
#        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
#    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
#  .~       .~       |   \\ -_    /  /-   /   ||      \   /
# /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
# |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
#          '         ~-|      /|    |-~\~~       __--~~
#                      |-~~-_/ |    |   ~\_   _-~            /\
#                           /  \     \__   \/~                \__
#                       _--~ _/ | .-~~____--~-/                  ~~==.
#                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
#                                 -_     ~\      ~~---l__i__i__i--~~_/
#                                 _-~-__   ~)  \--______________--~~
#                               //.-~~~-~_--~- |-------~~~~~~~~
#                                      //.-~~~--\
#                  神兽保佑
#                  永无BUG!
*/
package com.nanjing.water.service;
 
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.nanjing.water.common.*;
import com.nanjing.water.common.enums.*;
import com.nanjing.water.common.model.Tuple;
import com.nanjing.water.common.util.*;
import com.nanjing.water.repository.vo.SysDictDataVO;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import com.nanjing.water.repository.impl.MonitorVideoStreamingMapperImpl;
import com.nanjing.water.repository.po.MonitorVideoStreamingPO;
import com.nanjing.water.entity.request.monitorvideostreaming.ReqCreateMonitorVideoStreaming;
import com.nanjing.water.entity.request.monitorvideostreaming.ReqModifyMonitorVideoStreaming;
import com.nanjing.water.entity.search.SearchMonitorVideoStreaming;
import com.nanjing.water.repository.vo.MonitorVideoStreamingVO;
import com.nanjing.water.service.convert.MonitorVideoStreamingConvert;
 
/**
 * 监控视频播放流
 * @author lin.liu
 */
@Service
public class MonitorVideoStreamingService extends BaseService {
    @Autowired
    private MonitorVideoStreamingMapperImpl mapper;
 
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SysDictDataService sysDictDataService;
 
    public ExecutedResult<Long> create(ReqCreateMonitorVideoStreaming request) {
        // 转换po
        MonitorVideoStreamingPO item = MonitorVideoStreamingConvert.INSTANCE.toCreate(request);
        // 设置状态
        //item.setStatus(EState.NORMAL.getValue());
        // 设置记录创建时间
        item.setCreateTime(LocalDateTimeUtil.nowTimeStamp());
        // 是否删除(逻辑删除)初始值
        item.setIsDelete(EYesOrNo.NO.getValue());
 
        int rowCount = mapper.insert(item);
        if (rowCount != 1) {
            return ExecutedResult.failed("创建[监控视频播放流]失败。");
        }
        return ExecutedResult.success(item.getId());
    }
 
    public ExecutedResult<String> modify(ReqModifyMonitorVideoStreaming request) {
        // 验证记录是否存在
        ExecutedResult<MonitorVideoStreamingPO> checkExists = this.check4Id(request.getId());
        if (checkExists.isFailed()) {
            return ExecutedResult.failed(checkExists.getMsg());
        }
        // 转换po
        MonitorVideoStreamingPO item = MonitorVideoStreamingConvert.INSTANCE.toModify(request);
 
        int rowCount = mapper.updateById(item);
        if (rowCount != 1) {
            return ExecutedResult.failed("编辑[监控视频播放流]失败。");
        }
        return ExecutedResult.success();
    }
    public ExecutedResult<List<MonitorVideoStreamingPO>> getAllByPoint( Long pointId){
        List<MonitorVideoStreamingPO> result=mapper.getAllByPoint(pointId);
        return ExecutedResult.success(result);
    }
    public ExecutedResult<MonitorVideoStreamingVO> get(Long id) {
        MonitorVideoStreamingVO result = new MonitorVideoStreamingVO();
 
        MonitorVideoStreamingPO find = mapper.get(id);
        if (null != find) {
            // 转换vo
            result = MonitorVideoStreamingConvert.INSTANCE.toVo(find);
        }
        return ExecutedResult.success(result);
    }
 
//    public ExecutedResult<String> stop(Long id) {
//        // 验证记录是否存在
//        ExecutedResult<MonitorVideoStreamingPO> checkExists = this.check4Id(id);
//        if (checkExists.isFailed()) {
//            return ExecutedResult.failed(checkExists.getMsg());
//        }
//        MonitorVideoStreamingPO item = new MonitorVideoStreamingPO();
//        item.setId(id);
//        item.setStatus(EState.DISABLED.getValue());
//
//        int rowCount = mapper.updateById(item);
//        if (rowCount != 1) {
//           return ExecutedResult.failed("停用[监控视频播放流]失败。");
//       }
//       return ExecutedResult.success();
//   }
//
//    public ExecutedResult<String> enable(Long id) {
//        // 验证记录是否存在
//        ExecutedResult<MonitorVideoStreamingPO> checkExists = this.check4Id(id);
//        if (checkExists.isFailed()) {
//            return ExecutedResult.failed(checkExists.getMsg());
//        }
//        MonitorVideoStreamingPO item = new MonitorVideoStreamingPO();
//        item.setId(id);
//        item.setStatus(EState.NORMAL.getValue());
//
//        int rowCount = mapper.updateById(item);
//        if (rowCount != 1) {
//            return ExecutedResult.failed("启用[监控视频播放流]失败。");
//        }
//        return ExecutedResult.success();
//    }
//
//    public ExecutedResult<String> setSort(ReqSetSort request) {
//        // 验证记录是否存在
//        ExecutedResult<MonitorVideoStreamingPO> checkExists = this.check4Id(request.getId());
//        if (checkExists.isFailed()) {
//            return ExecutedResult.failed(checkExists.getMsg());
//        }
//        MonitorVideoStreamingPO item = new MonitorVideoStreamingPO();
//        item.setId(request.getId());
//        item.setSort(request.getSort());
//
//        int rowCount = mapper.updateById(item);
//        if (rowCount != 1) {
//            return ExecutedResult.failed("设置[监控视频播放流]排序值失败。");
//        }
//        return ExecutedResult.success();
//    }
//
//    public ExecutedResult<String> listSetSort(ReqListSetSort request) {
//        // id列表
//        List<Long> listId = request.getList().stream().map(ReqSetSort::getId).collect(Collectors.toList());
//        // 验证记录是否存在
//        ExecutedResult<List<MonitorVideoStreamingPO>> checkExists = this.check4Id(listId);
//        if (checkExists.isFailed()) {
//            return ExecutedResult.failed(checkExists.getMsg());
//        }
//
//        List<MonitorVideoStreamingPO> listUpdate = request.getList().stream()
//                .map(c -> {
//                    MonitorVideoStreamingPO item = new MonitorVideoStreamingPO();
//                    item.setId(c.getId());
//                    item.setSort(c.getSort());
//                    return item;
//                })
//                .collect(Collectors.toList());
//        Boolean result = mapper.modifyList(listUpdate);
//        if (result) {
//            return ExecutedResult.success();
//        }
//        return ExecutedResult.failed("[监控视频播放流]设置排序值失败");
//    }
//
//    public ExecutedResult<String> remove(Long id) {
//        Boolean result = mapper.deleteLogic(id);
//        if (BooleanUtils.isFalse(result)) {
//            return ExecutedResult.failed("删除[监控视频播放流]失败。");
//        }
//        return ExecutedResult.success();
//    }
//
//    public ExecutedResult<String> removeList(List<Long> ids) {
//        Boolean result = mapper.deleteLogic(ids);
//        if (BooleanUtils.isFalse(result)) {
//            return ExecutedResult.failed("删除[监控视频播放流]失败。");
//        }
//        return ExecutedResult.success();
//    }
public ExecutedResult<MonitorVideoStreamingPO> play(String deviceId,String channelId) throws IOException {
    MonitorVideoStreamingPO result=new MonitorVideoStreamingPO();
    ExecutedResult<String> token = getToken();
    if(token.isSuccess()&&StringUtil.isNotNullOrEmpty(token.getData())){
        ExecutedResult<String> executedResult = playStart(deviceId, channelId, token.getData());
        if(executedResult.isSuccess()&&StringUtil.isNotNullOrEmpty(executedResult.getData())){
            //查询数据是否存在
            result= mapper.getByDeviceId(deviceId, channelId);
            if(Objects.nonNull(result)){
                result.setState(EYesOrNo.YES.getValue());
                result.setUrl(executedResult.getData());
                result.setBeginTime(LocalDateTimeUtil.nowTimeStamp());
                result.setEndTime(result.getBeginTime()+ ConstantFactory.TIME_LEN_1MIN * 3L);
                result.setRemark("正在点播");
                mapper.updateById(result);
            }else {
                result=new MonitorVideoStreamingPO();
                result.setDeviceId(deviceId);
                result.setChannelId(channelId);
                result.setUrl(executedResult.getData());
                result.setState(EYesOrNo.YES.getValue());
                result.setRemark("正在点播");
                result.setBeginTime(LocalDateTimeUtil.nowTimeStamp());
                result.setEndTime(result.getBeginTime()+ ConstantFactory.TIME_LEN_1MIN * 3L);
                result.setCreateTime(LocalDateTimeUtil.nowTimeStamp());
                mapper.insert(result);
            }
 
        }
    }
    return ExecutedResult.success(result);
}
    public ExecutedResult<MonitorVideoStreamingPO> stop(String deviceId,String channelId,String remark) throws IOException{
        ExecutedResult<String> token = getToken();
        if(token.isSuccess()&&StringUtil.isNotNullOrEmpty(token.getData())){
            String httpsURL = "http://120.71.144.117:18097/api/play/stop/"+deviceId+'/'+channelId;
            URL myURL = new URL(httpsURL);
            // 打开连接
            HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
            // 设置请求方法为GET
            conn.setRequestMethod("GET");
            // 设置通用的请求属性
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("access-token",token.getData());
            // 建立连接
            conn.connect();
            // 读取响应
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String inputLine;
            StringBuilder content = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();
            conn.disconnect();
            String data = content.toString();
            Gson gson = new Gson();
            JsonObject jsonObject = gson.fromJson(data, JsonObject.class);
            String code= jsonObject.get("code").getAsString();
            if(code.equals("0")){
                MonitorVideoStreamingPO streamingPO = mapper.getByDeviceId(deviceId, channelId);
                if(Objects.nonNull(streamingPO)){
                    streamingPO.setState(EYesOrNo.NO.getValue());
                    streamingPO.setEndTime(LocalDateTimeUtil.nowTimeStamp());
                    streamingPO.setRemark("手动关闭点播");
                    mapper.updateById(streamingPO);
                }
                return ExecutedResult.success(streamingPO);
            }else {
                return ExecutedResult.failed("调用第三方接口失败");
            }
        }
        return ExecutedResult.failed("获取token失败");
    }
    public ExecutedResult<String> playStart(String deviceId,String channelId,String token) throws IOException {
        String httpsURL = "http://120.71.144.117:18097/api/play/start/"+deviceId+'/'+channelId;
        URL myURL = new URL(httpsURL);
        // 打开连接
        HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
        // 设置请求方法为GET
        conn.setRequestMethod("GET");
        // 设置通用的请求属性
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("access-token",token);
        // 建立连接
        conn.connect();
        // 读取响应
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
        String inputLine;
        StringBuilder content = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            content.append(inputLine);
        }
        in.close();
        conn.disconnect();
        String data = content.toString();
        Gson gson = new Gson();
        JsonObject jsonObject = gson.fromJson(data, JsonObject.class);
        String code= jsonObject.get("code").getAsString();
        if(code.equals("0")){
            JsonObject asJsonArray = jsonObject.get("data").getAsJsonObject();
            String httpsFlv = asJsonArray.get("https_flv").getAsString();
            return ExecutedResult.success(httpsFlv);
        }
        return ExecutedResult.failed("请求第三方接口返回失败");
    }
    public ExecutedResult<String> getToken() throws IOException {
        //获取缓存中token
        String token = redisTemplate.opsForValue().get("accessToken");
        if(StringUtil.isNotNullOrEmpty(token)){
            return ExecutedResult.success(token);
        }else {
            String userName="";
            String password="";
            ExecutedResult<List<SysDictDataVO>> wvpLogin = sysDictDataService.getListByType("wvp_login");
            if(wvpLogin.isSuccess()){
                List<SysDictDataVO> data = wvpLogin.getData();
                userName=data.stream().filter(x->x.getDictValue().equals("username")).findFirst().orElse(null).getDictLabel();
                password=data.stream().filter(x->x.getDictValue().equals("password")).findFirst().orElse(null).getDictLabel();
            }
            String httpsURL = "http://120.71.144.117:18097/api/user/login?username="+userName+"&password="+password; // 第三方接口URL
            URL myURL = new URL(httpsURL);
            // 打开连接
            HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
            // 设置请求方法为GET
            conn.setRequestMethod("GET");
            // 设置通用的请求属性
            conn.setRequestProperty("Content-Type", "application/json");
            // 建立连接
            conn.connect();
            // 读取响应
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String inputLine;
            StringBuilder content = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();
            conn.disconnect();
            String data = content.toString();
            Gson gson = new Gson();
            JsonObject jsonObject = gson.fromJson(data, JsonObject.class);
            JsonObject asJsonArray = jsonObject.get("data").getAsJsonObject();
            String accessToken = asJsonArray.get("accessToken").getAsString();
            if(StringUtil.isNotNullOrEmpty(accessToken)){
                redisTemplate.opsForValue().set("accessToken",accessToken);
                redisTemplate.expire("accessToken",24, TimeUnit.HOURS);
                return ExecutedResult.success(accessToken);
            }
        }
        return ExecutedResult.failed("token不存在");
    }
    public ExecutedResult<List<MonitorVideoStreamingVO>> getList(List<Long> listId) {
        List<MonitorVideoStreamingVO> result = new ArrayList<>();
 
        List<MonitorVideoStreamingPO> list = mapper.getList(listId);
        if (ListUtil.isNotNullOrEmpty(list)) {
            // 转换vo
            result = MonitorVideoStreamingConvert.INSTANCE.toVo(list);
        }
        return ExecutedResult.success(result);
    }
    public ExecutedResult<MonitorVideoStreamingPO> updateEndTime(String deviceId, String channelId) {
        MonitorVideoStreamingPO find = mapper.getByDeviceId(deviceId, channelId);
        if (Objects.isNull(find)) {
            return ExecutedResult.failed("视频信息不存在");
        }
 
        int rowCount = mapper.updateEndTime(find.getId());
        if (rowCount == 1) {
            return ExecutedResult.success();
        }
        return ExecutedResult.failed("点播续时失败");
    }
    public ExecutedResult<PagerResult<MonitorVideoStreamingVO>> search(SearchMonitorVideoStreaming search) {
        // 处理创建时间范围-查询参数
        Tuple<String, String> createTimeRange = ParameterUtil.getTimeRange(search.getCreateTimeRange());
        if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem1())) {
            search.setCreateTimeStart(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem1()).getTime());
        }
        if (StringUtil.isNotNullOrEmpty(createTimeRange.getItem2())) {
            search.setCreateTimeEnd(LocalDateTimeUtil.getTimeStamp(createTimeRange.getItem2()).getTime());
        }
 
        PagerResult<MonitorVideoStreamingPO> pageList = mapper.search(search);
        List<MonitorVideoStreamingVO> listVo = new ArrayList<>();
        List<MonitorVideoStreamingPO> list = pageList.getList();
        if (ListUtil.isNotNullOrEmpty(list)) {
            pageList.setLastId(list.get(list.size() - 1).getId());
            // 转换vo
            listVo = MonitorVideoStreamingConvert.INSTANCE.toVo(list);
        }
        PagerResult<MonitorVideoStreamingVO> result = new PagerResult<>(pageList.getLimit(), pageList.getPage(), pageList.getTotal(), listVo);
        result.setLastId(pageList.getLastId());
        return ExecutedResult.success(result);
    }
 
    protected ExecutedResult<MonitorVideoStreamingPO> check4Id(Long id) {
        MonitorVideoStreamingPO exists = mapper.get(id);
        if (Objects.isNull(exists)) {
            return ExecutedResult.failed("[监控视频播放流]不存在:" + id);
        }
        return ExecutedResult.success(exists);
    }
    protected ExecutedResult<List<MonitorVideoStreamingPO>> check4Id(List<Long> listId) {
        // 从数据库查找监控视频播放流
        List<MonitorVideoStreamingPO> list = mapper.getList(listId);
        if (ListUtil.isNullOrEmpty(list)) {
            return ExecutedResult.failed("[监控视频播放流]不存在." + listId);
        }
        // 数据库找到的id列表
        List<Long> listIdFind = list.stream().map(MonitorVideoStreamingPO::getId).collect(Collectors.toList());
        // 数量不一致
        if (listId.size() != listIdFind.size()) {
            // 筛选数据库不存在的监控视频播放流
            List<Long> listIdNotFound = listId.stream().filter(c -> !listIdFind.contains(c)).collect(Collectors.toList());
            if (ListUtil.isNullOrEmpty(list)) {
                return ExecutedResult.failed("[监控视频播放流]不存在." + listIdNotFound);
            }
        }
        return ExecutedResult.success(list);
    }}