2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-05-26 4e91adbb41c89688fe5b0ef03807cfe748a7d59d
ximon-admin/src/main/java/com/sandu/ximon/admin/service/VnnoxService.java
@@ -1,24 +1,31 @@
package com.sandu.ximon.admin.service;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.config.VnnoxConstant;
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.utils.RedisUtils;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.admin.utils.VnnoxAPIUtil;
import com.sandu.ximon.admin.utils.VnnoxProgramAPIUtil;
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.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.util.*;
import java.util.stream.Collectors;
/**
@@ -30,12 +37,14 @@
@AllArgsConstructor
public class VnnoxService {
   // @Autowired
    private VnnoxAPIUtil vnnoxAPIUtil;
   // @Autowired
    private RedisUtils redisUtils;
   // @Autowired
    private LedPlayerEntityService ledPlayerEntityService;
    private VnnoxProgramAPIUtil vnnoxProgramAPIUtil;
    /**
     * 设备校验注册
@@ -44,7 +53,7 @@
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public LedV2RegisterResultEntity validateSN(String name,String sn) throws URISyntaxException {
    public LedV2RegisterResultEntity validateSN(String name, String sn) throws URISyntaxException {
        LedV2RegisterResultEntity ledV2RegisterResultEntity = new LedV2RegisterResultEntity();
        //  判断数据库是否存在,若不存在,判断是否已经注册到Vnnox服务器
@@ -61,13 +70,13 @@
        Integer limit = 100;
        List<VnnoxPlayerResponse> playerList;
        //  获取Vnnox服务器列表
        VnnoxPlayerListResponse response = vnnoxAPIUtil.getPlayerList(limit, page,name);
        VnnoxPlayerListResponse response = vnnoxAPIUtil.getPlayerList(limit, page, name);
        playerList = response.getRows();
        Integer total = response.getTotal() - limit;
        while (total > 0) {
            page = page + 1;
            total = total - limit;
            response = vnnoxAPIUtil.getPlayerList(limit, page,name);
            response = vnnoxAPIUtil.getPlayerList(limit, page, name);
            playerList.addAll(response.getRows());
        }
        ledPlayerEntity = new LedPlayerEntity();
@@ -105,10 +114,7 @@
            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);
        //  根据屏幕状态调整REDIS标识位
        for (String playerId : vnnoxResult.getSuccess()) {
            redisUtils.set(VnnoxConstant.REDIS_SCREEN_STATUS + playerId, screenStatus, VnnoxConstant.REDIS_MAX_SAVE_TIME);
@@ -117,10 +123,7 @@
    }
    public VnnoxResult volChange(List<LedPlayerEntity> playerList, Integer vol) {
        VnnoxResult vnnoxResult = vnnoxAPIUtil.volChange(
                playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()),
                vol
        );
        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);
@@ -129,10 +132,7 @@
    }
    public VnnoxResult brightnessChange(List<LedPlayerEntity> playerList, Integer brightness) {
        VnnoxResult vnnoxResult = vnnoxAPIUtil.brightnessChange(
                playerList.stream().map(item -> item.getPlayerId()).collect(Collectors.toList()),
                brightness
        );
        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);
@@ -141,6 +141,7 @@
    }
    //截屏
    public Map<String, String> getScreenShotUrl(Integer id) {
        Map<String, String> map = new HashMap();
@@ -182,25 +183,165 @@
        return map;
    }
//    //重启
//    public VnnoxResult reboot(List<LedPlayerEntity> playerList) {
//        return vnnoxAPIUtil.reboot(
//                playerList.stream().map(
//                        item -> item.getPlayerId()
//                ).collect(Collectors.toList())
//        );
//    }
    public VnnoxResult reboot(List<LedPlayerEntity> playerList) {
        return vnnoxAPIUtil.reboot(
                playerList.stream().map(
                        item -> item.getPlayerId()
                ).collect(Collectors.toList())
        );
    //重启
    public VnnoxResult reboot(List<String> playerList) {
        return vnnoxAPIUtil.reboot(playerList);
    }
    public boolean updateDataName(Long  id, String name){
    /**
     * 同步播放
     *
     * @param
     * @return
     */
    public VnnoxResult syncPlay(List<String> playerList, Integer option) {
        return vnnoxAPIUtil.syncPlay(playerList, option);
    }
    /**
     * 对时
     *
     * @param
     * @return
     */
    public VnnoxResult ntpSync(List<String> playerList, boolean enable) {
        return vnnoxAPIUtil.ntpSync(playerList, enable);
    }
    public boolean updateDataName(Long id, String name) {
        LedPlayerEntity byId = ledPlayerEntityService.getById(id);
        if(byId==null){
        if (byId == null) {
            throw new BusinessException("未找到该设备");
        }
        LedPlayerEntity led=new LedPlayerEntity();
        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
     * @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);
            }
            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;
    }
    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(waterDataEntity)) {
//            list.add("水质环境监测实时数据:");
//            list.add("ph:"+waterDataEntity.getPh());
//            list.add("溶解氧:"+waterDataEntity.getDoData() + "(mg/L)");
//            list.add("电导率:"+waterDataEntity.getEc() + "(10^(-6)S)");
//            list.add("浊度:"+waterDataEntity.getNtu() + "NTU");
//        }
        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;
    }
}