added migrated 2.x add-ons

Signed-off-by: Kai Kreuzer <kai@openhab.org>
This commit is contained in:
Kai Kreuzer
2020-09-21 01:58:32 +02:00
parent bbf1a7fd29
commit 6df6783b60
11662 changed files with 1302875 additions and 11 deletions

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.binding.iammeter-${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-iammeter" description="Iammeter Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.iammeter/${project.version}</bundle>
</feature>
</features>

View File

@@ -0,0 +1,64 @@
/**
* 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.iammeter.internal;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.types.State;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* The {@link IammeterHandler} is responsible for handling commands, which are
* sent to one of the channels.
*
* @author Yang Bo - Initial contribution
*/
@NonNullByDefault
public class Iammeter3080THandler extends IammeterBaseHandler {
public Iammeter3080THandler(Thing thing) {
super(thing);
}
@SuppressWarnings("null")
@Override
protected void resolveData(String response) {
JsonElement iammeterDataElement = new JsonParser().parse(response);
JsonObject iammeterData = iammeterDataElement.getAsJsonObject();
String keyWord = "Datas";
if (iammeterData.has("Datas") && iammeterData.has("SN")) {
String groups[] = { "powerPhaseA", "powerPhaseB", "powerPhaseC" };
for (int row = 0; row < groups.length; row++) {
String gpName = groups[row];
List<Channel> chnList = getThing().getChannelsOfGroup(gpName);
for (IammeterWEM3080Channel channelConfig : IammeterWEM3080Channel.values()) {
Channel chnl = chnList.get(channelConfig.ordinal());
if (chnl != null) {
State state = getQuantityState(iammeterData.get(keyWord).getAsJsonArray().get(row)
.getAsJsonArray().get(channelConfig.ordinal()).toString(), channelConfig.getUnit());
updateState(chnl.getUID(), state);
}
}
updateStatus(ThingStatus.ONLINE);
}
}
}
}

View File

@@ -0,0 +1,126 @@
/**
* 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.iammeter.internal;
import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.measure.Unit;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.cache.ExpiringCache;
import org.openhab.core.io.net.http.HttpUtil;
import org.openhab.core.library.types.QuantityType;
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.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonSyntaxException;
/**
* The {@link IammeterHandler} is responsible for handling commands, which are
* sent to one of the channels.
*
* @author Yang Bo - Initial contribution
*/
@NonNullByDefault
public abstract class IammeterBaseHandler extends BaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(IammeterBaseHandler.class);
private @Nullable ScheduledFuture<?> refreshJob;
private IammeterConfiguration config;
private static final int TIMEOUT_MS = 5000;
private final ExpiringCache<Boolean> refreshCache = new ExpiringCache<>(Duration.ofSeconds(5), this::refresh);
public IammeterBaseHandler(Thing thing) {
super(thing);
config = getConfiguration();
}
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
if (command instanceof RefreshType) {
refreshCache.getValue();
}
}
@Override
public void initialize() {
ScheduledFuture<?> refreshJob = this.refreshJob;
config = getConfiguration();
if (refreshJob == null) {
refreshJob = scheduler.scheduleWithFixedDelay(this::refresh, 0, config.refreshInterval, TimeUnit.SECONDS);
this.refreshJob = refreshJob;
updateStatus(ThingStatus.UNKNOWN);
}
}
protected abstract void resolveData(String response);
@SuppressWarnings("null")
private boolean refresh() {
refreshCache.invalidateValue();
IammeterConfiguration config = this.config;
try {
String httpMethod = "GET";
String url = "http://" + config.username + ":" + config.password + "@" + config.host + ":" + config.port
+ "/monitorjson";
String response = HttpUtil.executeUrl(httpMethod, url, TIMEOUT_MS);
resolveData(response);
updateStatus(ThingStatus.ONLINE);
return true;
// Very rudimentary Exception differentiation
} catch (IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Communication error with the device: " + e.getMessage());
} catch (JsonSyntaxException je) {
logger.warn("Invalid JSON when refreshing source {}: {}", getThing().getUID(), je.getMessage());
updateStatus(ThingStatus.OFFLINE);
}
return false;
}
protected State getQuantityState(String value, Unit<?> unit) {
try {
return QuantityType.valueOf(Float.parseFloat(value), unit);
} catch (NumberFormatException e) {
return UnDefType.UNDEF;
}
}
@Override
public void dispose() {
ScheduledFuture<?> refreshJob = this.refreshJob;
if (refreshJob != null && !refreshJob.isCancelled()) {
refreshJob.cancel(true);
this.refreshJob = null;
}
super.dispose();
}
public IammeterConfiguration getConfiguration() {
return this.getConfigAs(IammeterConfiguration.class);
}
}

View File

