Explorar o código

增删环境检测仪时 删除表

hxf hai 1 ano
pai
achega
a6df84b938

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

@@ -2,6 +2,7 @@ package com.jiayue.biz.conf;
 
 import cn.hutool.core.date.DateTime;
 import cn.hutool.core.thread.ThreadFactoryBuilder;
+import cn.hutool.core.util.StrUtil;
 import com.jiayue.biz.domain.EnvironmentalData;
 import com.jiayue.biz.domain.PointAttribute;
 import com.jiayue.biz.domain.TunnelInfo;
@@ -55,23 +56,41 @@ public class Channel {
 
     private HashMap<String, ModbusTcpMasterBuilder> masterMap;
 
+
     @Bean
-//    @Scheduled(fixedDelay = 60000 * 5)
     public void timingTunnel() {
         List<TunnelInfo> tunnelInfoList = tunnelInfoService.list();
         for (TunnelInfo tunnelInfo : tunnelInfoList) {
-            ModbusTcpMasterBuilder master = start(tunnelInfo);
-            masterMap.put(tunnelInfo.getStationId(), master);
+            if(StrUtil.isNotBlank(tunnelInfo.getStatus()) && tunnelInfo.getStatus().equals("1")){
+                startTunnel(tunnelInfo);
+            }
+        }
+    }
+    @Scheduled(fixedDelay = 60000 * 5)
+    public void lastTimingTunnel() {
+        List<TunnelInfo> tunnelInfoList = tunnelInfoService.list();
+        for (TunnelInfo tunnelInfo : tunnelInfoList) {
+            if(StrUtil.isBlank(tunnelInfo.getStatus()) || tunnelInfo.getStatus().equals("0")){
+                startTunnel(tunnelInfo);
+                tunnelInfo.setStatus("1");
+            }
 
         }
+        tunnelInfoService.updateBatchById(tunnelInfoList);
+    }
+
+    public void startTunnel(TunnelInfo tunnelInfo) {
+        ModbusTcpMasterBuilder master = start(tunnelInfo);
+        masterMap.put(tunnelInfo.getWeatherLookNo(), master);
     }
 
+
     //创建线程
     public ModbusTcpMasterBuilder start(TunnelInfo tunnelInfo) {
         //创建通道后    对通道map 进行  采集数据
         //获取场站id   形成map
         ModbusTcpMasterBuilder master = new ModbusTcpMasterBuilder(tunnelInfo.getIp(), tunnelInfo.getPort());
-        master.getConfigInfoMap().put("stationId", tunnelInfo.getStationId());
+        master.getConfigInfoMap().put("weatherLookNo", tunnelInfo.getWeatherLookNo());
         //create  会阻塞线程
         calculatorThread.execute(master::create);
         master.isConnected();
@@ -79,10 +98,10 @@ public class Channel {
     }
 
     //关闭通道移除连接
-    public void stop(String stationId) {
-        if (masterMap.get(stationId).isConnected()) {
-            masterMap.get(stationId).stop();
-            masterMap.remove(stationId);
+    public void stop(String weatherLookNo) {
+        if (masterMap.get(weatherLookNo).isConnected()) {
+            masterMap.get(weatherLookNo).stop();
+            masterMap.remove(weatherLookNo);
         }
     }
 
@@ -93,19 +112,19 @@ public class Channel {
         DateTime dateTime = DateUtil.beginOfMinute(new Date());
         for (Map.Entry<String, ModbusTcpMasterBuilder> masterBuilderEntry : masterMap.entrySet()) {
             //获取通道map中的key
-            String stationId = masterBuilderEntry.getKey();
+            String weatherLookNo = masterBuilderEntry.getKey();
             //根据通道key获取ip端口号等数据
-            List<TunnelInfo> tunnelist = tunnelInfoList.stream().filter(t -> t.getStationId().equals(stationId)).collect(Collectors.toList());
+            List<TunnelInfo> tunnelist = tunnelInfoList.stream().filter(t -> t.getWeatherLookNo().equals(weatherLookNo)).collect(Collectors.toList());
             TunnelInfo tunnelInfo;
             if (!tunnelist.isEmpty()) {
                 tunnelInfo = tunnelist.get(0);
             } else {
                 //如果此通道不存在 关闭通道
-                this.stop(stationId);
+                this.stop(weatherLookNo);
                 continue;
             }
             //根据通道key获取点表数据
-            List<PointAttribute> pointAttributeList = attributeList.stream().filter(a -> a.getStationId().equals(stationId)).collect(Collectors.toList());
+            List<PointAttribute> pointAttributeList = attributeList.stream().filter(a -> a.getWeatherLookNo().equals(weatherLookNo)).collect(Collectors.toList());
             //拿到所有点位
             List<Integer> points = pointAttributeList.stream().map(PointAttribute::getPoint).collect(Collectors.toList());
             Map<Integer, ModbusDataTypeEnum> pointTypeMap = new HashMap<>();
@@ -146,10 +165,16 @@ public class Channel {
 
                 }
             } catch (ModbusException e) {
+                //出异常 停止 并且 设置状态
+                for (TunnelInfo info : tunnelist) {
+                    stop(info.getWeatherLookNo());
+                    info.setStatus("NO");
+                }
+                tunnelInfoService.updateBatchById(tunnelist);
                 log.info(e.getMsg());
                 throw new RuntimeException(e);
             }
-            environmentalDataService.insertOne(packageData(weatherLookList.get(0).getWeatherLookNo(), pointMap,dateTime));
+            environmentalDataService.insertOne(packageData(weatherLookList.get(0).getWeatherLookNo(), pointMap, dateTime));
         }
     }
 
@@ -164,7 +189,7 @@ public class Channel {
     }
 
 
-    public EnvironmentalData packageData(String weatherNo, HashMap<String, BigDecimal> pointMap,DateTime dateTime) {
+    public EnvironmentalData packageData(String weatherNo, HashMap<String, BigDecimal> pointMap, DateTime dateTime) {
         EnvironmentalData environmentalData = new EnvironmentalData();
         environmentalData.setTs(new Timestamp(dateTime.getTime()));
         environmentalData.setWeatherLookNo(weatherNo);

+ 9 - 2
wps-biz/src/main/java/com/jiayue/biz/controller/WeatherLookController.java

@@ -1,6 +1,7 @@
 package com.jiayue.biz.controller;
 
 import com.jiayue.biz.domain.WeatherLook;
+import com.jiayue.biz.service.EnvironmentalDataService;
 import com.jiayue.biz.service.WeatherLookService;
 import com.jiayue.common.annotation.Log;
 import com.jiayue.common.core.controller.BaseController;
@@ -14,12 +15,17 @@ import org.springframework.web.bind.annotation.*;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
+import java.util.stream.Collectors;
 
 @RequiredArgsConstructor(onConstructor_ = @Autowired)
 @RestController
 @RequestMapping("/weatherLook")
 public class WeatherLookController extends BaseController {
     private final WeatherLookService weatherLookService;
+
+    private final EnvironmentalDataService environmentalDataService;
+
+
     /**
      * 查询环境监测仪信息列表
      */
@@ -36,7 +42,7 @@ public class WeatherLookController extends BaseController {
     @Log(title = "环境监测仪信息", businessType = BusinessType.INSERT)
     @PostMapping
     public AjaxResult add(@RequestBody WeatherLook weatherLook) {
-        return toAjax(weatherLookService.save(weatherLook) ? 1 : 0);
+        return toAjax(environmentalDataService.saveWeather(weatherLook) ? 1 : 0);
     }
 
     /**
@@ -54,7 +60,8 @@ public class WeatherLookController extends BaseController {
     @Log(title = "环境监测仪信息", businessType = BusinessType.DELETE)
     @DeleteMapping("/{ids}")
     public AjaxResult remove(@PathVariable String ids) {
-        return toAjax(weatherLookService.removeById(ids) ? 1 : 0);
+        List<WeatherLook> weatherLookList = weatherLookService.list().stream().filter(w -> w.getId().equals(ids)).collect(Collectors.toList());
+        return toAjax(environmentalDataService.deleteWeather(ids,weatherLookList) ? 1 : 0);
     }
     @GetMapping("/infoList")
     public AjaxResult infoList() {

+ 1 - 3
wps-biz/src/main/java/com/jiayue/biz/domain/TunnelInfo.java

@@ -9,7 +9,7 @@ import lombok.NoArgsConstructor;
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
-//@TableName("tunnel_info")
+@TableName("tunnel_info")
 public class TunnelInfo {
     /*通道id*/
     @TableId
@@ -20,8 +20,6 @@ public class TunnelInfo {
     private String ip;
     //端口号
     private int port;
-    //场站Id
-    private String stationId;
     //数据类型 CDAB
     private String dataFormat;
     //设备编号

+ 9 - 0
wps-biz/src/main/java/com/jiayue/biz/mapper/EnvironmentalDataMapper.java

@@ -4,6 +4,7 @@ import cn.hutool.db.Entity;
 import com.baomidou.dynamic.datasource.annotation.DS;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.jiayue.biz.domain.EnvironmentalData;
+import org.apache.ibatis.annotations.Delete;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
 
@@ -26,7 +27,15 @@ public interface EnvironmentalDataMapper extends BaseMapper<EnvironmentalData> {
 
     @Select("select * from environmental_${weatherLookNo}")
     List<EnvironmentalData> selectAll(@Param("weatherLookNo") String weatherLookNo);
+
     @Select("select * from environmental_${weatherLookNo} order by ts desc limit 1")
     EnvironmentalData selectLastTimeData(@Param("weatherLookNo") String weatherLookNo);
 
+    @Select("CREATE TABLE IF NOT EXISTS environmental_${weatherLookNo} USING environmental_data TAGS (#{weatherLookNo})")
+    void createTable(@Param("weatherLookNo") String weatherLookNo);
+
+    @Delete("DROP TABLE IF EXISTS environmental_${weatherLookNo}")
+    void deleteTable(@Param("weatherLookNo") String weatherLookNo);
+
+
 }

+ 14 - 0
wps-biz/src/main/java/com/jiayue/biz/service/EnvironmentalDataService.java

@@ -2,6 +2,7 @@ package com.jiayue.biz.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.jiayue.biz.domain.EnvironmentalData;
+import com.jiayue.biz.domain.WeatherLook;
 
 import javax.servlet.http.HttpServletResponse;
 import java.sql.Timestamp;
@@ -23,4 +24,17 @@ public interface EnvironmentalDataService extends IService<EnvironmentalData> {
     void exportExcel(HttpServletResponse response, String weatherLookNo, long startTime, long endTime);
 
     ArrayList<HashMap<String, Object>> getTAndPaAndRh();
+
+    void createTable(String weatherLookNo);
+
+
+    void deleteTable(String weatherLookNo);
+
+
+
+    boolean saveWeather(WeatherLook weatherLook);
+
+    //删除
+    boolean deleteWeather(String ids, List<WeatherLook> weatherLookList);
+
 }

+ 23 - 6
wps-biz/src/main/java/com/jiayue/biz/service/impl/EnvironmentalDataServiceImpl.java

@@ -1,6 +1,5 @@
 package com.jiayue.biz.service.impl;
 
-import cn.hutool.db.Entity;
 import com.alibaba.excel.EasyExcel;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.jiayue.biz.domain.CustomStringStringConverter;
@@ -12,14 +11,12 @@ import com.jiayue.biz.service.WeatherLookService;
 import com.jiayue.biz.util.FileUtil;
 import com.jiayue.common.utils.DateUtil;
 import lombok.AllArgsConstructor;
-import org.springframework.beans.factory.annotation.Value;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.servlet.http.HttpServletResponse;
 import java.io.File;
-import java.io.IOException;
 import java.sql.Timestamp;
-import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -142,12 +139,10 @@ public class EnvironmentalDataServiceImpl extends ServiceImpl<EnvironmentalDataM
     }
 
     /**
-     *
      * @return 首页温湿压
      */
     public ArrayList<HashMap<String, Object>> getTAndPaAndRh() {
         List<WeatherLook> weatherLookList = weatherLookService.list();
-        weatherLookList = weatherLookList.stream().filter(w -> w.getWeatherLookNo().equals("1001")).collect(Collectors.toList());
         ArrayList<HashMap<String, Object>> list = new ArrayList<>();
         for (WeatherLook weatherLook : weatherLookList) {
             HashMap<String, Object> weatherMap = new HashMap<>();
@@ -184,5 +179,27 @@ public class EnvironmentalDataServiceImpl extends ServiceImpl<EnvironmentalDataM
 
     }
 
+    //创建子表
+    public void createTable(String weatherLookNo) {
+        baseMapper.createTable(weatherLookNo);
+    }
+
+    //删除子表
+    public void deleteTable(String weatherLookNo) {
+        baseMapper.deleteTable(weatherLookNo);
+    }
+
+
+    public boolean saveWeather(WeatherLook weatherLook) {
+        this.createTable(weatherLook.getWeatherLookNo());
+        return weatherLookService.save(weatherLook);
+    }
+
+    //删除
+    public boolean deleteWeather(String ids, List<WeatherLook> weatherLookList) {
+        this.deleteTable(weatherLookList.get(0).getWeatherLookNo());
+        return weatherLookService.removeById(weatherLookList.get(0).getId());
+    }
+
 
 }