2021与蓝度共同重构项目,服务端
liuhaonan
2022-11-18 385b687cc1a0a8f6de2320af6b6e0d654cd658ed
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
package com.sandu.ximon.admin.newnova.led;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5AtmosphereHeartbeatReportInnerFrame;
import com.sandu.ximon.admin.newnova.conf.ProgramPathConfig;
import com.sandu.ximon.admin.newnova.param.NewNovaLedParam;
import com.sandu.ximon.admin.newnova.param.ProgramPrarm;
import com.sandu.ximon.admin.newnova.program.NewNovaProgramService;
import com.sandu.ximon.admin.newnova.utils.NovaAPIUtil;
import com.sandu.ximon.admin.newnova.vo.ProWHVO;
import com.sandu.ximon.admin.newnova.vo.StatusVO;
import com.sandu.ximon.admin.newnova.vo.TrasfromStatusVO;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.service.AirDataService;
import com.sandu.ximon.admin.service.PoleBindingService;
import com.sandu.ximon.admin.service.PoleService;
import com.sandu.ximon.dao.bo.NewNovaGroupListBo;
import com.sandu.ximon.dao.domain.NewNovaLed;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.domain.PoleBinding;
import com.sandu.ximon.dao.mapper.NewNovaLedMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @author LiuHaoNan
 * @date 2022/11/9
 */
@Service
@AllArgsConstructor
public class NewNovaLedService extends BaseServiceImpl<NewNovaLedMapper, NewNovaLed> {
 
    private final PoleBindingService poleBindingService;
    private ProgramPathConfig filePathConfig;
 
    /**
     * 屏幕注册
     *
     * @param param
     * @return
     */
    public boolean saveLed(NewNovaLedParam param) {
        NewNovaLed one = getOne(Wrappers.lambdaQuery(NewNovaLed.class).eq(NewNovaLed::getSn, param.getSn()));
        if (one != null) {
            throw new BusinessException("设备已存在,请勿重复注册!");
        }
        NewNovaLed led = new NewNovaLed();
        led.setSn(param.getSn());
        led.setName(param.getName());
        led.setOnlineSign(0);
        return save(led);
    }
 
 
    /**
     * 编辑屏幕名称
     *
     * @param param
     * @return
     */
    public boolean editLed(NewNovaLedParam param) {
        if (param.getId() == null) {
            throw new BusinessException("屏幕id不能为空");
        }
        if (param.getName() == null || param.getName().isEmpty()) {
            throw new BusinessException("屏幕名称不能为空");
        }
 
        NewNovaLed one = getById(param.getId());
        if (one == null) {
            throw new BusinessException("设备不存在!");
        }
//        one.setSn(param.getSn());
        one.setName(param.getName());
        return updateById(one);
    }
 
    /**
     * 删除LED
     *
     * @param ids
     * @return
     */
    public boolean delLed(List<Long> ids) {
        if (ids.isEmpty()) {
            throw new BusinessException("id不能为空");
        }
        List<NewNovaLed> newNovaLeds = listByIds(ids);
        if (newNovaLeds.isEmpty()) {
            throw new BusinessException("设备不存在!");
        }
        //获取删除设备的设备码
        List<String> snList = newNovaLeds.stream().map(NewNovaLed::getSn).collect(Collectors.toList());
        //删除绑定关系
        SpringContextHolder.getBean(PoleBindingService.class).remove(Wrappers.lambdaQuery(PoleBinding.class)
                .eq(PoleBinding::getDeviceType, 12).in(PoleBinding::getDeviceCode, snList));
        //删除设备
        return removeByIds(ids);
    }
 
    /**
     * LED列表
     *
     * @param baseConditionVO
     * @param keyword
     * @param groupId
     * @param onlineStatus
     * @return
     */
    public List<NewNovaLed> listLed(BaseConditionVO baseConditionVO, String keyword, Long groupId, boolean onlineStatus) {
 
        NovaAPIUtil instanceUtil = NovaAPIUtil.getInstanceUtil();
        //排序
        PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
        List<NewNovaLed> newNovaListBos = baseMapper.listLed(keyword, groupId, SecurityUtils.getClientId());
        //获取在线状态
        instanceUtil.getOnlineStatus(newNovaListBos, onlineStatus);
        //获取屏幕开关
        instanceUtil.getScreenPowerState(newNovaListBos);
        //获取音量
        instanceUtil.getVolumeState(newNovaListBos);
        //获取亮度
        instanceUtil.getScreenBrightness(newNovaListBos);
        //获取同步状态
        instanceUtil.getSync(newNovaListBos);
 
        updateBatchById(newNovaListBos);
        return newNovaListBos;
    }
 
