浏览代码

"富平scada"

yangjunfeng 1 周之前
父节点
当前提交
daf0f25e4d

+ 27 - 0
tofly-zhongming-gas/service/zmrqScada/src/main/java/com/tofly/zmrqScada/Config/ScheduledScadaTask.java

@@ -71,4 +71,31 @@ public class ScheduledScadaTask {
         long l1 = System.currentTimeMillis();
         log.info("同步scada重庆公司站点监测数据 耗时:{}",l1-l);
     }
+
+    /**
+     * 有删除 插入操作 不用异步
+     * 同步scada富平公司站点数据
+     * 每天 1012小时间隔 调用一次
+     */
+    @Scheduled(cron="0 10 0/12 * * ? ")
+    @SneakyThrows
+    public void scadaDtuSyncFPList(){
+        long l = System.currentTimeMillis();
+        scadaDtuSyncService.scadaDtuSyncFPList();
+        long l1 = System.currentTimeMillis();
+        log.info("同步scada富平公司站点数据 耗时:{}",l1-l);
+    }
+
+    /**
+     * 同步scada富平公司站点监测数据
+     */
+    @Async
+    @Scheduled(cron="0 0/1 * * * ?")
+    @SneakyThrows
+    public void scadaDtuSynFPData(){
+        long l = System.currentTimeMillis();
+        scadaDtuSyncService.scadaDtuSynFPData();
+        long l1 = System.currentTimeMillis();
+        log.info("同步scada富平公司站点监测数据 耗时:{}",l1-l);
+    }
 }

+ 5 - 0
tofly-zhongming-gas/service/zmrqScada/src/main/java/com/tofly/zmrqScada/service/ScadaDtuSyncService.java

@@ -9,4 +9,9 @@ public interface ScadaDtuSyncService {
     void scadaDtuSynData() throws Exception;
 
     void scadaDtuSynCQData() throws Exception;
+
+    void scadaDtuSyncFPList() throws Exception;
+
+    void scadaDtuSynFPData() throws Exception;
+
 }

+ 156 - 1
tofly-zhongming-gas/service/zmrqScada/src/main/java/com/tofly/zmrqScada/service/impl/IScadaDtuSyncImpl.java

