2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.modbus.e3dc.dto;
15 import static org.junit.jupiter.api.Assertions.*;
17 import java.util.Optional;
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;
32 * The {@link DataBlockTest} Test Data Transfer Objects of frequent Data Block
34 * @author Bernd Weymann - Initial contribution
36 public class DataBlockTest {
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);
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");
60 public void testValidWallboxBlock() {
61 Optional<Data> wba = mc.parse(DataType.WALLBOX);
62 assertTrue(wba.isPresent());
63 WallboxArray a = (WallboxArray) wba.get();
65 Optional<WallboxBlock> o = a.getWallboxBlock(0);
66 WallboxBlock b = o.get();
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");
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");
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");
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");
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");
117 public void testInvalidInfoblock() {
118 Optional<Data> infoOpt = mc.parse(DataType.INFO);
119 assertFalse(infoOpt.isPresent());