]> git.basschouten.com Git - openhab-addons.git/blob
0aaaffbefb9d1a0afce85a6eb08bd52f68cdfb00
[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.internal.dto;
14
15 import javax.measure.quantity.Dimensionless;
16 import javax.measure.quantity.Power;
17
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;
23
24 /**
25  * The {@link PowerBlock} Data object for E3DC Info Block
26  *
27  * @author Bernd Weymann - Initial contribution
28  */
29 @NonNullByDefault
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;
43
44     /**
45      * For decoding see Modbus Register Mapping Chapter 3.1.2 page 14
46      *
47      * @param bArray - Modbus Registers as bytes from 40067 to 40083
48      */
49     public PowerBlock(byte[] bArray) {
50         // index handling to calculate the correct start index
51         ValueBuffer wrap = ValueBuffer.wrap(bArray);
52
53         // int32_swap value = 4 byte
54         long pvPowerSupplyL = wrap.getSInt32Swap();
55
56         /*
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
60          */
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);
67         } else {
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);
71         }
72
73         // int32_swap value = 4 byte
74         long householdPowerConsumptionL = wrap.getSInt32Swap();
75         householdPowerConsumption = QuantityType.valueOf(householdPowerConsumptionL, Units.WATT);
76
77         /*
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
81          */
82         long gridPower = wrap.getSInt32Swap();
83         if (gridPower > 0) {
84             // Power is provided by Grid
85             gridPowerSupply = QuantityType.valueOf(gridPower, Units.WATT);
86             gridPowerConsumpition = QuantityType.valueOf(0, Units.WATT);
87         } else {
88             // Power is consumed by Grid
89             gridPowerConsumpition = QuantityType.valueOf(gridPower * -1, Units.WATT);
90             gridPowerSupply = QuantityType.valueOf(0, Units.WATT);
91         }
92
93         // int32_swap value = 4 byte
94         externalPowerSupply = QuantityType.valueOf(wrap.getSInt32Swap(), Units.WATT);
95
96         // int32_swap value = 4 byte
97         wallboxPowerConsumption = QuantityType.valueOf(wrap.getSInt32Swap(), Units.WATT);
98
99         // int32_swap value = 4 byte
100         wallboxPVPowerConsumption = QuantityType.valueOf(wrap.getSInt32Swap(), Units.WATT);
101
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);
105
106         // uint16 for Battery State of Charge
107         batterySOC = QuantityType.valueOf(wrap.getSInt16(), Units.PERCENT);
108     }
109 }