2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-05-27 75072002f353a34ca0de4f8aaad04c8d1841407c
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
package com.sandu.ximon.admin.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.admin.config.RealtimeServerBean;
import com.sandu.ximon.admin.entity.*;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5AtmosphereHeartbeatReportInnerFrame;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.JsonUtil;
import com.sandu.ximon.admin.utils.LightemitUtils;
import com.sandu.ximon.admin.utils.LogUtils;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.admin.utils.request.SubTitleSet;
import com.sandu.ximon.dao.domain.LedSFile;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.domain.PoleLightemitEntity;
import com.sandu.ximon.dao.domain.PoleXixunPlayerEntity;
import com.sandu.ximon.dao.enums.OrderByEnums;
import com.sandu.ximon.dao.mapper.PoleXixunPlayerEntityMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * 熙讯节目
 */
@Service
@AllArgsConstructor
public class XiXunPlayerService extends BaseServiceImpl<PoleXixunPlayerEntityMapper, PoleXixunPlayerEntity> {
 
    private final LightemitUtils lightemitUtils;
    private final RealtimeServerBean realtimeServerBean;
    private final PoleLightemitService poleLightemitService;
    private final ClientService clientService;
    private final LedSFileService xiXunFileService;
 
    //熙讯节目列表
    public LambdaQueryWrapper<PoleXixunPlayerEntity> XixunPlayerList(String keyword) {
        if (SecurityUtils.getClientId() == null) {
            if (keyword != null && keyword.isEmpty()) {
                return Wrappers.lambdaQuery(PoleXixunPlayerEntity.class);
            } else {
                return Wrappers.lambdaQuery(PoleXixunPlayerEntity.class).like(PoleXixunPlayerEntity::getProgramName, keyword);
            }
 
        } else {
            if (keyword != null && keyword.isEmpty()) {
                return Wrappers.lambdaQuery(PoleXixunPlayerEntity.class).eq(PoleXixunPlayerEntity::getCreateUserId, SecurityUtils.getUserId())
                        .or(w -> {
                            w.eq(PoleXixunPlayerEntity::getClientId, SecurityUtils.getClientId());
                        });
            } else {
                return Wrappers.lambdaQuery(PoleXixunPlayerEntity.class).eq(PoleXixunPlayerEntity::getCreateUserId, SecurityUtils.getUserId())
                        .or(w -> {
                            w.eq(PoleXixunPlayerEntity::getClientId, SecurityUtils.getClientId());
                        }).like(PoleXixunPlayerEntity::getProgramName, keyword);
            }
        }
    }
 
    public List<PoleXixunPlayerEntity> listProgram(BaseConditionVO baseConditionVO, Integer order, Integer seq, String keyword) {
        LambdaQueryWrapper<PoleXixunPlayerEntity> wrapper = XixunPlayerList(keyword);
        //排序字段
        String orderByResult = "program_id";
        //正序、倒叙
        String orderBySeq = OrderByEnums.ASC.getCode();
        if (order != null) {
            switch (order) {
                case 1:
                    orderByResult = OrderByEnums.LED_S_PROGRAM_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;
        PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize(), orderBy);
        return list(wrapper);
    }
 
    public boolean insert(ProgramPro programPro) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        String json = gson.toJson(programPro);
        PoleXixunPlayerEntity poleXixunPlayer = new PoleXixunPlayerEntity();
        poleXixunPlayer.setProgramCode(programPro.get_id());
        poleXixunPlayer.setProgramName(programPro.getName());
        poleXixunPlayer.setHeight(programPro.getHeight());
        poleXixunPlayer.setWidth(programPro.getWidth());
        poleXixunPlayer.setTotalSize((float) programPro.getTotalSize() / 1000000 + "MB");
        poleXixunPlayer.setRequestBody(json);
        poleXixunPlayer.setScreenShot(programPro.getScreenShot());
        if (SecurityUtils.getClientId() != null) {
            poleXixunPlayer.setCreateUserId(SecurityUtils.getUserId());
            if (clientService.findClientId()) {
                poleXixunPlayer.setClientId(clientService.getClientId());
            }
        }
        boolean save = this.save(poleXixunPlayer);
        /**
         * 熙汛节目新增日志记录开始
         */
        String content = "{节目id:" + poleXixunPlayer.getProgramId()
                + ", 节目名称:" + poleXixunPlayer.getProgramName()
                + ", 节目内容:" + json
                + " }";
        StoreOperationRecordsUtils.storeOperationData(null, null, "熙汛节目添加", content);
        /**
         * 熙汛节目新增日志记录结束
         */
        return save;
    }
 
