2021与蓝度共同重构项目,服务端
liuhaonan
2022-08-10 ada0b243f16f12b5025253663eacfe53055f8310
ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java
@@ -34,6 +34,7 @@
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.domain.PoleBinding;
import com.sandu.ximon.dao.enums.DeviceRespStatusEnums;
import com.sandu.ximon.dao.enums.OrderByEnums;
import com.sandu.ximon.dao.mapper.LightMapper;
import com.sandu.ximon.dao.mapper.LightTaskMapper;
import lombok.AllArgsConstructor;
@@ -72,7 +73,7 @@
        Boolean hasKey = redisService.hasKey(LightKey.REPORT_MAC.key(deviceName));
        if (!hasKey) {
            int count = count(Wrappers.lambdaQuery(Light.class).eq(Light::getDeviceCode, deviceName));
            log.info("redis查不到路灯数据{}={}", count, deviceName);
            log.info("redis查不到路灯数据 数量:{}={}", count, deviceName);
            // 当前路灯表没有录入设备吗
            if (count == 0) {
                Light light = new Light();
@@ -80,15 +81,15 @@
                light.setLightPercent(heartBeatDataPackage.getLightPercent());
                light.setLight2Percent(heartBeatDataPackage.getLight2Percent());
                save(light);
                log.info("新增路灯");
            }
            redisService.set(LightKey.REPORT_MAC.key(deviceName), 1, LightKey.REPORT_MAC.expireSeconds());
            log.info("新增路灯");
        } else {
            Light light = new Light();
            light.setLightPercent(heartBeatDataPackage.getLightPercent());
            light.setLight2Percent(heartBeatDataPackage.getLight2Percent());
            update(light, Wrappers.lambdaUpdate(Light.class).eq(Light::getDeviceCode, deviceName));
            log.info("更新路灯亮度");
            boolean update = update(light, Wrappers.lambdaUpdate(Light.class).eq(Light::getDeviceCode, deviceName));
            log.info("更新路灯亮度 {}", update);
        }
    }
