2021与蓝度共同重构项目,服务端
liuhaonan
2022-10-25 d495f9b8cdc83663e4189bc3cc72ac9543ff5555
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
package com.sandu.ximon.admin.utils;
 
import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.sandu.common.file.FileUploadDto;
import com.sandu.ximon.admin.config.RealtimeServerBean;
import com.sandu.ximon.admin.service.PoleLightemitService;
import com.sandu.ximon.admin.utils.request.*;
import com.sandu.ximon.dao.domain.PoleLightemitEntity;
import com.squareup.okhttp.*;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
 
/**
 * 熙讯屏幕控制
 */
 
@Component
public class LightemitUtils {
    @Autowired
    RealtimeServerBean realtimeServerBean;
 
    @Autowired
    PoleLightemitService poleLightemitService;
    @Autowired
    ApplicationContext applicationContext;
    //body封装编码
    private final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
 
    private OkHttpClient client = new OkHttpClient();
 
    @Autowired
    private Snowflake snowflake;
 
 
    public void init() {
        //重新获取单例
        poleLightemitService = applicationContext.getBean(PoleLightemitService.class);
    }
 
    /**
     * post请求封装方法
     *
     * @param url  url
     * @param json body
     * @return
     */
    public String post(String url, String json) {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            LogUtils.error(e.getMessage());
            return "";
        }
    }
 
    /**
     * 获取led屏宽度
     *
     * @param ledCode led屏编号
     * @return
     */
    public String getScreenWidth(String ledCode) {
        String postBody = new Gson().toJson(new GetScreenWidth());
        //请求地址
        String url = realtimeServerBean.getCommand() + ledCode;
        //请求
        String result = post(url, postBody);
        if (StringUtils.isNotBlank(result)) {
            try {
                return JSONObject.parseObject(result).get("result").toString();
            } catch (Exception e) {
                return "64";
            }
        } else {
            return "64";
        }
    }
 
    /**
     * 获取led屏高度
     *
     * @param ledCode led屏编号
     * @return
     */
    public String getScreenHeight(String ledCode) {
        String postBody = new Gson().toJson(new GetScreenHeight());
        //请求地址
        String url = realtimeServerBean.getCommand() + ledCode;
        //请求
        String result = post(url, postBody);
        if (StringUtils.isNotBlank(result)) {
            try {
                return JSONObject.parseObject(result).get("result").toString();
            } catch (Exception e) {
                return "64";
            }
        } else {
            return "64";
        }
    }
 
    /**
     * 清屏操作
     *
     * @param ledCode
     */
    public String clear(String ledCode) {
        String postBody = new Gson().toJson(new Clear());
        //请求地址
        String url = realtimeServerBean.getCommand() + ledCode;
        //请求
        String result = post(url, postBody);
        LogUtils.error("清屏结果:" + result);
        return result;
    }
 
    /**
     * html传输至led屏
     *
     * @param ledCode
     * @param username
     */
    public void postHtml(String ledCode, String username) {
        //html网页内容
        PostHtml postHtml = new PostHtml(realtimeServerBean.getUrl() + username + "_" + ledCode + ".html");
        String postBody = new Gson().toJson(postHtml);
        //请求地址
        String url = realtimeServerBean.getCommand() + ledCode;
        //保存led数据
        poleLightemitService.updateRequestBody(ledCode, postBody);
        //请求
        String result = post(url, postBody);
    }
 
 
    /**
     * 视频传输至led屏
     *
     * @param screenWidth
     * @param screenHeight
     * @param ledCode
     */
    public String postFile(String screenWidth, String screenHeight, String ledCode, FileUploadDto fileUploadDto) {
 
        String fileUrl = fileUploadDto.getFileUrl();
        String[] split = fileUrl.split("/");
        String id = split[split.length - 1];
 
 
        //led开发板下载视频请求body
        DownloadFileToLocal downloadFileToLocal = new DownloadFileToLocal();
        downloadFileToLocal.url = realtimeServerBean.getUrl() + "https://ximonsmart.oss-cn-shanghai.aliyuncs.com/" + id;
        downloadFileToLocal.path += fileUrl;
 
        //led开发板下载视频请求
        this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(downloadFileToLocal));
 
        //led开发板视频列表请求body
        SetPlayList setPlayList = new SetPlayList();
        setPlayList.list[0] = fileUrl;
        setPlayList.pathList[0] = ledCode;
        try {
            setPlayList.width = Integer.valueOf(screenWidth);
            setPlayList.height = Integer.valueOf(screenHeight);
        } catch (Exception e) {
            return "推送失败";
        }
        String postBody = new Gson().toJson(setPlayList);
 
        //led开发板视频列表请求
        String post = this.post(realtimeServerBean.getCommand() + ledCode, postBody);
        LogUtils.error("视频上传结果:----" + post);
        if (post.contains("does not exist")) {
            return "推送失败";
        } else {
            //{"_type":"success","_id":"ce8dc3ff-dc88-43b7-8f55-60abd8700f1a","timestamp":1653555160535}
            if (post.startsWith("{") && post.endsWith("}") && post.contains("_type\":\"success")) {
                //保存led数据
                poleLightemitService.updateRequestBody(ledCode, postBody);
                return "推送成功";
            } else {
                return "推送失败";
            }
        }
    }
 
 
    /**
     * led屏字幕设置
     *
     * @param subTitleSet
     * @param ledCode
     */
    public void subTitleSet(SubTitleSet subTitleSet, String ledCode, Boolean isSave) {
        String postBody = new Gson().toJson(subTitleSet);
        if (isSave == true) {
            poleLightemitService.updateRequestBody(ledCode, postBody);
        }
        this.post(realtimeServerBean.getCommand() + ledCode, postBody);
    }
 
    /**
     * 清除播放列表
     *
     * @param ledCode
     */
    public void clearVideoPlay(String ledCode) {
        this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(new ClearVideoPlay()));
    }
 
    /**
     * 清除节目列表
     *
     * @param ledCode
     */
    public void clearPlayerTask(String ledCode) {
        this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(new ClearPlayerTask()));
    }
 
    /**
     * 发送最新的数据
     *
     * @param lightemitControlCode
     */
    public void sendLastCommand(String lightemitControlCode) {
        PoleLightemitEntity poleLightemitEntity = poleLightemitService.getLedByLightControlCode(lightemitControlCode);
        if (poleLightemitEntity == null || StringUtils.isBlank(poleLightemitEntity.getRequestBody())) {
            return;
        }
        this.post(realtimeServerBean.getCommand() + lightemitControlCode, poleLightemitEntity.getRequestBody());
    }
 
    /**
     * 获取led屏画面
     *
     * @param ledCode
     */
    public String getPicture(String ledCode) {
        String result = this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(new GetPicture()));
        //获取base64图片数据
        if (StringUtils.isBlank(result)) {
            return "";
        }
        String re;
        try {
            re = JSONObject.parseObject(result).get("result").toString();
        } catch (Exception e) {
            re = "0";
        }
 
        if (StringUtils.isBlank(re)) {
            return "";
        }
        //去除换行符
        re.replaceAll("\r|\n*", "");
        return re;
    }
 
    /**
     * 判断led屏是否开启
     *
     * @param ledCode
     * @return
     */
    public String getIsScreenOpen(String ledCode) {
        //发送屏幕是否开启的指令
        String result = this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(new IsScreenOpen()));
        if (StringUtils.isNotBlank(result)) {
            String re;
            try {
                re = JSONObject.parseObject(result).get("result").toString();
            } catch (Exception e) {
                re = "false";
            }
            return re;
        } else {
            return "";
        }
    }
 
 
    public boolean getLedOnLine(String ledCode) {
        //通过屏幕是否开启指令获取屏幕是否在线
        String result = this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(new IsScreenOpen()));
        result = result.trim();
        return result.startsWith("{") && result.endsWith("}");
    }
 
 
    /**
     * 控制屏幕开关
     */
    public String setScreenOpen(String ledCode, Boolean bool) {
        String result = this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(new setScreenOpen(bool)));
        if (StringUtils.isNotBlank(result)) {
            String re = "";
            try {
                re = JSONObject.parseObject(result).get("result").toString();
            } catch (Exception e) {
                re = "false";
            }
            return re;
        } else {
            return "";
        }
    }
 
