2021与蓝度共同重构项目,服务端
liuhaonan
2022-07-19 dca94edc9cd0681be081e36b0dba3bbe0f581ea0
ximon-admin/src/main/java/com/sandu/ximon/admin/utils/LightemitUtils.java
@@ -1,308 +1,323 @@
//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.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;
//
//    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);
//    }
//
//    /**
//     * 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 "";
//        }
//    }
//
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);
@@ -311,68 +326,83 @@
//        }
//        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);
//    }
//
//    /*
//    获取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;
//    }
//}
    /**
     * 设置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;
    }
}