2021与蓝度共同重构项目,服务端
liuhaonan
2022-09-06 4791362c19d2d27a51593d625abb24173d1c63f2
ximon-admin/src/main/java/com/sandu/ximon/admin/service/VnnoxService.java
@@ -1,27 +1,34 @@
package com.sandu.ximon.admin.service;
import cn.hutool.core.collection.CollectionUtil;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.log.Log;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.config.VnnoxConstant;
import com.sandu.ximon.admin.utils.LogUtils;
import com.sandu.ximon.admin.utils.RedisUtils;
import com.sandu.ximon.admin.utils.VnnoxAPIUtil;
import com.sandu.ximon.admin.dto.nova.AirDataProgram;
import com.sandu.ximon.admin.dto.nova.PlayerLayout;
import com.sandu.ximon.admin.dto.nova.PlayerPage;
import com.sandu.ximon.admin.dto.nova.PlayerWidget;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5AtmosphereHeartbeatReportInnerFrame;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.*;
import com.sandu.ximon.admin.utils.request.VnnoxScreenStatusType;
import com.sandu.ximon.admin.utils.response.VnnoxPlayerListResponse;
import com.sandu.ximon.admin.utils.response.VnnoxPlayerResponse;
import com.sandu.ximon.admin.utils.response.VnnoxResult;
import com.sandu.ximon.admin.utils.response.VnnoxResultResponse;
import com.sandu.ximon.admin.vo.NovaPushResultVO;
import com.sandu.ximon.admin.vo.NovaScreenShorUrlVO;
import com.sandu.ximon.dao.domain.LedPlayerEntity;
import com.sandu.ximon.dao.domain.LedPlayerInfoEntity;
import com.sandu.ximon.dao.domain.LedV2RegisterResultEntity;
import com.sandu.ximon.dao.enums.PoleBindingEnums;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -33,12 +40,15 @@
@AllArgsConstructor
public class VnnoxService {
    // @Autowired
    private VnnoxAPIUtil vnnoxAPIUtil;
    // @Autowired
    private RedisUtils redisUtils;
    // @Autowired
    private LedPlayerEntityService ledPlayerEntityService;
    private VnnoxProgramAPIUtil vnnoxProgramAPIUtil;
    private CountDownLatchUtil countDownLatchUtil;
    /**
     * 设备校验注册
@@ -100,7 +110,15 @@
     * @param screenStatus
     * @return
     */
    public VnnoxResult screenStatusChange(List<LedPlayerEntity> playerList, Integer screenStatus) {
    public Map<String, Object> screenStatusChange(List<LedPlayerEntity> playerList, Integer screenStatus) {
        playerList.forEach(item -> {
            if (item.getName() == null || item.getPlayerId() == null) {
                throw new BusinessException("playerId或playerName为空");
            }
            if (item.getPlayerId().isEmpty() || item.getName().isEmpty()) {
                throw new BusinessException("playerId或playerName为空");
            }
        });
        VnnoxScreenStatusType type;
        if (screenStatus.equals(0)) {
            type = VnnoxScreenStatusType.CLOSE;
@@ -108,39 +126,121 @@
            type = VnnoxScreenStatusType.OPEN;
        }
        VnnoxResult vnnoxResult = vnnoxAPIUtil.screenStatus(
                playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()),
                type
        );
        VnnoxResult vnnoxResult = vnnoxAPIUtil.screenStatus(playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()), type);
        List<String> success = vnnoxResult.getSuccess();
        List<String> fail = vnnoxResult.getFail();
        //拼接成功失败的结果
        Map<String, Object> result = new HashMap<>();
        List<NovaPushResultVO> successList = new ArrayList<>();
        List<NovaPushResultVO> faileList = new ArrayList<>();
        List<String> finalSuccess = success;
        List<String> finalFail = fail;
        playerList.forEach(n -> {
            if (finalSuccess.contains(n.getPlayerId())) {
                NovaPushResultVO vo = new NovaPushResultVO();
                vo.setName(n.getName());
                vo.setPlayerId(n.getPlayerId());
                successList.add(vo);
            } else if (finalFail.contains(n.getPlayerId())) {
                NovaPushResultVO vo = new NovaPushResultVO();
                vo.setName(n.getName());
                vo.setPlayerId(n.getPlayerId());
                faileList.add(vo);
            }
        });
        result.put("success", successList);
        result.put("fail", faileList);
        //  根据屏幕状态调整REDIS标识位
        for (String playerId : vnnoxResult.getSuccess()) {
            redisUtils.set(VnnoxConstant.REDIS_SCREEN_STATUS + playerId, screenStatus, VnnoxConstant.REDIS_MAX_SAVE_TIME);
            redisUtils.set(VnnoxConstant.REDIS_SCREEN_STATUS + playerId, screenStatus);
        }
        return vnnoxResult;
        return result;
    }
    public VnnoxResult volChange(List<LedPlayerEntity> playerList, Integer vol) {
        VnnoxResult vnnoxResult = vnnoxAPIUtil.volChange(
                playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()),
                vol
        );
    public Map<String, Object> volChange(List<LedPlayerEntity> playerList, Integer vol) {
        playerList.forEach(item -> {
            if (item.getName() == null || item.getPlayerId() == null) {
                throw new BusinessException("playerId或playerName为空");
            }
            if (item.getPlayerId().isEmpty() || item.getName().isEmpty()) {
                throw new BusinessException("playerId或playerName为空");
            }
        });
        VnnoxResult vnnoxResult = vnnoxAPIUtil.volChange(playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()), vol);
        //  根据音量调整REDIS标识位
        for (String playerId : vnnoxResult.getSuccess()) {
            redisUtils.set(VnnoxConstant.REDIS_VOL + playerId, vol, VnnoxConstant.REDIS_MAX_SAVE_TIME);
            redisUtils.set(VnnoxConstant.REDIS_VOL + playerId, vol);
        }
        return vnnoxResult;
        List<String> success = vnnoxResult.getSuccess();
        List<String> fail = vnnoxResult.getFail();
        //拼接成功失败的结果
        Map<String, Object> result = new HashMap<>();
        List<NovaPushResultVO> successList = new ArrayList<>();
        List<NovaPushResultVO> faileList = new ArrayList<>();
        List<String> finalSuccess = success;
        List<String> finalFail = fail;
        playerList.forEach(n -> {
            if (finalSuccess.contains(n.getPlayerId())) {
                NovaPushResultVO vo = new NovaPushResultVO();
                vo.setName(n.getName());
                vo.setPlayerId(n.getPlayerId());
                successList.add(vo);
            } else if (finalFail.contains(n.getPlayerId())) {
                NovaPushResultVO vo = new NovaPushResultVO();
                vo.setName(n.getName());
                vo.setPlayerId(n.getPlayerId());
                faileList.add(vo);
            }
        });
        result.put("success", successList);
        result.put("fail", faileList);
        return result;
    }
    public VnnoxResult brightnessChange(List<LedPlayerEntity> playerList, Integer brightness) {
        VnnoxResult vnnoxResult = vnnoxAPIUtil.brightnessChange(
                playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()),
                brightness
        );
    public Map<String, Object> brightnessChange(List<LedPlayerEntity> playerList, Integer brightness) {
        playerList.forEach(item -> {
            if (item.getName() == null || item.getPlayerId() == null) {
                throw new BusinessException("playerId或playerName为空");
            }
            if (item.getPlayerId().isEmpty() || item.getName().isEmpty()) {
                throw new BusinessException("playerId或playerName为空");
            }
        });
        VnnoxResult vnnoxResult = vnnoxAPIUtil.brightnessChange(playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()), brightness);
        //  根据亮度调整REDIS标识位
        for (String playerId : vnnoxResult.getSuccess()) {
            redisUtils.set(VnnoxConstant.REDIS_BRIGHTNESS + playerId, brightness, VnnoxConstant.REDIS_MAX_SAVE_TIME);
            redisUtils.set(VnnoxConstant.REDIS_BRIGHTNESS + playerId, brightness);
        }
        return vnnoxResult;
        List<String> success = vnnoxResult.getSuccess();
        List<String> fail = vnnoxResult.getFail();
        //拼接成功失败的结果
        Map<String, Object> result = new HashMap<>();
        List<NovaPushResultVO> successList = new ArrayList<>();
        List<NovaPushResultVO> faileList = new ArrayList<>();
        List<String> finalSuccess = success;
        List<String> finalFail = fail;
        playerList.forEach(n -> {
            if (finalSuccess.contains(n.getPlayerId())) {
                NovaPushResultVO vo = new NovaPushResultVO();
                vo.setName(n.getName());
                vo.setPlayerId(n.getPlayerId());
                successList.add(vo);
            } else if (finalFail.contains(n.getPlayerId())) {
                NovaPushResultVO vo = new NovaPushResultVO();
                vo.setName(n.getName());
                vo.setPlayerId(n.getPlayerId());
                faileList.add(vo);
            }
        });
        result.put("success", successList);
        result.put("fail", faileList);
        return result;
    }
