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