added migrated 2.x add-ons
Signed-off-by: Kai Kreuzer <kai@openhab.org>
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<features name="org.openhab.binding.omnikinverter-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
|
||||
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>
|
||||
|
||||
<feature name="openhab-binding-omnikinverter" description="Omnik Inverter Binding" version="${project.version}">
|
||||
<feature>openhab-runtime-base</feature>
|
||||
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.omnikinverter/${project.version}</bundle>
|
||||
</feature>
|
||||
</features>
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.omnikinverter.internal;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.apache.commons.lang.ArrayUtils;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Hans van den Bogert - Initial Contribution
|
||||
*
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class OmnikInverter {
|
||||
|
||||
private int serialNumber;
|
||||
private String host;
|
||||
private int port;
|
||||
private byte[] magicPacket;
|
||||
|
||||
public OmnikInverter(String host, int port, int serialNumber) throws IOException {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.serialNumber = serialNumber;
|
||||
this.magicPacket = generateMagicPacket();
|
||||
}
|
||||
|
||||
public OmnikInverterMessage pullCurrentStats() throws UnknownHostException, IOException {
|
||||
byte[] magicPacket = this.magicPacket;
|
||||
byte[] returnMessage = new byte[1024];
|
||||
|
||||
try (Socket socket = new Socket(host, port)) {
|
||||
socket.setSoTimeout(5000);
|
||||
socket.getOutputStream().write(magicPacket);
|
||||
socket.getInputStream().read(returnMessage);
|
||||
|
||||
return new OmnikInverterMessage(returnMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] generateMagicPacket() throws IOException {
|
||||
byte[] magic = { 0x68, 0x02, 0x40, 0x30 };
|
||||
|
||||
ByteBuffer serialByteBuffer = ByteBuffer.allocate(8).putInt(serialNumber).putInt(serialNumber);
|
||||
byte[] serialBytes = serialByteBuffer.array();
|
||||
// Reverse serialBytes in a very mutable way.
|
||||
ArrayUtils.reverse(serialBytes);
|
||||
|
||||
byte checksumCount = 115;
|
||||
for (byte b : serialBytes) {
|
||||
checksumCount += (char) b;
|
||||
}
|
||||
|
||||
byte[] checksum = ByteBuffer.allocate(1).put(checksumCount).array();
|
||||
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
|
||||
outputStream.write(magic);
|
||||
outputStream.write(serialBytes);
|
||||
outputStream.write(0x01);
|
||||
outputStream.write(0x00);
|
||||
outputStream.write(checksum);
|
||||
outputStream.write(0x16);
|
||||
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.omnikinverter.internal;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.core.thing.ThingTypeUID;
|
||||
|
||||
/**
|
||||
* The {@link OmnikInverterBindingConstants} class defines common constants, which are
|
||||
* used across the whole binding.
|
||||
*
|
||||
* @author Hans van den Bogert - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class OmnikInverterBindingConstants {
|
||||
|
||||
private static final String BINDING_ID = "omnikinverter";
|
||||
|
||||
// List of all Thing Type UIDs
|
||||
public static final ThingTypeUID THING_TYPE_OMNIK = new ThingTypeUID(BINDING_ID, "omnik");
|
||||
|
||||
// List of all Channel ids
|
||||
public static final String CHANNEL_POWER = "power";
|
||||
|
||||
public static final String CHANNEL_ENERGY_TODAY = "energyToday";
|
||||
|
||||
public static final String CHANNEL_ENERGY_TOTAL = "energyTotal";
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.omnikinverter.internal;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
|
||||
/**
|
||||
* The {@link OmnikInverterConfiguration} class contains fields mapping thing configuration parameters.
|
||||
*
|
||||
* @author Hans van den Bogert - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class OmnikInverterConfiguration {
|
||||
public String hostname = "";
|
||||
public int port;
|
||||
public int serial;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.omnikinverter.internal;
|
||||
|
||||
import static org.openhab.binding.omnikinverter.internal.OmnikInverterBindingConstants.THING_TYPE_OMNIK;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.omnikinverter.internal.handler.OmnikInverterHandler;
|
||||
import org.openhab.core.thing.Thing;
|
||||
import org.openhab.core.thing.ThingTypeUID;
|
||||
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
|
||||
import org.openhab.core.thing.binding.ThingHandler;
|
||||
import org.openhab.core.thing.binding.ThingHandlerFactory;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
|
||||
/**
|
||||
* The {@link OmnikInverterHandlerFactory} is responsible for creating things and thing
|
||||
* handlers.
|
||||
*
|
||||
* @author Hans van den Bogert - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
@Component(configurationPid = "binding.omnikinverter", service = ThingHandlerFactory.class)
|
||||
public class OmnikInverterHandlerFactory extends BaseThingHandlerFactory {
|
||||
|
||||
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_OMNIK);
|
||||
|
||||
@Override
|
||||
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
|
||||
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable ThingHandler createHandler(Thing thing) {
|
||||
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
|
||||
|
||||
if (THING_TYPE_OMNIK.equals(thingTypeUID)) {
|
||||
return new OmnikInverterHandler(thing);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.omnikinverter.internal;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Hans van den Bogert - Initial contribution
|
||||
*
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class OmnikInverterMessage {
|
||||
|
||||
private final byte[] bytes;
|
||||
|
||||
public OmnikInverterMessage(byte[] b) {
|
||||
this.bytes = b;
|
||||
}
|
||||
|
||||
public double getPower() {
|
||||
ByteBuffer buf = ByteBuffer.allocate(2);
|
||||
buf.put(bytes, 59, 2);
|
||||
buf.rewind();
|
||||
return buf.getShort();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @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 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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.omnikinverter.internal.handler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.NoRouteToHostException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.measure.quantity.Power;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.openhab.binding.omnikinverter.internal.OmnikInverter;
|
||||
import org.openhab.binding.omnikinverter.internal.OmnikInverterBindingConstants;
|
||||
import org.openhab.binding.omnikinverter.internal.OmnikInverterConfiguration;
|
||||
import org.openhab.binding.omnikinverter.internal.OmnikInverterMessage;
|
||||
import org.openhab.core.library.types.QuantityType;
|
||||
import org.openhab.core.library.unit.SmartHomeUnits;
|
||||
import org.openhab.core.thing.ChannelUID;
|
||||
import org.openhab.core.thing.Thing;
|
||||
import org.openhab.core.thing.ThingStatus;
|
||||
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;
|
||||
|
||||
/**
|
||||
* The {@link OmnikInverterHandler} is responsible for handling commands, which are
|
||||
* sent to one of the channels.
|
||||
*
|
||||
* @author Hans van den Bogert - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class OmnikInverterHandler extends BaseThingHandler {
|
||||
private @Nullable OmnikInverter inverter;
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
|
||||
|
||||
public OmnikInverterHandler(Thing thing) {
|
||||
super(thing);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCommand(ChannelUID channelUID, Command command) {
|
||||
if (OmnikInverterBindingConstants.CHANNEL_POWER.equals(channelUID.getId())) {
|
||||
if (command instanceof RefreshType) {
|
||||
updateData();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
|
||||
|
||||
try {
|
||||
inverter = new OmnikInverter(config.hostname, config.port, config.serial);
|
||||
scheduler.scheduleWithFixedDelay(this::updateData, 0, 10, TimeUnit.SECONDS);
|
||||
} catch (IOException e) {
|
||||
logger.debug("Could not instantiate OmnikInverter object: {}", e.getMessage());
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR,
|
||||
"Failed to initialize: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void updateData() {
|
||||
try {
|
||||
if (inverter != null) {
|
||||
OmnikInverterMessage message = inverter.pullCurrentStats();
|
||||
|
||||
updateStatus(ThingStatus.ONLINE);
|
||||
|
||||
QuantityType<Power> powerQuantity = new QuantityType<>(message.getPower(), SmartHomeUnits.WATT);
|
||||
updateState(OmnikInverterBindingConstants.CHANNEL_POWER, powerQuantity);
|
||||
|
||||
updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TODAY,
|
||||
new QuantityType<>(message.getEnergyToday(), SmartHomeUnits.KILOWATT_HOUR));
|
||||
|
||||
updateState(OmnikInverterBindingConstants.CHANNEL_ENERGY_TOTAL,
|
||||
new QuantityType<>(message.getTotalEnergy(), SmartHomeUnits.KILOWATT_HOUR));
|
||||
}
|
||||
} catch (UnknownHostException | NoRouteToHostException | ConnectException e) {
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
|
||||
} catch (IOException e) {
|
||||
logger.debug("Unknown exception when pulling data from the inverter: {}", e.getMessage());
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "Unknown error: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<binding:binding id="omnikinverter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:binding="https://openhab.org/schemas/binding/v1.0.0"
|
||||
xsi:schemaLocation="https://openhab.org/schemas/binding/v1.0.0 https://openhab.org/schemas/binding-1.0.0.xsd">
|
||||
|
||||
<name>OmnikInverter Binding</name>
|
||||
<description>This is the binding for the Omnik solar grid inverters. The integration is based on the undocumented API,
|
||||
which is reverse engineered at https://github.com/Woutrrr/Omnik-Data-Logger. As such, the known supported inverters
|
||||
are known to work with inverters with wifi bridge which start with the serial number '602' and '606'.</description>
|
||||
<author>Hans van den Bogert</author>
|
||||
</binding:binding>
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<thing:thing-descriptions bindingId="omnikinverter"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:thing="https://openhab.org/schemas/thing-description/v1.0.0"
|
||||
xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd">
|
||||
|
||||
<thing-type id="omnik">
|
||||
<label>OmnikInverter Binding Thing</label>
|
||||
<description>Thing for OmnikInverter Binding</description>
|
||||
|
||||
<channels>
|
||||
<channel id="power" typeId="power"/>
|
||||
<channel id="energyToday" typeId="energyToday"/>
|
||||
<channel id="energyTotal" typeId="energyTotal"/>
|
||||
</channels>
|
||||
|
||||
<config-description>
|
||||
<parameter name="hostname" type="text" required="true">
|
||||
<label>Hostname</label>
|
||||
<description>The hostname or IP of the Omnik Inverter</description>
|
||||
<context>network-address</context>
|
||||
</parameter>
|
||||
<parameter name="port" type="integer" required="true">
|
||||
<label>Port</label>
|
||||
<description>The TCP port of the Omnik inverter, usually 8899</description>
|
||||
<default>8899</default>
|
||||
</parameter>
|
||||
<parameter name="serial" type="integer" required="true">
|
||||
<label>Serial</label>
|
||||
<description>The serial of the Omnik inverter's Wifi module. This the number part only.</description>
|
||||
</parameter>
|
||||
</config-description>
|
||||
|
||||
</thing-type>
|
||||
|
||||
<channel-type id="power">
|
||||
<item-type>Number:Power</item-type>
|
||||
<label>Instantaneous Power</label>
|
||||
<description>The instantaneous power generation</description>
|
||||
<state readOnly="true" pattern="%.1f %unit%"/>
|
||||
</channel-type>
|
||||
<channel-type id="energyToday">
|
||||
<item-type>Number:Energy</item-type>
|
||||
<label>Energy Today</label>
|
||||
<description>The amount of energy generated today</description>
|
||||
<state readOnly="true" pattern="%.2f %unit%"/>
|
||||
</channel-type>
|
||||
<channel-type id="energyTotal">
|
||||
<item-type>Number:Energy</item-type>
|
||||
<label>Total Generated Energy</label>
|
||||
<description>The amount of generated energy in total</description>
|
||||
<state readOnly="true" pattern="%.2f %unit%"/>
|
||||
</channel-type>
|
||||
|
||||
</thing:thing-descriptions>
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2020 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.omnikinverter.internal.test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.openhab.binding.omnikinverter.internal.OmnikInverterMessage;
|
||||
|
||||
/**
|
||||
* @author Hans van den Bogert - Initial contribution
|
||||
*/
|
||||
public class OmnikInverterMessageTest {
|
||||
|
||||
private OmnikInverterMessage message;
|
||||
|
||||
@Before
|
||||
public void setUp() throws IOException {
|
||||
File file = new File("src/test/resources/omnik.output");
|
||||
message = new OmnikInverterMessage(Files.readAllBytes(file.toPath()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPower() {
|
||||
assertEquals(137.0, message.getPower(), 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTotalEnergy() {
|
||||
assertEquals(12412.7, message.getTotalEnergy(), 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetEnergyToday() {
|
||||
assertEquals(11.13, message.getEnergyToday(), 0.01);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user