    public NewNovaLed getInfo(Long ledId) {
        NovaAPIUtil instanceUtil = NovaAPIUtil.getInstanceUtil();
        NewNovaLed byId = getById(ledId);
        if (byId == null) {
            throw new BusinessException("设备不存在或id不正确 !");
        }
        PoleBinding poleBinding = SpringContextHolder.getBean(PoleBindingService.class).getOne(Wrappers.lambdaQuery(PoleBinding.class)
                .eq(PoleBinding::getDeviceCode, byId.getSn()).eq(PoleBinding::getDeviceType, 12));
        if (poleBinding != null) {
            Pole pole = SpringContextHolder.getBean(PoleService.class).getById(poleBinding.getPoleId());
            byId.setPoleId(pole.getId());
            byId.setPoleName(pole.getPoleName());
        }
        List<NewNovaLed> list = new ArrayList<>();
        list.add(byId);
        //获取在线状态
        instanceUtil.getOnlineStatus(list, false);
        //获取屏幕开关
        instanceUtil.getScreenPowerState(list);
        //获取音量
        instanceUtil.getVolumeState(list);
        //获取亮度
        instanceUtil.getScreenBrightness(list);
        //获取同步状态
        instanceUtil.getSync(list);
        //获取详情
        instanceUtil.getInfo(byId);
        return list.get(0);
    }
 
    /**
     * 获取整组的屏幕
     *
     * @param groupId
     */
    public List<NewNovaGroupListBo> getListByGroupId(Long groupId) {
        List<NewNovaGroupListBo> listByGroupId = baseMapper.getListByGroupId(groupId, SecurityUtils.getClientId());
        return listByGroupId;
    }
 
