]> git.basschouten.com Git - openhab-addons.git/blob
489bce6089dd6794a33a3c7fce4bb8170d8799db
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.modbus.e3dc.dto;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.util.Optional;
18
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.api.Test;
21 import org.openhab.binding.modbus.e3dc.internal.dto.EmergencyBlock;
22 import org.openhab.binding.modbus.e3dc.internal.dto.PowerBlock;
23 import org.openhab.binding.modbus.e3dc.internal.dto.StringBlock;
24 import org.openhab.binding.modbus.e3dc.internal.dto.WallboxArray;
25 import org.openhab.binding.modbus.e3dc.internal.dto.WallboxBlock;
26 import org.openhab.binding.modbus.e3dc.internal.modbus.Data;
27 import org.openhab.binding.modbus.e3dc.internal.modbus.Data.DataType;
28 import org.openhab.binding.modbus.e3dc.internal.modbus.Parser;
29 import org.openhab.core.library.types.OnOffType;
30
31 /**
32  * The {@link DataBlockTest} Test Data Transfer Objects of frequent Data Block
33  *
34  * @author Bernd Weymann - Initial contribution
35  */
36 public class DataBlockTest {
37     private Parser mc;
38     private Parser mcNegativePVSupply;
39
40     @BeforeEach
41     public void setup() {
42         {
43             byte[] dataBlock = new byte[] { 0, -14, 0, 0, -2, -47, -1, -1, 2, 47, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0,
44                     0, 0, 0, 0, 0, 0, 99, 99, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45                     0, 0, 1, 125, 2, 21, 0, 0, 0, 27, 0, 26, 0, 0, 0, 103, 0, -117, 0, 0 };
46             mc = new Parser(DataType.DATA);
47             mc.setArray(dataBlock);
48         }
49         {
50             // 65098 bytes [-2, 74]
51             // 65535 bytes [-1, -1]
52             byte[] dataBlock = new byte[] { -2, -74, -1, -1, -2, -47, -1, -1, 2, 47, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0,
53                     0, 0, 0, 0, 0, 0, 0, 99, 99, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54                     0, 0, 0, 1, 125, 2, 21, 0, 0, 0, 27, 0, 26, 0, 0, 0, 103, 0, -117, 0, 0 };
55             mcNegativePVSupply = new Parser(DataType.DATA);
56             mcNegativePVSupply.setArray(dataBlock);
57         }
58     }
59
60     @Test
61     public void testValidPowerBlock() {
62         Optional<Data> dataOpt = mc.parse(DataType.POWER);
63         assertTrue(dataOpt.isPresent());
64         PowerBlock b = (PowerBlock) dataOpt.get();
65         assertEquals("242 W", b.pvPowerSupply.toString(), "PV Supply");
66         assertEquals("14 W", b.gridPowerSupply.toString(), "Grid Supply");
67         assertEquals("0 W", b.gridPowerConsumpition.toString(), "Grid Consumption");
68         assertEquals("303 W", b.batteryPowerSupply.toString(), "Battery Supply");
69     }
70
71     @Test
72     public void testValidPowerBlockNegativePVSupply() {
73         Optional<Data> dataOpt = mcNegativePVSupply.parse(DataType.POWER);
74         assertTrue(dataOpt.isPresent());
75         PowerBlock b = (PowerBlock) dataOpt.get();
76         assertEquals("-330 W", b.pvPowerSupply.toString(), "PV Supply");
77         assertEquals("14 W", b.gridPowerSupply.toString(), "Grid Supply");
78         assertEquals("0 W", b.gridPowerConsumpition.toString(), "Grid Consumption");
79         assertEquals("303 W", b.batteryPowerSupply.toString(), "Battery Supply");
80     }
81
82     @Test
83     public void testValidWallboxBlock() {
84         Optional<Data> wba = mc.parse(DataType.WALLBOX);
85         assertTrue(wba.isPresent());
86         WallboxArray a = (WallboxArray) wba.get();
87         assertNotNull(a);
88         Optional<WallboxBlock> o = a.getWallboxBlock(0);
89         WallboxBlock b = o.get();
90         assertNotNull(b);
91         assertEquals(OnOffType.ON, b.wbAvailable, "Wallbox available");
92         assertEquals(OnOffType.ON, b.wbSunmode, "Wallbox Sunmode");
93         assertEquals(OnOffType.OFF, b.wb1phase, "Wallbox 1phase");
94         assertEquals(OnOffType.OFF, b.wbCharging, "Wallbox charging");
95     }
96
97     @Test
98     public void testValidEmergency() {
99         Optional<Data> dataOpt = mc.parse(DataType.EMERGENCY);
100         assertTrue(dataOpt.isPresent());
101         EmergencyBlock b = (EmergencyBlock) dataOpt.get();
102         assertEquals(EmergencyBlock.EP_NOT_SUPPORTED, b.epStatus.toFullString(), "EMS Status");
103         assertEquals(OnOffType.OFF, b.batteryChargingLocked, "Battery charging locked");
104         assertEquals(OnOffType.OFF, b.batteryDischargingLocked, "Battery discharging locked");
105         assertEquals(OnOffType.OFF, b.epPossible, "EP possible");
106         assertEquals(OnOffType.OFF, b.weatherPredictedCharging, "Weather Predicted charging");
107         assertEquals(OnOffType.OFF, b.regulationStatus, "Regulation Status");
108         assertEquals(OnOffType.OFF, b.chargeLockTime, "Charge Lock Time");
109         assertEquals(OnOffType.OFF, b.dischargeLockTime, "Discharge Lock Time");
110     }
111
112     @Test
113     public void testValidStringDetailsStringBlock() {
114         Optional<Data> dataOpt = mc.parse(DataType.STRINGS);
115         assertTrue(dataOpt.isPresent());
116         StringBlock b = (StringBlock) dataOpt.get();
117         assertEquals(381, b.string1Volt.intValue(), "String 1 V");
118         assertEquals("V", b.string1Volt.getUnit().toString(), "String 1 V");
119         assertEquals(533, b.string2Volt.intValue(), "String 2 V");
120         assertEquals("V", b.string2Volt.getUnit().toString(), "String 1 V");
121         assertEquals(0, b.string3Volt.intValue(), "String 3 V");
122         assertEquals("V", b.string3Volt.getUnit().toString(), "String 1 V");
123
124         assertEquals(0.27, b.string1Ampere.doubleValue(), 0.01, "String 1 A");
125         assertEquals("A", b.string1Ampere.getUnit().toString(), "String 1 A");
126         assertEquals(0.26, b.string2Ampere.doubleValue(), 0.01, "String 2 A");
127         assertEquals("A", b.string2Ampere.getUnit().toString(), "String 2 A");
128         assertEquals(0, b.string3Ampere.doubleValue(), 0.01, "String 3 A");
129         assertEquals("A", b.string3Ampere.getUnit().toString(), "String 3 A");
130
131         assertEquals(103, b.string1Watt.intValue(), "String 1 W");
132         assertEquals("W", b.string1Watt.getUnit().toString(), "String 1 W");
133         assertEquals(139, b.string2Watt.intValue(), "String 2 W");
134         assertEquals("W", b.string2Watt.getUnit().toString(), "String 2 W");
135         assertEquals(0, b.string3Watt.intValue(), "String 3 W");
136         assertEquals("W", b.string3Watt.getUnit().toString(), "String 3 W");
137     }
138
139     @Test
140     public void testInvalidInfoblock() {
141         Optional<Data> infoOpt = mc.parse(DataType.INFO);
142         assertFalse(infoOpt.isPresent());
143     }
144 }