From e55c8b0a92eb9715edd90c31dfd4de51a47b588b Mon Sep 17 00:00:00 2001
From: liuhaonan <31457034@qq.com>
Date: 星期五, 04 十一月 2022 17:40:08 +0800
Subject: [PATCH] changes

---
 ximon-admin/src/main/java/com/sandu/ximon/admin/service/PoleLightemitService.java |  238 ++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 187 insertions(+), 51 deletions(-)

diff --git a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/PoleLightemitService.java b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/PoleLightemitService.java
index 4a5b213..83a33ec 100644
--- a/ximon-admin/src/main/java/com/sandu/ximon/admin/service/PoleLightemitService.java
+++ b/ximon-admin/src/main/java/com/sandu/ximon/admin/service/PoleLightemitService.java
@@ -1,21 +1,23 @@
 package com.sandu.ximon.admin.service;
 
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.google.gson.Gson;
 import com.sandu.common.execption.BusinessException;
+import com.sandu.common.file.FileUploadDto;
 import com.sandu.common.service.impl.BaseServiceImpl;
 import com.sandu.common.util.SpringContextHolder;
 import com.sandu.ximon.admin.config.RealtimeServerBean;
 import com.sandu.ximon.admin.entity.*;
 import com.sandu.ximon.admin.param.PoleBindingParam;
+import com.sandu.ximon.admin.redis.DeviceRedisKey;
 import com.sandu.ximon.admin.security.SecurityUtils;
-import com.sandu.ximon.admin.utils.FileUtil;
-import com.sandu.ximon.admin.utils.HtmlTemplateUtils;
-import com.sandu.ximon.admin.utils.LightemitUtils;
-import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
+import com.sandu.ximon.admin.utils.*;
 import com.sandu.ximon.admin.vo.EquipmentInfomation;
+import com.sandu.ximon.admin.vo.RedisDeviceStatus;
 import com.sandu.ximon.dao.domain.Pole;
 import com.sandu.ximon.dao.domain.PoleLightemitEntity;
+import com.sandu.ximon.dao.enums.OrderByEnums;
 import com.sandu.ximon.dao.enums.PoleBindingEnums;
 import com.sandu.ximon.dao.mapper.PoleLightemitEntityMapper;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -24,6 +26,7 @@
 import org.springframework.context.ApplicationContext;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.PostConstruct;
 import java.io.File;
@@ -51,6 +54,12 @@
     @Autowired
     PoleLightemitEntityMapper poleLightemitDao;
 
