2021与蓝度共同重构项目,服务端
zhanzhiqin
2022-05-16 b6dccb271639d18249f796a51a492ed1a4c3f2a6
ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java
@@ -23,7 +23,9 @@
import com.sandu.ximon.admin.param.LightRemarkParam;
import com.sandu.ximon.admin.redis.LightKey;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.admin.vo.ControlLightCommandVO;
import com.sandu.ximon.admin.vo.EquipmentInfomation;
import com.sandu.ximon.dao.bo.LightBo;
import com.sandu.ximon.dao.bo.PoleTaskLightPowerBo;
import com.sandu.ximon.dao.domain.Light;
@@ -146,6 +148,33 @@
        return one;
    }
    /**
     * 首页灯杆绑定信息
     *
     * @param deviceCode
     * @return
     */
    public EquipmentInfomation getLightInfo(String deviceCode) {
        EquipmentInfomation equipmentInfo = new EquipmentInfomation();
        equipmentInfo.setEquipmentType("单灯");
        if (deviceCode == null || deviceCode.trim().length() == 0) {
            return equipmentInfo;
        }
        Light one = getOne(Wrappers.<Light>lambdaQuery().eq(Light::getDeviceCode, deviceCode));
        if (one != null) {
            equipmentInfo.setEquipmentMac(one.getDeviceCode());
            equipmentInfo.setEquipmentCreateTime(one.getCreateTime());
        }
        Object o = redisService.get(LightKey.REPORT_MAC.key(deviceCode));
        if (o != null) {
            equipmentInfo.setEquipmentState("在线");
        } else {
            equipmentInfo.setEquipmentState("离线");
        }
        return equipmentInfo;
    }
    public boolean addRemark(LightRemarkParam param) {
        Light light = getById(param.getLightId());
        if (light == null) {
@@ -174,7 +203,9 @@
            Map<String, Object> map = new HashMap<>();
            try {
                map.put("deviceCode", param.getDeviceCode());
                WrapResponseCommonFrame<A5LightBrightnessRespInnerFrame> frame = MainBoardInvokeSyncService.getInstance().sendRRPC(param.getDeviceCode(), a5Frame, A5LightBrightnessRespInnerFrame.class);
                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);
@@ -196,6 +227,20 @@
                resultList.add(map);
            }
        }
        /**
         * 服务端批量控灯日志记录开始
         */
        String content = "{控灯请求:" + paramList.toString()
                + ", 控灯结果:" + resultList.toString() + "}";
        List<String> codeList = new ArrayList<>();
        for (LightControlParam bean : paramList) {
            codeList.add(bean.getDeviceCode());
        }
        StoreOperationRecordsUtils.storeOperationData(codeList, null, "服务端批量控灯", content);
        /**
         * 服务端批量控灯日志记录结束
         */
        return resultList;
    }
@@ -259,7 +304,6 @@
            if (week < 1) {
                week = 7;
            }
            map.put(i, "周" + week);
            map.put(i, getlist(week));
        }
@@ -327,8 +371,7 @@
            if (week < 1) {
                week = 7;
            }
            map.put(i, "周" + week);
            map.put(i, getlist(week));
            map.put(i, getlistEnergy(week));
        }
@@ -480,7 +523,6 @@
     */
    public BigDecimal jisuan(List<LightTaskDto> list) {
        final BigDecimal[] bigDecimalResult = {new BigDecimal(0.00)};
        final BigDecimal[] bigEnergy = {new BigDecimal(0.00)};
        /**
         * 节能率计算开始
         */
@@ -490,35 +532,26 @@
            ControlLightCommandVO endTime = parseSwitchLightCommand(Task.getCloseOrder());
            //存放节能率
            //获取到单灯任务的节能率
            if (Task.getControlOrder() != null) {//有控等指令 拆分计算
            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));
                    BigDecimal bigEnergy1 = calculateEnergyConsumption(startTime, controlLightCommandVOS.get(i));
                    bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
                    if (controlLightCommandVOS.size() == (i + 1)) {
                        BigDecimal bigDecimal1 = calculateEnergySaving(controlLightCommandVOS.get(i), endTime);
                        BigDecimal bigEnergy2 = calculateEnergyConsumption(controlLightCommandVOS.get(i), endTime);
                        System.out.println(bigDecimal1 + "===========================");
                        System.out.println(bigEnergy + "时长*亮度===========================");
//                        System.out.println(bigDecimal1 + "===========================");
//                        saving.add(bigDecimal1);
                        bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal1);
                        bigEnergy[0] = bigEnergy[0].add(bigEnergy2);
                        break;
                    }
                    startTime = controlLightCommandVOS.get(i);
//                    saving.add(bigDecimal);
                    bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
                    bigEnergy[0] = bigEnergy[0].add(bigEnergy1);
                }
            } else {//无控灯指令 直接计算
                BigDecimal bigDecimal = calculateEnergySaving(startTime, endTime);
                BigDecimal Energy = calculateEnergyConsumption(startTime, endTime);
//                saving.add(bigDecimal);
                bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
                bigEnergy[0] = bigEnergy[0].add(Energy);
            }
        });
        return bigDecimalResult[0];
