2021与蓝度共同重构项目,服务端
liuhaonan
2022-11-04 e55c8b0a92eb9715edd90c31dfd4de51a47b588b
ximon-admin/src/main/java/com/sandu/ximon/admin/service/PoleLightemitService.java
@@ -1,19 +1,20 @@
package com.sandu.ximon.admin.service;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.gson.Gson;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.file.FileUploadDto;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.config.RealtimeServerBean;
import com.sandu.ximon.admin.entity.*;
import com.sandu.ximon.admin.param.PoleBindingParam;
import com.sandu.ximon.admin.redis.DeviceRedisKey;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.FileUtil;
import com.sandu.ximon.admin.utils.HtmlTemplateUtils;
import com.sandu.ximon.admin.utils.LightemitUtils;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.admin.utils.*;
import com.sandu.ximon.admin.vo.EquipmentInfomation;
import com.sandu.ximon.admin.vo.RedisDeviceStatus;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.domain.PoleLightemitEntity;
import com.sandu.ximon.dao.enums.OrderByEnums;
@@ -25,6 +26,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.io.File;
@@ -51,6 +53,12 @@
    @Autowired
    PoleLightemitEntityMapper poleLightemitDao;
    @Autowired
    LedSFileService xiXunFileService;
    @Autowired
    RedisUtils redisUtils;
    @PostConstruct
    public void init() {
@@ -123,6 +131,23 @@
            }
        }
        return temp;
    }
    /**
     * 熙讯设备列表(用于首页数据统计)
     *
     * @return
     */
    public List<PoleLightemitEntity> listLedOnHome() {
        List<PoleLightemitEntity> poleLightemitEntityList;
        //超管
        if (SecurityUtils.getClientId() == null) {
            poleLightemitEntityList = poleLightemitDao.listLed(null, null, null);
        } else {
            poleLightemitEntityList = poleLightemitDao.listLed(null, SecurityUtils.getUserId(), null);
        }
        return poleLightemitEntityList;
    }