//    //发送最新的数据
//    public void sendLastCommand(String lightemitControlCode) {
//        PoleLightemitEntity poleLightemitEntity = poleLightemitService.selectByLightemitControlCode(lightemitControlCode);
//        if(poleLightemitEntity == null || StringUtils.isBlank(poleLightemitEntity.getRequestBody())){
//            return;
//        }
//        this.post(realtimeServerBean.getCommand() + lightemitControlCode, poleLightemitEntity.getRequestBody());
//    }
 
    /**
     * 设置led音量
     */
    public void setVoiume(String ledCode, Integer volume) {
 
        SetVolume setVolume = new SetVolume();
        setVolume.arg1 = volume;
        String postBody = new Gson().toJson(setVolume);
 
        //请求
        String result = post(realtimeServerBean.getCommand() + ledCode, postBody);
        LogUtils.error("设置结果:-------------------" + result);
    }
 
 
    /**
     * 设置led亮度
     */
    public void setBrightness(String ledCode, Integer brightness) {
 
        SetBrightness setBrightness = new SetBrightness();
        setBrightness.arg1 = brightness;
        String postBody = new Gson().toJson(setBrightness);
 
        //请求
        String result = post(realtimeServerBean.getCommand() + ledCode, postBody);
        LogUtils.error("设置结果:-------------------" + result);
    }
 
    //    获取led音量
    public String getVoiume(String ledCode) {
        GetVolume getVolume = new GetVolume();
        String postBody = new Gson().toJson(getVolume);
 
        //请求
        String result = post(realtimeServerBean.getCommand() + ledCode, postBody);
        //请求
        if (StringUtils.isNotBlank(result)) {
            try {
                return JSONObject.parseObject(result).get("result").toString();
            } catch (Exception e) {
                return "0";
            }
        } else {
            return "0";
        }
    }
 
    /**
     * 查询定时
     *
     * @param ledCode
     * @return
     */
    public String getTimeSchedule(String ledCode) {
        String result = this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(new GetTimeSchedule()));
        //获取定时json数据
//        Map map = new Gson().fromJson(result,Map.class);
//        Gson gson = new Gson();
//        Task task =gson.fromJson((String)gson.fromJson(result,Map.class).get("task"),Task.class);
//        Schedules schedules =gson.fromJson((String)gson.fromJson(result,Map.class).get("schedule"),Schedules.class);
        if (StringUtils.isBlank(result)) {
            return "";
        }
        String re;
 
        //去除字符串中的\
        re = StringEscapeUtils.unescapeJavaScript(result);
        //去除字符串中的}"
        re = re.replace("}\"", "}");
        //去除字符串中"{
        re = re.replace("\"{", "{");
        re = re.replace("\"null\"", "{}");
        if (StringUtils.isBlank(re)) {
            return "";
        }
        return re;
    }
}