@@ -546,31 +579,34 @@
            ControlLightCommandVO startTime = parseSwitchLightCommand(Task.getOpenOrder());
            ControlLightCommandVO endTime = parseSwitchLightCommand(Task.getCloseOrder());
            //获取到单灯任务的节能率
            if (Task.getControlOrder() != null) {//有控等指令 拆分计算
            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));
//                    //计算能耗(总   时长*亮度*功率)
                    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);
                    if (controlLightCommandVOS.size() == (i + 1)) {
                        BigDecimal bigEnergy2 = calculateEnergyConsumption(controlLightCommandVOS.get(i), endTime);
                        System.out.println(bigEnergy + "时长*亮度===========================");
                        System.out.println(bigEnergy2 + "时长*亮度===========================");
                        //计算能耗(总   时长*亮度*功率)
                        BigDecimal totalEnergy = totalEnergy(bigEnergy2, Task.getLightAdress(), poleTaskLightPowerBos);
                        BigDecimal totalEnergy3 = totalEnergy(bigEnergy2, Task.getLightAdress(), poleTaskLightPowerBos);
//                        saving.add(bigDecimal1);
                        bigEnergy[0] = bigEnergy[0].add(totalEnergy);
                        bigEnergy[0] = bigEnergy[0].add(totalEnergy3);
                        break;
                    }
                    startTime = controlLightCommandVOS.get(i);
                    //计算能耗(总   时长*亮度*功率)
                    BigDecimal totalEnergy = totalEnergy(bigEnergy1, Task.getLightAdress(), poleTaskLightPowerBos);
                    bigEnergy[0] = bigEnergy[0].add(totalEnergy);
                }
            } else {//无控灯指令 直接计算
                BigDecimal bigDecimal = calculateEnergySaving(startTime, endTime);
                //计算能耗(部分   时长*亮度)
                BigDecimal Energy = calculateEnergyConsumption(startTime, endTime);
                //计算能耗(总   时长*亮度*功率)
                BigDecimal totalEnergy = totalEnergy(Energy, Task.getLightAdress(), poleTaskLightPowerBos);
@@ -648,12 +684,34 @@
    //计算节能率
    public BigDecimal calculateEnergySaving(ControlLightCommandVO v1, ControlLightCommandVO v2) {
        BigDecimal hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
        BigDecimal min = BigDecimal.valueOf((v2.getMin() - v1.getMin()) / 60);
        BigDecimal totalTime = (hour.add(min));
//        BigDecimal hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
//        BigDecimal hour;
        double hour;
        double min;
        //计算时长
        if (v2.getHour() > v1.getHour()) {
            hour = v2.getHour() - v1.getHour();
        } else if (v2.getHour() < v1.getHour()) {
            hour = 24 + (v2.getHour() - v1.getHour());
        } else {
            hour = 0;
        }
        //计算分钟
        BigDecimal energySaving = BigDecimal.valueOf((100 - v1.getBrightness())).divide(BigDecimal.valueOf(100)).multiply(totalTime).divide(BigDecimal.valueOf(24), 2);
        System.out.println(energySaving);
        if (v2.getMin() > v1.getMin()) {
            min = v2.getMin() - v1.getMin();
        } else if (v2.getMin() < v1.getMin()) {
            min = 60 + (v1.getMin() - v2.getMin());
            hour = hour - 1;
        } else {
            min = 0;
        }
        //计算时长
        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);
        System.out.println(energySaving + "节能率");
        return energySaving;
    }
@@ -661,11 +719,26 @@
    //计算能耗(部分    时长*亮度)
    public BigDecimal calculateEnergyConsumption(ControlLightCommandVO v1, ControlLightCommandVO v2) {
        //计算时长
        BigDecimal hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
        BigDecimal min = BigDecimal.valueOf((v2.getMin() - v1.getMin()) / 60);
        BigDecimal hour;
        if (v2.getHour() > v1.getHour()) {
            hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
        } else if (v2.getHour() < v1.getHour()) {
            hour = BigDecimal.valueOf((v2.getHour() + 24 - v1.getHour()));
        } else {
            hour = BigDecimal.valueOf(0);
        }
        BigDecimal min;
        if (v2.getMin() > v1.getMin()) {
            min = BigDecimal.valueOf((v2.getMin() - v1.getMin()) / 60);
        } else if (v2.getMin() < v1.getMin()) {
            min = BigDecimal.valueOf((v2.getMin() + 60 - v1.getMin()) / 60);
            hour = hour.subtract(BigDecimal.valueOf(1));
        } else {
            min = BigDecimal.valueOf(0);
        }
        BigDecimal totalTime = (hour.add(min));
        //计算亮灯时长*亮度
        BigDecimal energyConsumption = totalTime.multiply(BigDecimal.valueOf(v1.getBrightness()));
        BigDecimal energyConsumption = totalTime.multiply(BigDecimal.valueOf(v1.getBrightness())).divide(BigDecimal.valueOf(100));
        return energyConsumption;
    }