@@ -53,11 +53,29 @@ public class IScadaDtuSyncImpl implements ScadaDtuSyncService {
     private String companyId;
 
     /**
-     * 绵阳公司id
+     * 重庆公司id
      */
     @Value("${scada.cq.companyId}")
     private String cqCompanyId;
 
+    /**
+     * 富平公司id
+     */
+    @Value("${scada.fp.companyId}")
+    private String fpCompanyId;
+
+    /**
+     * 富平公司密匙
+     */
+    @Value("${scada.fp.appSecret}")
+    private String fpAppSecret;
+
+    /**
+     * 富平公司id
+     */
+    @Value("${scada.fp.clientId}")
+    private String fpClientId;
+
     /**
      * 对接密匙
      */
@@ -369,4 +387,141 @@ public class IScadaDtuSyncImpl implements ScadaDtuSyncService {
         }
     }
 
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void scadaDtuSyncFPList() throws Exception {
+        Map<String, Object> resultMap;
+        HttpClient client = HttpClients.createDefault();
+        // 要调用的接口路径
+        String url = ip + getDtuList;
+        long timeStamp = System.currentTimeMillis();
+        //建立HttpPost对象
+        HttpGet request = new HttpGet(url);
+        request.setHeader("User-Agent", "Apipost client Runtime/+https://www.apipost.cn/");
+        request.setHeader("Clientid", fpClientId);
+        request.setHeader("Companyid", fpCompanyId);
+        request.setHeader("Timestamp", timeStamp + "");
+        request.setHeader("Sign", SingUtil.getSing(getDtuList, fpCompanyId, timeStamp, fpAppSecret));
+
+        org.apache.http.HttpResponse res = client.execute(request);
+        //得到返回的数据
+        String response = EntityUtils.toString(res.getEntity());
+        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+            //将返回数据转换
+            resultMap = JSONObject.parseObject(response, new TypeReference<Map<String, Object>>() {
+            });
+            //返回数据
+            Integer code = (Integer) resultMap.get("code");
+            if (HttpStatus.SC_UNAUTHORIZED == code) {
+                log.error("获取站点信息列表,鉴权失败");
+            }
+            // 成功
+            if (0 == code) {
+                List<ScadaDtuListVo> data = JSONObject.parseArray(JSON.toJSONString(resultMap.get("data")), ScadaDtuListVo.class);
+                Long companyId = 2L;
+                data.forEach(info->{
+                    info.setCompanyId(companyId);
+                    String dtuId = info.getDtuId();
+                    info.getFieldList().forEach(i -> {
+                        i.setCompanyId(companyId);
+                        i.setDtuId(dtuId);
+                    });
+                });
+                synchronized (this) {
+                    // 排除重复站点
+                    Map<String, ScadaDtuListVo> collect = data.stream().collect(Collectors.toMap(ScadaDtuListVo::getDtuId, a -> a, (k1, k2) -> k1));
+                    Collection<ScadaDtuListVo> values = collect.values();
+                    // 清空站点列表
+                    scadaDtuService.deleteCompany(companyId);
+                    List<ScadaDtu> scadaDtuList = JSONObject.parseArray(JSON.toJSONString(values), ScadaDtu.class);
+                    scadaDtuService.saveBatch(scadaDtuList);
+                    List<ScadaDtuParModel> list = new ArrayList<>();
+                    values.forEach(info-> list.addAll(info.getFieldList()));
+                    // 清空参数绑定数据
+                    scadaDtuParModelService.deleteCompany(companyId);
+                    scadaDtuParModelService.saveBatch(list);
+                }
+            }
+
+        }
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void scadaDtuSynFPData() throws Exception {
+        Map<String, Object> resultMap;
+        HttpClient client = HttpClients.createDefault();
+        // 要调用的接口路径
+        String url = ip + getRealTimeDataList;
+        long timeStamp = System.currentTimeMillis();
+        //建立HttpPost对象
+        HttpGet request = new HttpGet(url);
+        request.setHeader("User-Agent", "Apipost client Runtime/+https://www.apipost.cn/");
+        request.setHeader("Clientid", fpClientId);
+        request.setHeader("Companyid", fpCompanyId);
+        request.setHeader("Timestamp", timeStamp + "");
+        request.setHeader("Sign", SingUtil.getSing(getRealTimeDataList, fpCompanyId, timeStamp, fpAppSecret));
+
+        org.apache.http.HttpResponse res = client.execute(request);
+        //得到返回的数据
+        String response = EntityUtils.toString(res.getEntity());
+        if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+            //将返回数据转换
+            resultMap = JSONObject.parseObject(response, new TypeReference<Map<String, Object>>() {
+            });
+            //返回数据
+            Integer code = (Integer) resultMap.get("code");
+            if (HttpStatus.SC_UNAUTHORIZED == code) {
+                log.error("获取站点实时数据列表,鉴权失败");
+            }
+            // 成功
+            if (0 == code) {
+                List<ScadaDtuDataVo> data = JSONObject.parseArray(JSON.toJSONString(resultMap.get("data")), ScadaDtuDataVo.class);
+                data.forEach(info->{
+                    String dtuId = info.getDtuId();
+                    info.getFieldValueList().forEach(i -> i.setDtuId(dtuId));
+                });
+                List<ScadaDyuData> originalList = new ArrayList<>();
+                // 存入历史表
+                data.forEach(info-> originalList.addAll(info.getFieldValueList()));
+                List<ScadaDyuData> list = originalList.stream()
+                        .sorted(Comparator.comparing(ScadaDyuData::getCollectTime).reversed())
+                        .collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
+                                new TreeSet<>(Comparator.comparing(o -> o.getDtuId() + "-" + o.getFieldName() + "-" + DateUtil.getDateFormat(o.getCollectTime(), "yyyyMMddHHmm")))), ArrayList::new));
+                for (ScadaDyuData scadaDyuData : list) {
+                    try {
+                        scadaDyuDataService.save(scadaDyuData);
+                    } catch (Exception ignored) {
+                    }
+                }
+                // 存入实时表
+                // 1.判断当前实时表是否存在对应dtuId和参数名称
+                // 2.存在则更新value值 不存在则新增
+                Set<String> dtuIdSet = list.stream().map(ScadaDyuData::getDtuId).collect(Collectors.toSet());
+                Set<String> fieldNameSet = list.stream().map(ScadaDyuData::getFieldName).collect(Collectors.toSet());
+                List<ScadaDyuDataReal> dataReals = scadaDyuDataRealService.list(new QueryWrapper<ScadaDyuDataReal>().lambda().in(ScadaDyuDataReal::getDtuId, dtuIdSet).in(ScadaDyuDataReal::getFieldName, fieldNameSet));
+                if(null!=dataReals && dataReals.size()>0) {
+                    dataReals.forEach(info -> {
+                        String dtuId = info.getDtuId();
+                        String fieldName = info.getFieldName();
+                        list.forEach(da -> {
+                            if(dtuId.equals(da.getDtuId()) && fieldName.equals(da.getFieldName())) {
+                                scadaDyuDataRealService.update(new UpdateWrapper<ScadaDyuDataReal>().lambda()
+                                        .eq(ScadaDyuDataReal::getDtuId,dtuId)
+                                        .eq(ScadaDyuDataReal::getFieldName,fieldName)
+                                        .set(ScadaDyuDataReal::getUpdateTime,da.getUpdateTime())
+                                        .set(ScadaDyuDataReal::getCollectTime,da.getCollectTime())
+                                        .set(ScadaDyuDataReal::getFieldValue,da.getFieldValue()));
+                            }
+                        });
+                    });
+                }else {
+                    List<ScadaDyuDataReal> scadaDyuDataRealList = JSONArray.parseArray(JSON.toJSONString(list), ScadaDyuDataReal.class);
+                    scadaDyuDataRealService.saveBatch(scadaDyuDataRealList);
+                }
+
+            }
+        }
+    }
+
 }