    /**
     * 编辑
     *
     * @param programPro
     * @return
     */
    public boolean update(ProgramPro programPro) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        String json = gson.toJson(programPro);
//        String jsondata = JsonUtil.jsonObj2Sting(programPro);
        PoleXixunPlayerEntity poleXixunPlayer = new PoleXixunPlayerEntity();
        poleXixunPlayer.setProgramCode(programPro.get_id());
        poleXixunPlayer.setProgramName(programPro.getName());
        poleXixunPlayer.setHeight(programPro.getHeight());
        poleXixunPlayer.setWidth(programPro.getWidth());
        poleXixunPlayer.setTotalSize((float) programPro.getTotalSize() / 1000000 + "MB");
        poleXixunPlayer.setRequestBody(json);
        poleXixunPlayer.setScreenShot(programPro.getScreenShot());
        if (SecurityUtils.getClientId() != null) {
            poleXixunPlayer.setCreateUserId(SecurityUtils.getUserId());
            if (clientService.findClientId()) {
                poleXixunPlayer.setClientId(clientService.getClientId());
            }
        }
        poleXixunPlayer.setProgramId(programPro.getProgramId());
        boolean updateResult = updateById(poleXixunPlayer);
        /**
         * 熙汛节目新增日志记录开始
         */
        String content = "{节目id:" + poleXixunPlayer.getProgramId()
                + ", 节目名称:" + poleXixunPlayer.getProgramName()
                + ", 节目内容:" + json
                + " }";
        StoreOperationRecordsUtils.storeOperationData(null, null, "熙汛节目编辑", content);
        /**
         * 熙汛节目新增日志记录结束
         */
        return updateResult;
    }
 
    public boolean deleteProgram(Long pid) {
        PoleXixunPlayerEntity byId = getById(pid);
        if (byId == null) {
            throw new BusinessException("未找到该节目");
        }
        /**
         * 熙汛节目删除日志记录开始
         */
        String content = "{删除的节目id:" + byId.getProgramId()
                + ", 删除的节目名称:" + byId.getProgramName()
                + " }";
        StoreOperationRecordsUtils.storeOperationData(null, null, "熙汛节目删除", content);
        /**
         * 熙汛节目删除日志记录结束
         */
        return removeById(pid);
    }
 
    public Object getByPid(Long pid) {
        PoleXixunPlayerEntity byId = getById(pid);
        List<Long> fileIds = new ArrayList<>();
        List<LedSFile> file = new ArrayList<>();
        if (byId == null) {
            throw new BusinessException("未找到该节目");
        }
        String json = byId.getRequestBody();
        ProgramPro programPro = new ProgramPro();
        try {
            Gson gson = new GsonBuilder().disableHtmlEscaping().create();
//            programPro = JsonUtil.convertJsonStringToObject(json, ProgramPro.class);
            programPro = gson.fromJson(json, ProgramPro.class);
            programPro.setProgramId(pid);
            programPro.setScreenShot(byId.getScreenShot());
            programPro.getLayers().forEach(
                    layerPro -> {
                        layerPro.getSources().forEach(
                                sourcePro -> {
                                    long fileId = Long.parseLong(sourcePro.getId());
                                    file.add(xiXunFileService.getById(fileId));
                                }
                        );
                    }
            );
            programPro.setFileList(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return programPro;
    }
 
 
    /**
     * 推送节目
     *
     * @param programId
     * @param lightemitIds
     */
    public List<Map> videoXixunPlayer(long programId, List<Long> lightemitIds) {
        //记录操作结果
        List<Map> mapList = new ArrayList<>();
 
 
        ItemPro items = new ItemPro();
        TaskPro taskPro = new TaskPro();
        CommandPro command = new CommandPro();
        XixunPlayerPro xixun = new XixunPlayerPro();
 
        PoleXixunPlayerEntity poleXixunPlayerEntity;
        items.set_id(UUID.randomUUID().toString());
        QueryWrapper<PoleXixunPlayerEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("program_id", programId);
        poleXixunPlayerEntity = this.getOne(queryWrapper);
        if (poleXixunPlayerEntity == null) {
            throw new BusinessException("节目数据异常!");
        }
 
        String json = poleXixunPlayerEntity.getRequestBody();
        if (StringUtils.isEmpty(json)) {
            throw new BusinessException("节目数据异常!");
        }
 
        ProgramPro pro;
        try {
            pro = JsonUtil.convertJsonStringToObject(json, ProgramPro.class);
            pro.getLayers().forEach(layerPro -> {
                layerPro.getSources().forEach(sourcePro -> {
                    long fileId = Long.parseLong(sourcePro.getId());
                    LedSFile file = xiXunFileService.getById(fileId);
                    if(file != null){
                        String fileUrl = file.getFileUrl();
                        String[] split = fileUrl.split("/");
                        sourcePro.setId(split[split.length - 1]);
                        sourcePro.setMd5(file.getMd5());
                    }
                });
            });
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("节目数据异常!");
        }
        items.set_program(pro);
        items.setRepeatTimes(1);
        //定时段,不做定时可为null
        items.setSchedulePros(null);
 
        taskPro.set_id(UUID.randomUUID().toString());
        taskPro.setName(poleXixunPlayerEntity.getProgramName());
 
        List<ItemPro> list2 = new ArrayList<>();
        list2.add(items);
        taskPro.setItems(list2);
 
        command.setId(UUID.randomUUID().toString());
        //这里是下方的post回调地址,需要修改IP地址
        command.setNotificationURL("");
        //资源下载链接的请求头,阿里云图片下载地址头
        command.setPreDownloadURL("https://ximonsmart.oss-cn-shanghai.aliyuncs.com/");
        command.setTask(taskPro);
 
        xixun.set_id(UUID.randomUUID().toString());
        xixun.setCommand(command);
        //命令固定类型,不可更改
        xixun.setType("commandXixunPlayer");
 
 
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        String jsondata = gson.toJson(xixun);
 
        Collection<PoleLightemitEntity> poleLightemitEntities = poleLightemitService.listByIds(lightemitIds);
 
        if (poleLightemitEntities != null) {
            Map map;
            for (PoleLightemitEntity entity : poleLightemitEntities) {
                map = new LinkedHashMap();
                String clearResult = lightemitUtils.clear(entity.getLightemitControlCode());
                if (clearResult.contains("does not exist")) {
                    map.put("LightemitId", entity.getLightemitId());
                    map.put("Result", "失败");
                    mapList.add(map);
                } else {
                    poleLightemitService.updateRequestBody(entity.getLightemitControlCode(), jsondata);
                    String post = lightemitUtils.post(realtimeServerBean.getCommand() + entity.getLightemitControlCode(), jsondata);
                    //{"_type":"success","_id":"ce8dc3ff-dc88-43b7-8f55-60abd8700f1a","timestamp":1653555160535}
                    if (post.startsWith("{") && post.endsWith("}") && post.contains("_type\":\"success")) {
                        map.put("LightemitId", entity.getLightemitId());
                        map.put("Result", "成功");
                        mapList.add(map);
                    } else {
                        map.put("LightemitId", entity.getLightemitId());
                        map.put("Result", "失败");
                        mapList.add(map);
                    }
                    LogUtils.error("结果:" + post);
                }
            }
        }
 
 
        /**
         * 熙汛节目推送日志记录开始
         */
        List<String> listCode = new ArrayList<>();
        for (PoleLightemitEntity bean : poleLightemitEntities) {
            listCode.add(bean.getLightemitControlCode());
        }
        String content = "{节目id:" + programId
                + ", 节目名称:" + poleXixunPlayerEntity.getProgramName()
                + ", 节目内容:" + json
                + " }";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "熙汛节目推送", content);
        /**
         * 熙汛节目推送日志记录结束
         */
 
        return mapList;
    }
 
 
    /**
     * 推送大气数据到熙讯LED
     */
    @Async("taskExecutor")
    public Boolean pushWeather(String lightemitControlCode, A5AtmosphereHeartbeatReportInnerFrame.HeartBeatDataPackage sensorEntity, Pole streetlightEntity) {
        if (StringUtils.isBlank(lightemitControlCode)) {
            return Boolean.valueOf(false);
        }
        Integer screenHeight = Integer.parseInt(lightemitUtils.getScreenHeight(lightemitControlCode));
        String size = String.valueOf(screenHeight / 18);
        //字幕主体拼接
        String body =
                "<head><style type=\"text/css\">body{background-color:#000;}</style></head>" +
                        "<p style=\"font-size:" + size + "px;line-height:17px;color:#fff\">" +
                        (streetlightEntity == null || streetlightEntity.getCity() == null ? "" : streetlightEntity.getCity().replace("市", "")) + "<br/>" +
                        "温度:" + (sensorEntity == null || sensorEntity.getTemperature() == null ? "   -" : sensorEntity.getTemperature()) + "℃<br/>" +
                        "湿度:" + (sensorEntity == null || sensorEntity.getHumidity() == null ? "-" : sensorEntity.getHumidity()) + "%RH<br/>" +
                        "亮度:" + (sensorEntity == null || sensorEntity.getLightIntensity() == null ? "-" : sensorEntity.getLightIntensity()) + "lux<br/>" +
                        "风速:" + (sensorEntity == null || sensorEntity.getWindSpeed() == null ? "-" : sensorEntity.getWindSpeed()) + "m/s<br/>" +
                        "CO2:" + (sensorEntity == null || sensorEntity.getCo2() == null ? "-" : sensorEntity.getCo2()) + "ppm<br/>" +
                        "甲醛:" + (sensorEntity == null || sensorEntity.getEch2o() == null ? "-" : sensorEntity.getEch2o()) + "mg/m³<br/>" +
                        "TVOC:" + (sensorEntity == null || sensorEntity.getTvoc() == null ? "-" : sensorEntity.getTvoc()) + "mg/m³<br/>" +
                        "PM25:" + (sensorEntity == null || sensorEntity.getPm25() == null ? "-" : sensorEntity.getPm25()) + "mg/m³<br/>" +
                        "PM10:" + (sensorEntity == null || sensorEntity.getPm10() == null ? "-" : sensorEntity.getPm10()) + "mg/m³<br/>";
 
        //组装请求参数
        SubTitleSet subTitleSet = new SubTitleSet();
        subTitleSet.html = body;
        subTitleSet.num = 1;
 
        //清屏操作
        lightemitUtils.clear(lightemitControlCode);
        //清除播放列表
        lightemitUtils.clearVideoPlay(lightemitControlCode);
        //清除节目列表
        lightemitUtils.clearPlayerTask(lightemitControlCode);
        try {
            lightemitUtils.subTitleSet(subTitleSet, lightemitControlCode, false);
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            //一分钟后清除屏幕
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    lightemitUtils.clear(lightemitControlCode);
                    //清除播放列表
                    lightemitUtils.clearVideoPlay(lightemitControlCode);
                    //清除节目列表
                    lightemitUtils.clearPlayerTask(lightemitControlCode);
                    //推送最后命令
                    lightemitUtils.sendLastCommand(lightemitControlCode);
 
                    /**
                     * 熙汛大气数据推送日志记录开始
                     //                     */
//                    String content = "{设备code:" + lightemitControlCode
//                            + ", 推送内容:" + body
//                            + " }";
//                    List<String> listCode = new ArrayList<>();
//                    listCode.add(lightemitControlCode);
//                    StoreOperationRecordsUtils.storeOperationData(listCode, null, "熙汛大气数据推送", content);
                    /**
                     * 熙汛大气数据推送日志记录结束
                     */
                }
            }, 60 * 1000);
        }
    }
 
}