+    @Autowired
+    LedSFileService xiXunFileService;
+
+    @Autowired
+    RedisUtils redisUtils;
+
     @PostConstruct
     public void init() {
         //閲嶆柊鑾峰彇鍗曚緥
@@ -64,13 +73,49 @@
      * @param isOnLine
      * @return
      */
-    public List<PoleLightemitEntity> listLed(String keyword, boolean isOnLine) {
+    public List<PoleLightemitEntity> listLed(String keyword, Integer order, Integer seq, boolean isOnLine) {
         List<PoleLightemitEntity> poleLightemitEntityList;
+        //鎺掑簭瀛楁
+        String orderByResult = "id";
+        //姝e簭銆佸�掑彊
+        String orderBySeq = OrderByEnums.ASC.getCode();
+        if (order != null) {
+            switch (order) {
+                case 1:
+                    orderByResult = OrderByEnums.LED_S_CODE.getCode();
+                    break;
+                case 2:
+                    orderByResult = OrderByEnums.LED_S_NAME.getCode();
+                    break;
+                case 3:
+                    orderByResult = OrderByEnums.LED_S_POLE_NAME.getCode();
+                    break;
+                case 4:
+                    orderByResult = OrderByEnums.LED_S_CREATE_TIME.getCode();
+                    break;
+                default:
+            }
+        }
+        if (seq != null) {
+            switch (seq) {
+                case 1:
+                    orderBySeq = " ASC";
+                    break;
+                case 2:
+                    orderBySeq = " DESC";
+                    break;
+                default:
+                    break;
+            }
+        }
+        //鎺掑簭鏂瑰紡
+        String orderBy = orderByResult + " " + orderBySeq;
+
         //瓒呯
         if (SecurityUtils.getClientId() == null) {
-            poleLightemitEntityList = poleLightemitDao.listLed(keyword, null);
+            poleLightemitEntityList = poleLightemitDao.listLed(keyword, null, orderBy);
         } else {
-            poleLightemitEntityList = poleLightemitDao.listLed(keyword, SecurityUtils.getUserId());
+            poleLightemitEntityList = poleLightemitDao.listLed(keyword, SecurityUtils.getUserId(), orderBy);
         }
 
         List<PoleLightemitEntity> temp = new ArrayList<>();
@@ -86,6 +131,52 @@
             }
         }
         return temp;
+    }
+
+    /**
+     * 鐔欒璁惧鍒楄〃锛堢敤浜庨椤垫暟鎹粺璁★級
+     *
+     * @return
+     */
+    public List<PoleLightemitEntity> listLedOnHome() {
+        List<PoleLightemitEntity> poleLightemitEntityList;
+        //瓒呯
+        if (SecurityUtils.getClientId() == null) {
+            poleLightemitEntityList = poleLightemitDao.listLed(null, null, null);
+        } else {
+            poleLightemitEntityList = poleLightemitDao.listLed(null, SecurityUtils.getUserId(), null);
+        }
+
+        return poleLightemitEntityList;
+    }
+
+
+    /**
+     * 缁戝畾浣跨敤
+     *
+     * @param keyword
+     * @return
+     */
+    public List<PoleLightemitEntity> listLedOnBinding(String keyword) {
+        List<PoleLightemitEntity> poleLightemitEntityList;
+
+
+        //瓒呯
+        if (SecurityUtils.getClientId() == null) {
+            poleLightemitEntityList = poleLightemitDao.listLedOnBinding(keyword, null);
+        } else {
+            poleLightemitEntityList = poleLightemitDao.listLedOnBinding(keyword, SecurityUtils.getUserId());
+        }
+
+        for (PoleLightemitEntity poleLightemitEntity : poleLightemitEntityList) {
+            //鏌ヨ璁惧鍦ㄧ嚎鐘舵��
+            boolean onLine = lightemitUtils.getLedOnLine(poleLightemitEntity.getLightemitControlCode());
+            //鏌ヨ灞忓箷鐨勫紑鍚姸鎬�
+            String isOpen = lightemitUtils.getIsScreenOpen(poleLightemitEntity.getLightemitControlCode());
+            poleLightemitEntity.setIsOpen(String.valueOf(isOpen));
+            poleLightemitEntity.setOnLine(onLine);
+        }
+        return poleLightemitEntityList;
     }
 
     /**
@@ -153,18 +244,19 @@
      */
     public EquipmentInfomation getLedByLightControlCodeInfo(String lightControlCode) {
         EquipmentInfomation equipmentInfo = new EquipmentInfomation();
-        equipmentInfo.setEquipmentType("鐔欒LED");
+        equipmentInfo.setEquipmentType("SLED");
         if (lightControlCode == null || lightControlCode.trim().length() == 0) {
             return equipmentInfo;
         }
 
         PoleLightemitEntity xiXun = getOne(Wrappers.lambdaQuery(PoleLightemitEntity.class).eq(PoleLightemitEntity::getLightemitControlCode, lightControlCode));
+        boolean isScreenOpen = false;
         if (xiXun != null) {
             equipmentInfo.setEquipmentCreateTime(xiXun.getCreateTime());
             equipmentInfo.setEquipmentMac(xiXun.getLightemitControlCode());
             equipmentInfo.setEquipmentName(xiXun.getLightemitName());
+            isScreenOpen = lightemitUtils.getLedOnLine(xiXun.getLightemitControlCode());
         }
-        boolean isScreenOpen = lightemitUtils.getLedOnLine(xiXun.getLightemitControlCode());
         if (isScreenOpen) {
             equipmentInfo.setEquipmentState("鍦ㄧ嚎");
         } else {
@@ -175,16 +267,21 @@
     }
 
 
+    @Transactional(rollbackFor = Exception.class)
     public void savePoleLightemit(PoleLightemitEntity poleLightemit) {
         boolean save = this.save(poleLightemit);
         // 缁戝畾鐏潌
 
-        if (save && poleLightemit.getPoleId() != null) {
+        if (save && poleLightemit.getStreetlightId() != null) {
             PoleBindingParam poleBindingParam = new PoleBindingParam();
             poleBindingParam.setDeviceCode(poleLightemit.getLightemitControlCode());
-            poleBindingParam.setDeviceType(1);
+            poleBindingParam.setDeviceType(10);
             poleBindingParam.setDeviceName(poleLightemit.getLightemitName());
-            poleBindingService.bindPole(poleLightemit.getPoleId(), poleBindingParam);
+
+            if (SpringContextHolder.getBean(PoleService.class).getById(poleLightemit.getStreetlightId()) == null) {
+                throw new BusinessException("鐏潌涓嶅瓨鍦�");
+            }
+            boolean b = poleBindingService.bindPole(poleLightemit.getStreetlightId(), poleBindingParam);
         }
 
         /**
@@ -207,15 +304,17 @@
             throw new BusinessException("鏈壘鍒癓ED灞�");
         }
 
-        boolean belong = SpringContextHolder.getBean(PoleBindingService.class).isBelong(byId.getLightemitControlCode(), PoleBindingEnums.XIXUN);
-        if (!belong) {
-            throw new BusinessException("璇ヨ澶囦笉灞炰簬鎮�,涓嶈兘淇敼璁惧淇℃伅");
+        if (SecurityUtils.getClientId() != null) {
+            boolean belong = SpringContextHolder.getBean(PoleBindingService.class).isBelong(byId.getLightemitControlCode(), PoleBindingEnums.XIXUN);
+            if (!belong) {
+                throw new BusinessException("璇ヨ澶囦笉灞炰簬鎮�,涓嶈兘淇敼璁惧淇℃伅");
+            }
         }
         poleLightemit.setLightemitId(ledId);
         boolean b = this.updateById(poleLightemit);
         // 缁戝畾鐏潌
 
-        if (b && poleLightemit.getPoleId() != null) {
+        if (b && poleLightemit.getStreetlightId() != null) {
             //鍏堝垹闄ょ粦瀹氬叧绯�
             poleBindingService.unBindPole(null, poleLightemit.getLightemitControlCode());
             //鍐嶇粦瀹�
@@ -223,15 +322,15 @@
             poleBindingParam.setDeviceCode(poleLightemit.getLightemitControlCode());
             poleBindingParam.setDeviceType(10);
             poleBindingParam.setDeviceName(poleLightemit.getLightemitName());
-            poleBindingService.bindPole(poleLightemit.getPoleId(), poleBindingParam);
+            poleBindingService.bindPole(poleLightemit.getStreetlightId(), poleBindingParam);
         }
 
         /**
          * 鐔欐睕led缂栬緫鏃ュ織璁板綍寮�濮�
          */
         List<String> listCode = new ArrayList<>();
-        listCode.add(poleLightemit.getLightemitControlCode());
-        String content = "{灞忓箷id锛�" + poleLightemit.getLightemitId() + "锛� 灞忓箷鍘熷悕锛�" + byId.getLightemitName() + "锛� 灞忓箷鍚嶇О锛�" + poleLightemit.getLightemitName() + "锛� 灞忓箷缂栫爜锛�" + poleLightemit.getLightemitControlCode() + " }";
+        listCode.add(byId.getLightemitControlCode());
+        String content = "{灞忓箷id锛�" + byId.getLightemitId() + "锛� 灞忓箷鍘熷悕锛�" + byId.getLightemitName() + "锛� 灞忓箷鍚嶇О锛�" + poleLightemit.getLightemitName() + "锛� 灞忓箷缂栫爜锛�" + byId.getLightemitControlCode() + " }";
         StoreOperationRecordsUtils.storeOperationData(listCode, null, "鐔欐睕LED缂栬緫", content);
         /**
          * 鐔欐睕led缂栬緫鏃ュ織璁板綍缁撴潫
@@ -242,7 +341,7 @@
     public boolean deletePoleLightemit(List<Long> ledIds) {
 
         List<PoleLightemitEntity> poleLightemitEntities = listByIds(ledIds);
-        if (poleLightemitEntities != null && poleLightemitEntities.size() != 0) {
+        if (poleLightemitEntities != null && poleLightemitEntities.size() == 0) {
             throw new BusinessException("璁惧涓嶅瓨鍦�");
         }
 
@@ -325,28 +424,26 @@
      * led涓婁紶瑙嗛
      *
      * @param lightemitControlCode
-     * @param filename
-     * @param path
-     * @param userName
-     * @throws IOException
      */
     @Async("taskExecutor")
-    public void videoUpload(String lightemitControlCode, String filename, String path, String userName) throws IOException {
+    public String videoUpload(String lightemitControlCode, FileUploadDto fileUploadDto, Integer type) {
         //鑾峰彇灞忓箷瀹�
         String screenWidth = lightemitUtils.getScreenWidth(lightemitControlCode);
         String screenHeight = lightemitUtils.getScreenHeight(lightemitControlCode);
 
         //鍙戦�佽棰戣嚦寮�鍙戞澘骞舵挱鏀�
-        lightemitUtils.postVideo(screenWidth, screenHeight, filename, lightemitControlCode);
+        String fileUrl = fileUploadDto.getFileUrl();
+        String[] split = fileUrl.split("/");
+        String id = split[split.length - 1];
+        String result = this.videoXixunPlayer(lightemitControlCode, id, fileUploadDto.getFileSize(), fileUploadDto.getMd5(), type, fileUploadDto.getFileType(), 999999);
 
-        //娓呭睆
-        lightemitUtils.clear(lightemitControlCode);
-
+        LogUtils.error("涓婁紶鎾斁缁撴灉:" + result);
+        return result;
     }
 
 
     @Async("taskExecutor")
-    public void videoXixunPlayer(String lightemitControlCode, String ip, String filename, Long filesize, Integer videoTime) {
+    public String videoXixunPlayer(String lightemitControlCode, String filename, Long filesize, String md5, Integer type, String fileType, Integer videoTime) {
         //鑾峰彇灞忓箷瀹�
         Integer screenWidth = Integer.valueOf(lightemitUtils.getScreenWidth(lightemitControlCode));
         Integer screenHeight = Integer.valueOf(lightemitUtils.getScreenHeight(lightemitControlCode));
@@ -361,18 +458,23 @@
         source.set_id(UUID.randomUUID().toString());
         //璇锋眰澶存嫾鎺ヨID涓鸿璧勬簮鐨勫畬鏁翠笅杞藉湴鍧�
         source.setId(filename);
-        //璁剧疆璧勬簮绫诲瀷锛屽叾浠栬祫婧愮被鍨嬭鍙傝�儀ixunplayer鑺傜洰json璇存槑鏂囨。
-        source.set_type("Video");
+        if (type == 1) {
+            //璁剧疆璧勬簮绫诲瀷锛屽叾浠栬祫婧愮被鍨嬭鍙傝�儀ixunplayer鑺傜洰json璇存槑鏂囨。
+            source.set_type("Video");
+            source.setMime("video/mp4");
+        } else {
+            source.set_type("Image");
+            source.setMime("image/jpeg");
+        }
         //璧勬簮鍚庣紑鍚�
-        source.setFileExt(".mp4");
+        source.setFileExt(fileType);
         //璧勬簮楂樺害
         source.setHeight(screenHeight);
         //璧勬簮瀹藉害
         source.setWidth(screenWidth);
         //璺濆乏
         source.setLeft(0);
-        source.setMd5("dd135d5d2d44d619a542db773ab529a4");
-        source.setMime("video/mp4");
+        source.setMd5(md5);
         source.setName(filename);
         source.setPlayTime(0);    //鎾斁璧峰鏃堕棿
         source.setSize(filesize);    //璧勬簮瀛楄妭鏁帮紝瑕佺簿鍑�
@@ -386,7 +488,7 @@
         List<Layer> list1 = new ArrayList<Layer>();
         list1.add(layer);
         pro.setLayers(list1);
-        pro.setName("demo");    //鑺傜洰鍚嶇О
+        pro.setName("鏂囦欢涓婁紶鎾斁");    //鑺傜洰鍚嶇О
         pro.setOthers(true);//濡傛灉涓簍rue锛屽垯璇ヨ妭鐩潵鑷涓夋柟鎺ュ彛锛屼笉鏄潵鑷垜浠嚜宸辩殑web锛屼簩娆″紑鍙戣鍔″繀璧嬪�间负true
         pro.setTotalSize(filesize);    //鎵�鏈夎祫婧愭�诲瓧鑺傛暟
         pro.setVersion(0);            //楂樼骇鑺傜洰=0锛岀畝鏄撹妭鐩�=2
@@ -402,17 +504,29 @@
         task.setItems(list2);
         command.setId(UUID.randomUUID().toString());
         //杩欓噷鏄笅鏂圭殑post鍥炶皟鍦板潃锛岄渶瑕佷慨鏀笽P鍦板潃
-        command.setNotificationURL("http://" + ip + ":" + port + "/machine-fast/serv/download/getJSON");
+        command.setNotificationURL("");
         //璧勬簮涓嬭浇閾炬帴鐨勮姹傚ご
-        command.setPreDownloadURL("http://" + ip + ":" + port + "/machine-fast/serv/download/downloadFile/");
+        command.setPreDownloadURL("https://ximonsmart.oss-cn-shanghai.aliyuncs.com/");
         command.setTask(task);
         xixun.set_id(UUID.randomUUID().toString());
         xixun.setCommand(command);
         xixun.setType("commandXixunPlayer");    //鍛戒护鍥哄畾绫诲瀷锛屼笉鍙洿鏀�
         String jsondata = new Gson().toJson(xixun);
-        lightemitUtils.clear(lightemitControlCode);
-        poleLightemitDao.updateRequestBody(lightemitControlCode, jsondata);
-        lightemitUtils.post(realtimeServerBean.getCommand() + lightemitControlCode, jsondata);
+        String clearResult = lightemitUtils.clear(lightemitControlCode);
+        if (clearResult.contains("does not exist")) {
+            return "鎺ㄩ�佸け璐�";
+        } else {
+            poleLightemitDao.updateRequestBody(lightemitControlCode, jsondata);
+            String post = lightemitUtils.post(realtimeServerBean.getCommand() + lightemitControlCode, jsondata);
+            LogUtils.error("缁撴灉:" + post);
+            //{"_type":"success","_id":"ce8dc3ff-dc88-43b7-8f55-60abd8700f1a","timestamp":1653555160535}
+            if (post.startsWith("{") && post.endsWith("}") && post.contains("_type\":\"success")) {
+                return "鎺ㄩ�佹垚鍔�";
+            } else {
+                return "鎺ㄩ�佸け璐�";
+            }
+
+        }
     }
 
     /**
@@ -424,16 +538,6 @@
     public void updateRequestBody(String ledCode, String postBody) {
         baseMapper.updateRequestBody(ledCode, postBody);
     }
-
-//    /**
-//     * 鏍规嵁led灞忕紪鐮佽幏鍙栫伅鏉嗘墍鏈変俊鎭�
-//     *
-//     * @param ledCode
-//     * @return
-//     */
-//    public PoleStreetlightEntity getStreetlightByLedCode(String ledCode) {
-//        return baseMapper.getStreetlightByLedCode(ledCode);
-//    }
 
 
     /**
@@ -458,4 +562,36 @@
         lightemitUtils.setBrightness(lightemitControlCode, brightness);
     }
 
+    public Object getProgram(Long lightemitId) {
+        PoleLightemitEntity xiXun = getById(lightemitId);
+
+        String requestBody = xiXun.getRequestBody();
+        if (requestBody == null || requestBody.isEmpty()) {
+            throw new BusinessException("鏈壘鍒拌妭鐩�");
+        }
+
+        Map map = JSON.parseObject(requestBody, Map.class);
+        return map;
+    }
+
+
+    /**
+     * 鍚慠edis涓瓨鍏ヨ澶囩姸鎬�
+     */
+    public void setCacheData() {
+        list().forEach(
+                xiXun -> {
+                    RedisDeviceStatus deviceStatus = new RedisDeviceStatus();
+                    deviceStatus.setDeviceId(xiXun.getLightemitControlCode());
+                    boolean ledOnLine = lightemitUtils.getLedOnLine(xiXun.getLightemitControlCode());
+                    if (ledOnLine) {
+                        deviceStatus.setStatus(0);
+                    } else {
+                        deviceStatus.setStatus(1);
+                    }
+                    redisUtils.set(DeviceRedisKey.XIXUN + xiXun.getLightemitControlCode(), JSON.toJSONString(deviceStatus));
+//                    redisUtils.delete(DeviceRedisKey.XIXUN + xiXun.getLightemitControlCode());
+                }
+        );
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3