2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-07-22 77edfb324184d304ae7763aae3ab609f2b495da4
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package com.sandu.ximon.admin.service;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.gson.Gson;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.file.FileUploadDto;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.config.RealtimeServerBean;
import com.sandu.ximon.admin.entity.*;
import com.sandu.ximon.admin.param.PoleBindingParam;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.*;
import com.sandu.ximon.admin.vo.EquipmentInfomation;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.domain.PoleLightemitEntity;
import com.sandu.ximon.dao.enums.OrderByEnums;
import com.sandu.ximon.dao.enums.PoleBindingEnums;
import com.sandu.ximon.dao.mapper.PoleLightemitEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.*;
 
/**
 * 熙讯设备
 */
@Service
@EnableCaching
public class PoleLightemitService extends BaseServiceImpl<PoleLightemitEntityMapper, PoleLightemitEntity> {
 
    @Autowired
    private PoleBindingService poleBindingService;
 
    LightemitUtils lightemitUtils;
    @Autowired
    private RealtimeServerBean realtimeServerBean;
    @Value("${server.port}")
    private String port;
    @Autowired
    ApplicationContext applicationContext;
 
    @Autowired
    PoleLightemitEntityMapper poleLightemitDao;
 
    @PostConstruct
    public void init() {
        //重新获取单例
        lightemitUtils = applicationContext.getBean(LightemitUtils.class);
    }
 
    /**
     * 熙讯list
     *
     * @param keyword
     * @param isOnLine
     * @return
     */
    public List<PoleLightemitEntity> listLed(String keyword, Integer order, Integer seq, boolean isOnLine) {
        List<PoleLightemitEntity> poleLightemitEntityList;
        //排序字段
        String orderByResult = "id";
        //正序、倒叙
        String orderBySeq = OrderByEnums.ASC.getCode();
        if (order != null) {
            switch (order) {
                case 1:
                    orderByResult = OrderByEnums.LED_S_CODE.getCode();
                    break;
                case 2:
                    orderByResult = OrderByEnums.LED_S_NAME.getCode();
                    break;
                case 3:
                    orderByResult = OrderByEnums.LED_S_POLE_NAME.getCode();
                    break;
                case 4:
                    orderByResult = OrderByEnums.LED_S_CREATE_TIME.getCode();
                    break;
                default:
            }
        }
        if (seq != null) {
            switch (seq) {
                case 1:
                    orderBySeq = " ASC";
                    break;
                case 2:
                    orderBySeq = " DESC";
                    break;
                default:
                    break;
            }
        }
        //排序方式
        String orderBy = orderByResult + " " + orderBySeq;
 
        //超管
        if (SecurityUtils.getClientId() == null) {
            poleLightemitEntityList = poleLightemitDao.listLed(keyword, null, orderBy);
        } else {
            poleLightemitEntityList = poleLightemitDao.listLed(keyword, SecurityUtils.getUserId(), orderBy);
        }
 
        List<PoleLightemitEntity> temp = new ArrayList<>();
        for (PoleLightemitEntity poleLightemitEntity : poleLightemitEntityList) {
            //查询设备在线状态
            boolean onLine = lightemitUtils.getLedOnLine(poleLightemitEntity.getLightemitControlCode());
            //查询屏幕的开启状态
            String isOpen = lightemitUtils.getIsScreenOpen(poleLightemitEntity.getLightemitControlCode());
            poleLightemitEntity.setIsOpen(String.valueOf(isOpen));
            poleLightemitEntity.setOnLine(onLine);
            if (!isOnLine || onLine) {
                temp.add(poleLightemitEntity);
            }
        }
        return temp;
    }
 
