]> git.basschouten.com Git - openhab-addons.git/blob
460c9c9b043b382b99228b4ca5287b5a7bc0461d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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
39     @BeforeEach
40     public void setup() {
41         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, 0, 0,
42                 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, 0, 0, 1,
43                 125, 2, 21, 0, 0, 0, 27, 0, 26, 0, 0, 0, 103, 0, -117, 0, 0 };
44         mc = new Parser(DataType.DATA);
45         mc.setArray(dataBlock);
46     }
47
48     @Test
49     public void testValidPowerBlock() {
50         Optional<Data> dataOpt = mc.parse(DataType.POWER);
51         assertTrue(dataOpt.isPresent());
52         PowerBlock b = (PowerBlock) dataOpt.get();
53         assertEquals("242.0 W", b.pvPowerSupply.toString(), "PV Supply");
54         assertEquals("14.0 W", b.gridPowerSupply.toString(), "Grid Supply");
55         assertEquals("0.0 W", b.gridPowerConsumpition.toString(), "Grid Consumption");
56         assertEquals("303.0 W", b.batteryPowerSupply.toString(), "Battery Supply");
57     }
58
59     @Test
60     public void testValidWallboxBlock() {
61         Optional<Data> wba = mc.parse(DataType.WALLBOX);
62         assertTrue(wba.isPresent());
63         WallboxArray a = (WallboxArray) wba.get();
64         assertNotNull(a);
65         Optional<WallboxBlock> o = a.getWallboxBlock(0);
66         WallboxBlock b = o.get();
67         assertNotNull(b);
68         assertEquals(OnOffType.ON, b.wbAvailable, "Wallbox available");
69         assertEquals(OnOffType.ON, b.wbSunmode, "Wallbox Sunmode");
70         assertEquals(OnOffType.OFF, b.wb1phase, "Wallbox 1phase");
71         assertEquals(OnOffType.OFF, b.wbCharging, "Wallbox charging");
72     }
73
74     @Test
75     public void testValidEmergency() {
76         Optional<Data> dataOpt = mc.parse(DataType.EMERGENCY);
77         assertTrue(dataOpt.isPresent());
78         EmergencyBlock b = (EmergencyBlock) dataOpt.get();
79         assertEquals(EmergencyBlock.EP_NOT_SUPPORTED, b.epStatus.toFullString(), "EMS Status");
80         assertEquals(OnOffType.OFF, b.batteryChargingLocked, "Battery charging locked");
81         assertEquals(OnOffType.OFF, b.batteryDischargingLocked, "Battery discharging locked");
82         assertEquals(OnOffType.OFF, b.epPossible, "EP possible");
83         assertEquals(OnOffType.OFF, b.weatherPredictedCharging, "Weather Predicted charging");
84         assertEquals(OnOffType.OFF, b.regulationStatus, "Regulation Status");
85         assertEquals(OnOffType.OFF, b.chargeLockTime, "Charge Lock Time");
86         assertEquals(OnOffType.OFF, b.dischargeLockTime, "Discharge Lock Time");
87     }
88
89     @Test
90     public void testValidStringDetailsStringBlock() {
91         Optional<Data> dataOpt = mc.parse(DataType.STRINGS);
92         assertTrue(dataOpt.isPresent());
93         StringBlock b = (StringBlock) dataOpt.get();
94         assertEquals(381, b.string1Volt.intValue(), "String 1 V");
95         assertEquals("V", b.string1Volt.getUnit().toString(), "String 1 V");
96         assertEquals(533, b.string2Volt.intValue(), "String 2 V");
97         assertEquals("V", b.string2Volt.getUnit().toString(), "String 1 V");
98         assertEquals(0, b.string3Volt.intValue(), "String 3 V");
99         assertEquals("V", b.string3Volt.getUnit().toString(), "String 1 V");
100
101         assertEquals(0.27, b.string1Ampere.doubleValue(), 0.01, "String 1 A");
102         assertEquals("A", b.string1Ampere.getUnit().toString(), "String 1 A");
103         assertEquals(0.26, b.string2Ampere.doubleValue(), 0.01, "String 2 A");
104         assertEquals("A", b.string2Ampere.getUnit().toString(), "String 2 A");
105         assertEquals(0, b.string3Ampere.doubleValue(), 0.01, "String 3 A");
106         assertEquals("A", b.string3Ampere.getUnit().toString(), "String 3 A");
107
108         assertEquals(103, b.string1Watt.intValue(), "String 1 W");
109         assertEquals("W", b.string1Watt.getUnit().toString(), "String 1 W");
110         assertEquals(139, b.string2Watt.intValue(), "String 2 W");
111         assertEquals("W", b.string2Watt.getUnit().toString(), "String 2 W");
112         assertEquals(0, b.string3Watt.intValue(), "String 3 W");
113         assertEquals("W", b.string3Watt.getUnit().toString(), "String 3 W");
114     }
115
116     @Test
117     public void testInvalidInfoblock() {
118         Optional<Data> infoOpt = mc.parse(DataType.INFO);
119         assertFalse(infoOpt.isPresent());
120     }
121 }