    /**
     * 推送大气
     *
     * @param id
     * @param duration
     * @param fontSize
     */
    public Object pushAirData(Long id, Long duration, Long fontSize) throws InterruptedException {
        NovaAPIUtil apiUtil = NovaAPIUtil.getInstanceUtil();
        NewNovaLed byId = getById(id);
        PoleBinding nova = poleBindingService.getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getDeviceCode, byId.getSn()).eq(PoleBinding::getDeviceType, 12));
        if (nova == null) {
            throw new BusinessException("未绑定诺瓦设备");
        }
        PoleBinding air = poleBindingService.getOne(Wrappers.lambdaQuery(PoleBinding.class).eq(PoleBinding::getPoleId, nova.getPoleId()).eq(PoleBinding::getDeviceType, 3));
        if (air == null) {
            throw new BusinessException("未绑定大气设备");
        }
 
        //获取大气监测数据
        A5AtmosphereHeartbeatReportInnerFrame.HeartBeatDataPackage data = SpringContextHolder.getBean(AirDataService.class).getDataByPoleid(nova.getPoleId());
 
        //解析大气数据
        Map pageInfo = parseData(data, duration,fontSize);
        ProgramPrarm page = new ProgramPrarm();
        page.setPageInfo(pageInfo);
        page.setInsertPlay(true);
        ProWHVO whvo = new ProWHVO("大气监测", 128, 256);
        page.setProgramMsg(whvo);
        StatusVO createPro = apiUtil.createPro(whvo);
        Integer pid = Integer.valueOf(createPro.getStatusData());
        page.setProgramID(pid);
 
        StatusVO editPro = apiUtil.editProgram(pid, pageInfo);
        if(editPro.getStatusCode()!=0){
            throw new BusinessException(editPro.getStatusData());
        }
        StatusVO genVO = apiUtil.genrateProgram(pid);
        if(genVO.getStatusCode()!=0){
            throw new BusinessException(genVO.getStatusData());
        }
        page.setProgramID(Integer.valueOf(createPro.getStatusData()));
        page.setStartPlayAfterTransferred(true);
        List<String> sns = new ArrayList<String>();
        sns.add(byId.getSn());
        page.setSnList(sns);
 
        List<TrasfromStatusVO> trasfromStatusVOS = apiUtil.trasfromProgram(page);
 
        File file = new File(filePathConfig.getOutPutPath() + "/program" + pid);
        SpringContextHolder.getBean(NewNovaProgramService.class).deleteFile(file);
 
        return trasfromStatusVOS;
    }
 
    /**
     * 处理大气数据
     *
     * @param data
     * @param duration
     * @param fontSize
     * @return
     */
 
    private Map parseData(A5AtmosphereHeartbeatReportInnerFrame.HeartBeatDataPackage data, Long duration, Long fontSize) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z+8:00'");
        Date end = new Date();
        end.setTime(end.getTime()+duration);
        String str = "{\n" +
                "        \"name\": \"大气监测\",\n" +
                "        \"widgetContainers\": [\n" +
                "            {\n" +
                "                \"contents\": {\n" +
                "                    \"widgets\": [\n" +
                "                        {\n" +
                "                            \"constraints\": [\n" +
                "                                {\n" +
                "                                    \"cron\": [\n" +
                "                                        \"\"\n" +
                "                                    ],\n" +
                "                                    \"endTime\": \"" + sdf.format(end) + "\",\n" +
                "                                    \"startTime\": \"" + sdf.format(new Date()) + "\"\n" +
                "                                }\n" +
                "                            ],\n" +
                "                            \"duration\":" + duration + ",\n" +
                "                            \"metadata\": {\n" +
                "                                \"content\": {\n" +
                "                                    \"autoPaging\": true,\n" +
                "                                    \"backgroundMusic\": {\n" +
                "                                        \"duration\": 0,\n" +
                "                                        \"isTextSync\": false\n" +
                "                                    },\n" +
                "                                    \"displayStyle\": {\n" +
                "                                        \"scrollAttributes\": {\n" +
                "                                            \"effects\": {\n" +
                "                                                \"animation\": \"MARQUEE_LEFT\",\n" +
                "                                                \"speed\": 3\n" +
                "                                            }\n" +
                "                                        },\n" +
                "                                        \"type\": \"STATIC\"\n" +
                "                                    },\n" +
                "                                    \"paragraphs\": [\n" +
                "                                        {\n" +
                "                                            \"backgroundColor\": \"#00000000\",\n" +
                "                                            \"horizontalAlignment\": \"LEFT\",\n" +
                "                                            \"letterSpacing\": 0,\n" +
                "                                            \"lineSpacing\": 0,\n" +
                "                                            \"lines\": [\n" +
                "                                                {\n" +
                "                                                    \"segs\": [\n" +
                "                                                        {\n" +
                "                                                            \"content\": \"环境监测:\"\n" +
                "                                                        }\n" +
                "                                                    ]\n" +
                "                                                },\n" +
                "                                                {\n" +
                "                                                    \"segs\": [\n" +
                "                                                        {\n" +
                "                                                            \"content\": \"温度:" + (data == null || data.getTemperature() == null ? " - " : data.getTemperature()) + "°\"" + "\n" +
                "                                                        }\n" +
                "                                                    ]\n" +
                "                                                },\n" +
                "                                                {\n" +
                "                                                    \"segs\": [\n" +
                "                                                        {\n" +
                "                                                            \"content\": \"湿度:" + (data == null || data.getHumidity() == null ? " - " : data.getHumidity()) + "%\"" + "\n" +
                "                                                        }\n" +
                "                                                    ]\n" +
                "                                                },\n" +
                "                                                {\n" +
                "                                                    \"segs\": [\n" +
                "                                                        {\n" +
                "                                                            \"content\": \"pm25:" + (data == null || data.getPm25() == null ? " - " : data.getPm25()) + "μg/m³\"" + "\n" +
                "                                                        }\n" +
                "                                                    ]\n" +
                "                                                },\n" +
                "                                                {\n" +
                "                                                    \"segs\": [\n" +
                "                                                        {\n" +
                "                                                            \"content\": \"pm10:" + (data == null || data.getPm10() == null ? " - " : data.getPm10()) + "μg/m³\"" + "\n" +
                "                                                        }\n" +
                "                                                    ]\n" +
                "                                                },\n" +
                "                                                {\n" +
                "                                                    \"segs\": [\n" +
                "                                                        {\n" +
                "                                                            \"content\": \"甲醛:" + (data == null || data.getEch2o() == null ? " - " : data.getEch2o()) + "μg/m³\"" + "\n" +
                "                                                        }\n" +
                "                                                    ]\n" +
                "                                                },\n" +
                "                                                {\n" +
                "                                                    \"segs\": [\n" +
                "                                                        {\n" +
                "                                                            \"content\": \"CO2:" + (data == null || data.getCo2() == null ? " - " : data.getCo2()) + "ppm\"" + "\n" +
                "                                                        }\n" +
                "                                                    ]\n" +
                "                                                }\n" +
                "                                            ],\n" +
                "                                            \"verticalAlignment\": \"TOP\"\n" +
                "                                        }\n" +
                "                                    ],\n" +
                "                                    \"textAttributes\": [\n" +
                "                                        {\n" +
                "                                            \"backgroundColor\": \"#ff000000\",\n" +
                "                                            \"attributes\": {\n" +
                "                                                \"font\": {\n" +
                "                                                    \"family\": [\n" +
                "                                                        \"Helvetica\"\n" +
                "                                                    ],\n" +
                "                                                    \"isUnderline\": false,\n" +
                "                                                    \"size\": "+fontSize+",\n" +
                "                                                    \"style\": \"NORMAL\"\n" +
                "                                                },\n" +
                "                                                \"letterSpacing\": 0,\n" +
                "                                                \"textColor\": \"#ffff0000\"\n" +
                "                                            }\n" +
                "                                        }\n" +
                "                                    ]\n" +
                "                                }\n" +
                "                            },\n" +
                "                            \"name\": \"环境监测\",\n" +
                "                            \"type\": \"ARCH_TEXT\"\n" +
                "                        }\n" +
                "                    ]\n" +
                "                },\n" +
                "                \"id\": 1,\n" +
                "                \"name\": \"widgetContainers1\"\n" +
                "            }\n" +
                "        ]\n" +
                "    }";
 
        return JSON.parseObject(str, Map.class);
    }
 
 
}