    /**
     * 熙讯设备列表(用于首页数据统计)
     *
     * @return
     */
    public List<PoleLightemitEntity> listLedOnHome() {
        List<PoleLightemitEntity> poleLightemitEntityList;
        //超管
        if (SecurityUtils.getClientId() == null) {
            poleLightemitEntityList = poleLightemitDao.listLed(null, null, null);
        } else {
            poleLightemitEntityList = poleLightemitDao.listLed(null, SecurityUtils.getUserId(), null);
        }
 
        return poleLightemitEntityList;
    }
 
 
    /**
     * 绑定使用
     *
     * @param keyword
     * @return
     */
    public List<PoleLightemitEntity> listLedOnBinding(String keyword) {
        List<PoleLightemitEntity> poleLightemitEntityList;
 
 
        //超管
        if (SecurityUtils.getClientId() == null) {
            poleLightemitEntityList = poleLightemitDao.listLedOnBinding(keyword, null);
        } else {
            poleLightemitEntityList = poleLightemitDao.listLedOnBinding(keyword, SecurityUtils.getUserId());
        }
 
        for (PoleLightemitEntity poleLightemitEntity : poleLightemitEntityList) {
            //查询设备在线状态
            boolean onLine = lightemitUtils.getLedOnLine(poleLightemitEntity.getLightemitControlCode());
            //查询屏幕的开启状态
            String isOpen = lightemitUtils.getIsScreenOpen(poleLightemitEntity.getLightemitControlCode());
            poleLightemitEntity.setIsOpen(String.valueOf(isOpen));
            poleLightemitEntity.setOnLine(onLine);
        }
        return poleLightemitEntityList;
    }
 
    /**
     * 取出在线的熙讯
     *
     * @param list
     * @return
     */
    public List<PoleLightemitEntity> ledOnline(List<PoleLightemitEntity> list) {
        List<PoleLightemitEntity> onLineList = new ArrayList<>();
        for (PoleLightemitEntity poleLightemitEntity : list) {
//            if(lightemitUtils.getIsScreenOpen(poleLightemitEntity.getLightemitControlCode()).contains("true")){
//                poleLightemitEntity.setIsOpen(lightemitUtils.getIsScreenOpen(poleLightemitEntity.getLightemitControlCode()).contains("true"));
//            }
            poleLightemitEntity.setIsOpen(lightemitUtils.getIsScreenOpen(poleLightemitEntity.getLightemitControlCode()));
            boolean ledOnLine = lightemitUtils.getLedOnLine(poleLightemitEntity.getLightemitControlCode());
            if (ledOnLine) {
                poleLightemitEntity.setOnLine(true);
                onLineList.add(poleLightemitEntity);
            } else {
                poleLightemitEntity.setOnLine(false);
            }
        }
        return onLineList;
    }
 
 
    /**
     * 设置在线状态
     *
     * @param list
     */
    public void ledOnlineCheck(List<PoleLightemitEntity> list) {
        for (PoleLightemitEntity poleLightemitEntity : list) {
            poleLightemitEntity.setIsOpen(lightemitUtils.getIsScreenOpen(poleLightemitEntity.getLightemitControlCode()));
            poleLightemitEntity.setOnLine(lightemitUtils.getLedOnLine(poleLightemitEntity.getLightemitControlCode()));
        }
    }
 
 
    /**
     * 查询单个LED
     *
     * @param lightControlCode
     * @return
     */
    public PoleLightemitEntity getLedByLightControlCode(String lightControlCode) {
        PoleLightemitEntity xiXun = getOne(Wrappers.lambdaQuery(PoleLightemitEntity.class).eq(PoleLightemitEntity::getLightemitControlCode, lightControlCode));
        Pole getpole = poleLightemitDao.getpole(lightControlCode);
        if (xiXun == null) {
            return null;
        }
        boolean isScreenOpen = lightemitUtils.getLedOnLine(xiXun.getLightemitControlCode());
 
        xiXun.setIsOpen(lightemitUtils.getIsScreenOpen(lightControlCode));
        xiXun.setOnLine(isScreenOpen);
        xiXun.setStreetlightName(getpole.getPoleName());
        xiXun.setStreetlightId(getpole.getId());
 
        return xiXun;
    }
 
