From dda268997ca8f8a364f7c19b45d7a43a50a98efe Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期二, 25 十月 2022 18:00:11 +0800
Subject: [PATCH] changes
---
ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java | 706 +++++++++++++++++++++++++++++++++++++++++++---------------
1 files changed, 516 insertions(+), 190 deletions(-)
diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java
index 03d895a..998120c 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java
@@ -3,6 +3,9 @@
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+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;
@@ -13,26 +16,30 @@
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.report.A5LightOperationReportInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.A5LightBrightnessReqInnerFrame;
+import com.sandu.ximon.admin.manager.iot.frame.inner.request.A5LightSetCalendarReqInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.response.A5LightBrightnessRespInnerFrame;
+import com.sandu.ximon.admin.manager.iot.rrpc.dto.CommonFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.WrapResponseCommonFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5OrderEnum;
import com.sandu.ximon.admin.manager.iot.rrpc.mainboard.MainBoardInvokeSyncService;
import com.sandu.ximon.admin.param.LightControlParam;
import com.sandu.ximon.admin.param.LightPowerSettingParam;
import com.sandu.ximon.admin.param.LightRemarkParam;
+import com.sandu.ximon.admin.redis.DeviceRedisKey;
import com.sandu.ximon.admin.redis.LightKey;
import com.sandu.ximon.admin.security.SecurityUtils;
+import com.sandu.ximon.admin.utils.RedisUtils;
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.admin.vo.RedisDeviceStatus;
import com.sandu.ximon.dao.bo.LightBo;
import com.sandu.ximon.dao.bo.PoleTaskLightPowerBo;
-import com.sandu.ximon.dao.domain.Light;
-import com.sandu.ximon.dao.domain.LightReportData;
-import com.sandu.ximon.dao.domain.Pole;
-import com.sandu.ximon.dao.domain.PoleBinding;
+import com.sandu.ximon.dao.domain.*;
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;
@@ -41,8 +48,6 @@
import java.math.BigDecimal;
import java.time.LocalDateTime;
-import java.time.LocalTime;
-import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
@@ -60,6 +65,7 @@
private final LightReportDataService lightReportDataService;
private final PoleBindingService bindingService;
private final PoleService poleService;
+ private final RedisUtils redisUtils;
/**
* 褰曞叆褰撳墠璁惧鐮佺殑璺伅鏁版嵁
@@ -71,7 +77,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();
@@ -79,15 +85,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);
}
}
@@ -96,15 +102,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";
+ //姝e簭銆佸�掑彊
+ 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) {
@@ -138,13 +202,17 @@
*/
public Light getLight(String deviceCode) {
Light one = getOne(Wrappers.<Light>lambdaQuery().eq(Light::getDeviceCode, deviceCode));
- Object o = redisService.get(LightKey.REPORT_MAC.key(deviceCode));
- if (o != null) {
- one.setOnlineStatus(1);
+ if (one == null) {
+ return null;
} else {
- one.setOnlineStatus(0);
+ Object o = redisService.get(LightKey.REPORT_MAC.key(deviceCode));
+ if (o != null) {
+ one.setOnlineStatus(1);
+ } else {
+ one.setOnlineStatus(0);
+ }
+ return one;
}
- return one;
}
/**
@@ -246,151 +314,6 @@
return resultList;
}
- /**
- * 鍗曠伅鑺傝兘鐜�
- *
- * @return
- */
- public List controlEnergySaving() {
- //鑾峰彇鍒版鍦ㄦ墽琛岀殑浠诲姟鍒楄〃
- 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<>();
-
- for (LightTaskDto lightTaskDto : lightTaskDtos) {
- List<Integer> weekList = lightTaskDto.getWeekList();
- 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:
- }
- }
- }
-
-
- //鑾峰彇褰撳墠鏄熸湡鍑� 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++) {
- currentWeekValue--;
- if (currentWeekValue < 1) {
- currentWeekValue = 7;
- }
- map = new LinkedHashMap();
- map.put("time",localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
- map.put("value", getlist(currentWeekValue));
- temp.add(map);
- }
-
-
- return temp;
- }
-
-
- /**
- * 鍗曠伅鑳借��
- *
- * @return
- */
- public List controlEnergy() {
- //鑾峰彇鍒版鍦ㄦ墽琛岀殑浠诲姟鍒楄〃
- 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<>();
-
- 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:
- }
- }
- }
-
- Integer currentWeekValue = now.getDayOfWeek().getValue();
- Map map;
- List<Map> temp = new ArrayList<>();
- //鑾峰彇褰撳墠鏃ユ湡 鏍煎紡涓簓yyy-MM-dd
- LocalDateTime localDateTime = now.with(LocalTime.MIN);
-
-
- for (int i = 1; i < 8; i++) {
- currentWeekValue--;
- if (currentWeekValue < 1) {
- currentWeekValue = 7;
- }
- map = new LinkedHashMap();
- map.put("time",localDateTime.minusDays(i).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
- map.put("value", getlistEnergy(currentWeekValue));
- temp.add(map);
- }
-
-
- return temp;
- }
-
public BigDecimal getlist(Integer week) {
@@ -462,9 +385,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();
@@ -574,6 +494,61 @@
*/
}
+ /**
+ * 鍗曚釜浠诲姟涓�澶╃殑鑺傝兘鐜�
+ *
+ * @return
+ */
+ public BigDecimal jisuan(LightTaskDto Task, LightTaskPoleRelation bean) {
+ 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];
+
+ /**
+ * 鑺傝兘鐜囪绠楃粨鏉�
+ */
+ }
+
/**
* 涓�澶╃殑鑳借��
@@ -599,20 +574,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);
@@ -622,9 +599,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];
@@ -633,6 +615,100 @@
* 璁$畻鑳借�楃粨鏉�
*/
}
+
+
+ /**
+ * 涓�澶╃殑鑳借��
+ *
+ * @param
+ * @param bean
+ * @return
+ */
+ public BigDecimal jisuanEnergy(LightTaskDto Task, LightTaskPoleRelation bean) {
+ //瀛樻斁鑳借��
+ 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());
+ List<PoleTaskLightPowerBo> poleTaskLightPowerBos = new ArrayList<>();
+ PoleTaskLightPowerBo poleTaskLightPowerBo = new PoleTaskLightPowerBo();
+ Light one = getOne(Wrappers.lambdaQuery(Light.class).eq(Light::getDeviceCode, bean.getDeviceCode()));
+
+ if (one != null) {
+ if ("0001".equals(bean.getLightAddress())) {
+ poleTaskLightPowerBo.setPower1(one.getPower1());
+ } else {
+ poleTaskLightPowerBo.setPower2(one.getPower2());
+ }
+ }
+ poleTaskLightPowerBos.add(poleTaskLightPowerBo);
+ 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) {
@@ -699,6 +775,9 @@
// BigDecimal hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
// BigDecimal hour;
+ if (v1.getBrightness() == 0) {
+ return new BigDecimal(1);
+ }
double hour;
double min;
//璁$畻鏃堕暱
@@ -722,8 +801,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;
}
@@ -731,27 +815,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;
}
@@ -777,16 +868,31 @@
}
/**
+ * 鐢ㄦ埛鎷ユ湁鐨勮矾鐏�(鐢ㄤ簬棣栭〉鏁版嵁缁熻)
+ *
+ * @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;
+ }
+
+ /**
* 鑾峰彇鐢ㄦ埛鎵�鏈夌殑璁惧鐮�
*/
- public CommonPage<String> listDeviceCode(int pageNo, int pageSize, String keyword, String deviceCode) {
+ public CommonPage<String> listDeviceCode(int pageNo, int pageSize, String keyword, String deviceCode, String orderBy) {
List<String> list;
if (SecurityUtils.getClientId() != null) {
PageHelper.startPage(pageNo, pageSize);
list = baseMapper.listCode(SecurityUtils.getUserId(), keyword, deviceCode);
} else {
- PageHelper.startPage(pageNo, pageSize);
+ PageHelper.startPage(pageNo, pageSize, orderBy);
list = baseMapper.listCode(null, keyword, deviceCode);
}
@@ -801,8 +907,228 @@
lights.forEach(light -> {
light.setPower1(lightPowerSettingParam.getPower1());
light.setPower2(lightPowerSettingParam.getPower2());
+ //瀛樺湪闈�0鍔熺巼 鍗充负瀛樺湪璇ョ伅澶�
+ if (lightPowerSettingParam.getPower1() != null || lightPowerSettingParam.getPower1() != 0) {
+ light.setLight1(1);
+ } else if (lightPowerSettingParam.getPower1() == 0) {
+ //鎵嬪姩璁剧疆鐏ご鍔熺巼涓�0 鍗充笉瀛樺湪璇ョ伅澶�
+ light.setLight1(0);
+ } else {
+ light.setLight1(0);
+ }
+ //瀛樺湪闈�0鍔熺巼 鍗充负瀛樺湪璇ョ伅澶�
+ if (lightPowerSettingParam.getPower2() != null || lightPowerSettingParam.getPower2() != 0) {
+ light.setLight2(1);
+ } else if (lightPowerSettingParam.getPower2() == 0) {
+ //鎵嬪姩璁剧疆鐏ご鍔熺巼涓�0 鍗充笉瀛樺湪璇ョ伅澶�
+ light.setLight2(0);
+ } else {
+ light.setLight2(0);
+ }
light.setLightCount(lightPowerSettingParam.getLightCount());
});
return updateBatchById(lights);
}
+
+
+ /**
+ * 鍚慠edis涓瓨鍏ヨ澶囩姸鎬�
+ */
+ public void setCacheData() {
+
+ List<String> collect = list().stream().map(Light::getDeviceCode).collect(Collectors.toList());
+// collect.forEach(
+// code -> {
+// redisUtils.delete(DeviceRedisKey.LIGHT_DEVICE + code);
+// }
+// );
+ List<List<String>> split = CollectionUtil.split(collect, 100);
+
+ List<BatchGetDeviceStateResponse.DeviceStatus> deviceStatuses = new ArrayList<>();
+ List<RedisDeviceStatus> statusList = new ArrayList<>();
+ for (List<String> splist : split) {
+ deviceStatuses = MainBoardInvokeSyncService.getInstance().batchGetDeviceState(splist);
+ if (deviceStatuses != null) {
+ for (BatchGetDeviceStateResponse.DeviceStatus deviceStatus : deviceStatuses) {
+ RedisDeviceStatus device = new RedisDeviceStatus();
+ device.setDeviceId(deviceStatus.getDeviceName());
+ if ("ONLINE".equals(deviceStatus.getStatus())) {
+ device.setStatus(0);
+ } else if ("OFFLINE".equals(deviceStatus.getStatus())) {
+ device.setStatus(1);
+ } else if ("UNACTIVE".equals(deviceStatus.getStatus())) {
+ device.setStatus(0);
+ } else if ("DISABLE".equals(deviceStatus.getStatus())) {
+ device.setStatus(0);
+ } else {
+ device.setStatus(2);
+ }
+ statusList.add(device);
+ }
+
+ }
+ }
+
+ statusList.forEach(status -> {
+ redisUtils.set(DeviceRedisKey.LIGHT_DEVICE + status.getDeviceId(), JSON.toJSONString(status));
+ });
+
+ }
+
+
+ /**
+ * 璁剧疆鏃ュ巻锛堝悓蹇冭烦鍖呬腑鐨�6瀛楄妭鏃ユ湡鏃堕棿锛�
+ *
+ * @return
+ */
+ public String SetCalendar(Long lightId, String address) {
+ Calendar cal = Calendar.getInstance();
+ //鑾峰彇褰撳墠鏃堕棿
+ int year = cal.get(Calendar.YEAR);
+ int month = cal.get(Calendar.MONTH);
+ int day = cal.get(Calendar.DATE);
+ int hour = cal.get(Calendar.HOUR_OF_DAY);
+ int min = cal.get(Calendar.MINUTE);
+ int sec = cal.get(Calendar.SECOND);
+
+ if (!"0001".equals(address) && !"0002".equals(address)) {
+ throw new BusinessException("鐏ご鍦板潃閿欒锛�");
+ }
+
+ Light one = getById(lightId);
+ if (one == null) {
+ System.out.println("鍗曠伅涓嶅瓨鍦紒");
+ }
+
+ A5LightSetCalendarReqInnerFrame setCalendarReqInnerFrame =
+ new A5LightSetCalendarReqInnerFrame(address, year % 100, month + 1, day, hour, min, sec);
+
+ A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_LIGHT_DATA.getCode(), setCalendarReqInnerFrame);
+ System.out.println(a5Frame + " -----a5Frame");
+
+ CommonFrame commonFrame;
+
+ commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(one.getDeviceCode(), a5Frame);
+ StoreOperationRecordsUtils.storeInnerFrameData(one.getDeviceCode(), "鍗曠伅甯�-璁剧疆鏃ュ巻", a5Frame, commonFrame);
+
+ System.out.println(commonFrame + " -----commonFrame");
+
+ A5LightOperationReportInnerFrame operationReportInnerFrame = new A5LightOperationReportInnerFrame().transformFrame(commonFrame.getPayload());
+ if (operationReportInnerFrame.isValidate()) {
+ return operationReportInnerFrame.getState();
+ } else {
+ throw new BusinessException("鏁版嵁鏍¢獙閿欒锛岃閲嶆柊璇锋眰");
+ }
+ }
+
+ /**
+ * 鍗曠伅涓诲姩鍚屾鏃堕棿璇锋眰
+ */
+ public void timeSynchronizationInitiative(String deviceCode, String lightAddress) {
+ //鍗曠伅淇℃伅
+ Light light = getLight(deviceCode);
+ if (light == null) {
+ log.error("鍗曠伅涓诲姩鍚屾鏃堕棿璇锋眰寮傚父锛屽崟鐏俊鎭笉瀛樺湪锛�");
+ return;
+ }
+ //鍗曠伅浠诲姟淇℃伅
+ LightTaskPoleRelation lightTaskPoleRelation = SpringContextHolder.getBean(LightTaskPoleRelationService.class)
+ .getOne(Wrappers.lambdaQuery(LightTaskPoleRelation.class)
+ .eq(LightTaskPoleRelation::getDeviceCode, deviceCode).eq(LightTaskPoleRelation::getLightAddress, lightAddress));
+
+ LightTask lightTask = null;
+ //鐏ご娌℃湁浠诲姟
+ if (lightTaskPoleRelation != null) {
+ if (lightTaskPoleRelation.getDeviceScheduled() != null) {
+ lightTask = JSONObject.parseObject(lightTaskPoleRelation.getDeviceScheduled(), LightTask.class);
+ }
+ }
+
+ timeSynchronization(light, lightAddress, lightTask);
+
+ }
+
+ /**
+ * 鍗曠伅鏃堕棿鍚屾
+ *
+ * @param light 鍗曠伅淇℃伅瀹炰綋
+ * @param lightTask 鍗曠伅浠诲姟瀹炰綋锛堜负绌烘爣璇嗗崟鐏病鏈夊畾鏃朵换鍔★級
+ */
+ public void timeSynchronization(Light light, String lightAddress, LightTask lightTask) {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ //鑾峰彇褰撳墠鏃堕棿鏃躲�佸垎
+ Calendar cal = Calendar.getInstance();
+
+ if (lightTask != null) {
+ String closeOrder = lightTask.getCloseOrder();
+ String openOrder = lightTask.getOpenOrder();
+ String controlOrder = lightTask.getControlOrder() == null ? null : lightTask.getControlOrder();
+
+ List<String> timeList = new ArrayList<>();
+ timeList.add(openOrder.substring(0, 4));
+ timeList.add(closeOrder.substring(0, 4));
+ if (controlOrder != null && controlOrder.length() % 7 == 0) {
+ for (int i = 0; i < controlOrder.length() / 7; i++) {
+ timeList.add(controlOrder.substring(i * 7, i * 7 + 4));
+ }
+ } else {
+ log.error("鎺х伅鎸囦护涓嶆纭紝鏁版嵁闀垮害涓嶄负7鐨勫�嶆暟锛�");
+ return;
+ }
+ //鏃堕棿鎺掑簭
+ timeList = timeList.stream().sorted().collect(Collectors.toList());
+
+ int count = 0;
+ boolean haveTime = false;
+
+ while (!haveTime && count <= 144) {
+ haveTime = judgeTime(cal, timeList);
+ cal.add(Calendar.MINUTE, 10);
+ count++;
+ }
+ if (haveTime) {
+ try {
+ long longTime = cal.getTimeInMillis() - System.currentTimeMillis();
+ log.error("鐫$湢鏃堕棿锛堟绉掞級锛�" + longTime);
+ Thread.sleep(longTime);
+ SetCalendar(light.getLightId(), lightAddress);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ } else {
+ log.error("鐏潌ID涓猴細" + light.getLightId() + "锛岀伅澶村湴鍧�涓猴細" + lightAddress + "娌℃湁鎵惧埌鍙悓姝ョ殑鏃堕棿锛岃妫�鏌ヤ换鍔★紒");
+ }
+
+ } else {
+ SetCalendar(light.getLightId(), lightAddress);
+ }
+ }
+ }).start();
+ }
+
+ /**
+ * 鍒ゆ柇鏃堕棿鏄惁鍙互鎵ц鍚屾鎸囦护
+ *
+ * @param time
+ * @param timeList
+ * @return true 鍙互鎵ц ,false 涓嶅彲鎵ц
+ */
+ private boolean judgeTime(Calendar time, List<String> timeList) {
+ int hour = time.get(Calendar.HOUR_OF_DAY);
+ int min = time.get(Calendar.MINUTE);
+ int allMin = hour * 60 + min;
+ for (String value : timeList) {
+ int targetHour = Integer.valueOf(value.substring(0, 2));
+ int targetMin = Integer.valueOf(value.substring(2, 4));
+ int allTargetMin = targetHour * 60 + targetMin;
+ if (allMin >= allTargetMin - 10 && allMin <= allTargetMin + 10) {
+ return false;
+ } else {
+ continue;
+ }
+ }
+ return true;
+ }
}
--
Gitblit v1.9.3