@@ -0,0 +1,32 @@
/**
* 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.iammeter.internal;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.ThingTypeUID;
/**
* The {@link IammeterBindingConstants} class defines common constants, which are
* used across the whole binding.
*
* @author yang bo - Initial contribution
*/
@NonNullByDefault
public class IammeterBindingConstants {
public static final String BINDING_ID = "iammeter";
// List of all Thing Type UIDs
public static final ThingTypeUID THING_TYPE_POWERMETER = new ThingTypeUID(BINDING_ID, "powermeter");
public static final ThingTypeUID THING_TYPE_POWERMETER_3080T = new ThingTypeUID(BINDING_ID, "powermeter3080T");
}

View File

@@ -0,0 +1,30 @@
/**
* 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.iammeter.internal;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* The {@link IammeterConfiguration} class contains fields mapping thing configuration parameters.
*
* @author Yang Bo - Initial contribution
*/
@NonNullByDefault
public class IammeterConfiguration {
public String host = "127.0.0.1";
public int port = 80;
public int refreshInterval = 30;
public String username = "admin";
public String password = "admin";
}

View File

@@ -0,0 +1,58 @@
/**
* 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.iammeter.internal;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.Thing;
import org.openhab.core.types.State;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* The {@link IammeterHandler} is responsible for handling commands, which are
* sent to one of the channels.
*
* @author Yang Bo - Initial contribution
*/
@NonNullByDefault
public class IammeterHandler extends IammeterBaseHandler {
public IammeterHandler(Thing thing) {
super(thing);
}
@Override
protected void resolveData(String response) {
JsonElement iammeterDataElement = new JsonParser().parse(response);
JsonObject iammeterData = iammeterDataElement.getAsJsonObject();
String keyWord = "Data";
if (iammeterData.has("data") || (iammeterData.has("Data") && iammeterData.has("SN"))) {
if (iammeterData.has("data")) {
keyWord = "data";
}
for (IammeterWEM3080Channel channelConfig : IammeterWEM3080Channel.values()) {
Channel channel = getThing().getChannel(channelConfig.getId());
if (channel != null) {
State state = getQuantityState(
iammeterData.get(keyWord).getAsJsonArray().get(channelConfig.ordinal()).toString(),
channelConfig.getUnit());
updateState(channel.getUID(), state);
}
}
}
}
}

View File

@@ -0,0 +1,62 @@
/**
* 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.iammeter.internal;
import static org.openhab.binding.iammeter.internal.IammeterBindingConstants.*;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
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 IammeterHandlerFactory} is responsible for creating things and thing
* handlers.
*
* @author Yang Bo - Initial contribution
*/
@NonNullByDefault
@Component(configurationPid = "binding.iammeter", service = ThingHandlerFactory.class)
public class IammeterHandlerFactory extends BaseThingHandlerFactory {
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = new HashSet<>();
static {
SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_POWERMETER);
SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_POWERMETER_3080T);
}
@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_POWERMETER.equals(thingTypeUID)) {
return new IammeterHandler(thing);
} else if (THING_TYPE_POWERMETER_3080T.equals(thingTypeUID)) {
return new Iammeter3080THandler(thing);
}
return null;
}
}

View File

@@ -0,0 +1,48 @@
/**
* 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.iammeter.internal;
import javax.measure.Unit;
import org.openhab.core.library.unit.SmartHomeUnits;
/**
* The {@link IammeterWEM3080Channel} Enum defines common constants, which are
* used across the whole binding.
*
* @author Yang Bo - Initial contribution
*/
public enum IammeterWEM3080Channel {
CHANNEL_VOLTAGE("voltage", SmartHomeUnits.VOLT),
CHANNEL_CURRENT("current", SmartHomeUnits.AMPERE),
CHANNEL_POWER("power", SmartHomeUnits.WATT),
CHANNEL_IMPORTENERGY("importenergy", SmartHomeUnits.KILOWATT_HOUR),
CHANNEL_EXPORTGRID("exportgrid", SmartHomeUnits.KILOWATT_HOUR);
private final String id;
private final Unit<?> unit;
IammeterWEM3080Channel(String id, Unit<?> unit) {
this.id = id;
this.unit = unit;
}
public String getId() {
return id;
}
public Unit<?> getUnit() {
return unit;
}
}

View File

