瀏覽代碼

新增通道时添加到临时map中

hxf 1 年之前
父節點
當前提交
ab859dbbe1

+ 14 - 6
wps-biz/src/main/java/com/jiayue/biz/conf/Channel.java

@@ -11,6 +11,7 @@ import com.jiayue.biz.vo.AttributeAndPointVo;
 import com.jiayue.common.utils.DateUtil;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.annotation.Scheduled;
@@ -36,7 +37,7 @@ import java.util.stream.Collectors;
 @AllArgsConstructor
 @Slf4j
 public class Channel {
-    private final
+    @Autowired
     TunnelInfoService tunnelInfoService;
 
     private final WeatherLookService weatherLookService;
@@ -63,6 +64,13 @@ public class Channel {
         }
     }
 
+    //添加通道
+    public void addMasterMap(TunnelInfo tunnelInfo) {
+        if (tunnelInfo != null) {
+            startTunnel(tunnelInfo);
+        }
+    }
+
     //定时重启异常通道
     @Scheduled(fixedDelay = 60000)
     public void restartTunnel() {
@@ -91,9 +99,9 @@ public class Channel {
     }
 
     //关闭通道移除连接
-    public void stop(String weatherLookNo) {
-        masterMap.get(weatherLookNo).stop();
-        masterMap.remove(weatherLookNo);
+    public void stop(String id) {
+        masterMap.get(id).stop();
+        masterMap.remove(id);
     }
 
 
@@ -121,7 +129,7 @@ public class Channel {
             //根据通道key获取点表数据
             List<AttributeAndPointVo> attributeAndPointVos = attributeAndPointVoList.stream().filter(g -> g.getTunnelId().equals(masterBuilderEntry.getKey())).collect(Collectors.toList());
             if (attributeAndPointVos.size() == 0) {
-                log.info("{}此通道没有设置点位",tunnelInfo.getTunnelName());
+                log.info("{}此通道没有设置点位", tunnelInfo.getTunnelName());
                 continue;
             }
             //拿到所有点位
@@ -133,7 +141,7 @@ public class Channel {
 //            判断是否存在此塔
             List<WeatherLook> weatherLookList = weatherLookService.lambdaQuery().eq(WeatherLook::getWeatherLookNo, attributeAndPointVos.get(0).getEquipmentId()).list();
             if (weatherLookList.isEmpty()) {
-                log.info("此{}环境监测仪不存在",attributeAndPointVos.get(0).getEquipmentId());
+                log.info("此{}环境监测仪不存在", attributeAndPointVos.get(0).getEquipmentId());
                 continue;
             }
             // 例如: key:wsAve val:数据

+ 3 - 3
wps-biz/src/main/java/com/jiayue/biz/controller/TunnelInfoController.java

@@ -34,7 +34,7 @@ public class TunnelInfoController extends BaseController {
     @Log(title = "环境监测仪信息", businessType = BusinessType.INSERT)
     @PostMapping
     public AjaxResult add(@RequestBody TunnelInfo tunnelInfo) {
-        return toAjax(tunnelInfoService.save(tunnelInfo) ? 1 : 0);
+        return toAjax(tunnelInfoService.addTunnelForMasterMap(tunnelInfo) ? 1 : 0);
     }
 
     /**
@@ -43,7 +43,7 @@ public class TunnelInfoController extends BaseController {
     @Log(title = "环境监测仪信息", businessType = BusinessType.UPDATE)
     @PutMapping
     public AjaxResult edit(@RequestBody TunnelInfo tunnelInfo) {
-        return toAjax(tunnelInfoService.updateById(tunnelInfo) ? 1 : 0);
+        return toAjax(tunnelInfoService.updateTunnelForMasterMap(tunnelInfo) ? 1 : 0);
     }
 
     /**
@@ -52,7 +52,7 @@ public class TunnelInfoController extends BaseController {
     @Log(title = "环境监测仪信息", businessType = BusinessType.DELETE)
     @DeleteMapping("/{ids}")
     public AjaxResult remove(@PathVariable String ids) {
-        return toAjax(tunnelInfoService.removeById(ids) ? 1 : 0);
+        return toAjax(tunnelInfoService.deleteTunnelForMasterMap(ids) ? 1 : 0);
     }
 
     /**

+ 7 - 0
wps-biz/src/main/java/com/jiayue/biz/service/TunnelInfoService.java

@@ -9,4 +9,11 @@ import java.util.Map;
 public interface TunnelInfoService extends IService<TunnelInfo> {
     /*首页通道信息*/
     List<Map<String,Object>> homepageTunnelInfo();
+
+
+    boolean addTunnelForMasterMap(TunnelInfo tunnelInfo);
+
+    boolean updateTunnelForMasterMap(TunnelInfo tunnelInfo);
+
+    boolean deleteTunnelForMasterMap(String id);
 }

+ 59 - 8
wps-biz/src/main/java/com/jiayue/biz/service/impl/TunnelInfoServiceImpl.java

@@ -1,9 +1,12 @@
 package com.jiayue.biz.service.impl;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.jiayue.biz.conf.Channel;
 import com.jiayue.biz.domain.TunnelInfo;
 import com.jiayue.biz.mapper.TunnelInfoMapper;
 import com.jiayue.biz.service.TunnelInfoService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
@@ -16,21 +19,69 @@ import java.util.Map;
  */
 @Service
 public class TunnelInfoServiceImpl extends ServiceImpl<TunnelInfoMapper, TunnelInfo> implements TunnelInfoService {
+
+    @Lazy
+    @Autowired
+    Channel channel;
+
     /*首页通道信息*/
-    public List<Map<String,Object>> homepageTunnelInfo(){
-        List<Map<String,Object>> list = new ArrayList<>();
+    public List<Map<String, Object>> homepageTunnelInfo() {
+        List<Map<String, Object>> list = new ArrayList<>();
         try {
             List<TunnelInfo> tunnelInfoList = this.list();
-            for(TunnelInfo tunnelInfo : tunnelInfoList){
-                Map<String,Object> map = new HashMap<>();
-                map.put("tunnelName",tunnelInfo.getTunnelName());
-                map.put("address",tunnelInfo.getIp()+":"+tunnelInfo.getPort());
-                map.put("status",tunnelInfo.getStatus());
+            for (TunnelInfo tunnelInfo : tunnelInfoList) {
+                Map<String, Object> map = new HashMap<>();
+                map.put("tunnelName", tunnelInfo.getTunnelName());
+                map.put("address", tunnelInfo.getIp() + ":" + tunnelInfo.getPort());
+                map.put("status", tunnelInfo.getStatus());
                 list.add(map);
             }
-        }catch (Exception e){
+        } catch (Exception e) {
             e.printStackTrace();
         }
         return list;
     }
+
+
+    /**
+     * 添加
+     *
+     * @param tunnelInfo 通道信息
+     * @return true/false
+     */
+    public boolean addTunnelForMasterMap(TunnelInfo tunnelInfo) {
+        channel.addMasterMap(tunnelInfo);
+        return this.save(tunnelInfo);
+
+    }
+    /**
+     * 修改
+     *
+     * @param tunnelInfo 通道信息
+     * @return true/false
+     */
+    public boolean updateTunnelForMasterMap(TunnelInfo tunnelInfo) {
+        List<TunnelInfo> tunnelInfoList = this.lambdaQuery().eq(TunnelInfo::getId, tunnelInfo.getId()).list();
+        if (!tunnelInfoList.get(0).getIp().equals(tunnelInfo.getIp()) || tunnelInfoList.get(0).getPort() != tunnelInfo.getPort()) {
+            channel.stop(tunnelInfo.getId());
+            channel.addMasterMap(tunnelInfo);
+        }
+
+        return this.updateById(tunnelInfo);
+    }
+
+    /**
+     * 删除
+     *
+     * @param id id
+     * @return true/false
+     */
+    public boolean deleteTunnelForMasterMap(String id) {
+
+        channel.stop(id);
+
+        return this.removeById(id);
+    }
+
+
 }