lloyd 2 mēneši atpakaļ
vecāks
revīzija
5a8ee03ab5

+ 29 - 29
src/main/java/com/gct/etl/BHModbusDataBody.java

@@ -16,61 +16,61 @@ import java.time.LocalDateTime;
16 16
 @NoArgsConstructor
17 17
 public class BHModbusDataBody {
18 18
 
19
-    private Float maxLoad;
19
+    public Float maxLoad;
20 20
 
21
-    private Float minLoad;
21
+    public Float minLoad;
22 22
 
23
-    private Float theoryUpLoad;
23
+    public Float theoryUpLoad;
24 24
 
25
-    private Float theoryDownLoad;
25
+    public Float theoryDownLoad;
26 26
 
27
-    private Float fillCoefficient;
27
+    public Float fillCoefficient;
28 28
 
29
-    private Float emptyFillCoefficient;
29
+    public Float emptyFillCoefficient;
30 30
 
31
-    private Float liqProdMass;
31
+    public Float liqProdMass;
32 32
 
33
-    private Float oilProdMass;
33
+    public Float oilProdMass;
34 34
 
35
-    private Float waterProdMass;
35
+    public Float waterProdMass;
36 36
 
37
-    private Float liqProdVolume;
37
+    public Float liqProdVolume;
38 38
 
39
-    private Float oilProdVolume;
39
+    public Float oilProdVolume;
40 40
 
41
-    private Float waterProdVolume;
41
+    public Float waterProdVolume;
42 42
 
43
-    private Float theoryDisplacement;
43
+    public Float theoryDisplacement;
44 44
 
45
-    private Float pumpEfficient;
45
+    public Float pumpEfficient;
46 46
 
47
-    private Integer gkCode;
47
+    public Integer gkCode;
48 48
 
49
-    private Float inversionDynamicLiquidLevel;
49
+    public Float inversionDynamicLiquidLevel;
50 50
 
51
-    private Float liquidCorrectDifference;
51
+    public Float liquidCorrectDifference;
52 52
 
53
-    private Float submergenceDegree;
53
+    public Float submergenceDegree;
54 54
 
55
-    private Float downStrokeMaxElec;
55
+    public Float downStrokeMaxElec;
56 56
 
57
-    private Float upStrokeMaxElec;
57
+    public Float upStrokeMaxElec;
58 58
 
59
-    private Float elecBalance;
59
+    public Float elecBalance;
60 60
 
61
-    private Float downStrokeMaxPower;
61
+    public Float downStrokeMaxPower;
62 62
 
63
-    private Float upStrokeMaxPower;
63
+    public Float upStrokeMaxPower;
64 64
 
65
-    private Float powerBalance;
65
+    public Float powerBalance;
66 66
 
67
-    private Float stroke;
67
+    public Float stroke;
68 68
 
69
-    private Float downholeSystemEfficient;
69
+    public Float downholeSystemEfficient;
70 70
 
71
-    private Float  upholeSystemEfficient;
71
+    public Float  upholeSystemEfficient;
72 72
 
73
-    private Float systemEfficient;
73
+    public Float systemEfficient;
74 74
 
75
-    private Float elecCostPerHundredMeter;
75
+    public Float elecCostPerHundredMeter;
76 76
 }

+ 42 - 30
src/main/java/com/gct/etl/BHModbusReadConfig.java

@@ -1,70 +1,82 @@
1 1
 package com.gct.etl;
2 2
 
3 3
 import com.gct.etl.ModbusReadConfig.Section;
4
+import com.gct.etl.parser.ByteToFloat;
5
+import com.gct.etl.parser.DefaultParser;
4 6
 import lombok.Data;
5 7
 
8
+import java.util.HashMap;
9
+import java.util.Map;
10
+
6 11
 /**
7 12
  * @author xusirui 2024/7/9
8 13
  */
9 14
 @Data
