Skip to content

Commit

Permalink
start implementing channel refresh
Browse files Browse the repository at this point in the history
Signed-off-by: Laurent ARNAL <[email protected]>
  • Loading branch information
lo92fr committed Dec 13, 2024
1 parent a25386b commit 3f6a06a
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.smartthings.internal.discovery.SmartthingsDiscoveryService;
import org.openhab.binding.smartthings.internal.handler.SmartthingsBridgeHandler;
import org.openhab.binding.smartthings.internal.handler.SmartthingsCloudBridgeHandler;
import org.openhab.binding.smartthings.internal.handler.SmartthingsThingHandler;
Expand Down Expand Up @@ -62,7 +61,6 @@ public class SmartthingsHandlerFactory extends BaseThingHandlerFactory implement
private final SmartthingsAuthService authService;
private final OAuthFactory oAuthFactory;
private final SmartthingsTypeRegistry typeRegistry;
private @NonNullByDefault({}) SmartthingsDiscoveryService discoService;

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openhab.binding.smartthings.internal.dto.SmartthingsDevice;
import org.openhab.binding.smartthings.internal.dto.SmartthingsLocation;
import org.openhab.binding.smartthings.internal.dto.SmartthingsRoom;
import org.openhab.binding.smartthings.internal.dto.SmartthingsStatus;
import org.openhab.binding.smartthings.internal.type.SmartthingsException;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.slf4j.Logger;
Expand Down Expand Up @@ -251,11 +252,11 @@ public void sendCommand(String deviceId, String jsonMsg) throws SmartthingsExcep
}
}

