2021与蓝度共同重构项目,服务端
liuhaonan
2022-05-27 161b775f40d5d168cc98240165c57887ee8c8df8
ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java
@@ -3,6 +3,7 @@
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyuncs.iot.model.v20180120.BatchGetDeviceStateResponse;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.sandu.common.domain.CommonPage;
@@ -23,6 +24,7 @@
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;
@@ -32,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;
@@ -40,10 +43,9 @@
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -96,15 +98,73 @@
     *
     * @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);
        // 获取最近的上报时间
        List<String> deviceCodeList = listLight.stream().map(Light::getDeviceCode).collect(Collectors.toList());
        //拆分list
        List<List<String>> split = CollectionUtil.split(deviceCodeList, 100);
        List<BatchGetDeviceStateResponse.DeviceStatus> deviceStatuses = null;
        for (List<String> splist : split) {
            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())) {
                                lightBo.setOnlineStatus(0);
                            } else if ("OFFLINE".equals(deviceStatus.getStatus())) {
                                lightBo.setOnlineStatus(1);
                            } else {
                                lightBo.setOnlineStatus(2);
                            }
                        }
                    }
                }
            }
        }
        if (CollectionUtil.isNotEmpty(deviceCodeList)) {
            List<LightReportData> reportDataList = lightReportDataService.getNewestReportByDeviceCode(deviceCodeList);
            for (LightBo lightBo : listLight) {
@@ -202,7 +262,11 @@
            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);
@@ -225,6 +289,20 @@
            }
        }
        /**
         * 服务端批量控灯日志记录开始
         */
        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;
    }
@@ -233,7 +311,7 @@
     *
     * @return
     */
    public Map controlEnergySaving() {
    public List controlEnergySaving() {
        //获取到正在执行的任务列表
        List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask();
        LocalDateTime now = LocalDateTime.now();
@@ -248,7 +326,6 @@
        for (LightTaskDto lightTaskDto : lightTaskDtos) {
            List<Integer> weekList = lightTaskDto.getWeekList();
            System.out.println(weekList + "===========================");
            for (Integer one : weekList) {
                switch (one) {
                    case 1:
@@ -277,21 +354,29 @@
            }
        }
        Integer week = now.getDayOfWeek().getValue();
        Map map = new HashMap();
        //获取当前星期几 1-7
        int currentWeekValue = now.getDayOfWeek().getValue();
        Map map;
        List<Map> temp = new ArrayList<>();
        //获取日期 精确到天
        LocalDateTime localDateTime = now.with(LocalTime.MIN);
        for (int i = 1; i < 8; i++) {
            week--;
            if (week < 1) {
                week = 7;
            currentWeekValue--;
            if (currentWeekValue < 1) {
                currentWeekValue = 7;
            }
            map.put(i, getlist(week));
            map = new LinkedHashMap();
            map.put("time", localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            map.put("value", getlist(currentWeekValue));
            temp.add(map);
        }
        return map;
        return temp;
    }
@@ -300,7 +385,7 @@
     *
     * @return
     */
    public Map controlEnergy() {
    public List controlEnergy() {
        //获取到正在执行的任务列表
        List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask();
        LocalDateTime now = LocalDateTime.now();
@@ -344,21 +429,26 @@
            }
        }
        Integer week = now.getDayOfWeek().getValue();
        Map map = new HashMap();
        Integer currentWeekValue = now.getDayOfWeek().getValue();
        Map map;
        List<Map> temp = new ArrayList<>();
        //获取当前日期  格式为yyyy-MM-dd
        LocalDateTime localDateTime = now.with(LocalTime.MIN);
        for (int i = 1; i < 8; i++) {
            week--;
            if (week < 1) {
                week = 7;
            currentWeekValue--;
            if (currentWeekValue < 1) {
                currentWeekValue = 7;
            }
            map.put(i, getlistEnergy(week));
            map = new LinkedHashMap();
            map.put("time", localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            map.put("value", getlistEnergy(currentWeekValue));
            temp.add(map);
        }
        return map;
        return temp;
    }
@@ -432,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();
@@ -523,7 +610,7 @@
                    bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
                    if (controlLightCommandVOS.size() == (i + 1)) {
                        BigDecimal bigDecimal1 = calculateEnergySaving(controlLightCommandVOS.get(i), endTime);
                        System.out.println(bigDecimal1 + "===========================");
//                        System.out.println(bigDecimal1 + "===========================");
//                        saving.add(bigDecimal1);
                        bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal1);
                        break;
@@ -537,6 +624,61 @@
                bigDecimalResult[0] = bigDecimalResult[0].add(bigDecimal);
            }
        });
        return bigDecimalResult[0];
        /**
         * 节能率计算结束
         */
    }
    /**
     * 单个任务一天的节能率
     *
     * @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];
        /**
@@ -569,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);
@@ -592,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];
@@ -603,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) {
@@ -668,54 +898,75 @@
    public BigDecimal calculateEnergySaving(ControlLightCommandVO v1, ControlLightCommandVO v2) {
//        BigDecimal hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
        BigDecimal hour;
//        BigDecimal hour;
        if (v1.getBrightness() == 0) {
            return new BigDecimal(1);
        }
        double hour;
        double min;
        //计算时长
        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 = BigDecimal.valueOf((v2.getMin() - v1.getMin()) / 60);
        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);
            min = 60 + (v1.getMin() - v2.getMin());
            hour = hour - 1;
        } else {
            min = BigDecimal.valueOf(0);
            min = 0;
        }
        //计算时长
        BigDecimal totalTime = BigDecimal.valueOf(hour * 60 + min);
        BigDecimal totalTime = (hour.add(min));
        BigDecimal energySaving = BigDecimal.valueOf((100 - v1.getBrightness())).divide(BigDecimal.valueOf(100)).multiply(totalTime).divide(BigDecimal.valueOf(24));
        System.out.println(energySaving);
        //计算节能率 ( (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;
    }
    //计算能耗(部分    时长*亮度)
    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);
            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;
    }