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.internal.dto;
15 import java.nio.ByteBuffer;
17 import javax.measure.quantity.Dimensionless;
18 import javax.measure.quantity.Power;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.modbus.e3dc.internal.modbus.Data;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.library.unit.SmartHomeUnits;
26 * The {@link PowerBlock} Data object for E3DC Info Block
28 * @author Bernd Weymann - Initial contribution
31 public class PowerBlock implements Data {
32 public QuantityType<Power> pvPowerSupply;
33 public QuantityType<Power> batteryPowerSupply;
34 public QuantityType<Power> batteryPowerConsumption;
35 public QuantityType<Power> householdPowerConsumption;
36 public QuantityType<Power> gridPowerConsumpition;
37 public QuantityType<Power> gridPowerSupply;
38 public QuantityType<Power> externalPowerSupply;
39 public QuantityType<Power> wallboxPowerConsumption;
40 public QuantityType<Power> wallboxPVPowerConsumption;
41 public QuantityType<Dimensionless> autarky;
42 public QuantityType<Dimensionless> selfConsumption;
43 public QuantityType<Dimensionless> batterySOC;
46 * For decoding see Modbus Register Mapping Chapter 3.1.2 page 14
48 * @param bArray - Modbus Registers as bytes from 40067 to 40083
50 public PowerBlock(byte[] bArray) {
51 // index handling to calculate the correct start index
52 ByteBuffer wrap = ByteBuffer.wrap(bArray);
54 // int32_swap value = 4 byte
55 long pvPowerSupplyL = DataConverter.getInt32Swap(wrap);
58 * int32_swap value don't provide negative values!
59 * Positive value - Battery is charging = Power consumer
60 * Negative value - Battery is discharging = Power supplier
62 pvPowerSupply = QuantityType.valueOf(pvPowerSupplyL, SmartHomeUnits.WATT);
63 long batteryPower = DataConverter.getInt32Swap(wrap);
64 if (batteryPower > 0) {
65 // Battery is charging so Power is consumed by Battery
66 batteryPowerSupply = QuantityType.valueOf(0, SmartHomeUnits.WATT);
67 batteryPowerConsumption = QuantityType.valueOf(batteryPower, SmartHomeUnits.WATT);
69 // Battery is discharging so Power is provided by Battery
70 batteryPowerSupply = QuantityType.valueOf(batteryPower * -1, SmartHomeUnits.WATT);
71 batteryPowerConsumption = QuantityType.valueOf(0, SmartHomeUnits.WATT);
74 // int32_swap value = 4 byte
75 long householdPowerConsumptionL = DataConverter.getInt32Swap(wrap);
76 householdPowerConsumption = QuantityType.valueOf(householdPowerConsumptionL, SmartHomeUnits.WATT);
79 * int32_swap value don't provide negative values!
80 * Positive value - Power provided towards Grid = Power consumer
81 * Negative value - Power requested from Grid = Power supplier
83 long gridPower = DataConverter.getInt32Swap(wrap);
85 // Power is provided by Grid
86 gridPowerSupply = QuantityType.valueOf(gridPower, SmartHomeUnits.WATT);
87 gridPowerConsumpition = QuantityType.valueOf(0, SmartHomeUnits.WATT);
89 // Power is consumed by Grid
90 gridPowerConsumpition = QuantityType.valueOf(gridPower * -1, SmartHomeUnits.WATT);
91 gridPowerSupply = QuantityType.valueOf(0, SmartHomeUnits.WATT);
94 // int32_swap value = 4 byte
95 externalPowerSupply = QuantityType.valueOf(DataConverter.getInt32Swap(wrap), SmartHomeUnits.WATT);
97 // int32_swap value = 4 byte
98 wallboxPowerConsumption = QuantityType.valueOf(DataConverter.getInt32Swap(wrap), SmartHomeUnits.WATT);
100 // int32_swap value = 4 byte
101 wallboxPVPowerConsumption = QuantityType.valueOf(DataConverter.getInt32Swap(wrap), SmartHomeUnits.WATT);
103 // unit8 + uint8 - one register with split value for Autarky & Self Consumption
104 autarky = QuantityType.valueOf(wrap.get(), SmartHomeUnits.PERCENT);
105 selfConsumption = QuantityType.valueOf(wrap.get(), SmartHomeUnits.PERCENT);
107 // uint16 for Battery State of Charge
108 batterySOC = QuantityType.valueOf(wrap.getShort(), SmartHomeUnits.PERCENT);