@@ -0,0 +1,50 @@
/**
* 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.iammeter.internal;
import javax.measure.Unit;
import org.openhab.core.library.unit.SmartHomeUnits;
/**
* The {@link IammeterWEM3080TChannel} Enum defines common constants, which are
* used across the whole binding.
*
* @author Yang Bo - Initial contribution
*/
public enum IammeterWEM3080TChannel {
CHANNEL_VOLTAGE("voltage", SmartHomeUnits.VOLT),
CHANNEL_CURRENT("current", SmartHomeUnits.AMPERE),
CHANNEL_POWER("power", SmartHomeUnits.WATT),
CHANNEL_IMPORTENERGY("importenergy", SmartHomeUnits.KILOWATT_HOUR),
CHANNEL_EXPORTGRID("exportgrid", SmartHomeUnits.KILOWATT_HOUR),
CHANNEL_FREQUENCY("frequency", SmartHomeUnits.HERTZ),
CHANNEL_PF("pf", SmartHomeUnits.HERTZ);
private final String id;
private final Unit<?> unit;
IammeterWEM3080TChannel(String id, Unit<?> unit) {
this.id = id;
this.unit = unit;
}
public String getId() {
return id;
}
public Unit<?> getUnit() {
return unit;
}
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<binding:binding id="iammeter" 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>Iammeter Binding</name>
<description>The Iammeter binding pull your Iammeter power meter data from LAN.</description>
<author>Yang Bo</author>
</binding:binding>

View File

@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<config-description:config-descriptions
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:config-description="https://openhab.org/schemas/config-description/v1.0.0"
xsi:schemaLocation="https://openhab.org/schemas/config-description/v1.0.0 https://openhab.org/schemas/config-description-1.0.0.xsd">
<config-description uri="thing-type:iammeter:powermeter">
<parameter name="host" type="text" required="true">
<label>Device's IP Address</label>
<description>IP address for your device</description>
<context>network-address</context>
<default>127.0.0.1</default>
</parameter>
<parameter name="port" type="text" required="true" min="1" max="65535">
<label>Device's Port</label>
<description>Port</description>
<default>80</default>
</parameter>
<parameter name="username" type="text" required="false">
<label>User Name</label>
<description>User name to access device</description>
<default>admin</default>
</parameter>
<parameter name="password" type="text" required="false">
<label>Password</label>
<description>password to access device</description>
<context>password</context>
<default>admin</default>
</parameter>
<parameter name="refreshInterval" type="integer" required="false" unit="s">
<label>Refresh interval in seconds</label>
<default>60</default>
</parameter>
</config-description>
</config-description:config-descriptions>

View File

@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<thing:thing-descriptions bindingId="iammeter"
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="powermeter">
<label>Iammeter Power Meter 3162/3080</label>
<description>Single phase PowerMeter for Iammeter Binding</description>
<channels>
<channel id="voltage" typeId="voltage"/>
<channel id="current" typeId="current"/>
<channel id="power" typeId="power"/>
<channel id="importenergy" typeId="importenergy"/>
<channel id="exportgrid" typeId="exportgrid"/>
</channels>
<config-description-ref uri="thing-type:iammeter:powermeter"/>
</thing-type>
<channel-type id="voltage">
<item-type>Number:ElectricPotential</item-type>
<label>Voltage</label>
<description>voltage for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="current">
<item-type>Number:ElectricCurrent</item-type>
<label>Current</label>
<description>current for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="power">
<item-type>Number:Power</item-type>
<label>Power</label>
<description>power for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="importenergy">
<item-type>Number:Energy</item-type>
<label>ImportEnergy</label>
<description>importenergy for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="exportgrid">
<item-type>Number:Energy</item-type>
<label>Exportgrid</label>
<description>exportgrid for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
</thing:thing-descriptions>

View File

@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<thing:thing-descriptions bindingId="iammeter"
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="powermeter3080T">
<label>Iammeter Power Meter 3080T</label>
<description>3 phases PowerMeter for Iammeter 3080T Binding</description>
<channel-groups>
<channel-group id="powerPhaseA" typeId="powerPhaseGroup">
<label>Power Phase A</label>
<description>Power phase 1 for Iammeter device</description>
</channel-group>
<channel-group id="powerPhaseB" typeId="powerPhaseGroup">
<label>Power Phase B</label>
<description>Power phase 2 for Iammeter device</description>
</channel-group>
<channel-group id="powerPhaseC" typeId="powerPhaseGroup">
<label>Power Phase C</label>
<description>Power phase 3 for Iammeter device</description>
</channel-group>
</channel-groups>
<config-description-ref uri="thing-type:iammeter:powermeter"/>
</thing-type>
<channel-group-type id="powerPhaseGroup">
<label>Power Phase</label>
<channels>
<channel id="voltage" typeId="voltage"/>
<channel id="current" typeId="current"/>
<channel id="power" typeId="power"/>
<channel id="importenergy" typeId="importenergy"/>
<channel id="exportgrid" typeId="exportgrid"/>
<channel id="frequency" typeId="frequency"/>
<channel id="pf" typeId="pf"/>
</channels>
</channel-group-type>
<channel-type id="voltage">
<item-type>Number:ElectricPotential</item-type>
<label>Voltage</label>
<description>voltage for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="current">
<item-type>Number:ElectricCurrent</item-type>
<label>Current</label>
<description>current for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="power">
<item-type>Number:Power</item-type>
<label>Power</label>
<description>power for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="importenergy">
<item-type>Number:Energy</item-type>
<label>ImportEnergy</label>
<description>importenergy for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="exportgrid">
<item-type>Number:Energy</item-type>
<label>Exportgrid</label>
<description>exportgrid for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="frequency" advanced="true">
<item-type>Number:Frequency</item-type>
<label>Frequency</label>
<description>frequency for phase A</description>
<state pattern="%.2f %unit%" readOnly="true"></state>
</channel-type>
<channel-type id="pf" advanced="true">
<item-type>Number</item-type>
<label>Power Factor</label>
<description>power factor for phase A</description>
<state pattern="%.2f" readOnly="true"></state>
</channel-type>
</thing:thing-descriptions>