From b3424c34d8d92bc55a413fdc5d1fdb1448ac6c4d Mon Sep 17 00:00:00 2001 From: Hans van den Bogert Date: Sun, 28 Nov 2021 08:39:10 +0100 Subject: [PATCH] [omnikinverter] feature: Add channels for voltage and current Signed-off-by: Hans van den Bogert --- .../OmnikInverterBindingConstants.java | 23 +++ .../internal/OmnikInverterMessage.java | 158 ++++++++++++++++-- .../handler/OmnikInverterHandler.java | 38 ++++- .../resources/OH-INF/thing/thing-types.xml | 63 +++++++ .../test/OmnikInverterMessageTest.java | 45 +++++ 5 files changed, 310 insertions(+), 17 deletions(-) diff --git a/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/OmnikInverterBindingConstants.java b/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/OmnikInverterBindingConstants.java index 6716b0520a52c..1e8260de58142 100644 --- a/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/OmnikInverterBindingConstants.java +++ b/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/OmnikInverterBindingConstants.java @@ -30,7 +30,30 @@ public class OmnikInverterBindingConstants { public static final ThingTypeUID THING_TYPE_OMNIK = new ThingTypeUID(BINDING_ID, "omnik"); // List of all Channel ids + public static final String CHANNEL_CURRENT_PV1 = "currentPV1"; + public static final String CHANNEL_CURRENT_PV2 = "currentPV2"; + public static final String CHANNEL_CURRENT_PV3 = "currentPV3"; + + public static final String CHANNEL_VOLTAGE_PV1 = "voltagePV1"; + public static final String CHANNEL_VOLTAGE_PV2 = "voltagePV2"; + public static final String CHANNEL_VOLTAGE_PV3 = "voltagePV3"; + public static final String CHANNEL_POWER = "power"; + public static final String CHANNEL_POWER_AC1 = "powerAC1"; + public static final String CHANNEL_POWER_AC2 = "powerAC2"; + public static final String CHANNEL_POWER_AC3 = "powerAC3"; + + public static final String CHANNEL_CURRENT_AC1 = "currentAC1"; + public static final String CHANNEL_CURRENT_AC2 = "currentAC2"; + public static final String CHANNEL_CURRENT_AC3 = "currentAC3"; + + public static final String CHANNEL_VOLTAGE_AC1 = "voltageAC1"; + public static final String CHANNEL_VOLTAGE_AC2 = "voltageAC2"; + public static final String CHANNEL_VOLTAGE_AC3 = "voltageAC3"; + + public static final String CHANNEL_FREQUENCY_AC1 = "frequencyAC1"; + public static final String CHANNEL_FREQUENCY_AC2 = "frequencyAC2"; + public static final String CHANNEL_FREQUENCY_AC3 = "frequencyAC3"; public static final String CHANNEL_ENERGY_TODAY = "energyToday"; diff --git a/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/OmnikInverterMessage.java b/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/OmnikInverterMessage.java index 9d778314f7542..dd8cb41226e3e 100644 --- a/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/OmnikInverterMessage.java +++ b/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/OmnikInverterMessage.java @@ -30,11 +30,153 @@ public OmnikInverterMessage(byte[] b) { this.bytes = b; } - public double getPower() { + private double getShort(int offset, int compensationFactor) { ByteBuffer buf = ByteBuffer.allocate(2); - buf.put(bytes, 59, 2); + buf.put(bytes, offset, 2); + buf.rewind(); + return (double) buf.getShort() / compensationFactor; + } + + private double getInt(int offset, int compensationFactor) { + ByteBuffer buf = ByteBuffer.allocate(4); + buf.put(bytes, offset, 4); buf.rewind(); - return buf.getShort(); + return (double) buf.getInt() / compensationFactor; + } + + /** + * @return the voltage for PV1 + */ + public double getVoltagePV1() { + return getShort(33, 10); + } + + /** + * @return the voltage for PV2 + */ + public double getVoltagePV2() { + return getShort(35, 10); + } + + /** + * @return the voltage for PV3 + */ + public double getVoltagePV3() { + return getShort(37, 10); + } + + /** + * @return the amperage for PV1 + */ + public double getCurrentPV1() { + return getShort(39, 10); + } + + /** + * @return the amperage for PV2 + */ + public double getCurrentPV2() { + return getShort(41, 10); + } + + /** + * @return the amperage for PV3 + */ + public double getCurrentPV3() { + return getShort(43, 10); + } + + /** + * @return the amperage for AC1 + */ + public double getAmperageAC1() { + return getShort(45, 10); + } + + /** + * @return the amperage for AC2 + */ + public double getAmperageAC2() { + return getShort(47, 10); + } + + /** + * @return the amperage for AC3 + */ + public double getAmperageAC3() { + return getShort(49, 10); + } + + /** + * @return the voltage for AC1 + */ + public double getVoltageAC1() { + return getShort(51, 10); + } + + /** + * @return the voltage for AC2 + */ + public double getVoltageAC2() { + return getShort(53, 10); + } + + /** + * @return the voltage for AC3 + */ + public double getVoltageAC3() { + return getShort(55, 10); + } + + /** + * @return the Frequency for AC1 + */ + public double getFrequencyAC1() { + return getShort(57, 100); + } + + /** + * @return the power for AC1 + * + * @deprecated + */ + public double getPower() { + return getShort(59, 1); + } + + /** + * @return the power for AC1 + */ + public double getPowerAC1() { + return getShort(59, 1); + } + + /** + * @return the Frequency for AC2 + */ + public double getFrequencyAC2() { + return getShort(61, 100); + } + + /** + * @return the power for AC2 + */ + public double getPowerAC2() { + return getShort(63, 1); + } + + /** + * @return the Frequency for AC3 + */ + public double getFrequencyAC3() { + return getShort(65, 100); + } + + /** + * @return the power for AC3 + */ + public double getPowerAC3() { + return getShort(67, 1); } /** @@ -42,10 +184,7 @@ public double getPower() { * @return the total energy outputted this day in kWh */ public double getEnergyToday() { - ByteBuffer buf = ByteBuffer.allocate(2); - buf.put(bytes, 69, 2); - buf.rewind(); - return (buf.getShort() / 100.0); + return getShort(69, 100); } /** @@ -53,9 +192,6 @@ public double getEnergyToday() { * @return the total energy outputted in kWh */ public double getTotalEnergy() { - ByteBuffer buf = ByteBuffer.allocate(4); - buf.put(bytes, 71, 4); - buf.rewind(); - return buf.getInt() / 10.0; + return getInt(71, 10); } } diff --git a/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/handler/OmnikInverterHandler.java b/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/handler/OmnikInverterHandler.java index d8d2a3110ba83..bc40edbf181a7 100644 --- a/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/handler/OmnikInverterHandler.java +++ b/bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/handler/OmnikInverterHandler.java @@ -19,6 +19,8 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +import javax.measure.quantity.ElectricCurrent; +import javax.measure.quantity.ElectricPotential; import javax.measure.quantity.Power; import org.eclipse.jdt.annotation.NonNullByDefault; @@ -35,7 +37,6 @@ import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.binding.BaseThingHandler; import org.openhab.core.types.Command; -import org.openhab.core.types.RefreshType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,11 +59,9 @@ public OmnikInverterHandler(Thing thing) { @Override public void handleCommand(ChannelUID channelUID, Command command) { - if (OmnikInverterBindingConstants.CHANNEL_POWER.equals(channelUID.getId())) { - if (command instanceof RefreshType) { - updateData(); - } - } + // if (command instanceof RefreshType) { + // updateData(); + // } } @Override @@ -94,6 +93,33 @@ private void updateData() { QuantityType powerQuantity = new QuantityType<>(message.getPower(), Units.WATT); updateState(OmnikInverterBindingConstants.CHANNEL_POWER, powerQuantity); + QuantityType powerQuantity1 = new QuantityType<>(message.getPowerAC1(), Units.WATT); + updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC1, powerQuantity1); + + QuantityType powerQuantity2 = new QuantityType<>(message.getPowerAC2(), Units.WATT); + updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC2, powerQuantity2); + + QuantityType powerQuantity3 = new QuantityType<>(message.getPowerAC3(), Units.WATT); + updateState(OmnikInverterBindingConstants.CHANNEL_POWER_AC3, powerQuantity3); + + QuantityType pvAmp1 = new QuantityType<>(message.getCurrentPV1(), Units.AMPERE); + updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV1, pvAmp1); + + QuantityType pvAmp2 = new QuantityType<>(message.getCurrentPV2(), Units.AMPERE); + updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV2, pvAmp2); + + QuantityType pvAmp3 = new QuantityType<>(message.getCurrentPV3(), Units.AMPERE); + updateState(OmnikInverterBindingConstants.CHANNEL_CURRENT_PV3, pvAmp3); + + QuantityType pvVoltage1 = new QuantityType<>(message.getVoltagePV1(), Units.VOLT); + updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV1, pvVoltage1); + + QuantityType pvVoltage2 = new QuantityType<>(message.getVoltagePV2(), Units.VOLT); + updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV2, pvVoltage2); + + QuantityType pvVoltage3 = new QuantityType<>(message.getVoltagePV3(), Units.VOLT); + updateState(OmnikInverterBindingConstants.CHANNEL_VOLTAGE_PV3, pvVoltage3); + updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TODAY, new QuantityType<>(message.getEnergyToday(), Units.KILOWATT_HOUR)); diff --git a/bundles/org.openhab.binding.omnikinverter/src/main/resources/OH-INF/thing/thing-types.xml b/bundles/org.openhab.binding.omnikinverter/src/main/resources/OH-INF/thing/thing-types.xml index 12ec7a4de08bf..e91951453ba9e 100644 --- a/bundles/org.openhab.binding.omnikinverter/src/main/resources/OH-INF/thing/thing-types.xml +++ b/bundles/org.openhab.binding.omnikinverter/src/main/resources/OH-INF/thing/thing-types.xml @@ -12,6 +12,15 @@ + + + + + + + + + @@ -39,6 +48,60 @@ The instantaneous power generation + + Number:Power + + The instantaneous power generation + + + + Number:Power + + The instantaneous power generation + + + + Number:Power + + The instantaneous power generation + + + + Number:Current + + The instantaneous power generation + + + + Number:Current + + The instantaneous power generation + + + + Number:Current + + The instantaneous power generation + + + + Number:Voltage + + The voltage of PV1 + + + + Number:Voltage + + The voltage of PV2 + + + + Number:Voltage + + The voltage of PV3 + + Number:Energy diff --git a/bundles/org.openhab.binding.omnikinverter/src/test/java/org/openhab/binding/omnikinverter/internal/test/OmnikInverterMessageTest.java b/bundles/org.openhab.binding.omnikinverter/src/test/java/org/openhab/binding/omnikinverter/internal/test/OmnikInverterMessageTest.java index 8ae385449b498..54d4a16541565 100644 --- a/bundles/org.openhab.binding.omnikinverter/src/test/java/org/openhab/binding/omnikinverter/internal/test/OmnikInverterMessageTest.java +++ b/bundles/org.openhab.binding.omnikinverter/src/test/java/org/openhab/binding/omnikinverter/internal/test/OmnikInverterMessageTest.java @@ -40,6 +40,51 @@ public void testGetPower() { assertEquals(137.0, message.getPower(), 0.01); } + @Test + public void testGetPowerAC1() { + assertEquals(137.0, message.getPowerAC1(), 0.01); + } + + @Test + public void testGetPowerAC2() { + assertEquals(-1.0, message.getPowerAC2(), 0.01); + } + + @Test + public void testGetPowerAC3() { + assertEquals(-1.0, message.getPowerAC3(), 0.01); + } + + @Test + public void testGetCurrentPV1() { + assertEquals(0.5, message.getCurrentPV1(), 0.01); + } + + @Test + public void testGetCurrentPV2() { + assertEquals(0.6, message.getCurrentPV2(), 0.01); + } + + @Test + public void testGetCurrentPV3() { + assertEquals(-0.1, message.getCurrentPV3(), 0.01); + } + + @Test + public void testGetVoltagePV1() { + assertEquals(160.0, message.getVoltagePV1(), 0.01); + } + + @Test + public void testGetVoltagePV2() { + assertEquals(131.9, message.getVoltagePV2(), 0.01); + } + + @Test + public void testGetVoltagePV3() { + assertEquals(-0.1, message.getVoltagePV3(), 0.01); + } + @Test public void testGetTotalEnergy() { assertEquals(12412.7, message.getTotalEnergy(), 0.01);