-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Chris Jackson <[email protected]>
- Loading branch information
Showing
5 changed files
with
193 additions
and
1 deletion.
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
178 changes: 178 additions & 0 deletions
178
...gbee/src/main/java/org/openhab/binding/zigbee/internal/converter/ZigBeeConverterPM25.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,178 @@ | ||
/** | ||
* Copyright (c) 2010-2024 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.zigbee.internal.converter; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
import org.eclipse.jdt.annotation.NonNull; | ||
import org.openhab.binding.zigbee.ZigBeeBindingConstants; | ||
import org.openhab.binding.zigbee.converter.ZigBeeBaseChannelConverter; | ||
import org.openhab.binding.zigbee.handler.ZigBeeThingHandler; | ||
import org.openhab.binding.zigbee.internal.converter.config.ZclReportingConfig; | ||
import org.openhab.core.config.core.Configuration; | ||
import org.openhab.core.library.types.DecimalType; | ||
import org.openhab.core.thing.Channel; | ||
import org.openhab.core.thing.ThingUID; | ||
import org.openhab.core.thing.binding.builder.ChannelBuilder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.zsmartsystems.zigbee.CommandResult; | ||
import com.zsmartsystems.zigbee.ZigBeeEndpoint; | ||
import com.zsmartsystems.zigbee.zcl.ZclAttribute; | ||
import com.zsmartsystems.zigbee.zcl.ZclAttributeListener; | ||
import com.zsmartsystems.zigbee.zcl.clusters.ZclPm25MeasurementCluster; | ||
import com.zsmartsystems.zigbee.zcl.protocol.ZclClusterType; | ||
|
||
/** | ||
* Converter for the illuminance channel | ||
* | ||
* @author Chris Jackson - Initial Contribution | ||
* | ||
*/ | ||
public class ZigBeeConverterPM25 extends ZigBeeBaseChannelConverter implements ZclAttributeListener { | ||
private Logger logger = LoggerFactory.getLogger(ZigBeeConverterPM25.class); | ||
|
||
private static BigDecimal CHANGE_DEFAULT = new BigDecimal(5000); | ||
private static BigDecimal CHANGE_MIN = new BigDecimal(10); | ||
private static BigDecimal CHANGE_MAX = new BigDecimal(20000); | ||
|
||
private ZclPm25MeasurementCluster cluster; | ||
private ZclAttribute attribute; | ||
|
||
private ZclReportingConfig configReporting; | ||
|
||
@Override | ||
public Set<Integer> getImplementedClientClusters() { | ||
return Collections.singleton(ZclPm25MeasurementCluster.CLUSTER_ID); | ||
} | ||
|
||
@Override | ||
public Set<Integer> getImplementedServerClusters() { | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public boolean initializeDevice() { | ||
ZclPm25MeasurementCluster serverCluster = (ZclPm25MeasurementCluster) endpoint | ||
.getInputCluster(ZclPm25MeasurementCluster.CLUSTER_ID); | ||
if (serverCluster == null) { | ||
logger.error("{}: Error opening device PM2.5 measurement cluster", endpoint.getIeeeAddress()); | ||
return false; | ||
} | ||
|
||
ZclReportingConfig reporting = new ZclReportingConfig(channel); | ||
|
||
try { | ||
CommandResult bindResponse = bind(serverCluster).get(); | ||
if (bindResponse.isSuccess()) { | ||
// Configure reporting - no faster than once per second - no slower than 2 hours. | ||
ZclAttribute attribute = serverCluster.getAttribute(ZclPm25MeasurementCluster.ATTR_MEASUREDVALUE); | ||
CommandResult reportingResponse = attribute.setReporting(reporting.getReportingTimeMin(), | ||
reporting.getReportingTimeMax(), reporting.getReportingChange()).get(); | ||
handleReportingResponse(reportingResponse, POLLING_PERIOD_DEFAULT, reporting.getPollingPeriod()); | ||
} | ||
} catch (InterruptedException | ExecutionException e) { | ||
logger.debug("{}: Exception configuring measured value reporting", endpoint.getIeeeAddress(), e); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean initializeConverter(ZigBeeThingHandler thing) { | ||
super.initializeConverter(thing); | ||
cluster = (ZclPm25MeasurementCluster) endpoint.getInputCluster(ZclPm25MeasurementCluster.CLUSTER_ID); | ||
if (cluster == null) { | ||
logger.error("{}: Error opening device PM2.5 measurement cluster", endpoint.getIeeeAddress()); | ||
return false; | ||
} | ||
|
||
attribute = cluster.getAttribute(ZclPm25MeasurementCluster.ATTR_MEASUREDVALUE); | ||
if (attribute == null) { | ||
logger.error("{}: Error opening device PM2.5 measurement attribute", endpoint.getIeeeAddress()); | ||
return false; | ||
} | ||
|
||
// Add a listener, then request the status | ||
cluster.addAttributeListener(this); | ||
|
||
// Create a configuration handler and get the available options | ||
configReporting = new ZclReportingConfig(channel); | ||
configReporting.setAnalogue(CHANGE_DEFAULT, CHANGE_MIN, CHANGE_MAX); | ||
configOptions = new ArrayList<>(); | ||
configOptions.addAll(configReporting.getConfiguration()); | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
public void disposeConverter() { | ||
cluster.removeAttributeListener(this); | ||
} | ||
|
||
@Override | ||
public int getPollingPeriod() { | ||
return configReporting.getPollingPeriod(); | ||
} | ||
|
||
@Override | ||
public void handleRefresh() { | ||
attribute.readValue(0); | ||
} | ||
|
||
@Override | ||
public void updateConfiguration(@NonNull Configuration currentConfiguration, | ||
Map<String, Object> updatedParameters) { | ||
if (configReporting.updateConfiguration(currentConfiguration, updatedParameters)) { | ||
try { | ||
ZclAttribute attribute = cluster.getAttribute(ZclPm25MeasurementCluster.ATTR_MEASUREDVALUE); | ||
CommandResult reportingResponse; | ||
reportingResponse = attribute.setReporting(configReporting.getReportingTimeMin(), | ||
configReporting.getReportingTimeMax(), configReporting.getReportingChange()).get(); | ||
handleReportingResponse(reportingResponse, configReporting.getPollingPeriod(), | ||
configReporting.getReportingTimeMax()); | ||
} catch (InterruptedException | ExecutionException e) { | ||
logger.debug("{}: Illuminance measurement exception setting reporting", endpoint.getIeeeAddress(), e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Channel getChannel(ThingUID thingUID, ZigBeeEndpoint endpoint) { | ||
if (endpoint.getInputCluster(ZclPm25MeasurementCluster.CLUSTER_ID) == null) { | ||
logger.trace("{}: Illuminance measurement cluster not found", endpoint.getIeeeAddress()); | ||
return null; | ||
} | ||
return ChannelBuilder | ||
.create(createChannelUID(thingUID, endpoint, ZigBeeBindingConstants.CHANNEL_NAME_PM25_VALUE), | ||
ZigBeeBindingConstants.ITEM_TYPE_NUMBER) | ||
.withType(ZigBeeBindingConstants.CHANNEL_PM25_VALUE) | ||
.withLabel(ZigBeeBindingConstants.CHANNEL_LABEL_PM25_VALUE).withProperties(createProperties(endpoint)) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void attributeUpdated(ZclAttribute attribute, Object val) { | ||
if (attribute.getClusterType() == ZclClusterType.PM2_5_MEASUREMENT | ||
&& attribute.getId() == ZclPm25MeasurementCluster.ATTR_MEASUREDVALUE) { | ||
logger.debug("{}: ZigBee attribute reports {}", endpoint.getIeeeAddress(), attribute); | ||
updateChannelState(new DecimalType(Math.pow(10.0, (Integer) val / 10000.0) - 1)); | ||
} | ||
} | ||
} |
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
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
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