@@ -97,10 +98,43 @@
     *
     * @return 返回组合数据dto
     */
    public List<LightBo> listLight(int pageNo, int pageSize, String keyword) {
    public List<LightBo> listLight(int pageNo, int pageSize, String keyword, Integer order, Integer seq) {
        Long clientId = SecurityUtils.getClientId();
      //  PageHelper.startPage(pageNo, pageSize);
        //排序字段
        String orderByResult = "light_id";
        //正序、倒叙
        String orderBySeq = OrderByEnums.ASC.getCode();
        if (order != null) {
            switch (order) {
                case 1:
                    orderByResult = OrderByEnums.LIGHT_POLE_NAME.getCode();
                    break;
                case 2:
                    orderByResult = OrderByEnums.LIGHT_UPDATE_TIME.getCode();
                    break;
                case 3:
                    orderByResult = OrderByEnums.LIGHT_CREATE_TIME.getCode();
                    break;
                default:
            }
        }
        if (seq != null) {
            switch (seq) {
                case 1:
                    orderBySeq = OrderByEnums.ASC.getCode();
                    break;
                case 2:
                    orderBySeq = OrderByEnums.DESC.getCode();
                    break;
                default:
                    break;
            }
        }
        //排序方式
        String orderBy = orderByResult + " " + orderBySeq;
        PageHelper.startPage(pageNo, pageSize, orderBy);
        List<LightBo> listLight = baseMapper.listLight(clientId, keyword);
@@ -109,19 +143,19 @@
        //拆分list
        List<List<String>> split = CollectionUtil.split(deviceCodeList, 100);
        List<BatchGetDeviceStateResponse.DeviceStatus> deviceStatuses=null;
        List<BatchGetDeviceStateResponse.DeviceStatus> deviceStatuses = null;
        for (List<String> splist : split) {
            deviceStatuses=MainBoardInvokeSyncService.getInstance().batchGetDeviceState(splist);
            deviceStatuses = MainBoardInvokeSyncService.getInstance().batchGetDeviceState(splist);
            if (deviceStatuses != null) {
                for (LightBo lightBo : listLight) {
                    for (BatchGetDeviceStateResponse.DeviceStatus deviceStatus : deviceStatuses) {
                        if (lightBo.getDeviceCode() != null && lightBo.getDeviceCode().equals(deviceStatus.getDeviceName())) {
                            if("ONLINE".equals(deviceStatus.getStatus()) ){
                            if ("ONLINE".equals(deviceStatus.getStatus())) {
                                lightBo.setOnlineStatus(0);
                            }else if("OFFLINE".equals(deviceStatus.getStatus())){
                            } else if ("OFFLINE".equals(deviceStatus.getStatus())) {
                                lightBo.setOnlineStatus(1);
                            }else {
                            } else {
                                lightBo.setOnlineStatus(2);
                            }
@@ -230,7 +264,9 @@
                map.put("deviceCode", param.getDeviceCode());
                WrapResponseCommonFrame<A5LightBrightnessRespInnerFrame> frame
                        = MainBoardInvokeSyncService.getInstance().sendRRPC(param.getDeviceCode(), a5Frame, A5LightBrightnessRespInnerFrame.class);
                //存储控制帧指令
                StoreOperationRecordsUtils.storeInnerFrameData(param.getDeviceCode(), "单灯帧-亮度控制", a5Frame, frame);
                if (frame == null) {
                    map.put("status", DeviceRespStatusEnums.OTHER_ERROR.getCode());
                    resultList.add(map);
@@ -334,7 +370,7 @@
                currentWeekValue = 7;
            }
            map = new LinkedHashMap();
            map.put("time",localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            map.put("time", localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            map.put("value", getlist(currentWeekValue));
            temp.add(map);
        }
@@ -406,7 +442,7 @@
                currentWeekValue = 7;
            }
            map = new LinkedHashMap();
            map.put("time",localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            map.put("time", localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            map.put("value", getlistEnergy(currentWeekValue));
            temp.add(map);
        }
@@ -486,9 +522,6 @@
    public BigDecimal getlistEnergy(Integer week) {
        Long clientId = SecurityUtils.getClientId();
//        List<LightBo> listLight = baseMapper.listLight(clientId, null);
//        List<String> deviceCodeList = listLight.stream().map(LightBo::getDeviceCode).collect(Collectors.toList());
        //获取到正在执行的任务列表
        List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask();
@@ -598,6 +631,61 @@
         */
    }
    /**
     * 单个任务一天的节能率
     *
     * @return
     */
    public BigDecimal jisuan(LightTaskDto Task) {
        final BigDecimal[] bigDecimalResult = {new BigDecimal(0.00)};
        /**
         * 节能率计算开始
         */
        //获取昨天的星期数
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime yesterday = now.minusDays(1);
        int week = yesterday.getDayOfWeek().getValue();
        //判断Task.getWeekList()是否包含昨天的星期数   不包含直接返回0  不进行计算
        if (Task.getWeekList() != null && !Task.getWeekList().isEmpty()) {
            if (!Task.getWeekList().contains(week)) {
                return new BigDecimal(1);
            }
        } else {
            return new BigDecimal(1);
        }
        ControlLightCommandVO startTime = parseSwitchLightCommand(Task.getOpenOrder());
        ControlLightCommandVO endTime = parseSwitchLightCommand(Task.getCloseOrder());
        //存放节能率
        //获取到单灯任务的节能率
        if (Task.getControlOrder() != null && !Task.getControlOrder().isEmpty()) {//有控等指令 拆分计算
            List<ControlLightCommandVO> controlLightCommandVOS = parseControlLightCommand(Task.getControlOrder());
            for (int i = 0; i < controlLightCommandVOS.size(); i++) {
                BigDecimal bigDecimal = calculateEnergySaving(startTime, controlLightCommandVOS.get(i));
                bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
                if (controlLightCommandVOS.size() == (i + 1)) {
                    BigDecimal bigDecimal1 = calculateEnergySaving(controlLightCommandVOS.get(i), endTime);
                    bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal1);
                    break;
                }
                startTime = controlLightCommandVOS.get(i);
//                    saving.add(bigDecimal);
            }
        } else {//无控灯指令 直接计算
            BigDecimal bigDecimal = calculateEnergySaving(startTime, endTime);
//                saving.add(bigDecimal);
            bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
        }
        return bigDecimalResult[0];
        /**
         * 节能率计算结束
         */
    }
    /**
     * 一天的能耗
@@ -623,20 +711,22 @@
                for (int i = 0; i < controlLightCommandVOS.size(); i++) {
                    //得到时长*亮度
                    BigDecimal bigEnergy1 = calculateEnergyConsumption(startTime, controlLightCommandVOS.get(i));
//                    //计算能耗(总   时长*亮度*功率)
                    BigDecimal totalEnergy = totalEnergy(bigEnergy1, Task.getLightAdress(), poleTaskLightPowerBos);
                    bigEnergy[0] = bigEnergy[0].add(totalEnergy);
//                    BigDecimal totalEnergy4 = totalEnergy(bigEnergy1, Task.getLightAdress(), poleTaskLightPowerBos);
//                    bigEnergy[0] = bigEnergy[0].add(totalEnergy4);
                    //判断 Energy==0 则不计算
                    if (bigEnergy1.compareTo(new BigDecimal(0)) != 0) {
                        //计算能耗(总   时长*亮度*功率)
                        BigDecimal totalEnergy = totalEnergy(bigEnergy1, Task.getLightAdress(), poleTaskLightPowerBos);
                        bigEnergy[0] = bigEnergy[0].add(totalEnergy);
                    }
                    if (controlLightCommandVOS.size() == (i + 1)) {
                        BigDecimal bigEnergy2 = calculateEnergyConsumption(controlLightCommandVOS.get(i), endTime);
                        System.out.println(bigEnergy2 + "时长*亮度===========================");
                        //计算能耗(总   时长*亮度*功率)
                        BigDecimal totalEnergy3 = totalEnergy(bigEnergy2, Task.getLightAdress(), poleTaskLightPowerBos);
//                        saving.add(bigDecimal1);
                        bigEnergy[0] = bigEnergy[0].add(totalEnergy3);
                        if (bigEnergy2.compareTo(new BigDecimal(0)) != 0) {
                            BigDecimal totalEnergy3 = totalEnergy(bigEnergy2, Task.getLightAdress(), poleTaskLightPowerBos);
                            bigEnergy[0] = bigEnergy[0].add(totalEnergy3);
                        }
                        break;
                    }
                    startTime = controlLightCommandVOS.get(i);
@@ -646,9 +736,14 @@
                //计算能耗(部分   时长*亮度)
                BigDecimal Energy = calculateEnergyConsumption(startTime, endTime);
                //计算能耗(总   时长*亮度*功率)
                BigDecimal totalEnergy = totalEnergy(Energy, Task.getLightAdress(), poleTaskLightPowerBos);
                //判断 Energy==0 则不计算
                if (Energy.compareTo(new BigDecimal(0)) != 0) {
                    BigDecimal totalEnergy = totalEnergy(Energy, Task.getLightAdress(), poleTaskLightPowerBos);
                    bigEnergy[0] = bigEnergy[0].add(totalEnergy);
                }
//                saving.add(bigDecimal);
                bigEnergy[0] = bigEnergy[0].add(totalEnergy);
            }
        });
        return bigEnergy[0];
@@ -657,6 +752,87 @@
         * 计算能耗结束
         */
    }
    /**
     * 一天的能耗
     *
     * @param
     * @return
     */
    public BigDecimal jisuanEnergy(LightTaskDto Task) {
        //存放能耗
        final BigDecimal[] bigEnergy = {new BigDecimal(0.00)};
        //获取昨天的星期数
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime yesterday = now.minusDays(1);
        int week = yesterday.getDayOfWeek().getValue();
        //判断Task.getWeekList()是否包含昨天的星期数   不包含直接返回0  不进行计算
        if (Task.getWeekList() != null && !Task.getWeekList().isEmpty()) {
            if (!Task.getWeekList().contains(week)) {
                return BigDecimal.ZERO;
            }
        } else {
            return BigDecimal.ZERO;
        }
        /**
         * 节能率计算开始
         */
        List<PoleTaskLightPowerBo> poleTaskLightPowerBos = SpringContextHolder.getBean(LightTaskMapper.class).listLightTaskByTaskId(Task.getTaskId());
        ControlLightCommandVO startTime = parseSwitchLightCommand(Task.getOpenOrder());
        ControlLightCommandVO endTime = parseSwitchLightCommand(Task.getCloseOrder());
        //获取到单灯任务的节能率
        if (!Task.getControlOrder().isEmpty()) {//有控等指令 拆分计算
            List<ControlLightCommandVO> controlLightCommandVOS = parseControlLightCommand(Task.getControlOrder());
            for (int i = 0; i < controlLightCommandVOS.size(); i++) {
                //得到时长*亮度
                BigDecimal bigEnergy1 = calculateEnergyConsumption(startTime, controlLightCommandVOS.get(i));
                //判断 Energy==0 则不计算
                if (bigEnergy1.compareTo(new BigDecimal(0)) != 0) {
                    //计算能耗(总   时长*亮度*功率)
                    BigDecimal totalEnergy = totalEnergy(bigEnergy1, Task.getLightAdress(), poleTaskLightPowerBos);
                    bigEnergy[0] = bigEnergy[0].add(totalEnergy);
                }
                if (controlLightCommandVOS.size() == (i + 1)) {
                    BigDecimal bigEnergy2 = calculateEnergyConsumption(controlLightCommandVOS.get(i), endTime);
                    System.out.println(bigEnergy2 + "时长*亮度===========================");
                    //计算能耗(总   时长*亮度*功率)
                    if (bigEnergy2.compareTo(new BigDecimal(0)) != 0) {
                        BigDecimal totalEnergy3 = totalEnergy(bigEnergy2, Task.getLightAdress(), poleTaskLightPowerBos);
                        bigEnergy[0] = bigEnergy[0].add(totalEnergy3);
                    }
                    break;
                }
                startTime = controlLightCommandVOS.get(i);
                //计算能耗(总   时长*亮度*功率)
            }
        } else {//无控灯指令 直接计算
            //计算能耗(部分   时长*亮度)
            BigDecimal Energy = calculateEnergyConsumption(startTime, endTime);
            //计算能耗(总   时长*亮度*功率)
            //判断 Energy==0 则不计算
            if (Energy.compareTo(new BigDecimal(0)) != 0) {
                BigDecimal totalEnergy = totalEnergy(Energy, Task.getLightAdress(), poleTaskLightPowerBos);
                bigEnergy[0] = bigEnergy[0].add(totalEnergy);
            }
//                saving.add(bigDecimal);
        }
        return bigEnergy[0];
        /**
         * 计算能耗结束
         */
    }
    private BigDecimal totalEnergy(BigDecimal bigEnergy2, String lightAdress, List<PoleTaskLightPowerBo> poleTaskLightPowerBos) {
@@ -723,6 +899,9 @@
//        BigDecimal hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
//        BigDecimal hour;
        if (v1.getBrightness() == 0) {
            return new BigDecimal(1);
        }
        double hour;
        double min;
        //计算时长
@@ -746,8 +925,13 @@
        //计算时长
        BigDecimal totalTime = BigDecimal.valueOf(hour * 60 + min);
        //计算节能率 ( (1-v1.getBrightness()/100)*totalTime/3600 )   保留两位小数
        BigDecimal energySaving = BigDecimal.valueOf((1 - v1.getBrightness() / 100) * totalTime.doubleValue() / 3600).setScale(2, BigDecimal.ROUND_HALF_UP);
        //计算节能率 ( (1-v1.getBrightness()/100)*totalTime/1440 )   保留两位小数
        // 计算 1-v1.getBrightness()/100的值
        BigDecimal Brightness = BigDecimal.valueOf(100 - v1.getBrightness());
        //Brightness/100*totalTime/1440  保留两位小数       Brightness没有除以100  在总时间*100
        BigDecimal energySaving = Brightness.multiply(totalTime).divide(new BigDecimal(144000), 2, BigDecimal.ROUND_HALF_UP);
        System.out.println(energySaving + "节能率");
        return energySaving;
    }
@@ -755,27 +939,34 @@
    //计算能耗(部分    时长*亮度)
    public BigDecimal calculateEnergyConsumption(ControlLightCommandVO v1, ControlLightCommandVO v2) {
        if (v1.getBrightness() == 0) {
            return BigDecimal.ZERO;
        }
        double hour;
        double min;
        //计算时长
        BigDecimal hour;
        if (v2.getHour() > v1.getHour()) {
            hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
            hour = v2.getHour() - v1.getHour();
        } else if (v2.getHour() < v1.getHour()) {
            hour = BigDecimal.valueOf((v2.getHour() + 24 - v1.getHour()));
            hour = 24 + (v2.getHour() - v1.getHour());
        } else {
            hour = BigDecimal.valueOf(0);
            hour = 0;
        }
        BigDecimal min;
        //计算分钟
        if (v2.getMin() > v1.getMin()) {
            min = BigDecimal.valueOf((v2.getMin() - v1.getMin()) / 60);
            min = v2.getMin() - v1.getMin();
        } else if (v2.getMin() < v1.getMin()) {
            min = BigDecimal.valueOf((v2.getMin() + 60 - v1.getMin()) / 60);
            hour = hour.subtract(BigDecimal.valueOf(1));
            min = 60 + (v1.getMin() - v2.getMin());
            hour = hour - 1;
        } else {
            min = BigDecimal.valueOf(0);
            min = 0;
        }
        BigDecimal totalTime = (hour.add(min));
        //计算时长
        BigDecimal totalTime = BigDecimal.valueOf(hour + min / 60);
        //计算亮灯时长*亮度
        BigDecimal energyConsumption = totalTime.multiply(BigDecimal.valueOf(v1.getBrightness())).divide(BigDecimal.valueOf(100));
        BigDecimal energyConsumption = totalTime.multiply(BigDecimal.valueOf(v1.getBrightness())).divide(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        return energyConsumption;
    }
@@ -798,6 +989,21 @@
            lightList.add(light);
        }
        return lightList;
    }
    /**
     * 用户拥有的路灯(用于首页数据统计)
     *
     * @return
     */
    public List<LightBo> listLightOnHome() {
        List<LightBo> listLight;
        if (SecurityUtils.getClientId() != null) {
            listLight = baseMapper.listLight(SecurityUtils.getUserId(), null);
        } else {
            listLight = baseMapper.listLight(null, null);
        }
        return listLight;
    }
    /**
@@ -825,6 +1031,16 @@
        lights.forEach(light -> {
            light.setPower1(lightPowerSettingParam.getPower1());
            light.setPower2(lightPowerSettingParam.getPower2());
            if (lightPowerSettingParam.getPower1() != null) {
                light.setLight1(1);
            } else {
                light.setLight1(0);
            }
            if (lightPowerSettingParam.getPower2() != null) {
                light.setLight2(1);
            } else {
                light.setLight2(0);
            }
            light.setLightCount(lightPowerSettingParam.getLightCount());
        });
        return updateBatchById(lights);