forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[miio] dynamically generate channelTypes
Simplify the json database creation for new models and less chance for errors Related to openhab#7276 Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com>
- Loading branch information
Showing
6 changed files
with
312 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
....miio/src/main/java/org/openhab/binding/miio/internal/basic/BasicChannelTypeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* 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.miio.internal.basic; | ||
|
||
import static org.openhab.binding.miio.internal.MiIoBindingConstants.BINDING_ID; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.thing.type.ChannelType; | ||
import org.openhab.core.thing.type.ChannelTypeBuilder; | ||
import org.openhab.core.thing.type.ChannelTypeProvider; | ||
import org.openhab.core.thing.type.ChannelTypeUID; | ||
import org.openhab.core.thing.type.StateChannelTypeBuilder; | ||
import org.openhab.core.types.StateDescriptionFragmentBuilder; | ||
import org.openhab.core.types.StateOption; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Provide channelTypes for Mi IO Basic devices | ||
* | ||
* @author Marcel Verpaalen - Initial contribution | ||
*/ | ||
@Component(service = { ChannelTypeProvider.class, BasicChannelTypeProvider.class }) | ||
@NonNullByDefault | ||
public class BasicChannelTypeProvider implements ChannelTypeProvider { | ||
private final List<ChannelType> channelTypes = new CopyOnWriteArrayList<>(); | ||
private final Logger logger = LoggerFactory.getLogger(BasicChannelTypeProvider.class); | ||
|
||
@Override | ||
public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) { | ||
return channelTypes; | ||
} | ||
|
||
@Override | ||
public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) { | ||
for (ChannelType channelType : channelTypes) { | ||
if (channelType.getUID().equals(channelTypeUID)) { | ||
return channelType; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public void addChannelType(MiIoBasicChannel miChannel, String model) { | ||
ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, | ||
model.toUpperCase().replace(".", "_") + "_" + miChannel.getChannel()); | ||
logger.debug("Adding channel definitions for {} -> {}", channelTypeUID, miChannel.getFriendlyName()); | ||
try { | ||
final StateDescriptionDTO stateDescription = miChannel.getStateDescription(); | ||
StateChannelTypeBuilder channelTypeBuilder = ChannelTypeBuilder.state(channelTypeUID, | ||
miChannel.getFriendlyName(), miChannel.getType()); // | ||
if (stateDescription != null) { | ||
StateDescriptionFragmentBuilder sdf = StateDescriptionFragmentBuilder.create(); | ||
final BigDecimal maximum = stateDescription.getMaximum(); | ||
if (maximum != null) { | ||
sdf.withMaximum(maximum); | ||
} | ||
final BigDecimal minimum = stateDescription.getMinimum(); | ||
if (minimum != null) { | ||
sdf.withMinimum(minimum); | ||
} | ||
final BigDecimal step = stateDescription.getStep(); | ||
if (step != null) { | ||
sdf.withStep(step); | ||
} | ||
final String pattern = stateDescription.getPattern(); | ||
if (pattern != null) { | ||
sdf.withPattern(pattern); | ||
} | ||
final Boolean readOnly = stateDescription.getReadOnly(); | ||
if (readOnly != null) { | ||
sdf.withReadOnly(readOnly); | ||
} | ||
List<OptionsValueListDTO> optionList = stateDescription.getOptions(); | ||
if (optionList != null) { | ||
List<StateOption> options = new ArrayList<>(); | ||
for (OptionsValueListDTO option : optionList) { | ||
String value = option.getValue(); | ||
if (value != null) { | ||
options.add(new StateOption(value, option.getDescription())); | ||
} | ||
} | ||
sdf.withOptions(options); | ||
} | ||
channelTypeBuilder.withStateDescriptionFragment(sdf.build()); | ||
logger.debug("added stateDescription: {}", sdf); | ||
} | ||
channelTypes.add(channelTypeBuilder.build()); | ||
} catch ( | ||
|
||
Exception ex) { | ||
logger.warn("Failed creating channelType {}: {}", channelTypeUID, ex.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...nding.miio/src/main/java/org/openhab/binding/miio/internal/basic/OptionsValueListDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* 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.miio.internal.basic; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import com.google.gson.annotations.Expose; | ||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* Mapping properties from json for miot device info | ||
* | ||
* @author Marcel Verpaalen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class OptionsValueListDTO { | ||
|
||
@SerializedName("value") | ||
@Expose | ||
@Nullable | ||
public String value; | ||
@SerializedName("description") | ||
@Expose | ||
@Nullable | ||
public String description; | ||
|
||
@Nullable | ||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Nullable | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...nding.miio/src/main/java/org/openhab/binding/miio/internal/basic/StateDescriptionDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* 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.miio.internal.basic; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
import com.google.gson.annotations.Expose; | ||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* Mapping properties from json for state descriptions | ||
* | ||
* @author Marcel Verpaalen - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class StateDescriptionDTO { | ||
|
||
@SerializedName("minimum") | ||
@Expose | ||
@Nullable | ||
private BigDecimal minimum; | ||
@SerializedName("maximum") | ||
@Expose | ||
@Nullable | ||
private BigDecimal maximum; | ||
@SerializedName("step") | ||
@Expose | ||
@Nullable | ||
private BigDecimal step; | ||
@SerializedName("pattern") | ||
@Expose | ||
@Nullable | ||
private String pattern; | ||
@SerializedName("readOnly") | ||
@Expose | ||
@Nullable | ||
private Boolean readOnly; | ||
@SerializedName("options") | ||
@Expose | ||
@Nullable | ||
public List<OptionsValueListDTO> options = null; | ||
|
||
@Nullable | ||
public BigDecimal getMinimum() { | ||
return minimum; | ||
} | ||
|
||
public void setMinimum(BigDecimal minimum) { | ||
this.minimum = minimum; | ||
} | ||
|
||
@Nullable | ||
public BigDecimal getMaximum() { | ||
return maximum; | ||
} | ||
|
||
public void setMaximum(BigDecimal maximum) { | ||
this.maximum = maximum; | ||
} | ||
|
||
@Nullable | ||
public BigDecimal getStep() { | ||
return step; | ||
} | ||
|
||
public void setStep(BigDecimal step) { | ||
this.step = step; | ||
} | ||
|
||
@Nullable | ||
public String getPattern() { | ||
return pattern; | ||
} | ||
|
||
public void setPattern(String pattern) { | ||
this.pattern = pattern; | ||
} | ||
|
||
@Nullable | ||
public Boolean getReadOnly() { | ||
return readOnly; | ||
} | ||
|
||
public void setReadOnly(Boolean readOnly) { | ||
this.readOnly = readOnly; | ||
} | ||
|
||
@Nullable | ||
public List<OptionsValueListDTO> getOptions() { | ||
return options; | ||
} | ||
|
||
public void setOptions(List<OptionsValueListDTO> options) { | ||
this.options = options; | ||
} | ||
} |
Oops, something went wrong.