@@ -219,18 +244,19 @@
     */
    public EquipmentInfomation getLedByLightControlCodeInfo(String lightControlCode) {
        EquipmentInfomation equipmentInfo = new EquipmentInfomation();
        equipmentInfo.setEquipmentType("熙讯LED");
        equipmentInfo.setEquipmentType("SLED");
        if (lightControlCode == null || lightControlCode.trim().length() == 0) {
            return equipmentInfo;
        }
        PoleLightemitEntity xiXun = getOne(Wrappers.lambdaQuery(PoleLightemitEntity.class).eq(PoleLightemitEntity::getLightemitControlCode, lightControlCode));
        boolean isScreenOpen = false;
        if (xiXun != null) {
            equipmentInfo.setEquipmentCreateTime(xiXun.getCreateTime());
            equipmentInfo.setEquipmentMac(xiXun.getLightemitControlCode());
            equipmentInfo.setEquipmentName(xiXun.getLightemitName());
            isScreenOpen = lightemitUtils.getLedOnLine(xiXun.getLightemitControlCode());
        }
        boolean isScreenOpen = lightemitUtils.getLedOnLine(xiXun.getLightemitControlCode());
        if (isScreenOpen) {
            equipmentInfo.setEquipmentState("在线");
        } else {
@@ -241,6 +267,7 @@
    }
    @Transactional(rollbackFor = Exception.class)
    public void savePoleLightemit(PoleLightemitEntity poleLightemit) {
        boolean save = this.save(poleLightemit);
        // 绑定灯杆
@@ -248,9 +275,13 @@
        if (save && poleLightemit.getStreetlightId() != null) {
            PoleBindingParam poleBindingParam = new PoleBindingParam();
            poleBindingParam.setDeviceCode(poleLightemit.getLightemitControlCode());
            poleBindingParam.setDeviceType(1);
            poleBindingParam.setDeviceType(10);
            poleBindingParam.setDeviceName(poleLightemit.getLightemitName());
            poleBindingService.bindPole(poleLightemit.getStreetlightId(), poleBindingParam);
            if (SpringContextHolder.getBean(PoleService.class).getById(poleLightemit.getStreetlightId()) == null) {
                throw new BusinessException("灯杆不存在");
            }
            boolean b = poleBindingService.bindPole(poleLightemit.getStreetlightId(), poleBindingParam);
        }
        /**
@@ -310,7 +341,7 @@
    public boolean deletePoleLightemit(List<Long> ledIds) {
        List<PoleLightemitEntity> poleLightemitEntities = listByIds(ledIds);
        if (poleLightemitEntities != null && poleLightemitEntities.size() != 0) {
        if (poleLightemitEntities != null && poleLightemitEntities.size() == 0) {
            throw new BusinessException("设备不存在");
        }
@@ -393,28 +424,26 @@
     * led上传视频
     *
     * @param lightemitControlCode
     * @param filename
     * @param path
     * @param userName
     * @throws IOException
     */
    @Async("taskExecutor")
    public void videoUpload(String lightemitControlCode, String filename, String path, String userName) throws IOException {
    public String videoUpload(String lightemitControlCode, FileUploadDto fileUploadDto, Integer type) {
        //获取屏幕宽
        String screenWidth = lightemitUtils.getScreenWidth(lightemitControlCode);
        String screenHeight = lightemitUtils.getScreenHeight(lightemitControlCode);
        //发送视频至开发板并播放
        lightemitUtils.postVideo(screenWidth, screenHeight, filename, lightemitControlCode);
        String fileUrl = fileUploadDto.getFileUrl();
        String[] split = fileUrl.split("/");
        String id = split[split.length - 1];
        String result = this.videoXixunPlayer(lightemitControlCode, id, fileUploadDto.getFileSize(), fileUploadDto.getMd5(), type, fileUploadDto.getFileType(), 999999);
        //清屏
        lightemitUtils.clear(lightemitControlCode);
        LogUtils.error("上传播放结果:" + result);
        return result;
    }
    @Async("taskExecutor")
    public void videoXixunPlayer(String lightemitControlCode, String ip, String filename, Long filesize, Integer videoTime) {
    public String videoXixunPlayer(String lightemitControlCode, String filename, Long filesize, String md5, Integer type, String fileType, Integer videoTime) {
        //获取屏幕宽
        Integer screenWidth = Integer.valueOf(lightemitUtils.getScreenWidth(lightemitControlCode));
        Integer screenHeight = Integer.valueOf(lightemitUtils.getScreenHeight(lightemitControlCode));
@@ -429,18 +458,23 @@
        source.set_id(UUID.randomUUID().toString());
        //请求头拼接该ID为该资源的完整下载地址
        source.setId(filename);
        //设置资源类型,其他资源类型请参考xixunplayer节目json说明文档
        source.set_type("Video");
        if (type == 1) {
            //设置资源类型,其他资源类型请参考xixunplayer节目json说明文档
            source.set_type("Video");
            source.setMime("video/mp4");
        } else {
            source.set_type("Image");
            source.setMime("image/jpeg");
        }
        //资源后缀名
        source.setFileExt(".mp4");
        source.setFileExt(fileType);
        //资源高度
        source.setHeight(screenHeight);
        //资源宽度
        source.setWidth(screenWidth);
        //距左
        source.setLeft(0);
        source.setMd5("dd135d5d2d44d619a542db773ab529a4");
        source.setMime("video/mp4");
        source.setMd5(md5);
        source.setName(filename);
        source.setPlayTime(0);    //播放起始时间
        source.setSize(filesize);    //资源字节数,要精准
@@ -454,7 +488,7 @@
        List<Layer> list1 = new ArrayList<Layer>();
        list1.add(layer);
        pro.setLayers(list1);
        pro.setName("demo");    //节目名称
        pro.setName("文件上传播放");    //节目名称
        pro.setOthers(true);//如果为true,则该节目来自第三方接口,不是来自我们自己的web,二次开发请务必赋值为true
        pro.setTotalSize(filesize);    //所有资源总字节数
        pro.setVersion(0);            //高级节目=0,简易节目=2
@@ -470,17 +504,29 @@
        task.setItems(list2);
        command.setId(UUID.randomUUID().toString());
        //这里是下方的post回调地址,需要修改IP地址
        command.setNotificationURL("http://" + ip + ":" + port + "/machine-fast/serv/download/getJSON");
        command.setNotificationURL("");
        //资源下载链接的请求头
        command.setPreDownloadURL("http://" + ip + ":" + port + "/machine-fast/serv/download/downloadFile/");
        command.setPreDownloadURL("https://ximonsmart.oss-cn-shanghai.aliyuncs.com/");
        command.setTask(task);
        xixun.set_id(UUID.randomUUID().toString());
        xixun.setCommand(command);
        xixun.setType("commandXixunPlayer");    //命令固定类型,不可更改
        String jsondata = new Gson().toJson(xixun);
        lightemitUtils.clear(lightemitControlCode);
        poleLightemitDao.updateRequestBody(lightemitControlCode, jsondata);
        lightemitUtils.post(realtimeServerBean.getCommand() + lightemitControlCode, jsondata);
        String clearResult = lightemitUtils.clear(lightemitControlCode);
        if (clearResult.contains("does not exist")) {
            return "推送失败";
        } else {
            poleLightemitDao.updateRequestBody(lightemitControlCode, jsondata);
            String post = lightemitUtils.post(realtimeServerBean.getCommand() + lightemitControlCode, jsondata);
            LogUtils.error("结果:" + post);
            //{"_type":"success","_id":"ce8dc3ff-dc88-43b7-8f55-60abd8700f1a","timestamp":1653555160535}
            if (post.startsWith("{") && post.endsWith("}") && post.contains("_type\":\"success")) {
                return "推送成功";
            } else {
                return "推送失败";
            }
        }
    }
    /**
@@ -492,16 +538,6 @@
    public void updateRequestBody(String ledCode, String postBody) {
        baseMapper.updateRequestBody(ledCode, postBody);
    }
//    /**
//     * 根据led屏编码获取灯杆所有信息
//     *
//     * @param ledCode
//     * @return
//     */
//    public PoleStreetlightEntity getStreetlightByLedCode(String ledCode) {
//        return baseMapper.getStreetlightByLedCode(ledCode);
//    }
    /**
@@ -526,4 +562,36 @@
        lightemitUtils.setBrightness(lightemitControlCode, brightness);
    }
    public Object getProgram(Long lightemitId) {
        PoleLightemitEntity xiXun = getById(lightemitId);
        String requestBody = xiXun.getRequestBody();
        if (requestBody == null || requestBody.isEmpty()) {
            throw new BusinessException("未找到节目");
        }
        Map map = JSON.parseObject(requestBody, Map.class);
        return map;
    }
    /**
     * 向Redis中存入设备状态
     */
    public void setCacheData() {
        list().forEach(
                xiXun -> {
                    RedisDeviceStatus deviceStatus = new RedisDeviceStatus();
                    deviceStatus.setDeviceId(xiXun.getLightemitControlCode());
                    boolean ledOnLine = lightemitUtils.getLedOnLine(xiXun.getLightemitControlCode());
                    if (ledOnLine) {
                        deviceStatus.setStatus(0);
                    } else {
                        deviceStatus.setStatus(1);
                    }
                    redisUtils.set(DeviceRedisKey.XIXUN + xiXun.getLightemitControlCode(), JSON.toJSONString(deviceStatus));
//                    redisUtils.delete(DeviceRedisKey.XIXUN + xiXun.getLightemitControlCode());
                }
        );
    }
}