public @Nullable JsonObject sendStatus(String deviceId, String jsonMsg) throws SmartthingsException {
public @Nullable SmartthingsStatus getStatus(String deviceId) throws SmartthingsException {
try {
String uri = baseUrl + deviceEndPoint + "/" + deviceId + "/status";

JsonObject res = doRequest(JsonObject.class, uri, jsonMsg, false);
SmartthingsStatus res = doRequest(SmartthingsStatus.class, uri, null, false);
return res;
} catch (final Exception e) {
throw new SmartthingsException("SmartthingsApi : Unable to send status", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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.smartthings.internal.dto;

import java.util.Hashtable;

/**
* Data object for Smartthings device description
*
* @author Laurent ARNAL - Initial contribution
*/

public class SmartthingsStatus {
public Hashtable<String, SmartthingsStatusComponent> components;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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.smartthings.internal.dto;

import java.util.Hashtable;

/**
* Data object for Smartthings device description
*
* @author Laurent ARNAL - Initial contribution
*/

public class SmartthingsStatusCapabilities extends Hashtable<String, SmartthingsStatusProperties> {
private static final long serialVersionUID = -4719613645562518233L;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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.smartthings.internal.dto;

import java.util.Hashtable;

/**
* Data object for Smartthings device description
*
* @author Laurent ARNAL - Initial contribution
*/

public class SmartthingsStatusComponent extends Hashtable<String, SmartthingsStatusCapabilities> {
private static final long serialVersionUID = -4719613645562518232L;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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.smartthings.internal.dto;

/**
* Data object for Smartthings device description
*
* @author Laurent ARNAL - Initial contribution
*/

public class SmartthingsStatusProperties {
public Object value;
public String timestamp;
public String unit;
public Object data;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.smartthings.internal.SmartthingsBindingConstants;
import org.openhab.binding.smartthings.internal.api.SmartthingsApi;
import org.openhab.binding.smartthings.internal.converter.SmartthingsConverter;
import org.openhab.binding.smartthings.internal.dto.SmartthingsStateData;
import org.openhab.binding.smartthings.internal.dto.SmartthingsStatus;
import org.openhab.binding.smartthings.internal.dto.SmartthingsStatusCapabilities;
import org.openhab.binding.smartthings.internal.dto.SmartthingsStatusComponent;
import org.openhab.binding.smartthings.internal.dto.SmartthingsStatusProperties;
import org.openhab.binding.smartthings.internal.type.SmartthingsException;
import org.openhab.core.config.core.status.ConfigStatusMessage;
import org.openhab.core.library.types.OnOffType;
Expand All @@ -40,13 +45,9 @@
import org.openhab.core.thing.binding.ConfigStatusThingHandler;
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.JsonObject;

/**
* @author Bob Raker - Initial contribution
*/
Expand Down Expand Up @@ -93,40 +94,14 @@ public void handleCommand(ChannelUID channelUID, Command command) {

String jsonMsg = "";
if (command instanceof RefreshType) {
SmartthingsApi api = cloudBridge.getSmartthingsApi();
Map<String, String> properties = this.getThing().getProperties();
String deviceId = properties.get("deviceId");

if (deviceId != null) {
try {
JsonObject res = api.sendStatus(deviceId, jsonMsg);
if (res != null) {
JsonObject cp = res.get("components").getAsJsonObject();
JsonObject main = cp.get("main").getAsJsonObject();
JsonObject sw = main.get("switch").getAsJsonObject();
JsonObject sw2 = sw.get("switch").getAsJsonObject();
String value = sw2.get("value").getAsString();
if (("on".equals(value))) {
updateState(channelUID, OnOffType.ON);
} else {
updateState(channelUID, OnOffType.OFF);
}

logger.trace("");
}
} catch (SmartthingsException ex) {
logger.error("Unable to update device : {}", deviceId);
}
}
refreshDevice();
} else {
// @todo : review this
if (converter != null) {
jsonMsg = converter.convertToSmartthings(channelUID, command);
}
}

// try {

if (command instanceof OnOffType) {
OnOffType onOff = (OnOffType) command;
String val = onOff.toString().toLowerCase();
Expand Down Expand Up @@ -173,39 +148,65 @@ else if (command instanceof PercentType) {
}
}

/**
* State messages sent from the hub arrive here, are processed and the openHab state is updated.
*
* @param stateData
*/
public void handleStateMessage(SmartthingsStateData stateData) {
// First locate the channel
Channel matchingChannel = null;
for (Channel ch : thing.getChannels()) {
if (ch.getUID().getAsString().endsWith(stateData.capabilityAttribute)) {
matchingChannel = ch;
break;
}
}
if (matchingChannel == null) {
return;
}
public void refreshDevice() {
Bridge bridge = getBridge();

SmartthingsConverter converter = converters.get(matchingChannel.getUID());
SmartthingsCloudBridgeHandler cloudBridge = (SmartthingsCloudBridgeHandler) bridge.getHandler();
SmartthingsApi api = cloudBridge.getSmartthingsApi();
Map<String, String> properties = this.getThing().getProperties();

String deviceId = properties.get("deviceId");

if (deviceId != null) {
try {
SmartthingsStatus status = api.getStatus(deviceId);

List<Channel> channels = thing.getChannels();

if (status != null) {

if (status.components.containsKey("main")) {
SmartthingsStatusComponent component = status.components.get("main");

for (String capaKey : component.keySet()) {
SmartthingsStatusCapabilities capa = component.get(capaKey);

for (String propertyKey : capa.keySet()) {
SmartthingsStatusProperties props = capa.get(propertyKey);
Object value = props.value;
String timestamp = props.timestamp;

String channelName = (StringUtils
.join(StringUtils.splitByCharacterTypeCamelCase(propertyKey), '-') + "-channel")
.toLowerCase();

ChannelUID chanUid = new ChannelUID(this.getThing().getUID(), "light_default",
channelName);
Channel chan = thing.getChannel(chanUid);

if (chan != null) {
if (propertyKey.equals("switch")) {
if (("on".equals(value))) {
updateState(chanUid, OnOffType.ON);
} else {
updateState(chanUid, OnOffType.OFF);
}
}
if (propertyKey.equals("level")) {
updateState(chanUid, new PercentType(((Double) value).intValue()));
}
}
logger.info("");
}

// If value from Smartthings is null then stop here
State state;
if (converter != null) {
if (stateData.value != null) {
state = converter.convertToOpenHab(matchingChannel.getAcceptedItemType(), stateData);
} else {
state = UnDefType.NULL;
}
}
}
} catch (SmartthingsException ex) {
logger.error("Unable to update device : {}", deviceId);
}

updateState(matchingChannel.getUID(), state);
logger.trace("Smartthings updated State for channel: {} to {}", matchingChannel.getUID().getAsString(),
state.toString());
}

}

@Override
Expand Down Expand Up @@ -239,6 +240,8 @@ public void initialize() {
}
}

refreshDevice();

pollingJob = scheduler.scheduleWithFixedDelay(this::pollingCode, 0, 5, TimeUnit.SECONDS);

updateStatus(ThingStatus.ONLINE);
Expand Down Expand Up @@ -307,14 +310,6 @@ private void pollingCode() {
}

private boolean validateConfig(SmartthingsThingConfig config) {
/*
* String name = config.smartthingsName;
* if (name.isEmpty()) {
* updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
* "Smartthings device name is missing");
* return false;
* }
*/
return true;
}

Expand Down

0 comments on commit 3f6a06a

Please sign in to comment.