    /**
     * 首页灯杆绑定信息
     */
    public EquipmentInfomation getLedByLightControlCodeInfo(String lightControlCode) {
        EquipmentInfomation equipmentInfo = new EquipmentInfomation();
        equipmentInfo.setEquipmentType("熙讯LED");
        if (lightControlCode == null || lightControlCode.trim().length() == 0) {
            return equipmentInfo;
        }
 
        PoleLightemitEntity xiXun = getOne(Wrappers.lambdaQuery(PoleLightemitEntity.class).eq(PoleLightemitEntity::getLightemitControlCode, lightControlCode));
        boolean isScreenOpen = false;
        if (xiXun != null) {
            equipmentInfo.setEquipmentCreateTime(xiXun.getCreateTime());
            equipmentInfo.setEquipmentMac(xiXun.getLightemitControlCode());
            equipmentInfo.setEquipmentName(xiXun.getLightemitName());
            isScreenOpen = lightemitUtils.getLedOnLine(xiXun.getLightemitControlCode());
        }
        if (isScreenOpen) {
            equipmentInfo.setEquipmentState("在线");
        } else {
            equipmentInfo.setEquipmentState("离线");
        }
 
        return equipmentInfo;
    }
 
 
    public void savePoleLightemit(PoleLightemitEntity poleLightemit) {
        boolean save = this.save(poleLightemit);
        // 绑定灯杆
 
        if (save && poleLightemit.getStreetlightId() != null) {
            PoleBindingParam poleBindingParam = new PoleBindingParam();
            poleBindingParam.setDeviceCode(poleLightemit.getLightemitControlCode());
            poleBindingParam.setDeviceType(1);
            poleBindingParam.setDeviceName(poleLightemit.getLightemitName());
            poleBindingService.bindPole(poleLightemit.getStreetlightId(), poleBindingParam);
        }
 
        /**
         * 熙汛led新增日志记录开始
         */
        List<String> listCode = new ArrayList<>();
        listCode.add(poleLightemit.getLightemitControlCode());
        String content = "{屏幕id:" + poleLightemit.getLightemitId() + ", 屏幕名称:" + poleLightemit.getLightemitName() + ", 屏幕编码:" + poleLightemit.getLightemitControlCode() + " }";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "熙汛LED新增", content);
        /**
         * 熙汛led新增日志记录结束
         */
        //  poleStreetlightLightemitService.saveStreetlightLightemit(poleLightemit.getLightemitId(),poleLightemit.getStreetlightId());
    }
 
 
    public void updatePoleLightemit(Long ledId, PoleLightemitEntity poleLightemit) {
        PoleLightemitEntity byId = getById(ledId);
        if (byId == null) {
            throw new BusinessException("未找到LED屏");
        }
 
        if (SecurityUtils.getClientId() != null) {
            boolean belong = SpringContextHolder.getBean(PoleBindingService.class).isBelong(byId.getLightemitControlCode(), PoleBindingEnums.XIXUN);
            if (!belong) {
                throw new BusinessException("该设备不属于您,不能修改设备信息");
            }
        }
        poleLightemit.setLightemitId(ledId);
        boolean b = this.updateById(poleLightemit);
        // 绑定灯杆
 
        if (b && poleLightemit.getStreetlightId() != null) {
            //先删除绑定关系
            poleBindingService.unBindPole(null, poleLightemit.getLightemitControlCode());
            //再绑定
            PoleBindingParam poleBindingParam = new PoleBindingParam();
            poleBindingParam.setDeviceCode(poleLightemit.getLightemitControlCode());
            poleBindingParam.setDeviceType(10);
            poleBindingParam.setDeviceName(poleLightemit.getLightemitName());
            poleBindingService.bindPole(poleLightemit.getStreetlightId(), poleBindingParam);
        }
 
        /**
         * 熙汛led编辑日志记录开始
         */
        List<String> listCode = new ArrayList<>();
        listCode.add(byId.getLightemitControlCode());
        String content = "{屏幕id:" + byId.getLightemitId() + ", 屏幕原名:" + byId.getLightemitName() + ", 屏幕名称:" + poleLightemit.getLightemitName() + ", 屏幕编码:" + byId.getLightemitControlCode() + " }";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "熙汛LED编辑", content);
        /**
         * 熙汛led编辑日志记录结束
         */
        // poleStreetlightLightemitService.saveStreetlightLightemit(poleLightemit.getLightemitId(),poleLightemit.getStreetlightId());
    }
 
    public boolean deletePoleLightemit(List<Long> ledIds) {
 
        List<PoleLightemitEntity> poleLightemitEntities = listByIds(ledIds);
        if (poleLightemitEntities != null && poleLightemitEntities.size() != 0) {
            throw new BusinessException("设备不存在");
        }
 
        List<String> listCode = new ArrayList<>();
        // 删除设备绑定
        if (poleLightemitEntities != null && poleLightemitEntities.size() != 0) {
            for (PoleLightemitEntity poleLightemitEntitie : poleLightemitEntities) {
                if (poleLightemitEntitie.getLightemitControlCode() != null) {
                    //删除绑定关系//先删除绑定关系
                    listCode.add(poleLightemitEntitie.getLightemitControlCode());
                    poleBindingService.unBindPole(poleLightemitEntitie.getLightemitControlCode());
                }
            }
        }
        //删除设备
        boolean b = removeByIds(ledIds);
 
        /**
         * 熙汛led删除日志记录开始
         */
        List<String> nameList = new ArrayList<>();
        for (PoleLightemitEntity poleLightemitEntitie : poleLightemitEntities) {
            nameList.add(poleLightemitEntitie.getLightemitName());
        }
 
        String content = "{删除熙汛LED的设备信息:" + nameList.toString() + " }";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "熙汛LED删除", content);
        /**
         * 熙汛led删除日志记录结束
         */
        return b;
        // poleStreetlightLightemitService.saveStreetlightLightemit(poleLightemit.getLightemitId(),poleLightemit.getStreetlightId());
    }
 
    /**
     * led上传图片
     *
     * @param lightemitControlCode
     * @param path
     * @param filenameList
     * @param userName
     * @throws IOException
     */
    @Async("taskExecutor")
    public void photoUpload(String lightemitControlCode, String path, List<String> filenameList, String userName) throws IOException {
        //获取屏幕宽
        String screenWidth = lightemitUtils.getScreenWidth(lightemitControlCode);
        String screenHeight = lightemitUtils.getScreenHeight(lightemitControlCode);
 
        //html地址
        String filenameTemp = path + userName + "_" + lightemitControlCode + ".html";
        File htmlFile = new File(filenameTemp);
        if (!htmlFile.exists() || htmlFile.delete()) {
            htmlFile.createNewFile();
        }
 
        //获取body的html
        Map<String, Object> params = new HashMap<>();
        params.put("imgList", filenameList);
        params.put("screenWidth", screenWidth);
        params.put("screenHeight", screenHeight);
 
        String body = new HtmlTemplateUtils().renderPath("classpath:templates/lightemitTemplates/photoTemplates.html", params);
 
        boolean flag = new FileUtil().writeToFile(body, filenameTemp);
 
        if (flag) {
            //清屏操作
            lightemitUtils.clear(lightemitControlCode);
            //清除播放列表
            lightemitUtils.clearVideoPlay(lightemitControlCode);
            //清除节目列表
            lightemitUtils.clearPlayerTask(lightemitControlCode);
            //推送图片
            lightemitUtils.postHtml(lightemitControlCode, userName);
        }
    }
 
    /**
     * led上传视频
     *
     * @param lightemitControlCode
     */
    @Async("taskExecutor")
    public String videoUpload(String lightemitControlCode, FileUploadDto fileUploadDto, Integer type) {
        //获取屏幕宽
        String screenWidth = lightemitUtils.getScreenWidth(lightemitControlCode);
        String screenHeight = lightemitUtils.getScreenHeight(lightemitControlCode);
 
        //发送视频至开发板并播放
        String fileUrl = fileUploadDto.getFileUrl();
        String[] split = fileUrl.split("/");
        String id = split[split.length - 1];
        String result = this.videoXixunPlayer(lightemitControlCode, id, fileUploadDto.getFileSize(), fileUploadDto.getMd5(), type, fileUploadDto.getFileType(), 999999);
 
        LogUtils.error("上传播放结果:" + result);
        return result;
    }
 
 
    @Async("taskExecutor")
    public String videoXixunPlayer(String lightemitControlCode, String filename, Long filesize, String md5, Integer type, String fileType, Integer videoTime) {
        //获取屏幕宽
        Integer screenWidth = Integer.valueOf(lightemitUtils.getScreenWidth(lightemitControlCode));
        Integer screenHeight = Integer.valueOf(lightemitUtils.getScreenHeight(lightemitControlCode));
        Source source = new Source();
        Layer layer = new Layer();
        Program pro = new Program();
        TaskItem items = new TaskItem();
        ProgramsTask task = new ProgramsTask();
        Command command = new Command();
        XixunPlayer xixun = new XixunPlayer();
        //所有_id都可用UUID随机生成
        source.set_id(UUID.randomUUID().toString());
        //请求头拼接该ID为该资源的完整下载地址
        source.setId(filename);
        if (type == 1) {
            //设置资源类型,其他资源类型请参考xixunplayer节目json说明文档
            source.set_type("Video");
            source.setMime("video/mp4");
        } else {
            source.set_type("Image");
            source.setMime("image/jpeg");
        }
        //资源后缀名
        source.setFileExt(fileType);
        //资源高度
        source.setHeight(screenHeight);
        //资源宽度
        source.setWidth(screenWidth);
        //距左
        source.setLeft(0);
        source.setMd5(md5);
        source.setName(filename);
        source.setPlayTime(0);    //播放起始时间
        source.setSize(filesize);    //资源字节数,要精准
        source.setTimeSpan(videoTime);        //该资源播放时长
        source.setTop(0);            //距顶
        List<Source> list = new ArrayList<Source>();
        list.add(source);
        layer.setSources(list);
        pro.set_id(UUID.randomUUID().toString());
        pro.setHeight(screenHeight);        //节目高度
        List<Layer> list1 = new ArrayList<Layer>();
        list1.add(layer);
        pro.setLayers(list1);
        pro.setName("文件上传播放");    //节目名称
        pro.setOthers(true);//如果为true,则该节目来自第三方接口,不是来自我们自己的web,二次开发请务必赋值为true
        pro.setTotalSize(filesize);    //所有资源总字节数
        pro.setVersion(0);            //高级节目=0,简易节目=2
        pro.setWidth(screenWidth);        //节目宽度
        items.set_id(UUID.randomUUID().toString());
        items.set_program(pro);
        items.setRepeatTimes(1);
        items.setSchedules(null);//定时段,不做定时可为null
        task.set_id(UUID.randomUUID().toString());
        task.setName("这是一个示例");
        List<TaskItem> list2 = new ArrayList<TaskItem>();
        list2.add(items);
        task.setItems(list2);
        command.setId(UUID.randomUUID().toString());
        //这里是下方的post回调地址,需要修改IP地址
        command.setNotificationURL("");
        //资源下载链接的请求头
        command.setPreDownloadURL("https://ximonsmart.oss-cn-shanghai.aliyuncs.com/");
        command.setTask(task);
        xixun.set_id(UUID.randomUUID().toString());
        xixun.setCommand(command);
        xixun.setType("commandXixunPlayer");    //命令固定类型,不可更改
        String jsondata = new Gson().toJson(xixun);
        String clearResult = lightemitUtils.clear(lightemitControlCode);
        if (clearResult.contains("does not exist")) {
            return "推送失败";
        } else {
            poleLightemitDao.updateRequestBody(lightemitControlCode, jsondata);
            String post = lightemitUtils.post(realtimeServerBean.getCommand() + lightemitControlCode, jsondata);
            LogUtils.error("结果:" + post);
            //{"_type":"success","_id":"ce8dc3ff-dc88-43b7-8f55-60abd8700f1a","timestamp":1653555160535}
            if (post.startsWith("{") && post.endsWith("}") && post.contains("_type\":\"success")) {
                return "推送成功";
            } else {
                return "推送失败";
            }
 
        }
    }
 
    /**
     * 更新请求数据
     *
     * @param ledCode
     * @param postBody
     */
    public void updateRequestBody(String ledCode, String postBody) {
        baseMapper.updateRequestBody(ledCode, postBody);
    }
 
 
    /**
     * 根据led屏编码设置音量
     *
     * @param lightemitControlCode
     * @param volume
     */
    @Async("taskExecutor")
    public void setVolume(String lightemitControlCode, Integer volume) {
        lightemitUtils.setVoiume(lightemitControlCode, volume);
    }
 
    /**
     * 根据led屏编码设置亮度
     *
     * @param lightemitControlCode
     * @param brightness
     */
    @Async("taskExecutor")
    public void setBrightness(String lightemitControlCode, Integer brightness) {
        lightemitUtils.setBrightness(lightemitControlCode, brightness);
    }
 
}