@@ -186,70 +286,344 @@
        return map;
    }
    //重启
    public VnnoxResult reboot(List<LedPlayerEntity> playerList) {
        return vnnoxAPIUtil.reboot(
                playerList.stream().map(
                        item -> item.getPlayerId()
                ).collect(Collectors.toList())
        );
    // 批量截屏
    public List<NovaScreenShorUrlVO> getScreenShotUrl(List<NovaPushResultVO> playerIds) throws InterruptedException {
        List<NovaScreenShorUrlVO> screenShorUrlList = new ArrayList<>();
        //从playerIds中获取playerId
        List<String> AllplayerIdsList = playerIds.stream().map(item -> item.getPlayerId()).collect(Collectors.toList());
        List<List<String>> split = CollectionUtil.split(AllplayerIdsList, 100);
        for (List<String> playerIdsList : split) {
            VnnoxResult vnnoxResult = vnnoxAPIUtil.screenShot(playerIdsList);
            LocalDateTime now = LocalDateTime.now();
            NovaScreenShorUrlVO screenShorUrlVO;
            for (String successStrId : vnnoxResult.getSuccess()) {
                screenShorUrlVO = new NovaScreenShorUrlVO();
                String url = null;
                int num = 0;
                while (num < 3) {
                    url = redisUtils.get(VnnoxConstant.REDIS_SCREEN_SHOT + successStrId);
                    if (url != null) {
                        break;
                    } else {
                        num++;
                        Thread.sleep(500);
                    }
                }
                screenShorUrlVO.setShortUrl(url);
                screenShorUrlVO.setPlayerId(successStrId);
                screenShorUrlVO.setTime(now);
                for (NovaPushResultVO bean : playerIds) {
                    if (bean.getPlayerId().equals(successStrId)) {
                        screenShorUrlVO.setName(bean.getName());
                        break;
                    }
                }
                screenShorUrlList.add(screenShorUrlVO);
            }
            for (String failStrId : vnnoxResult.getFail()) {
                screenShorUrlVO = new NovaScreenShorUrlVO();
                String url = null;
                screenShorUrlVO.setShortUrl(url);
                screenShorUrlVO.setPlayerId(failStrId);
                screenShorUrlVO.setTime(now);
                for (NovaPushResultVO bean : playerIds) {
                    if (bean.getPlayerId().equals(failStrId)) {
                        screenShorUrlVO.setName(bean.getName());
                        break;
                    }
                }
                screenShorUrlList.add(screenShorUrlVO);
            }
        }
        return screenShorUrlList;
    }
//    //重启
//    public VnnoxResult reboot(List<LedPlayerEntity> playerList) {
//        return vnnoxAPIUtil.reboot(
//                playerList.stream().map(
//                        item -> item.getPlayerId()
//                ).collect(Collectors.toList())
//        );
//    }
    //重启
    public Map<String, Object> reboot(List<NovaPushResultVO> nova) {
        List<String> playerIds = new ArrayList<>();
        nova.forEach(item -> {
            if (item.getName() == null || item.getPlayerId() == null) {
                throw new BusinessException("playerId或playerName为空");
            }
            if (item.getPlayerId().isEmpty() || item.getName().isEmpty()) {
                throw new BusinessException("playerId或playerName为空");
            }
            playerIds.add(item.getPlayerId());
        });
        VnnoxResult reboot = vnnoxAPIUtil.reboot(playerIds);
        List<String> success = reboot.getSuccess();
        List<String> fail = reboot.getFail();
        //拼接成功失败的结果
        Map<String, Object> result = new HashMap<>();
        List<NovaPushResultVO> successList = new ArrayList<>();
        List<NovaPushResultVO> faileList = new ArrayList<>();
        List<String> finalSuccess = success;
        List<String> finalFail = fail;
        nova.forEach(n -> {
            if (finalSuccess.contains(n.getPlayerId())) {
                successList.add(n);
            } else if (finalFail.contains(n.getPlayerId())) {
                faileList.add(n);
            }
        });
        result.put("success", successList);
        result.put("fail", faileList);
        return result;
    }
    /**
     * 同步播放
     *
     * @param
     * @return
     */
    public Map<String, Object> syncPlay(List<NovaPushResultVO> nova, Integer option) {
        List<String> playerIds = new ArrayList<>();
        nova.forEach(item -> {
            if (item.getName() == null || item.getPlayerId() == null) {
                throw new BusinessException("playerId或playerName为空");
            }
            if (item.getPlayerId().isEmpty() || item.getName().isEmpty()) {
                throw new BusinessException("playerId或playerName为空");
            }
            playerIds.add(item.getPlayerId());
        });
        VnnoxResult vnnoxResult = vnnoxAPIUtil.syncPlay(playerIds, option);
        List<String> success = vnnoxResult.getSuccess();
        List<String> fail = vnnoxResult.getFail();
        //拼接成功失败的结果
        Map<String, Object> result = new HashMap<>();
        List<NovaPushResultVO> successList = new ArrayList<>();
        List<NovaPushResultVO> faileList = new ArrayList<>();
        List<String> finalSuccess = success;
        List<String> finalFail = fail;
        nova.forEach(n -> {
            if (finalSuccess.contains(n.getPlayerId())) {
                successList.add(n);
            } else if (finalFail.contains(n.getPlayerId())) {
                faileList.add(n);
            }
        });
        result.put("success", successList);
        result.put("fail", faileList);
        return result;
    }
    /**
     * 对时
     *
     * @param
     * @return
     */
    public Map<String, Object> ntpSync(List<NovaPushResultVO> nova, boolean enable) {
        List<String> playerIds = new ArrayList<>();
        nova.forEach(item -> {
            if (item.getName() == null || item.getPlayerId() == null) {
                throw new BusinessException("playerId或playerName为空");
            }
            if (item.getPlayerId().isEmpty() || item.getName().isEmpty()) {
                throw new BusinessException("playerId或playerName为空");
            }
            playerIds.add(item.getPlayerId());
        });
        VnnoxResult vnnoxResult = vnnoxAPIUtil.ntpSync(playerIds, enable);
        List<String> success = vnnoxResult.getSuccess();
        List<String> fail = vnnoxResult.getFail();
        //拼接成功失败的结果
        Map<String, Object> result = new HashMap<>();
        List<NovaPushResultVO> successList = new ArrayList<>();
        List<NovaPushResultVO> faileList = new ArrayList<>();
        List<String> finalSuccess = success;
        List<String> finalFail = fail;
        nova.forEach(n -> {
            if (finalSuccess.contains(n.getPlayerId())) {
                successList.add(n);
            } else if (finalFail.contains(n.getPlayerId())) {
                faileList.add(n);
            }
        });
        result.put("success", successList);
        result.put("fail", faileList);
        return result;
    }
    public boolean updateDataName(Long id, String name) {
        LedPlayerEntity byId = ledPlayerEntityService.getById(id);
        if (byId == null) {
            throw new BusinessException("未找到该设备");
        }
        if (SecurityUtils.getClientId() != null) {
            boolean belong = SpringContextHolder.getBean(PoleBindingService.class).isBelong(byId.getSn(), PoleBindingEnums.VONNOX);
            if (!belong) {
                throw new BusinessException("该设备不属于您,不能修改设备信息");
            }
        }
        LedPlayerEntity led = new LedPlayerEntity();
        led.setId(id);
        led.setName(name);
        /**
         * 诺瓦设备编辑 日志记录开始
         */
        List<String> listCode = new ArrayList<>(1);
        listCode.add(byId.getSn());
        String content = "{ 设备id:" + led.getId() + "设备code:" + byId.getSn() + "诺瓦playId:" + led.getPlayerId() + "诺瓦设备原名:" + byId.getName() + "诺瓦设备更改后的名称:" + led.getName() + "}";
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "诺瓦设备编辑", content);
        /**
         * 诺瓦设备编辑 日志记录结束
         */
        return ledPlayerEntityService.updateById(led);
    }
    /**
     * 获取缓存数据
     * @param list
     *
     * @param
     * @return
     */
    public List<LedPlayerEntity> setCacheInfo(List<LedPlayerEntity> list) {
        for(LedPlayerEntity playerEntity: list) {
            String playerId = playerEntity.getPlayerId();
            LedPlayerInfoEntity cache = new LedPlayerInfoEntity();
            //  设置屏幕状态
            String result = redisUtils.get(VnnoxConstant.REDIS_SCREEN_STATUS + playerId);
            Integer screenStatus = 1;
            if(null != result) {
                screenStatus = Integer.parseInt(result);
    public List<LedPlayerEntity> setCacheInfo(List<LedPlayerEntity> ledList) {
        List<String> temp = new ArrayList<>();
        for (LedPlayerEntity item : ledList) {
            String playerId = item.getPlayerId();
            if (redisUtils.get(VnnoxConstant.REDIS_VOL + playerId) == null
                    || redisUtils.get(VnnoxConstant.REDIS_BRIGHTNESS + playerId) == null) {
                temp.add(item.getPlayerId());
            }
            cache.setScreenStatus(screenStatus);
            //  设置屏幕音量
            result = redisUtils.get(VnnoxConstant.REDIS_VOL + playerId);
            Integer vol = 0;
            if(null != result) {
                vol = Integer.parseInt(result);
            }
            cache.setVol(vol);
            //  设置屏幕亮度
            result = redisUtils.get(VnnoxConstant.REDIS_BRIGHTNESS + playerId);
            Integer brightness = 0;
            if(null != result) {
                brightness = Integer.parseInt(result);
            }
            cache.setBrightness(brightness);
            //  设置缩略图访问路径
            result = redisUtils.get(VnnoxConstant.REDIS_SCREEN_SHOT + playerId);
            System.out.println(playerId+"的路径结果:-----------------------------"+result);
            if(null != result) {
                playerEntity.setScreenShotUrl(result);
            } else {
                playerEntity.setScreenShotUrl("");
            }
            playerEntity.setPlayerInfo(cache);
        }
        return list;
        if (temp.size() > 0) {
            List<List<String>> split = CollectionUtil.split(temp, 100);
            for (List<String> playIds : split) {
                System.out.println(playIds.size());
                vnnoxAPIUtil.asyncStatus(playIds);
            }
        }
        List<List<LedPlayerEntity>> split = CollectionUtil.split(ledList, 100);
        for (List<LedPlayerEntity> list : split) {
            for (LedPlayerEntity playerEntity : list) {
                String playerId = playerEntity.getPlayerId();
                LedPlayerInfoEntity cache = new LedPlayerInfoEntity();
                //  设置屏幕状态
                String result = redisUtils.get(VnnoxConstant.REDIS_SCREEN_STATUS + playerId);
                Integer screenStatus = 1;
                if (null != result) {
                    screenStatus = Integer.parseInt(result);
                }
                cache.setScreenStatus(screenStatus);
                //  设置屏幕音量
                result = redisUtils.get(VnnoxConstant.REDIS_VOL + playerId);
                Integer vol = 0;
                if (null != result) {
                    vol = Float.valueOf(result).intValue();
                }
                cache.setVol(vol);
                //  设置屏幕亮度
                result = redisUtils.get(VnnoxConstant.REDIS_BRIGHTNESS + playerId);
                Integer brightness = 0;
                if (null != result) {
                    brightness = Float.valueOf(result).intValue();
                }
                cache.setBrightness(brightness);
                //  设置缩略图访问路径
                result = redisUtils.get(VnnoxConstant.REDIS_SCREEN_SHOT + playerId);
                if (null != result) {
                    playerEntity.setScreenShotUrl(result);
                } else {
                    playerEntity.setScreenShotUrl("");
                }
                playerEntity.setPlayerInfo(cache);
            }
        }
        return ledList;
    }
    public VnnoxResult publishWaterData(String playerId, A5AtmosphereHeartbeatReportInnerFrame.HeartBeatDataPackage poleSensorEntity) {
        List<String> playerIds = new ArrayList<>();
        playerIds.add(playerId);
        AirDataProgram playerProgram = new AirDataProgram();
        playerProgram.setPlayerIds(playerIds);
        PlayerPage playerPage = new PlayerPage();
        playerPage.setName("AirData page");
        List<PlayerWidget> widgetList = generateWaterAndSensorWidget(poleSensorEntity);
        playerPage.setWidgets(widgetList);
        List<PlayerPage> list = new ArrayList<>();
        list.add(playerPage);
        playerProgram.setPages(list);
        playerProgram.setNoticeUrl(VnnoxConstant.NOTIFY_URL);
        /**
         * 诺瓦推送大气数据 日志记录开始
         ////         */
//        String content = "{诺瓦playerId:" + playerId + ",节目内容:" + JSON.toJSONString(playerProgram) + " }";
//        StoreOperationRecordsUtils.storeOperationData(null, null, "大气数据推送到诺瓦", content);
        /**
         * 诺瓦推送大气数据 日志记录结束
         */
        return vnnoxProgramAPIUtil.normalProgram(playerProgram).getData();
    }
    private List<PlayerWidget> generateWaterAndSensorWidget(A5AtmosphereHeartbeatReportInnerFrame.HeartBeatDataPackage poleSensorEntity) {
        List widgetList = new ArrayList<>();
        List<String> list = new ArrayList<>();
        if (Objects.nonNull(poleSensorEntity)) {
            list.add("环境监测:");
            list.add("温度:" + poleSensorEntity.getTemperature() + "°");
            list.add("湿度:" + poleSensorEntity.getHumidity() + "%");
            list.add("pm25:" + poleSensorEntity.getPm25() + "μg/m³");
            list.add("pm10:" + poleSensorEntity.getPm10() + "μg/m³");
            list.add("甲醛:" + poleSensorEntity.getEch2o() + "μg/m³");
            list.add("CO2:" + poleSensorEntity.getCo2() + "ppm");
        }
        widgetList.add(new PlayerWidget().generateScrollUpTextWidget("weather", 1, 100000L, list, "#FFFFFF", 15, new PlayerLayout("0%", "5%", "100%", "100%")));
        return widgetList;
    }
    /**
     * 更新诺瓦屏幕分辨率
     */
    public void saveInfo() {
        List<LedPlayerEntity> list = vnnoxAPIUtil.saveCurrentInfo(ledPlayerEntityService.list());
        boolean b = SpringContextHolder.getBean(LedPlayerEntityService.class).updateBatchById(list);
        if (b) {
            LogUtils.error("更新诺瓦屏幕分辨率成功");
        }
    }
    public void asyncStatus(List<String> playerIds) {
        VnnoxResultResponse vnnoxResultResponse = vnnoxAPIUtil.asyncStatus(playerIds);
    }
}