10 15
 public class BHModbusReadConfig {
11
-    private int offset = 7501;
16
+    public int offset = 7501;
17
+    public int stationId = 1;
18
+    public Map<Class, RegisterParser> parsers = new HashMap<Class, RegisterParser>();
19
+
20
+    {
21
+        parsers.put(Float.class, new ByteToFloat());
22
+        parsers.put(Integer.class, new DefaultParser());
23
+    }
12 24
 
13
-    private Section maxLoad = new Section(1, 9500 - offset, 2);
25
+    public Section maxLoad = new Section(stationId, 9500 - offset, 2);
14 26
 
15
-    private Section minLoad = new Section(1, 9502 - offset, 2);
27
+    public Section minLoad = new Section(stationId, 9502 - offset, 2);
16 28
 
17
-    private Section theoryUpLoad = new Section(1, 9504 - offset, 2);
29
+    public Section theoryUpLoad = new Section(stationId, 9504 - offset, 2);
18 30
 
19
-    private Section theoryDownLoad = new Section(1, 9506 - offset, 2);
31
+    public Section theoryDownLoad = new Section(stationId, 9506 - offset, 2);
20 32
 
21
-    private Section fillCoefficient = new Section(1, 9508 - offset, 2);
33
+    public Section fillCoefficient = new Section(stationId, 9508 - offset, 2);
22 34
 
23
-    private Section emptyFillCoefficient = new Section(1, 9510 - offset, 2);
35
+    public Section emptyFillCoefficient = new Section(stationId, 9510 - offset, 2);
24 36
 
25
-    private Section liqProdMass = new Section(1, 9512 - offset, 2);
37
+    public Section liqProdMass = new Section(stationId, 9512 - offset, 2);
26 38
 
27
-    private Section oilProdMass = new Section(1, 9514 - offset, 2);
39
+    public Section oilProdMass = new Section(stationId, 9514 - offset, 2);
28 40
 
29
-    private Section waterProdMass = new Section(1, 9516 - offset, 2);
41
+    public Section waterProdMass = new Section(stationId, 9516 - offset, 2);
30 42
 
31
-    private Section liqProdVolume = new Section(1, 9518 - offset, 2);
43
+    public Section liqProdVolume = new Section(stationId, 9518 - offset, 2);
32 44
 
33
-    private Section oilProdVolume = new Section(1, 9520 - offset, 2);
45
+    public Section oilProdVolume = new Section(stationId, 9520 - offset, 2);
34 46
 
35
-    private Section waterProdVolume = new Section(1, 9522 - offset, 2);
47
+    public Section waterProdVolume = new Section(stationId, 9522 - offset, 2);
36 48
 
37
-    private Section theoryDisplacement = new Section(1, 9524 - offset, 2);
49
+    public Section theoryDisplacement = new Section(stationId, 9524 - offset, 2);
38 50
 
39
-    private Section pumpEfficient = new Section(1, 9526 - offset, 2);
51
+    public Section pumpEfficient = new Section(stationId, 9526 - offset, 2);
40 52
 
41
-    private Section gkCode = new Section(1, 9528 - offset, 1);
53
+    public Section gkCode = new Section(stationId, 9528 - offset, 1);
42 54
 
43
-    private Section inversionDynamicLiquidLevel = new Section(1, 9529 - offset, 2);
55
+    public Section inversionDynamicLiquidLevel = new Section(stationId, 9529 - offset, 2);
44 56
 
45
-    private Section liquidCorrectDifference = new Section(1, 9531 - offset, 2);
57
+    public Section liquidCorrectDifference = new Section(stationId, 9531 - offset, 2);
46 58
 
47
-    private Section submergenceDegree = new Section(1, 9533 - offset, 2);
59
+    public Section submergenceDegree = new Section(stationId, 9533 - offset, 2);
48 60
 
49
-    private Section downStrokeMaxElec = new Section(1, 9535 - offset, 2);
61
+    public Section downStrokeMaxElec = new Section(stationId, 9535 - offset, 2);
50 62
 
51
-    private Section upStrokeMaxElec = new Section(1, 9537 - offset, 2);
63
+    public Section upStrokeMaxElec = new Section(stationId, 9537 - offset, 2);
52 64
 
53
-    private Section elecBalance = new Section(1, 9539 - offset, 2);
65
+    public Section elecBalance = new Section(stationId, 9539 - offset, 2);
54 66
 
55
-    private Section downStrokeMaxPower = new Section(1, 9541 - offset, 2);
67
+    public Section downStrokeMaxPower = new Section(stationId, 9541 - offset, 2);
56 68
 
57
-    private Section upStrokeMaxPower = new Section(1, 9543 - offset, 2);
69
+    public Section upStrokeMaxPower = new Section(stationId, 9543 - offset, 2);
58 70
 
59
-    private Section powerBalance = new Section(1, 9545 - offset, 2);
71
+    public Section powerBalance = new Section(stationId, 9545 - offset, 2);
60 72
 
61
-    private Section stroke = new Section(1, 9547 - offset, 2);
73
+    public Section stroke = new Section(stationId, 9547 - offset, 2);
62 74
 
63
-    private Section downholeSystemEfficient = new Section(1, 9549 - offset, 2);
75
+    public Section downholeSystemEfficient = new Section(stationId, 9549 - offset, 2);
64 76
 
65
-    private Section upholeSystemEfficient = new Section(1, 9551 - offset, 2);
77
+    public Section upholeSystemEfficient = new Section(stationId, 9551 - offset, 2);
66 78
 
67
-    private Section systemEfficient = new Section(1, 9553 - offset, 2);
79
+    public Section systemEfficient = new Section(stationId, 9553 - offset, 2);
68 80
 
69
-    private Section elecCostPerHundredMeter = new Section(1, 9555 - offset, 2);
81
+    public Section elecCostPerHundredMeter = new Section(stationId, 9555 - offset, 2);
70 82
 }

