Browse Source

同步scada代码,scada报警支持时间

haiqiu 2 years ago
parent
commit
0a61ce9149

+ 2 - 3
hnls-scada/src/main/java/com/tofly/scada/ScadaApplication.java

@@ -18,12 +18,11 @@ import org.springframework.scheduling.annotation.EnableScheduling;
 @EnableAsync
 @EnableFeignClients(basePackages ={"com.tofly.api","com.tofly.common.log"},defaultConfiguration = {FeignConfig.class})
 @ComponentScan({"com.tofly.*"})
-@MapperScan({"com.tofly.*.mapper", "com.tofly.*.custom.mapper","com.tofly.gpsboot.mapper.*"})
+@MapperScan({"com.tofly.*.mapper"})
 public class ScadaApplication {
 
-
     public static void main(String[] args) {
-        SpringApplication.run(ScadaApplication.class,args);
+        SpringApplication.run(ScadaApplication.class, args);
     }
 
 }

+ 8 - 8
hnls-scada/src/main/java/com/tofly/scada/common/FeignConfig.java

@@ -25,30 +25,30 @@ public class FeignConfig implements RequestInterceptor {
         //添加token
         requestTemplate.header("Authorization", request.getHeader("Authorization"));
         //将body的数据写入参数
-        if("GET".equals(requestTemplate.method())){
-            try{
+        if ("GET".equals(requestTemplate.method())) {
+            try {
                 //获取body内容
                 String json = requestTemplate.requestBody().asString();
                 if (StringUtils.isEmpty(json)) {
-                    return ;
+                    return;
                 }
-                Map<String, Object> map= JSON.parseObject(json);
+                Map<String, Object> map = JSON.parseObject(json);
                 Set<String> set = map.keySet();
                 Iterator<String> it = set.iterator();
                 while (it.hasNext()) {
                     String key = (String) it.next();
                     Object values = map.get(key);
-                    if (values!=null && !"".equals(values) && !"null".equals(values)) {
+                    if (values != null && !"".equals(values) && !"null".equals(values)) {
                         //将body的参数写入queries
-                        requestTemplate.query(key,values.toString());
+                        requestTemplate.query(key, values.toString());
                     }
                 }
-                Class requestClass=requestTemplate.getClass();
+                Class requestClass = requestTemplate.getClass();
                 Field field = requestClass.getDeclaredField("body");
                 field.setAccessible(true);
                 //修改body为空。
                 field.set(requestTemplate, Request.Body.empty());
-            }catch (Exception e){
+            } catch (Exception e) {
 
             }
 

+ 7 - 10
hnls-scada/src/main/java/com/tofly/scada/controller/ScadaMsgController.java

@@ -1,12 +1,13 @@
 package com.tofly.scada.controller;
 
 import com.tofly.common.core.entity.ResultRespone;
+import com.tofly.scada.entity.Scada;
 import com.tofly.scada.service.ScadaService;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 /**
@@ -22,19 +23,15 @@ public class ScadaMsgController {
     @Autowired
     private ScadaService scadaService;
 
-
     /**
-     * 获取同步scada信息数据ID
+     * 获取同步scada信息数据
      *
-     * @param id scadaID
      * @return
      */
+    @ApiOperation(value = "手动更新scada数据(数据测试)")
     @RequestMapping("/scada")
-    public ResultRespone getMsg(@RequestParam(required = false) Long id) {
-        log.info("获取到同步scada信息数据ID:-----{}-----", id);
-        if (id != null) {
-            scadaService.insertData(id);
-        }
-        return ResultRespone.success(id);
+    public ResultRespone getMsg(Scada scada){
+        scadaService.insertData(scada);
+        return ResultRespone.success(scada);
     }
 }

+ 7 - 0
hnls-scada/src/main/java/com/tofly/scada/entity/Scada.java

@@ -4,8 +4,10 @@ import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.KeySequence;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
+import com.fasterxml.jackson.annotation.JsonFormat;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
+import org.springframework.format.annotation.DateTimeFormat;
 
 import java.util.Date;
 
@@ -27,9 +29,14 @@ public class Scada {
     @ApiModelProperty(value = "指标值")
     private String value;
 
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     @ApiModelProperty(value = "指标采集时间")
     private Date scadaTime;
 
+
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     @ApiModelProperty(value = "指标更新时间")
     private Date updateTime;
 }

+ 10 - 0
hnls-scada/src/main/java/com/tofly/scada/entity/vo/AllocationVo.java

@@ -2,11 +2,14 @@ package com.tofly.scada.entity.vo;
 
 import com.baomidou.mybatisplus.annotation.*;
 import com.tofly.scada.entity.Scada;
+import com.tofly.scada.entity.ScadaReport;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 
 import java.io.Serializable;
 import java.util.Date;
+import java.util.List;
 
 /**
  * 指标配置
@@ -124,6 +127,13 @@ public class AllocationVo implements Serializable {
      */
     private Scada scada;
 
+    /**
+     * 绑定报警类型列表
+     */
+    @ApiModelProperty(value = "绑定报警类型列表",hidden = true)
+    @TableField(exist = false)
+    private List<ScadaReport> scadaReports;
+
     @TableField(exist = false)
     private static final long serialVersionUID = 1L;
 }

+ 10 - 2
hnls-scada/src/main/java/com/tofly/scada/mapper/ScadaMapper.java

@@ -79,9 +79,9 @@ public interface ScadaMapper extends BaseMapper<Scada> {
      * @return 历史数据列表
      */
     @Select("select  * from ${tableName} " +
-            "where ID = #{id}}")
+            "where ID = #{id}")
     List<Scada> selectHistoryById(@Param("tableName") String tableName,
-                                    @Param("start") Long id);
+                                    @Param("id") Long id);
 
     /**
      * 添加月份数据
@@ -92,6 +92,14 @@ public interface ScadaMapper extends BaseMapper<Scada> {
     int insertMouth(@Param("scadas") List<Scada> scadas,@Param("tableName") String tableName);
 
 
+    /**
+     * 添加月份数据
+     * @param scada 数据
+     * @param tableName 表名
+     * @return 影响条数
+     */
+    int insertByOne(@Param("item") Scada scada,@Param("tableName") String tableName);
+
     /**
      * 根据表名和时间段删除表数据
      * @param tableName 表名

+ 1 - 2
hnls-scada/src/main/java/com/tofly/scada/service/ScadaService.java

@@ -63,7 +63,6 @@ public interface ScadaService {
 
     /**
      * 根据scada数据ID存储数据(暂时分表到月份表上)
-     * @param scadaId 数据ID
      */
-    void insertData(Long scadaId);
+    void insertData(Scada scada);
 }

+ 96 - 38
hnls-scada/src/main/java/com/tofly/scada/service/impl/ScadaServiceImpl.java

@@ -572,6 +572,64 @@ public class ScadaServiceImpl implements ScadaService {
 
     @Override
     public List<DeviceArchiveManageVo> getAppData(DeviceQuery deviceQuery) {
+        QueryWrapper<DeviceArchiveManage> queryWrapper = getQueryWrapper(deviceQuery);
+        List<DeviceArchiveManage> deviceArchiveManages = deviceArchiveManageMapper.pageList(queryWrapper);
+        if (CollectionUtils.isEmpty(deviceArchiveManages)) {
+            return null;
+        }
+        List<DeviceArchiveManageVo> manageVos = deviceArchiveManages.stream().map(deviceArchiveManage -> {
+            DeviceArchiveManageVo vo = new DeviceArchiveManageVo();
+            BeanUtils.copyProperties(deviceArchiveManage, vo);
+            //查询绑定的指标
+            getAllocation(vo);
+            return vo;
+        }).collect(Collectors.toList());
+        return manageVos;
+    }
+
+
+    /**
+     * 获取关联指标
+     * @param vo
+     */
+    private void getAllocation(DeviceArchiveManageVo vo) {
+        QueryWrapper<Allocation> allocationQueryWrapper = new QueryWrapper<>();
+        allocationQueryWrapper.eq("DEVICE_ID", vo.getId());
+        List<Allocation> allocations = allocationMapper.selectList(allocationQueryWrapper);
+        if (!CollectionUtils.isEmpty(allocations)) {
+            List<AllocationVo> allocationVos = allocations.stream().map(allocation -> {
+                AllocationVo allocationVo = new AllocationVo();
+                BeanUtils.copyProperties(allocation, allocationVo);
+                //查询scada数据
+                List<Scada> dataByCode = scadaMapper.getListDataByCode(allocation.getVariableCode());
+                if (!CollectionUtils.isEmpty(dataByCode)) {
+                    allocationVo.setScada(dataByCode.get(0));
+                }
+                //查询绑定的报警列表
+                Set<Long> reportIdSets = getReportIdSets(Collections.singletonList(allocation));
+                if (!CollectionUtils.isEmpty(reportIdSets)) {
+                    logger.info("获取指标报警相关....");
+                    QueryWrapper<ScadaReport> scadaReportQueryWrapper = new QueryWrapper<>();
+                    scadaReportQueryWrapper.in("ID", reportIdSets);
+                    List<ScadaReport> scadaReports = reportMapper.selectList(scadaReportQueryWrapper);
+                    if (!CollectionUtils.isEmpty(scadaReports)){
+                        allocationVo.setScadaReports(scadaReports);
+                    }
+                }
+                //返回数据
+                return allocationVo;
+            }).collect(Collectors.toList());
+            vo.setAllocations(allocationVos);
+        }
+    }
+
+
+    /**
+     * 设置查询条件
+     * @param deviceQuery
+     * @return
+     */
+    private QueryWrapper<DeviceArchiveManage> getQueryWrapper(DeviceQuery deviceQuery) {
         QueryWrapper<DeviceArchiveManage> queryWrapper = new QueryWrapper<>();
         if (!ObjectUtils.isEmpty(deviceQuery)) {
             if (StringUtils.hasText(deviceQuery.getType())) {
@@ -581,52 +639,53 @@ public class ScadaServiceImpl implements ScadaService {
                 queryWrapper.eq("manage.STATUS", deviceQuery.getStatus());
             }
             if (StringUtils.hasText(deviceQuery.getQuery())) {
-                queryWrapper.like("manage.ADDRESS", deviceQuery.getQuery());
+                queryWrapper.and(q->{
+                    q.like("manage.ADDRESS", deviceQuery.getQuery())
+                            .or().like("manage.NAME", deviceQuery.getQuery())
+                            .or().like("manage.DESCRIBE", deviceQuery.getQuery());
+                });
+
             }
             if (StringUtils.hasText(deviceQuery.getCode())){
-                queryWrapper.like("allocation.VARIABLE_CODE",deviceQuery.getCode());
+                queryWrapper.like("allocation.VARIABLE_CODE", deviceQuery.getCode());
             }
             if (deviceQuery.getDeviceId()!=null){
-                queryWrapper.eq("manage.ID",deviceQuery.getDeviceId());
+                queryWrapper.eq("manage.ID", deviceQuery.getDeviceId());
             }
             if (deviceQuery.getLatitude()!=null && deviceQuery.getLongitude()!=null){
                 queryWrapper.eq("manage.LATITUDE", deviceQuery.getLatitude())
-                        .eq("manage.LONGITUDE",deviceQuery.getLongitude());
+                        .eq("manage.LONGITUDE", deviceQuery.getLongitude());
             }
         }
-        List<DeviceArchiveManage> deviceArchiveManages = deviceArchiveManageMapper.pageList(queryWrapper);
-        if (CollectionUtils.isEmpty(deviceArchiveManages)) {
-            return null;
-        }
-        List<DeviceArchiveManageVo> manageVos = deviceArchiveManages.stream().map(deviceArchiveManage -> {
-            DeviceArchiveManageVo vo = new DeviceArchiveManageVo();
-            BeanUtils.copyProperties(deviceArchiveManage, vo);
-            //查询绑定的指标
-            QueryWrapper<Allocation> allocationQueryWrapper = new QueryWrapper<>();
-            allocationQueryWrapper.eq("DEVICE_ID", vo.getId());
-            List<Allocation> allocations = allocationMapper.selectList(allocationQueryWrapper);
-            if (!CollectionUtils.isEmpty(allocations)) {
-                List<AllocationVo> allocationVos = allocations.stream().map(allocation -> {
-                    AllocationVo allocationVo = new AllocationVo();
-                    BeanUtils.copyProperties(allocation, allocationVo);
-                    //查询scada数据
-                    List<Scada> dataByCode = scadaMapper.getListDataByCode(allocation.getVariableCode());
-                    if (!CollectionUtils.isEmpty(dataByCode)) {
-                        allocationVo.setScada(dataByCode.get(0));
-                    }
-                    return allocationVo;
-                }).collect(Collectors.toList());
-                vo.setAllocations(allocationVos);
-            }
-
-            return vo;
-        }).collect(Collectors.toList());
-        return manageVos;
+        return queryWrapper;
     }
 
+
     @Override
-    public void insertData(Long scadaId) {
-        Scada scada = scadaMapper.selectById(scadaId);
+    public void insertData(Scada scada) {
+        syncSave(scada);
+    }
+
+    @Async
+    public void syncSave(Scada data){
+        if (ObjectUtils.isEmpty(data)){
+            throw new RuntimeException("参数为为空");
+        }
+        Scada scada = null;
+        if (data.getId()!=null){
+            scada = scadaMapper.selectById(data.getId());
+        }else if (StringUtils.hasText(data.getCode())){
+            Page<Scada> page = new Page<>(1,1);
+            QueryWrapper<Scada> scadaQueryWrapper = new QueryWrapper<>();
+            scadaQueryWrapper.eq("CODE",data.getCode())
+                    .orderByDesc("SCADA_TIME");
+            Page<Scada> scadas = scadaMapper.selectPage(page,scadaQueryWrapper);
+            List<Scada> records = scadas.getRecords();
+            if (!CollectionUtils.isEmpty(records)){
+                scada = records.get(0);
+            }
+        }
+
         log.info("获取到同步scada信息数据:-----{}-----", scada.toString());
         if (!ObjectUtils.isEmpty(scada)) {
             //检测是否为系统创建的指标
@@ -642,10 +701,8 @@ public class ScadaServiceImpl implements ScadaService {
             //检测数据是否存在,不存在就插入
             if (CollectionUtils.isEmpty(scadaMapper.selectHistoryById(tableName,scada.getId()))){
                 log.info("scada数据不存在月份表中,正在执行新增数据");
-                List<Scada> scadas = new ArrayList<>();
-                scadas.add(scada);
-                scadaMapper.insertMouth(scadas, tableName);
-                log.info("新增数据成功:-----{}-----", scadas);
+                scadaMapper.insertByOne(scada, tableName);
+                log.info("新增数据成功:-----{}-----", scada);
             }
             //检测报警信息
             sendNotice(scada);
@@ -683,6 +740,7 @@ public class ScadaServiceImpl implements ScadaService {
                         messagepush.setMessage(createMsg(scadaReport.getMsgTemplate(),scadaReport));
                         messagepush.setType("8");
                         messagepush.setTableName("SCADA_HISTORY");
+                        messagepush.setCreateTime(scada.getScadaTime());
                         sendTidingsUtil.sendMsg(messagepush, scadaReport.getLiaisonPeople());
                         logger.info("发送报警消息:{}", messagepush);
                     }

+ 5 - 5
hnls-scada/src/main/java/com/tofly/scada/service/impl/StatisticsScadaServiceImpl.java

@@ -68,7 +68,7 @@ public class StatisticsScadaServiceImpl extends ServiceImpl<StatisticsScadaMappe
             List<StatisticsScadaVo> statisticsScadaVos = new ArrayList<>();
             for (Map.Entry<String, List<ScadaVo>> entity : listMap.entrySet()) {
                 StatisticsScadaVo statisticsScadaVo = new StatisticsScadaVo();
-
+                logger.info("有数据分组计算相关....");
                 List<ScadaVo> scadaList = entity.getValue();
                 double average = scadaList.stream().map(scada -> Double.parseDouble(scada.getValue()))
                         .mapToDouble(Double::shortValue).average().getAsDouble();
@@ -92,13 +92,13 @@ public class StatisticsScadaServiceImpl extends ServiceImpl<StatisticsScadaMappe
                 statisticsScadaVo.setScadaVos(scadaList);
 
                 //查询指标报警上下限计算合格率相关
+                logger.info("查询指标报警上下限计算合格率相关....");
                 QueryWrapper<Allocation> allocationQueryWrapper = new QueryWrapper<>();
                 allocationQueryWrapper.eq("VARIABLE_CODE", scadaList.get(0).getCode());
                 List<Allocation> allocations = allocationMapper.selectList(allocationQueryWrapper);
-                List<Allocation> collectAllocations = allocations.stream()
-                        .filter(allocation -> StringUtils.hasText(allocation.getType())).collect(Collectors.toList());
-                if (!CollectionUtils.isEmpty(collectAllocations)) {
-                    Set<Long> reportIdSets = ScadaServiceImpl.getReportIdSets(collectAllocations);
+                if (!CollectionUtils.isEmpty(allocations)) {
+                    Set<Long> reportIdSets = ScadaServiceImpl.getReportIdSets(allocations);
+                    logger.info("获取模版ID集合:{}....",reportIdSets);
                     if (!CollectionUtils.isEmpty(reportIdSets)) {
                         logger.info("计算指标报警上下限计算合格率相关....");
                         QueryWrapper<ScadaReport> scadaReportQueryWrapper = new QueryWrapper<>();

+ 9 - 1
hnls-scada/src/main/resources/mapper/ScadaMapper.xml

@@ -9,11 +9,19 @@
         <foreach collection="scadas" item="item" separator="union all" open="(" close=")">
             select
             #{item.id,jdbcType=NUMERIC},#{item.code,jdbcType=VARCHAR},#{item.value,jdbcType=VARCHAR},
-             #{item.scadaTime,jdbcType=DATE},#{item.updateTime,jdbcType=DATE}
+             #{item.scadaTime,jdbcType=TIMESTAMP},#{item.updateTime,jdbcType=TIMESTAMP}
             from dual
         </foreach>
     </insert>
 
+    <insert id="insertByOne">
+        INSERT INTO
+            "TF_XRTY"."${tableName}" ("ID", "CODE", "VALUE", "SCADA_TIME", "UPDATE_TIME")
+        VALUES
+            (#{item.id,jdbcType=NUMERIC},#{item.code,jdbcType=VARCHAR},#{item.value,jdbcType=VARCHAR},
+             #{item.scadaTime,jdbcType=TIMESTAMP},#{item.updateTime,jdbcType=TIMESTAMP})
+    </insert>
+
     <resultMap id="map" type="com.tofly.scada.entity.Scada">
         <id column="ID" property="id"></id>
         <result column="CODE" property="code"></result>