|
@@ -3,8 +3,17 @@ package com.jjt.data;
|
|
|
import cn.hutool.json.JSONArray;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import com.jjt.JjtApplication;
|
|
|
+import com.jjt.biz.domain.TwinDeviceYhj;
|
|
|
+import com.jjt.biz.service.ITwinDeviceYhjService;
|
|
|
+import com.jjt.calc.domain.TwinCalcHourYhj;
|
|
|
+import com.jjt.calc.mapper.TwinCalcHourYhjMapper;
|
|
|
+import com.jjt.calc.service.ITwinCalcDayYhjService;
|
|
|
+import com.jjt.calc.service.ITwinCalcHourYhjService;
|
|
|
import com.jjt.common.utils.DateUtils;
|
|
|
import com.jjt.utils.IotService;
|
|
|
+import org.apache.ibatis.session.ExecutorType;
|
|
|
+import org.apache.ibatis.session.SqlSession;
|
|
|
+import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
@@ -12,7 +21,6 @@ import org.apache.poi.ss.usermodel.Workbook;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
-import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.FileOutputStream;
|
|
@@ -21,9 +29,7 @@ import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.LocalTime;
|
|
|
import java.time.ZoneOffset;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
/**
|
|
@@ -33,6 +39,186 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|
|
public class PrintingTest {
|
|
|
@Resource
|
|
|
private IotService iotService;
|
|
|
+ @Resource
|
|
|
+ private SqlSessionFactory factory;
|
|
|
+ @Resource
|
|
|
+ private ITwinDeviceYhjService yhjService;
|
|
|
+ @Resource
|
|
|
+ private ITwinCalcHourYhjService calcHourYhjService;
|
|
|
+ @Resource
|
|
|
+ private ITwinCalcDayYhjService calcDayYhjService;
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ Integer[] lines = {1, 2, 3, 4, 5, 6, 7, 8};
|
|
|
+ List<String> lists = new ArrayList<>();
|
|
|
+ String[] fields = {"Formula_data_set_1", "Formula_data_act_5"};
|
|
|
+ String device = "yhj_line%s_PLC1.%s";
|
|
|
+ for (int i = 0; i < lines.length; i++) {
|
|
|
+ for (String field : fields) {
|
|
|
+ String tmp = String.format(device, lines[i], field);
|
|
|
+ lists.add(tmp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ System.err.println(lists);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void day() {
|
|
|
+ String day = "2025-03-02";
|
|
|
+ LocalDate localDate = LocalDate.parse(day);
|
|
|
+ calcDayYhjService.day(localDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void t2() {
|
|
|
+ iotService.setToken();
|
|
|
+ String st = "2025-03-02";
|
|
|
+ String ed = "2025-03-06";
|
|
|
+ LocalDate localDate = LocalDate.parse(st);
|
|
|
+ LocalDate endDate = LocalDate.parse(ed);
|
|
|
+ LocalDateTime start = LocalDateTime.of(localDate, LocalTime.MIN);
|
|
|
+ LocalDateTime end = LocalDateTime.of(endDate, LocalTime.MIN).plusHours(23);
|
|
|
+ LocalDateTime curr = LocalDateTime.now();
|
|
|
+ if (end.isAfter(curr)) {
|
|
|
+ end = curr.minusHours(1);
|
|
|
+ }
|
|
|
+ do {
|
|
|
+ int i = start.getHour();
|
|
|
+ System.err.println(start.toLocalDate().toString() + "\t" + i);
|
|
|
+ calcHourYhjService.calc(start, start.plusHours(1));
|
|
|
+// if (i == 6) {
|
|
|
+// dayService.day(start.toLocalDate());
|
|
|
+// }
|
|
|
+ start = start.plusHours(1);
|
|
|
+ } while (!start.isAfter(end));
|
|
|
+// calcHourYhjService.calc();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void t1() {
|
|
|
+ iotService.setToken();
|
|
|
+ List<TwinDeviceYhj> list = yhjService.selectTwinDeviceYhjList(new TwinDeviceYhj());
|
|
|
+ List<TwinCalcHourYhj> calcList = new ArrayList<>();
|
|
|
+ // 使用设备编码作为Map的key, 使用设备对象本身作为Map的value, 如果有重复的key,保留现有的value
|
|
|
+ Map<String, TwinDeviceYhj> yhjMap = new HashMap<>();
|
|
|
+ List<String> fieldList = new ArrayList<>();
|
|
|
+ String prefix = "root.tl.suxi";
|
|
|
+ String[] fields = {"Formula_data_set_1", "Formula_data_act_5"};
|
|
|
+ for (TwinDeviceYhj yhj : list) {
|
|
|
+ for (String field : fields) {
|
|
|
+ String tmp = yhj.getDeviceCode() + "." + field;
|
|
|
+ fieldList.add(tmp);
|
|
|
+ yhjMap.put(prefix + "." + tmp, yhj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String sql = "select %s from " + prefix + " where time>%s and time <=%s";
|
|
|
+
|
|
|
+ sql = String.format(sql, String.join(",", fieldList), "2025-03-02T00:00:00", "2025-03-02T01:00:00");
|
|
|
+ JSONObject jsonObject = iotService.query(sql);
|
|
|
+ JSONObject data = jsonObject.getJSONObject("data");
|
|
|
+ JSONArray columnNames = data.getJSONArray("columnNames");
|
|
|
+ JSONArray values = data.getJSONArray("values");
|
|
|
+ JSONArray timestamps = data.getJSONArray("timestamps");
|
|
|
+ //记录上一次记录的值
|
|
|
+ Integer[] last = new Integer[columnNames.size()];
|
|
|
+ //记录最后一次的值,防中间有数,后面没数
|
|
|
+ Integer[] over = new Integer[columnNames.size()];
|
|
|
+ //当前记录值
|
|
|
+ Integer[] total = new Integer[columnNames.size()];
|
|
|
+ //记录第一次记录的值,如果清0,则记录清零时刻的值
|
|
|
+ Integer[] first = new Integer[columnNames.size()];
|
|
|
+ //记录统计值
|
|
|
+ Integer[] curr = new Integer[columnNames.size()];
|
|
|
+ for (int i = 0; i < values.size(); i++) {
|
|
|
+ JSONArray da = values.getJSONArray(i);
|
|
|
+ da.toList(Integer.class).toArray(curr);
|
|
|
+ if (i == 0) {
|
|
|
+ cloneArray(curr, last);
|
|
|
+ cloneArray(curr, first);
|
|
|
+ cloneArray(curr, over);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (int j = 0; j < columnNames.size(); j++) {
|
|
|
+ String name = columnNames.getStr(j);
|
|
|
+ if (name.contains("Formula_data_set_1")) {
|
|
|
+ //如果是版距,则需要记录变化
|
|
|
+ if (curr[j] != null && last[j] != null && curr[j].intValue() != last[j].intValue()) {
|
|
|
+ //这里要用j+1才行
|
|
|
+ calc(j + 1, last, first, total);
|
|
|
+ combo(calcList, yhjMap.get(name), last[j], total[j + 1]);
|
|
|
+ total[j + 1] = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (name.contains("Formula_data_act_5")) {
|
|
|
+ //如果是米长,则需要记录是否清0
|
|
|
+ //如果当前值为小于上一个,且上一个值不为0,则计算
|
|
|
+ //因为会出现数据波动,停机再启动之后的第一个点不为0,为0.00几几几的
|
|
|
+ if (curr[j] != null && last[j] != null && curr[j] < last[j] && last[j] != 0) {
|
|
|
+ calc(j, last, first, total);
|
|
|
+ cloneArray(curr, first);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //将本次的值,设为上一条记录了
|
|
|
+ cloneArray(curr, last);
|
|
|
+ cloneArray(curr, over);
|
|
|
+ cloneArrayFirst(curr, first);
|
|
|
+ }
|
|
|
+ for (int j = 0; j < columnNames.size(); j++) {
|
|
|
+ String name = columnNames.getStr(j);
|
|
|
+ if (name.contains("Formula_data_set_1")) {
|
|
|
+ calc(j + 1, over, first, total);
|
|
|
+ combo(calcList, yhjMap.get(name), over[j], total[j + 1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ try (SqlSession sqlSession = factory.openSession(ExecutorType.BATCH, false)) {
|
|
|
+ if (calcList.size() > 0) {
|
|
|
+ TwinCalcHourYhjMapper mapper = sqlSession.getMapper(TwinCalcHourYhjMapper.class);
|
|
|
+ calcList.forEach(mapper::insertTwinCalcHourYhj);
|
|
|
+ sqlSession.commit();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.err.println(total);
|
|
|
+ }
|
|
|
+
|
|
|
+ void cloneArrayFirst(Integer[] arr, Integer[] target) {
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
+ if (arr[i] != null && target[i] == null) {
|
|
|
+ target[i] = arr[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void cloneArray(Integer[] arr, Integer[] target) {
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
+ if (arr[i] != null) {
|
|
|
+ target[i] = arr[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void combo(List<TwinCalcHourYhj> calcList, TwinDeviceYhj yhj, Integer distance, Integer length) {
|
|
|
+ if (distance != null && length != null) {
|
|
|
+ TwinCalcHourYhj calcHourYhj = new TwinCalcHourYhj();
|
|
|
+ calcHourYhj.setDeviceId(yhj.getDeviceId());
|
|
|
+ calcHourYhj.setDistance(distance);
|
|
|
+ calcHourYhj.setLength(length);
|
|
|
+ calcList.add(calcHourYhj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void calc(int j, Integer[] last, Integer[] first, Integer[] total) {
|
|
|
+ if (last[j] != null && first[j] != null) {
|
|
|
+ if (total[j] == null) {
|
|
|
+ total[j] = 0;
|
|
|
+ }
|
|
|
+ total[j] += (last[j] - first[j]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 印花设备数据
|
|
@@ -42,6 +228,7 @@ public class PrintingTest {
|
|
|
iotService.setToken();
|
|
|
Integer[] lines = {1, 2, 3, 4, 5, 6, 7, 8};
|
|
|
List<String[]>[] lists = new ArrayList[8];
|
|
|
+ String[] fields = {"Formula_data_set_1", "Formula_data_act_5"};
|
|
|
for (int i = 0; i < lines.length; i++) {
|
|
|
String table = "root.tl.suxi.yhj_line" + lines[i] + "_PLC1";
|
|
|
lists[i] = new ArrayList<>();
|