+ 7 - 23
src/main/java/com/gct/etl/ModbusRTUReader.java

@@ -233,17 +233,7 @@ public class ModbusRTUReader {
233 233
         return arr;
234 234
     }
235 235
 
236
-    private static Double readDoubleSingle(ModbusReadConfig.Section reg, RegisterParser<Double> parser) {
237
-        Register[] registers = tryReadMultipleRegisters(reg.stationId, reg.register, reg.length);
238
-        return parser.apply(registers);
239
-    }
240
-
241
-    private static Integer readIntegerSingle(ModbusReadConfig.Section reg, RegisterParser<Integer> parser) {
242
-        Register[] registers = tryReadMultipleRegisters(reg.stationId, reg.register, reg.length);
243
-        return parser.apply(registers);
244
-    }
245
-
246
-    private static Float readFloatSingle(ModbusReadConfig.Section reg, RegisterParser<Float> parser) {
236
+    private static <T> T readSingle(ModbusReadConfig.Section reg, RegisterParser<T> parser) {
247 237
         Register[] registers = tryReadMultipleRegisters(reg.stationId, reg.register, reg.length);
248 238
         return parser.apply(registers);
249 239
     }
@@ -267,9 +257,9 @@ public class ModbusRTUReader {
267 257
 
268 258
         ModbusDataBody body = new ModbusDataBody();
269 259
         body.card = readDiagramCard();
270
-        body.s = readDoubleSingle(regConfig.sReg, regConfig.sParser);
271
-        body.n = readDoubleSingle(regConfig.nReg, regConfig.nParser);
272
-        body.pointNum = readIntegerSingle(regConfig.pointNumReg, regConfig.pointNumParser);
260
+        body.s = readSingle(regConfig.sReg, regConfig.sParser);
261
+        body.n = readSingle(regConfig.nReg, regConfig.nParser);
262
+        body.pointNum = readSingle(regConfig.pointNumReg, regConfig.pointNumParser);
273 263
         body.prodTime = readTime(regConfig.prodTimeReg, regConfig.dateTimeParser);
274 264
         return body;
275 265
     }
@@ -280,16 +270,10 @@ public class ModbusRTUReader {
280 270
             BHModbusDataBody body = new BHModbusDataBody();
281 271
             BHModbusReadConfig readConfig = new BHModbusReadConfig();
282 272
             for (Field element : BHModbusDataBody.class.getDeclaredFields()) {
283
-                String filedName = element.getName().substring(0, 1).toUpperCase() + element.getName().substring(1);
284
-                String getMethodName = "get" + filedName;
285
-                String setMethodName = "set" + filedName;
286
-                ModbusReadConfig.Section regConfig = (ModbusReadConfig.Section) BHModbusReadConfig.class.getMethod(getMethodName).invoke(readConfig);
273
+                ModbusReadConfig.Section regConfig = (ModbusReadConfig.Section) BHModbusReadConfig.class.getDeclaredField(element.getName()).get(readConfig);
287 274
                 if (Objects.isNull(regConfig)) continue;
288
-                if (element.getType().equals(Float.class)) {
289
-                    BHModbusDataBody.class.getMethod(setMethodName,Float.class).invoke(body, readFloatSingle(regConfig, new ByteToFloat()));
290
-                } else if (element.getType().equals(Integer.class)) {
291
-                    BHModbusDataBody.class.getMethod(setMethodName,Integer.class).invoke(body, readIntegerSingle(regConfig, new DefaultParser()));
292
-                }
275
+                Object val = readSingle(regConfig, readConfig.parsers.get(element.getType()));
276
+                element.set(body, val);
293 277
             }
294 278
             return body;
295 279
         } catch (Exception e) {