|
@@ -1,23 +1,27 @@
|
|
|
package com.example.opc_da.policy;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
+//import cn.hutool.core.convert.Convert;
|
|
|
+//import cn.hutool.core.date.DatePattern;
|
|
|
+//import cn.hutool.core.date.DateUtil;
|
|
|
+//import cn.hutool.core.date.StopWatch;
|
|
|
import com.cqcy.ei.influxdb.entity.Item;
|
|
|
import com.example.opc_common.entity.ReportDataPolicy;
|
|
|
import com.example.opc_common.entity.ReportDataPolicyItem;
|
|
|
import com.example.opc_da.config.SpringContextUtils;
|
|
|
+//import com.example.opc_da.service.CollectorService;
|
|
|
import com.example.opc_da.task.AsyncTask;
|
|
|
import com.example.opc_da.util.QueryServiceUtil;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.TimerTask;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
+//import java.util.stream.IntStream;
|
|
|
|
|
|
public class FreReportDataPolicyTask extends ReportDataPolicyTask {
|
|
|
|
|
|
private final AsyncTask asyncTask = SpringContextUtils.getBean(AsyncTask.class);
|
|
|
+ //private final CollectorService collectorService = SpringContextUtils.getBean(CollectorService.class);
|
|
|
|
|
|
public FreReportDataPolicyTask(ReportDataPolicy reportDataPolicy) {
|
|
|
super(reportDataPolicy);
|
|
@@ -48,6 +52,8 @@ public class FreReportDataPolicyTask extends ReportDataPolicyTask {
|
|
|
|
|
|
//查询此报表策略最新一轮数据
|
|
|
List<Item> itemDataByLast = getItemDataHistoryByLast();
|
|
|
+ //System.out.println("time0:" + itemDataByLast.get(0).getTime());
|
|
|
+ //System.out.println("time1:" + itemDataByLast.get(1).getTime());
|
|
|
|
|
|
//过滤已经存在的数据
|
|
|
List<Item> validItemDataList = itemDataByLast.stream().filter(i ->
|
|
@@ -67,6 +73,95 @@ public class FreReportDataPolicyTask extends ReportDataPolicyTask {
|
|
|
lastItemDataByLast = itemDataByLast;
|
|
|
|
|
|
}
|
|
|
+ /*@Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ //判断数据组是否到达了,今天的结束时间
|
|
|
+ Long currentTime = System.currentTimeMillis();
|
|
|
+ if (currentTime<=startTime || currentTime >= endTime){
|
|
|
+ stop();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 采集器采集多条数据,而策略采集只需要一条数据的条件判断
|
|
|
+ List<Map<String, Object>> itemIntervalMap = collectorService.getItemIntervalByReportDataPolicyId(policyItemList.get(0).getReportDataPolicyId());
|
|
|
+ if(itemIntervalMap == null || itemIntervalMap.size() == 0){
|
|
|
+ throw new RuntimeException("采集器采集间隔设置错误");
|
|
|
+ }
|
|
|
+ Long collectorItemInterval = Convert.toLong(itemIntervalMap.get(0).get("itemInterval"));
|
|
|
+ String collectorItemUnit = Convert.toStr(itemIntervalMap.get(0).get("itemUnit"));
|
|
|
+ switch (collectorItemUnit){
|
|
|
+ case "d" :
|
|
|
+ collectorItemInterval *= 24 * 60 * 60 * 1000;
|
|
|
+ break;
|
|
|
+ case "h" :
|
|
|
+ collectorItemInterval *= 60 * 60 * 1000;
|
|
|
+ break;
|
|
|
+ case "m" :
|
|
|
+ collectorItemInterval *= 60 * 1000;
|
|
|
+ break;
|
|
|
+ case "s" :
|
|
|
+ collectorItemInterval *= 1000;
|
|
|
+ break;
|
|
|
+ case "ms" :
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new RuntimeException("采集器采集间隔设置错误");
|
|
|
+ }
|
|
|
+ Long policyItemInterval = getPeriod() * 1000;
|
|
|
+ String groupByInterval = null;
|
|
|
+ if(collectorItemInterval < policyItemInterval){
|
|
|
+ groupByInterval = getPeriod() + "s";
|
|
|
+ }else{
|
|
|
+ groupByInterval = 1 + "ms";
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询此报表策略最新一轮数据
|
|
|
+ List<Item> itemDataByLast = getItemDataHistoryByLast(groupByInterval);
|
|
|
+
|
|
|
+ // 过滤已经插入时间对应的数据
|
|
|
+ // 拆分为多条数据
|
|
|
+ Map<String, List<Item>> itemDataMapByTime = itemDataByLast.stream().filter(item -> {
|
|
|
+ if(lastItemDataByLast.size() == 0){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return DateUtil.parse(lastItemDataByLast.get(0).getTime(), DatePattern.NORM_DATETIME_MS_FORMAT)
|
|
|
+ .before(DateUtil.parse(item.getTime(), DatePattern.NORM_DATETIME_MS_FORMAT));
|
|
|
+ }).collect(Collectors.groupingBy(Item::getTime));
|
|
|
+
|
|
|
+ if(itemDataMapByTime.isEmpty()){
|
|
|
+ // 无新增数据
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 数据时间正排
|
|
|
+ List<String> collect = itemDataMapByTime.keySet().stream().sorted().collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 循环插入新增的数据
|
|
|
+ IntStream.range(0,collect.size()).forEach(index -> {
|
|
|
+
|
|
|
+ //将得到的数据转换为Map<点位名称,Item实体类>
|
|
|
+ Map<String, Item> map = itemDataMapByTime.get(collect.get(index)).stream().collect(Collectors.toMap(Item::getName, i -> i));
|
|
|
+ //将报表采集策略的点位,赋值相应的值
|
|
|
+ for(ReportDataPolicyItem policyItem : policyItemList){
|
|
|
+ Item item = map.get(policyItem.getItemReadName());
|
|
|
+ QueryServiceUtil.itemParentCountValue(policyItem, item);
|
|
|
+ }
|
|
|
+ lastItemDataByLast = itemDataMapByTime.get(collect.get(index));
|
|
|
+ if(index > 0){
|
|
|
+ try {
|
|
|
+ Thread.sleep(getPeriod() * 1000);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ addPolicyItemList(policyItemList);
|
|
|
+ });
|
|
|
+
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }*/
|
|
|
};
|
|
|
}
|
|
|
|