| | |
| | | package com.sandu.ximon.admin.service; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.date.DateTime; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.HexUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.redis.RedisService; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | import com.sandu.common.util.SpringContextHolder; |
| | | import com.sandu.ximon.admin.dto.LightTaskDto; |
| | | import com.sandu.ximon.admin.manager.iot.frame.A5Frame; |
| | | import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5LightHeartbeatReportInnerFrame; |
| | | import com.sandu.ximon.admin.manager.iot.frame.inner.request.A5LightBrightnessReqInnerFrame; |
| | |
| | | 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.vo.ControlLightCommandVO; |
| | | import com.sandu.ximon.dao.bo.LightBo; |
| | | import com.sandu.ximon.dao.domain.Light; |
| | | import com.sandu.ximon.dao.domain.LightReportData; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | |
| | | * |
| | | * @return |
| | | */ |
| | | public List<LightReportData> controlEnergySaving() { |
| | | public Map controlEnergySaving() { |
| | | Long clientId = SecurityUtils.getClientId(); |
| | | // List<LightBo> listLight = baseMapper.listLight(clientId, null); |
| | | // List<String> deviceCodeList = listLight.stream().map(LightBo::getDeviceCode).collect(Collectors.toList()); |
| | | List<String> deviceCodeList = new ArrayList<>(); |
| | | deviceCodeList.add("32313243305005ffa633ffff"); |
| | | // DateTime startDate = DateUtil.date(); |
| | | // DateTime endDate = DateUtil.offsetDay(startDate, 3); |
| | | // if (CollectionUtil.isNotEmpty(deviceCodeList)) { |
| | | // |
| | | // List<LightReportDataBo> reportDataList = lightReportDataService.reportDataList(deviceCodeList, startDate, endDate); |
| | | // } |
| | | |
| | | //获取用户最近7天的单灯数据 |
| | | List<LightReportData> reportDataList = lightReportDataService.list(Wrappers.<LightReportData>lambdaQuery() |
| | | .in(LightReportData::getDeviceCode, deviceCodeList) |
| | | .ge(LightReportData::getCreateTime, DateUtil.offsetDay(DateUtil.yesterday(), -7)) |
| | | .orderByDesc(LightReportData::getCreateTime)); |
| | | //获取到正在执行的任务列表 |
| | | List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask(); |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | |
| | | List<LightTaskDto> list1 = new ArrayList<>(); |
| | | List<LightTaskDto> list2 = new ArrayList<>(); |
| | | List<LightTaskDto> list3 = new ArrayList<>(); |
| | | List<LightTaskDto> list4 = new ArrayList<>(); |
| | | List<LightTaskDto> list5 = new ArrayList<>(); |
| | | List<LightTaskDto> list6 = new ArrayList<>(); |
| | | List<LightTaskDto> list7 = new ArrayList<>(); |
| | | |
| | | LocalDateTime dateTime = LocalDateTime.now(); |
| | | dateTime.plusDays(7L); |
| | | reportDataList.forEach( |
| | | reportData -> { |
| | | LocalDateTime createTime = reportData.getCreateTime(); |
| | | if(dateTime.plusDays(-1L).getDayOfMonth()==createTime.getDayOfMonth()){ |
| | | |
| | | } |
| | | for (LightTaskDto lightTaskDto : lightTaskDtos) { |
| | | List<Integer> weekList = lightTaskDto.getWeekList(); |
| | | System.out.println(weekList+"==========================="); |
| | | for (Integer one : weekList) { |
| | | switch (one) { |
| | | case 1: |
| | | list1.add(lightTaskDto); |
| | | break; |
| | | case 2: |
| | | list2.add(lightTaskDto); |
| | | break; |
| | | case 4: |
| | | list3.add(lightTaskDto); |
| | | break; |
| | | case 8: |
| | | list4.add(lightTaskDto); |
| | | break; |
| | | case 16: |
| | | list5.add(lightTaskDto); |
| | | break; |
| | | case 32: |
| | | list6.add(lightTaskDto); |
| | | break; |
| | | case 64: |
| | | list7.add(lightTaskDto); |
| | | break; |
| | | default: |
| | | } |
| | | ); |
| | | |
| | | |
| | | } |
| | | } |
| | | Map map = new HashMap(); |
| | | map.put("-1", null); |
| | | map.put("1", jisuan(list1)); |
| | | map.put("2", jisuan(list2)); |
| | | map.put("3", jisuan(list3)); |
| | | map.put("4", jisuan(list4)); |
| | | map.put("5", jisuan(list5)); |
| | | map.put("6", jisuan(list6)); |
| | | map.put("7", jisuan(list7)); |
| | | |
| | | |
| | | return reportDataList; |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 一天的节能率 |
| | | * |
| | | * @param list |
| | | * @return |
| | | */ |
| | | public BigDecimal jisuan(List<LightTaskDto> list) { |
| | | BigDecimal bigDecimalResult = new BigDecimal(0.00); |
| | | /** |
| | | * 节能率计算开始 |
| | | */ |
| | | list.forEach(Task -> { |
| | | // List<BigDecimal> saving = new ArrayList<>(); |
| | | ControlLightCommandVO startTime = parseSwitchLightCommand(Task.getOpenOrder()); |
| | | ControlLightCommandVO endTime = parseSwitchLightCommand(Task.getCloseOrder()); |
| | | //存放节能率 |
| | | //获取到单灯任务的节能率 |
| | | if (Task.getControlOrder() != null) {//有控等指令 拆分计算 |
| | | List<ControlLightCommandVO> controlLightCommandVOS = parseControlLightCommand(Task.getControlOrder()); |
| | | |
| | | |
| | | for (int i = 0; i < controlLightCommandVOS.size(); i++) { |
| | | BigDecimal bigDecimal = calculateEnergySaving(startTime, controlLightCommandVOS.get(i)); |
| | | if (controlLightCommandVOS.size() == (i+1)) { |
| | | BigDecimal bigDecimal1 = calculateEnergySaving(controlLightCommandVOS.get(i), endTime); |
| | | System.out.println(bigDecimal1+"==========================="); |
| | | // saving.add(bigDecimal1); |
| | | bigDecimalResult.add(bigDecimal1); |
| | | break; |
| | | } |
| | | startTime = controlLightCommandVOS.get(i); |
| | | // saving.add(bigDecimal); |
| | | bigDecimalResult.add(bigDecimal); |
| | | } |
| | | } else {//无控灯指令 直接计算 |
| | | BigDecimal bigDecimal = calculateEnergySaving(startTime, endTime); |
| | | // saving.add(bigDecimal); |
| | | bigDecimalResult.add(bigDecimal); |
| | | } |
| | | }); |
| | | return bigDecimalResult; |
| | | |
| | | /** |
| | | * 节能率计算结束 |
| | | */ |
| | | } |
| | | |
| | | /** |
| | | * 解析开关灯指令 |
| | | */ |
| | | public ControlLightCommandVO parseSwitchLightCommand(String command) { |
| | | char[] chars = command.toCharArray(); |
| | | ControlLightCommandVO vo = new ControlLightCommandVO(); |
| | | if (chars.length == 7) { |
| | | int hour = Integer.valueOf(command.substring(0, 2)); |
| | | int min = Integer.valueOf(command.substring(2, 4)); |
| | | int brightness = Integer.valueOf(command.substring(4, 7)); |
| | | vo.setHour(hour); |
| | | vo.setMin(min); |
| | | vo.setBrightness(brightness); |
| | | } |
| | | return vo; |
| | | } |
| | | |
| | | /** |
| | | * 解析控灯指令 |
| | | */ |
| | | public List<ControlLightCommandVO> parseControlLightCommand(String command) { |
| | | char[] chars = command.toCharArray(); |
| | | int i = chars.length / 7; |
| | | |
| | | List<ControlLightCommandVO> list = new ArrayList<>(); |
| | | |
| | | for (int j = 0; j < i; j++) { |
| | | int hour = Integer.valueOf(command.substring(0 + (j * 7), 2 + (j * 7))); |
| | | int min = Integer.valueOf(command.substring(2 + (j * 7), 4 + (j * 7))); |
| | | int brightness = Integer.valueOf(command.substring(4 + (j * 7), 7 + (j * 7))); |
| | | ControlLightCommandVO vo = new ControlLightCommandVO(); |
| | | vo.setHour(hour); |
| | | vo.setMin(min); |
| | | vo.setBrightness(brightness); |
| | | list.add(vo); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | //计算节能率 |
| | | 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 energySaving = BigDecimal.valueOf((100 - v1.getBrightness())).divide(BigDecimal.valueOf(100)).multiply(totalTime).divide(BigDecimal.valueOf(24),2); |
| | | System.out.println(energySaving); |
| | | return energySaving; |
| | | } |
| | | |
| | | // //计算节能率 |
| | | // public void calculateEnergySaving(ControlLightCommandVO v1,ControlLightCommandVO v2){ |
| | | // int hour = v2.getHour()- v1.getHour(); |
| | | // int min = (v2.getMin()- v1.getMin()); |
| | | // int brightness = v2.getBrightness()- v1.getBrightness(); |
| | | // int totalTime=(hour*60+min)*60; |
| | | // Double energySaving = (1-(v1.getBrightness()/100.0))*totalTime/(36*24); |
| | | // } |
| | | |
| | | |
| | | /** |
| | | * 用户拥有的路灯 |
| | | * |
| | | * @return |