Browse Source

增加用电量计算

wukai 1 year ago
parent
commit
3ba992c04a

+ 32 - 226
ruoyi-admin/src/main/java/com/ruoyi/biz/domain/TwinCalc2hr.java

@@ -5,8 +5,7 @@ import com.ruoyi.common.annotation.Excel;
 import com.ruoyi.common.core.domain.BaseEntity;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
-import org.apache.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
+import lombok.Data;
 
 import java.math.BigDecimal;
 import java.util.Date;
@@ -17,6 +16,7 @@ import java.util.Date;
  * @author ruoyi
  * @date 2024-05-14
  */
+@Data
 @ApiModel(value = "ITwinCalc2hr", description = "2小时统计数据")
 public class TwinCalc2hr extends BaseEntity {
     private static final long serialVersionUID = 1L;
@@ -38,6 +38,9 @@ public class TwinCalc2hr extends BaseEntity {
         this.lengthB = BigDecimal.ZERO;
         this.weightA = BigDecimal.ZERO;
         this.weightB = BigDecimal.ZERO;
+        this.kwh = BigDecimal.ZERO;
+        this.kwhA = BigDecimal.ZERO;
+        this.kwhB = BigDecimal.ZERO;
     }
 
     /**
@@ -208,6 +211,24 @@ public class TwinCalc2hr extends BaseEntity {
     @Excel(name = "更新时间", width = 30, dateFormat = "yyyy-MM-dd")
     @ApiModelProperty("更新时间")
     private Date updatedTime;
+    /**
+     * 总电量
+     */
+    @Excel(name = "总电量")
+    @ApiModelProperty("总电量")
+    private BigDecimal kwh;
+    /**
+     * A班电量
+     */
+    @Excel(name = "A班电量")
+    @ApiModelProperty("A班电量")
+    private BigDecimal kwhA;
+    /**
+     * A班电量
+     */
+    @Excel(name = "B班电量")
+    @ApiModelProperty("B班电量")
+    private BigDecimal kwhB;
 
     /**
      * 计算累加数据
@@ -231,233 +252,18 @@ public class TwinCalc2hr extends BaseEntity {
         this.lengthB = this.lengthB.add(calc2hr.getLengthB());
         this.weightA = this.weightA.add(calc2hr.getWeightA());
         this.weightB = this.weightB.add(calc2hr.getWeightB());
-    }
-
-    public void setId(Long id) {
-        this.id = id;
-    }
-
-    public Long getId() {
-        return id;
-    }
-
-    public void setDataDate(Date dataDate) {
-        this.dataDate = dataDate;
-    }
-
-    public Date getDataDate() {
-        return dataDate;
-    }
-
-    public void setTimePeriod(String timePeriod) {
-        this.timePeriod = timePeriod;
-    }
-
-    public String getTimePeriod() {
-        return timePeriod;
-    }
-
-    public void setDeviceId(Long deviceId) {
-        this.deviceId = deviceId;
-    }
-
-    public Long getDeviceId() {
-        return deviceId;
-    }
-
-    public void setLengthA(BigDecimal lengthA) {
-        this.lengthA = lengthA;
-    }
-
-    public BigDecimal getLengthA() {
-        return lengthA;
-    }
-
-    public void setOpenTimeA(BigDecimal openTimeA) {
-        this.openTimeA = openTimeA;
-    }
-
-    public BigDecimal getOpenTimeA() {
-        return openTimeA;
-    }
-
-    public void setCloseTimeA(BigDecimal closeTimeA) {
-        this.closeTimeA = closeTimeA;
-    }
-
-    public BigDecimal getCloseTimeA() {
-        return closeTimeA;
-    }
-
-    public void setLengthB(BigDecimal lengthB) {
-        this.lengthB = lengthB;
-    }
-
-    public BigDecimal getLengthB() {
-        return lengthB;
-    }
-
-    public void setCloseTimeB(BigDecimal closeTimeB) {
-        this.closeTimeB = closeTimeB;
-    }
-
-    public BigDecimal getCloseTimeB() {
-        return closeTimeB;
-    }
-
-    public void setOpenTimeB(BigDecimal openTimeB) {
-        this.openTimeB = openTimeB;
-    }
-
-    public BigDecimal getOpenTimeB() {
-        return openTimeB;
-    }
-
-    public void setWeight(BigDecimal weight) {
-        this.weight = weight;
-    }
-
-    public BigDecimal getWeight() {
-        return weight;
-    }
-
-    public void setWeightA(BigDecimal weightA) {
-        this.weightA = weightA;
-    }
-
-    public BigDecimal getWeightA() {
-        return weightA;
-    }
-
-    public void setWeightB(BigDecimal weightB) {
-        this.weightB = weightB;
-    }
-
-    public BigDecimal getWeightB() {
-        return weightB;
-    }
-
-    public void setAlarm(Long alarm) {
-        this.alarm = alarm;
-    }
-
-    public Long getAlarm() {
-        return alarm;
-    }
-
-    public void setStop1A(Long stop1A) {
-        this.stop1A = stop1A;
-    }
-
-    public Long getStop1A() {
-        return stop1A;
-    }
-
-    public void setStop2A(Long stop2A) {
-        this.stop2A = stop2A;
-    }
-
-    public Long getStop2A() {
-        return stop2A;
-    }
-
-    public void setStop3A(Long stop3A) {
-        this.stop3A = stop3A;
-    }
-
-    public Long getStop3A() {
-        return stop3A;
-    }
-
-    public void setStop1B(Long stop1B) {
-        this.stop1B = stop1B;
-    }
-
-    public Long getStop1B() {
-        return stop1B;
-    }
-
-    public void setStop2B(Long stop2B) {
-        this.stop2B = stop2B;
-    }
-
-    public Long getStop2B() {
-        return stop2B;
-    }
-
-    public void setStop3B(Long stop3B) {
-        this.stop3B = stop3B;
-    }
-
-    public Long getStop3B() {
-        return stop3B;
-    }
-
-    public void setCreatedBy(String createdBy) {
-        this.createdBy = createdBy;
-    }
-
-    public String getCreatedBy() {
-        return createdBy;
-    }
-
-    public void setCreatedTime(Date createdTime) {
-        this.createdTime = createdTime;
-    }
-
-    public Date getCreatedTime() {
-        return createdTime;
-    }
-
-    public void setUpdatedBy(String updatedBy) {
-        this.updatedBy = updatedBy;
-    }
-
-    public String getUpdatedBy() {
-        return updatedBy;
-    }
-
-    public void setUpdatedTime(Date updatedTime) {
-        this.updatedTime = updatedTime;
-    }
-
-    public Date getUpdatedTime() {
-        return updatedTime;
+        this.kwh = this.kwh.add(calc2hr.getKwh());
+        this.kwhA = this.kwhA.add(calc2hr.getKwhA());
+        this.kwhB = this.kwhB.add(calc2hr.getKwhB());
     }
 
     public Object[] toArray() {
-        Object[] objects = {this.dataDate, this.timePeriod, this.deviceId, this.lengthA, this.openTimeA, this.closeTimeA, this.lengthB, this.closeTimeB, this.openTimeB, this.weight, this.weightA, this.weightB, this.alarm, this.stop1A, this.stop2A, this.stop3A, this.stop1B, this.stop2B, this.stop3B, getRemark()};
+        Object[] objects = {
+                this.dataDate, this.timePeriod, this.deviceId, this.lengthA, this.openTimeA,
+                this.closeTimeA, this.lengthB, this.closeTimeB, this.openTimeB, this.weight,
+                this.weightA, this.weightB, this.alarm, this.stop1A, this.stop2A,
+                this.stop3A, this.stop1B, this.stop2B, this.stop3B, getRemark(),
+                this.kwh, this.kwhA, this.kwhB};
         return objects;
     }
-
-    @Override
-    public String toString() {
-        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
-                .append("id", getId())
-                .append("dataDate", getDataDate())
-                .append("timePeriod", getTimePeriod())
-                .append("deviceId", getDeviceId())
-                .append("lengthA", getLengthA())
-                .append("openTimeA", getOpenTimeA())
-                .append("closeTimeA", getCloseTimeA())
-                .append("lengthB", getLengthB())
-                .append("closeTimeB", getCloseTimeB())
-                .append("openTimeB", getOpenTimeB())
-                .append("weight", getWeight())
-                .append("weightA", getWeightA())
-                .append("weightB", getWeightB())
-                .append("alarm", getAlarm())
-                .append("stop1A", getStop1A())
-                .append("stop2A", getStop2A())
-                .append("stop3A", getStop3A())
-                .append("stop1B", getStop1B())
-                .append("stop2B", getStop2B())
-                .append("stop3B", getStop3B())
-                .append("createdBy", getCreatedBy())
-                .append("createdTime", getCreatedTime())
-                .append("updatedBy", getUpdatedBy())
-                .append("updatedTime", getUpdatedTime())
-                .append("remark", getRemark())
-                .toString();
-    }
 }

+ 58 - 8
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/AsyncServiceImpl.java

@@ -17,6 +17,7 @@ import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.*;
 import java.util.concurrent.Future;
+import java.util.stream.Collectors;
 
 /**
  * 多线程执行任务
@@ -71,7 +72,7 @@ public class AsyncServiceImpl {
         calc2hr.setDataDate(date);
         calc2hr.setTimePeriod(period + "");
         Map<String, Object> map = calc(table, startTime, endTime);
-        //0.已织造米数 1.A班组总开机时间 2.A班组总停机时间 3.A班总产量 4.B班组总开机时间 5.B班组总停机时间 6.B班总产量 7.A班总重量 8.B班总重量
+        //0.已织造米数 1.A班组总开机时间 2.A班组总停机时间 3.A班总产量 4.B班组总开机时间 5.B班组总停机时间 6.B班总产量 7.A班总重量 8.B班总重量 9.总能耗 10.A班能耗 11.B班能耗
         float[] total = (float[]) map.get("total");
         calc2hr.setLengthA(BigDecimal.valueOf(total[3]));
         calc2hr.setLengthB(BigDecimal.valueOf(total[6]));
@@ -81,6 +82,9 @@ public class AsyncServiceImpl {
         calc2hr.setOpenTimeB(BigDecimal.valueOf(total[4]));
         calc2hr.setCloseTimeA(BigDecimal.valueOf(total[2]));
         calc2hr.setCloseTimeB(BigDecimal.valueOf(total[5]));
+        calc2hr.setKwh(BigDecimal.valueOf(total[9]));
+        calc2hr.setKwhA(BigDecimal.valueOf(total[10]));
+        calc2hr.setKwhB(BigDecimal.valueOf(total[11]));
 
         Map<String, Long> stop = (Map<String, Long>) map.get("stop");
         calc2hr.setStop1A(stop.get("stop1_A"));
@@ -122,6 +126,24 @@ public class AsyncServiceImpl {
         return new AsyncResult<>(result);
     }
 
+    /**
+     * 字段列表,方便看下标
+     */
+    private final String[] fields = {"Capacity_data_2", "Capacity_data_37", "Capacity_data_38", "Capacity_data_39", "Capacity_data_42",
+            "Capacity_data_43", "Capacity_data_44", "Capacity_data_48", "Formula_data_3", "Formula_data_13",
+            "Capacity_data_36", "Capacity_data_41", "Alarm_unit_1", "Alarm_unit_2", "Alarm_unit_3",
+            "Alarm_unit_4", "Alarm_unit_5", "Alarm_unit_6", "Alarm_unit_7", "Alarm_unit_8",
+            "Alarm_unit_9", "Alarm_unit_10", "Alarm_unit_11", "Alarm_unit_12", "Alarm_unit_13",
+            "Alarm_unit_14", "Alarm_unit_15", "Alarm_unit_16", "Alarm_unit_17", "Alarm_unit_18",
+            "Alarm_unit_19", "Alarm_unit_20", "Alarm_unit_21", "Alarm_unit_22", "Alarm_unit_23",
+            "Alarm_unit_24", "Alarm_unit_25", "Alarm_unit_26", "Alarm_unit_27", "Capacity_data_33"
+    };
+
+    /**
+     * 字段列表,方便查找位置
+     */
+    private final List<String> fieldList = Arrays.stream(fields).collect(Collectors.toList());
+
 
     /**
      * //    0        Capacity_data_2	已织造米数
@@ -136,8 +158,8 @@ public class AsyncServiceImpl {
      * //    9        Formula_data_13	卷曲幅宽
      */
     public Map<String, Object> calc(String table, long startTime, long endTime) {
-        String sql = "select Capacity_data_2,Capacity_data_37,Capacity_data_38,Capacity_data_39,Capacity_data_42,Capacity_data_43,Capacity_data_44,Capacity_data_48,Formula_data_3,Formula_data_13,Capacity_data_36,Capacity_data_41," + "Alarm_unit_1,Alarm_unit_2,Alarm_unit_3,Alarm_unit_4,Alarm_unit_5,Alarm_unit_6,Alarm_unit_7,Alarm_unit_8,Alarm_unit_9,Alarm_unit_10,Alarm_unit_11,Alarm_unit_12,Alarm_unit_13,Alarm_unit_14,Alarm_unit_15,Alarm_unit_16,Alarm_unit_17,Alarm_unit_18,Alarm_unit_19,Alarm_unit_20,Alarm_unit_21,Alarm_unit_22,Alarm_unit_23,Alarm_unit_24,Alarm_unit_25,Alarm_unit_26,Alarm_unit_27" + " from %s where time>%s and time <=%s";
-        sql = String.format(sql, table, startTime, endTime);
+        String sql = "select %s from %s where time>%s and time <=%s";
+        sql = String.format(sql, Arrays.stream(fields).collect(Collectors.joining(",")), table, startTime, endTime);
         JSONObject jsonObject = iotService.query(sql);
 
         JSONObject data = jsonObject.getJSONObject("data");
@@ -148,8 +170,8 @@ public class AsyncServiceImpl {
         float[] first = new float[7];
         //上一个时间点数据
         float[] last = new float[7];
-        //统计数据,后面2个分别代表A班重量,B班重
-        float[] total = new float[9];
+        //统计数据,后面5个分别代表7.A班重量,8.B班重量 9.总电量  10.A班电量  11.B班电
+        float[] total = new float[12];
         //告警数据统计
         boolean[] lastAlarms = new boolean[26];
         //最后一个值为int型,需要单独计算
@@ -188,6 +210,8 @@ public class AsyncServiceImpl {
                 continue;
             }
 
+            calcKwh(da, total);
+
             for (int j = 0; j < first.length; j++) {
                 //如果当前值为小于上一个,且上一个值不为0,则计算
                 //因为会出现数据波动,停机再启动之后的第一个点不为0,为0.00几几几的
@@ -213,11 +237,12 @@ public class AsyncServiceImpl {
                     //10 A班登入 11B班登入
                     String key = "stop" + curr48;
                     alarmRecord.add(new Pair<>(key, timestamps.getLong(i)));
-
-                    if (da.getBool(10)) {
+                    //A班登入
+                    if (da.getBool(fieldList.indexOf("Capacity_data_36"))) {
                         key += "_A";
                     }
-                    if (da.getBool(11)) {
+                    //B班登入
+                    if (da.getBool(fieldList.indexOf("Capacity_data_41"))) {
                         key += "_B";
                     }
                     if (stopMap.get(key) != null) {
@@ -251,6 +276,31 @@ public class AsyncServiceImpl {
     }
 
     /**
+     * 能耗计算
+     * total[9].总电量  total[10].A班电量  total[11].B班电量
+     *
+     * @param da    当前数据
+     * @param total 统计数组
+     */
+
+    private void calcKwh(JSONArray da, float[] total) {
+        BigDecimal v = da.getBigDecimal(fieldList.indexOf("Capacity_data_33"));
+        //单位为W 换算成当前这一秒的用电量 需要除以1000变成千瓦,再除以3600 变成千瓦时
+        v = v.divide(BigDecimal.valueOf(3600), 5, RoundingMode.HALF_UP);
+        v = v.divide(BigDecimal.valueOf(1000), 5, RoundingMode.HALF_UP);
+        float kwh = v.floatValue();
+        total[9] = total[9] + kwh;
+        //A班登入
+        if (da.getBool(fieldList.indexOf("Capacity_data_36"))) {
+            total[10] = total[10] + kwh;
+        }
+        //B班登入
+        if (da.getBool(fieldList.indexOf("Capacity_data_41"))) {
+            total[11] = total[11] + kwh;
+        }
+    }
+
+    /**
      * 停机次数计算
      */
     private boolean calcStops(int startNum, int endNum, JSONArray values, int curr) {

+ 7 - 2
ruoyi-admin/src/main/java/com/ruoyi/biz/service/impl/TaskServiceImpl.java

@@ -166,8 +166,13 @@ public class TaskServiceImpl implements ITaskService {
 
         Date d = new Date();
         log.info("总共消耗:{}ms", d.getTime() - s.getTime());
-        String sql = "INSERT INTO TWIN_CALC_2HR (DATA_DATE, TIME_PERIOD, DEVICE_ID, LENGTH_A, OPEN_TIME_A, CLOSE_TIME_A, LENGTH_B, CLOSE_TIME_B, OPEN_TIME_B, WEIGHT, WEIGHT_A, WEIGHT_B, ALARM, STOP1_A, STOP2_A, STOP3_A, STOP1_B, STOP2_B, STOP3_B,REMARK)" +
-                " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
+        String sql = "INSERT INTO TWIN_CALC_2HR (" +
+                "DATA_DATE,TIME_PERIOD,DEVICE_ID,LENGTH_A,OPEN_TIME_A," +
+                "CLOSE_TIME_A,LENGTH_B,CLOSE_TIME_B,OPEN_TIME_B,WEIGHT," +
+                "WEIGHT_A,WEIGHT_B,ALARM,STOP1_A,STOP2_A," +
+                "STOP3_A,STOP1_B,STOP2_B,STOP3_B,REMARK," +
+                "KWH,KWH_A,KWH_B)" +
+                " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
         jdbcTemplate.batchUpdate(sql, calc2hrList);
 
         sql = "INSERT INTO TWIN_RECORD_ALARMS (DEVICE_ID, ALARM_TYPE,ALARM_CODE,DATA_TIME) VALUES (?,?,?,?)";

+ 3 - 2
ruoyi-admin/src/main/java/com/ruoyi/biz/tools/IotTools.java

@@ -1,12 +1,13 @@
 package com.ruoyi.biz.tools;
 
-import cn.hutool.core.text.UnicodeUtil;
 import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpResponse;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import lombok.extern.slf4j.Slf4j;
 
+import java.nio.charset.StandardCharsets;
+
 /**
  * IotTools$
  *
@@ -46,7 +47,7 @@ public class IotTools {
                 }
             }
         }
-        String res = UnicodeUtil.toString(execute.body());
+        String res = new String(execute.body().getBytes(), StandardCharsets.UTF_8);
         return JSONUtil.parseObj(res, true);
     }
 

+ 19 - 0
ruoyi-admin/src/main/resources/mapper/biz/TwinCalc2hrMapper.xml

@@ -18,6 +18,9 @@
         <result property="weight" column="WEIGHT"/>
         <result property="weightA" column="WEIGHT_A"/>
         <result property="weightB" column="WEIGHT_B"/>
+        <result property="kwh" column="kwh"/>
+        <result property="kwhA" column="kwhA"/>
+        <result property="kwhB" column="kwhB"/>
         <result property="alarm" column="ALARM"/>
         <result property="stop1A" column="STOP1_A"/>
         <result property="stop2A" column="STOP2_A"/>
@@ -46,6 +49,9 @@
                WEIGHT,
                WEIGHT_A,
                WEIGHT_B,
+               KWH,
+               KWH_A,
+               KWH_B,
                ALARM,
                STOP1_A,
                STOP2_A,
@@ -110,6 +116,9 @@
                SUM(OPEN_TIME_B)  OPEN_TIME_B,
                SUM(WEIGHT_A)     WEIGHT_A,
                SUM(WEIGHT_B)     WEIGHT_B,
+               SUM(KWH)          KWH,
+               SUM(KWH_A)        KWH_A,
+               SUM(KWH_B)        KWH_B,
                SUM(ALARM)        ALARM,
                SUM(STOP1_A)      STOP1_A,
                SUM(STOP2_A)      STOP2_A,
@@ -131,6 +140,9 @@
                SUM(OPEN_TIME_B)  OPEN_TIME_B,
                SUM(WEIGHT_A)     WEIGHT_A,
                SUM(WEIGHT_B)     WEIGHT_B,
+               SUM(KWH)          KWH,
+               SUM(KWH_A)        KWH_A,
+               SUM(KWH_B)        KWH_B,
                SUM(ALARM)        ALARM,
                SUM(STOP1_A)      STOP1_A,
                SUM(STOP2_A)      STOP2_A,
@@ -154,6 +166,9 @@
                SUM(OPEN_TIME_B)                    OPEN_TIME_B,
                SUM(WEIGHT_A)                       WEIGHT_A,
                SUM(WEIGHT_B)                       WEIGHT_B,
+               SUM(KWH)                            KWH,
+               SUM(KWH_A)                          KWH_A,
+               SUM(KWH_B)                          KWH_B,
                SUM(ALARM)                          ALARM,
                SUM(STOP1_A)                        STOP1_A,
                SUM(STOP2_A)                        STOP2_A,
@@ -265,6 +280,9 @@
         SUM(OPEN_TIME_B) OPEN_TIME_B,
         SUM(WEIGHT_A) WEIGHT_A,
         SUM(WEIGHT_B) WEIGHT_B,
+        SUM(KWH) KWH,
+        SUM(KWH_A) KWH_A,
+        SUM(KWH_B) KWH_B,
         SUM(ALARM) ALARM,
         SUM(STOP1_A) STOP1_A,
         SUM(STOP2_A) STOP2_A,
@@ -272,6 +290,7 @@
         SUM(STOP1_B) STOP1_B,
         SUM(STOP2_B) STOP2_B,
         SUM(STOP3_B) STOP3_B
+
         FROM TWIN_CALC_2HR
         <where>
             DATA_DATE >= #{start}

+ 43 - 33
ruoyi-admin/src/test/java/com/jjt/DataPF.java

@@ -10,12 +10,19 @@ import com.ruoyi.biz.service.ITaskService;
 import com.ruoyi.biz.service.ITwinCalc2hrService;
 import com.ruoyi.biz.service.ITwinDeviceService;
 import com.ruoyi.biz.service.impl.AsyncServiceImpl;
+import javafx.util.Pair;
 import org.junit.jupiter.api.Test;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.scheduling.annotation.EnableAsync;
 
 import javax.annotation.Resource;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * DataProcess$
@@ -41,8 +48,13 @@ public class DataPF {
     private ITwinCalc2hrService calc2hrService;
 
     public static void main(String[] args) {
-        TwinCalc2hr calc = new TwinCalc2hr();
-        System.err.println(calc);
+        String[] fields = {"Capacity_data_2", "Capacity_data_37", "Capacity_data_38", "Capacity_data_39", "Capacity_data_42", "Capacity_data_43", "Capacity_data_44", "Capacity_data_48", "Formula_data_3", "Formula_data_13", "Capacity_data_36", "Capacity_data_41",
+                "Alarm_unit_1", "Alarm_unit_2", "Alarm_unit_3", "Alarm_unit_4", "Alarm_unit_5", "Alarm_unit_6", "Alarm_unit_7", "Alarm_unit_8", "Alarm_unit_9", "Alarm_unit_10", "Alarm_unit_11", "Alarm_unit_12", "Alarm_unit_13", "Alarm_unit_14", "Alarm_unit_15", "Alarm_unit_16", "Alarm_unit_17", "Alarm_unit_18", "Alarm_unit_19", "Alarm_unit_20", "Alarm_unit_21", "Alarm_unit_22", "Alarm_unit_23", "Alarm_unit_24", "Alarm_unit_25", "Alarm_unit_26", "Alarm_unit_27",
+                "Capacity_data_33"};
+        String xx = Arrays.stream(fields).collect(Collectors.joining(","));
+        List<String> fieldList = Arrays.stream(fields).collect(Collectors.toList());
+        System.err.println(xx);
+        System.err.println(fieldList.stream().collect(Collectors.joining(",")));
     }
 
     @Test
@@ -56,43 +68,41 @@ public class DataPF {
         taskService.calcLastPeriod();
     }
 
+    @Test
+    void tt() {
+        String table = "root.tl.suxi.knittings2_plc1";
+        LocalDateTime start = LocalDateTime.of(2024, 5, 28, 12, 0);
+        LocalDateTime end = LocalDateTime.of(2024, 5, 28, 14, 0);
+        start = start.minusSeconds(1);
+        Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
+        Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
+        Map<String, Object> map = asyncService.calc(table, startTime, endTime);
+        System.err.println(map);
+        float[] total = (float[]) map.get("total");
+        for (float d : total) {
+            System.err.println(d);
+        }
+        System.err.println("======================");
+        int[] times = (int[]) map.get("alarms");
+        for (int d : times) {
+            System.err.println(d);
+        }
+        List<Pair<String, Long>> alarmRecord = (List<Pair<String, Long>>) map.get("alarmRecord");
+        alarmRecord.forEach(pair -> {
+            System.err.println(pair.getKey() + "\t" + pair.getValue());
+        });
+        System.err.println(map.get("alarms"));
+    }
+
 
     @Test
     void data() {
 //        taskService.calc("2024-05-15");
-//        taskService.calc("2024-05-16");
-//        taskService.calc("2024-05-17");
-//        taskService.calc("2024-05-18");
-//        taskService.calc("2024-05-19");
-//        taskService.calc("2024-05-20");
 //        taskService.calc("2024-05-21",12);
-        for (int i = 6; i <= 8; i++) {
-            taskService.calc("2024-05-24", i);
-        }
-//        taskService.calcLastPeriod();
-//        String table = "root.tl.suxi.knittings97_plc1";
-//        long startTime = 1715709599000l;
-//        long endTime = 1715716800000l;
-////        long startTime = 1715939999000l;
-////        System.err.println(new Date(startTime));
-////        long endTime = 1715954400000l;
-////        System.err.println(new Date(endTime));
-//        Map<String, Object> map = asyncService.calc(table, startTime, endTime);
-//        System.err.println(map);
-//        float[] total = (float[]) map.get("total");
-//        for (float d : total) {
-//            System.err.println(d);
-//        }
-//        System.err.println("======================");
-//        int[] times = (int[]) map.get("alarms");
-//        for (int d : times) {
-//            System.err.println(d);
+//        for (int i = 11; i <= 12; i++) {
+//            taskService.calc("2024-05-26", i);
 //        }
-//        List<Pair<String, Long>> alarmRecord = (List<Pair<String, Long>>) map.get("alarmRecord");
-//        alarmRecord.forEach(pair -> {
-//            System.err.println(pair.getKey() + "\t" + pair.getValue());
-//        });
-//        System.err.println(map.get("alarms"));
+        taskService.calc("2024-05-27");
     }
 
     @Test

+ 93 - 14
ruoyi-admin/src/test/java/com/jjt/DataProcess.java

@@ -1,26 +1,27 @@
 package com.jjt;
 
-import cn.hutool.core.text.UnicodeUtil;
-import cn.hutool.http.HttpRequest;
-import cn.hutool.http.HttpResponse;
-import cn.hutool.http.HttpUtil;
-import cn.hutool.http.Method;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
-import cn.hutool.json.JSONUtil;
 import com.ruoyi.RuoYiApplication;
 import com.ruoyi.biz.service.IIotService;
+import com.ruoyi.common.utils.DateUtils;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+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.time.Instant;
-import java.time.LocalDateTime;
-import java.time.ZoneId;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.time.*;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
-import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * DataProcess$
@@ -36,13 +37,91 @@ public class DataProcess {
     private IIotService iotService;
 
     @Test
-    void db() {
-        String sql = "select * from sys_user";
-        for (Map<String, Object> stringObjectMap : jdbcTemplate.queryForList(sql)) {
-            System.err.println(stringObjectMap.get("user_name"));
+    void excel() {
+        String code = "106";
+        String table = "root.tl.suxi.knittings" + code + "_plc1";
+        Workbook workbook = new XSSFWorkbook();
+        for (int i = 21; i <= 26; i++) {
+            String date = "2024-05-" + i;
+            System.err.println(date);
+            List<String[]> list = new ArrayList<>();
+            for (int j = 1; j <= 12; j++) {
+                List<String[]> pList = get(table, date, j);
+                list.addAll(pList);
+            }
+            Sheet sheet = workbook.createSheet(date);
+            toExcel(list, sheet);
+        }
+        try (FileOutputStream outputStream = new FileOutputStream("D:\\SYSTEM\\Desktop\\temp\\excel\\小经编机" + code + "号.xlsx")) {
+            workbook.write(outputStream);
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                workbook.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
         }
     }
 
+    public List<String[]> get(String table, String date, int period) {
+        List<String[]> list = new ArrayList<>();
+        LocalDate localDate = LocalDate.parse(date);
+        LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
+        LocalDateTime start = ldt.plusHours(2 * period).minusHours(2);
+        LocalDateTime end = start.plusHours(2);
+        start = start.minusSeconds(1);
+        Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
+        Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
+        String sql = "select Capacity_data_5,Capacity_data_6,Capacity_data_7,Capacity_data_8,Capacity_data_9,Capacity_data_10,Capacity_data_11,Capacity_data_12,Capacity_data_13,Capacity_data_14,Capacity_data_15,Capacity_data_16,Capacity_data_17,Capacity_data_18,Capacity_data_19" +
+                " from %s where time>%s and time <=%s";
+        sql = String.format(sql, table, startTime, endTime);
+        iotService.query(sql);
+        JSONObject jsonObject = iotService.query(sql);
+        JSONObject data = jsonObject.getJSONObject("data");
+        JSONArray values = data.getJSONArray("values");
+        JSONArray timestamps = data.getJSONArray("timestamps");
+        for (int i = 0; i < values.size(); i++) {
+            String[] temp = new String[16];
+            temp[0] = timestamps.getStr(i);
+            JSONArray da = values.getJSONArray(i);
+            for (int j = 1; j < temp.length; j++) {
+                temp[j] = da.getStr(j - 1);
+            }
+            list.add(temp);
+        }
+        return list;
+    }
+
+    public void toExcel(List<String[]> list, Sheet sheet) {
+        String[] names = {"时间", "GB1剩余时间", "GB2剩余时间", "GB3剩余时间", "GB4剩余时间", "GB5剩余时间", "GB1盘头剩余周长", "GB2盘头剩余周长", "GB3盘头剩余周长", "GB4盘头剩余周长", "GB5盘头剩余周长", "GB1盘头剩余圈数", "GB2盘头剩余圈数", "GB3盘头剩余圈数", "GB4盘头剩余圈数", "GB5盘头剩余圈数"};
+
+        Row title = sheet.createRow(0);
+        for (int i = 0; i < names.length; i++) {
+            Cell cell = title.createCell(i);
+            cell.setCellValue(names[i]);
+        }
+        AtomicInteger index = new AtomicInteger(1);
+        list.forEach(d -> {
+            Row row = sheet.createRow(index.get());
+            Long time = Long.parseLong(d[0]);
+            String date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date(time));
+            Cell cell = row.createCell(0);
+            cell.setCellValue(date);
+            for (int j = 1; j <= 10; j++) {
+                cell = row.createCell(j);
+                cell.setCellValue(Float.parseFloat(d[j]));
+
+            }
+            for (int j = 11; j < d.length; j++) {
+                cell = row.createCell(j);
+                cell.setCellValue(Integer.parseInt(d[j]));
+            }
+            index.getAndIncrement();
+        });
+    }
+
 
     @Test
     void data() {

+ 34 - 12
ruoyi-admin/src/test/java/com/jjt/LastProcess.java

@@ -1,16 +1,13 @@
 package com.jjt;
 
-import cn.hutool.core.text.UnicodeUtil;
-import cn.hutool.http.HttpRequest;
-import cn.hutool.http.HttpResponse;
-import cn.hutool.http.HttpUtil;
-import cn.hutool.http.Method;
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.ruoyi.RuoYiApplication;
+import com.ruoyi.biz.domain.TwinDevice;
 import com.ruoyi.biz.service.IIotService;
 import com.ruoyi.biz.service.ITwinDeviceService;
+import com.ruoyi.biz.service.impl.AsyncServiceImpl;
 import org.junit.jupiter.api.Test;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.jdbc.core.JdbcTemplate;
@@ -23,6 +20,8 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
 
 /**
  * DataProcess$
@@ -38,6 +37,8 @@ public class LastProcess {
     private ITwinDeviceService deviceService;
     @Resource
     private IIotService iotService;
+    @Resource
+    private AsyncServiceImpl asyncService;
 
     @Test
     void db() {
@@ -50,12 +51,33 @@ public class LastProcess {
 
     @Test
     void data() {
-        String table = "root.tl.suxi.knittings97_plc1";
-        process(table);
-//        deviceService.selectTwinDeviceList(new TwinDevice()).forEach(device -> {
-//            String table = device.getDevicePath();
-//            process(table);
-//        });
+        List<TwinDevice> list = deviceService.selectTwinDeviceList(new TwinDevice());
+        List<Future<Map<String, Object>>> futureList = new ArrayList<>();
+        Long[] xx = {2L, 5L, 9L, 35L, 42L};
+        for (int i = 0; i < list.size(); i++) {
+            TwinDevice twinDevice = list.get(i);
+
+            for (int j = 0; j < xx.length; j++) {
+                futureList.add(asyncService.currData(twinDevice));
+            }
+        }
+
+        try {
+            Thread.sleep(5000);
+            for (Future<Map<String, Object>> future : futureList) {
+                Map<String, Object> map = future.get();
+                TwinDevice device = (TwinDevice) map.get("device");
+                System.err.print(device.getDeviceName());
+                for (int i = 5; i <= 19; i++) {
+                    System.err.print("\t" + map.get("Capacity_data_" + i));
+                }
+                System.err.println();
+            }
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        } catch (ExecutionException e) {
+            throw new RuntimeException(e);
+        }
     }
 
     @Test
@@ -91,7 +113,7 @@ public class LastProcess {
         for (int i = 0; i < array.size(); i++) {
             JSONArray da = array.getJSONArray(i);
             String key = da.getStr(0);
-            key = key.replace(table+".", "");
+            key = key.replace(table + ".", "");
             map.put(key, da.get(1));
             arr[i + 2] = da.get(1);
         }