2 * Copyright (c) 2010-2023 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.internal.dto;
15 import javax.measure.quantity.Dimensionless;
16 import javax.measure.quantity.Power;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.modbus.e3dc.internal.modbus.Data;
20 import org.openhab.core.io.transport.modbus.ValueBuffer;
21 import org.openhab.core.library.types.QuantityType;
22 import org.openhab.core.library.unit.Units;
25 * The {@link PowerBlock} Data object for E3DC Info Block
27 * @author Bernd Weymann - Initial contribution
30 public class PowerBlock implements Data {
31 public QuantityType<Power> pvPowerSupply;
32 public QuantityType<Power> batteryPowerSupply;
33 public QuantityType<Power> batteryPowerConsumption;
34 public QuantityType<Power> householdPowerConsumption;
35 public QuantityType<Power> gridPowerConsumpition;
36 public QuantityType<Power> gridPowerSupply;
37 public QuantityType<Power> externalPowerSupply;
38 public QuantityType<Power> wallboxPowerConsumption;
39 public QuantityType<Power> wallboxPVPowerConsumption;
40 public QuantityType<Dimensionless> autarky;
41 public QuantityType<Dimensionless> selfConsumption;
42 public QuantityType<Dimensionless> batterySOC;
45 * For decoding see Modbus Register Mapping Chapter 3.1.2 page 14
47 * @param bArray - Modbus Registers as bytes from 40067 to 40083
49 public PowerBlock(byte[] bArray) {
50 // index handling to calculate the correct start index
51 ValueBuffer wrap = ValueBuffer.wrap(bArray);
53 // int32_swap value = 4 byte
54 long pvPowerSupplyL = wrap.getSInt32Swap();
57 * int32_swap value don't provide negative values!
58 * Positive value - Battery is charging = Power consumer
59 * Negative value - Battery is discharging = Power supplier
61 pvPowerSupply = QuantityType.valueOf(pvPowerSupplyL, Units.WATT);
62 long batteryPower = wrap.getSInt32Swap();
63 if (batteryPower > 0) {
64 // Battery is charging so Power is consumed by Battery
65 batteryPowerSupply = QuantityType.valueOf(0, Units.WATT);
66 batteryPowerConsumption = QuantityType.valueOf(batteryPower, Units.WATT);
68 // Battery is discharging so Power is provided by Battery
69 batteryPowerSupply = QuantityType.valueOf(batteryPower * -1, Units.WATT);
70 batteryPowerConsumption = QuantityType.valueOf(0, Units.WATT);
73 // int32_swap value = 4 byte
74 long householdPowerConsumptionL = wrap.getSInt32Swap();
75 householdPowerConsumption = QuantityType.valueOf(householdPowerConsumptionL, Units.WATT);
78 * int32_swap value don't provide negative values!
79 * Positive value - Power provided towards Grid = Power consumer
80 * Negative value - Power requested from Grid = Power supplier
82 long gridPower = wrap.getSInt32Swap();
84 // Power is provided by Grid
85 gridPowerSupply = QuantityType.valueOf(gridPower, Units.WATT);
86 gridPowerConsumpition = QuantityType.valueOf(0, Units.WATT);
88 // Power is consumed by Grid
89 gridPowerConsumpition = QuantityType.valueOf(gridPower * -1, Units.WATT);
90 gridPowerSupply = QuantityType.valueOf(0, Units.WATT);
93 // int32_swap value = 4 byte
94 externalPowerSupply = QuantityType.valueOf(wrap.getSInt32Swap(), Units.WATT);
96 // int32_swap value = 4 byte
97 wallboxPowerConsumption = QuantityType.valueOf(wrap.getSInt32Swap(), Units.WATT);
99 // int32_swap value = 4 byte
100 wallboxPVPowerConsumption = QuantityType.valueOf(wrap.getSInt32Swap(), Units.WATT);
102 // unit8 + uint8 - one register with split value for Autarky & Self Consumption
103 autarky = QuantityType.valueOf(wrap.getSInt8(), Units.PERCENT);
104 selfConsumption = QuantityType.valueOf(wrap.getSInt8(), Units.PERCENT);
106 // uint16 for Battery State of Charge
107 batterySOC = QuantityType.valueOf(wrap.getSInt16(), Units.PERCENT);