package com.sandu.ximon.admin.utils; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import com.sandu.ximon.admin.config.NginxConfigBean; import com.sandu.ximon.admin.config.RealtimeServerBean; import com.sandu.ximon.admin.service.PoleLightemitService; import com.sandu.ximon.admin.utils.request.*; 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; @Autowired NginxConfigBean nginxConfigBean; //body封装编码 private final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private OkHttpClient client = new OkHttpClient(); 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 void clear(String ledCode) { String postBody = new Gson().toJson(new Clear()); //请求地址 String url = realtimeServerBean.getCommand() + ledCode; //请求 // poleLightemitService.updateRequestBody(ledCode, ""); String result = post(url, postBody); LogUtils.error("清屏结果:" + result); } /** * html传输至led屏 * * @param ledCode * @param username */ public void postHtml(String ledCode, String username) { 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); } /** * html传输至led屏, 通过url获取html * * @param ledCode * @param username */ public void postHtmlUseNginx(String ledCode, String username) { PostHtml postHtml = new PostHtml(nginxConfigBean.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 filename * @param ledCode */ public void postVideo(String screenWidth, String screenHeight, String filename, String ledCode) { //led开发板下载视频请求body DownloadFileToLocal downloadFileToLocal = new DownloadFileToLocal(); downloadFileToLocal.url = realtimeServerBean.getUrl() + filename; downloadFileToLocal.path += filename; //led开发板下载视频请求 this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(downloadFileToLocal)); //led开发板视频列表请求body SetPlayList setPlayList = new SetPlayList(); setPlayList.list[0] = "/data/data/com.xixun.xy.conn/files/local/abc/" + filename; // setPlayList.pathList[0] = ""; try { setPlayList.width = Integer.valueOf(screenWidth); setPlayList.height = Integer.valueOf(screenHeight); } catch (Exception e) { return; } String postBody = new Gson().toJson(setPlayList); //保存led数据 poleLightemitService.updateRequestBody(ledCode, postBody); //led开发板视频列表请求 this.post(realtimeServerBean.getCommand() + ledCode, postBody); } /** * 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) { // poleLightemitService.updateRequestBody(ledCode, ""); this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(new ClearVideoPlay())); } /** * 清除节目列表 * * @param ledCode */ public void clearPlayerTask(String ledCode) { // poleLightemitService.updateRequestBody(ledCode, ""); this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(new ClearPlayerTask())); } /** * 获取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())); //获取base64图片数据 if (StringUtils.isNotBlank(result)) { String re; try { re = JSONObject.parseObject(result).get("result").toString(); } catch (Exception e) { re = "false"; } return re; } else { return ""; } } /* 启动xwalk */ /*public void startActivity(String ledCode){ String result = this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(new StartActivity())); }*/ /* 使用xwalk加载网页 */ /*public void callXwalkFn(String ledCode,String username){ CallXwalkFn callXwalkFn = new CallXwalkFn(); callXwalkFn.setArgUrl(realtimeServerBean.getUrl() + username + "_" + ledCode + ".html"); String postBody = new Gson().toJson(callXwalkFn); //请求地址 String url = realtimeServerBean.getCommand() + ledCode; //请求 String result = post(url,postBody); }*/ /* 控制屏幕开关 */ public String setScreenOpen(String ledCode, Boolean bool) { String result = this.post(realtimeServerBean.getCommand() + ledCode, new Gson().toJson(new setScreenOpen(bool))); //获取base64图片数据 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; } }