From d35545d88b9123b92b50aaf1b0fa4e3f20fd2209 Mon Sep 17 00:00:00 2001
From: zhanzhiqin <895896009@qq.com>
Date: 星期五, 20 五月 2022 11:23:50 +0800
Subject: [PATCH] 摄像头

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/LightService.java |  191 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 144 insertions(+), 47 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 f990228..8babd79 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,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,7 +24,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;
@@ -39,10 +42,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;
 
 /**
@@ -104,6 +106,31 @@
 
         // 鑾峰彇鏈�杩戠殑涓婃姤鏃堕棿
         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) {
@@ -146,6 +173,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 +228,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);
@@ -197,6 +255,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;
     }
 
@@ -205,7 +277,7 @@
      *
      * @return
      */
-    public Map controlEnergySaving() {
+    public List controlEnergySaving() {
         //鑾峰彇鍒版鍦ㄦ墽琛岀殑浠诲姟鍒楄〃
         List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask();
         LocalDateTime now = LocalDateTime.now();
@@ -220,7 +292,6 @@
 
         for (LightTaskDto lightTaskDto : lightTaskDtos) {
             List<Integer> weekList = lightTaskDto.getWeekList();
-            System.out.println(weekList + "===========================");
             for (Integer one : weekList) {
                 switch (one) {
                     case 1:
@@ -249,21 +320,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;
     }
 
 
@@ -272,7 +351,7 @@
      *
      * @return
      */
-    public Map controlEnergy() {
+    public List controlEnergy() {
         //鑾峰彇鍒版鍦ㄦ墽琛岀殑浠诲姟鍒楄〃
         List<LightTaskDto> lightTaskDtos = SpringContextHolder.getBean(LightTaskService.class).listTask();
         LocalDateTime now = LocalDateTime.now();
@@ -316,21 +395,26 @@
             }
         }
 
-        Integer week = now.getDayOfWeek().getValue();
-
-        Map map = new HashMap();
+        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++) {
-            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;
     }
 
 
@@ -495,7 +579,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;
@@ -541,20 +625,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);
@@ -564,9 +650,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];
@@ -640,28 +731,33 @@
     public BigDecimal calculateEnergySaving(ControlLightCommandVO v1, ControlLightCommandVO v2) {
 
 //        BigDecimal hour = BigDecimal.valueOf((v2.getHour() - v1.getHour()));
-        BigDecimal hour;
+//        BigDecimal hour;
+        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/3600 )   淇濈暀涓や綅灏忔暟
+        BigDecimal energySaving = BigDecimal.valueOf((1 - v1.getBrightness() / 100) * totalTime.doubleValue() / 3600).setScale(2, BigDecimal.ROUND_HALF_UP);
+        System.out.println(energySaving + "鑺傝兘鐜�");
         return energySaving;
     }
 
@@ -682,6 +778,7 @@
             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);
         }

--
Gitblit v1.9.3