Skip to content

Commit

Permalink
[wink] Added the ability to configure the binding through paperUI as …
Browse files Browse the repository at this point in the history
…well as… (openhab#9)

* Added the ability to configure the binding through paperUI as well as services config file

* dealt with issues found in static analysis

* try to deal with slow connections from authentication service

* Update binding.xml
  • Loading branch information
shawncrosby authored and sebmarchand committed Aug 13, 2017
1 parent ea45eb3 commit a67e98e
Show file tree
Hide file tree
Showing 17 changed files with 118 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,38 @@
<name>Wink Binding</name>
<description>This is the OpenHab binding for the Wink Hub.</description>
<author>Shawn Crosby and Sebastien Marchand</author>

<service-id>org.openhab.wink</service-id>

<config-description>
<parameter name="client_id" type="text">
<label>Wink App ID</label>
<description>ID obtained from developer.wink.com</description>
<required>false</required>
</parameter>
<parameter name="client_secret" type="text">
<label>Wink App Secret</label>
<description>Secret for App obtained from developer.wink.com</description>
<required>false</required>
</parameter>
<parameter name="refresh_token" type="text">
<label>App Refresh Token</label>
<description>Valid refresh token generated from authentication with client_id and client_secret</description>
<required>false</required>
</parameter>
<parameter name="auth_service" type="text">
<label>Auth Service</label>
<description>Set this to 'delegated' if you want to use a hosted service to manage wink tokens at https://openhab-authservice.herokuapp.com rather than getting your own wink api application id</description>
<options>
<option value="delegated">Delegated</option>
</options>
<required>false</required>
</parameter>
<parameter name="auth_service_token" type="text">
<label>Auth Service Token</label>
<description>Token provided by the auth service that identifies you to the auth service</description>
<required>false</required>
</parameter>
</config-description>

</binding:binding>
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ thing-type.wink.sample.description = <Your localized Thing description>

# channel types
channel-type.wink.sample-channel.label = <Your localized Channel label>
channel-type.wink.sample-channel.description = <Your localized Channel description>
channel-type.wink.sample-channel.description = <Your localized Channel description>
11 changes: 6 additions & 5 deletions addons/binding/org.openhab.binding.wink/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ Bundle-Name: Wink Binding
Bundle-SymbolicName: org.openhab.binding.wink;singleton:=true
Bundle-Vendor: openHAB
Bundle-Version: 2.2.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ClassPath: .,
lib/pubnub-gson-4.4.3-all.jar
Import-Package:
com.google.common.base;version="10.0.1",
com.google.common.base,
com.google.common.collect,
javax.ws.rs.client,
javax.ws.rs.core,
org.apache.commons.lang,
org.apache.commons.logging;version="1.1.1",
org.apache.commons.logging,
org.eclipse.smarthome.config.core,
org.eclipse.smarthome.config.discovery,
org.eclipse.smarthome.core.library.types,
Expand All @@ -25,10 +25,11 @@ Import-Package:
org.glassfish.jersey.client,
org.glassfish.jersey.media.sse,
org.openhab.binding.wink,
org.openhab.binding.wink.client,
org.openhab.binding.wink.handler,
org.osgi.framework,
org.osgi.service.cm;version="1.5.0",
org.osgi.service.component;version="1.2.2",
org.osgi.service.cm,
org.osgi.service.component,
org.slf4j
Service-Component: OSGI-INF/*.xml
Export-Package: org.openhab.binding.wink,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0"
immediate="true"
activate="activate"
deactivate="deactivate"
name="org.openhab.wink.AuthenticationConfigurationService">
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" immediate="true" name="org.openhab.wink.AuthenticationConfigurationService">

<implementation class="org.openhab.binding.wink.internal.AuthenticationConfigurationService"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public class WinkBindingConstants {
public static final String BINDING_ID = "wink";

// List of all Thing Type UIDs
public final static ThingTypeUID THING_TYPE_WINK_HUB_2 = new ThingTypeUID(BINDING_ID, "wink_hub_2");
public final static ThingTypeUID THING_TYPE_LIGHT_BULB = new ThingTypeUID(BINDING_ID, "light_bulb");
public final static ThingTypeUID THING_TYPE_REMOTE = new ThingTypeUID(BINDING_ID, "remote");
public final static ThingTypeUID THING_TYPE_BINARY_SWITCH = new ThingTypeUID(BINDING_ID, "binary_switch");
public final static ThingTypeUID THING_TYPE_LOCK = new ThingTypeUID(BINDING_ID, "lock");
public static final ThingTypeUID THING_TYPE_WINK_HUB_2 = new ThingTypeUID(BINDING_ID, "wink_hub_2");
public static final ThingTypeUID THING_TYPE_LIGHT_BULB = new ThingTypeUID(BINDING_ID, "light_bulb");
public static final ThingTypeUID THING_TYPE_REMOTE = new ThingTypeUID(BINDING_ID, "remote");
public static final ThingTypeUID THING_TYPE_BINARY_SWITCH = new ThingTypeUID(BINDING_ID, "binary_switch");
public static final ThingTypeUID THING_TYPE_LOCK = new ThingTypeUID(BINDING_ID, "lock");

// List of all Channel ids for a light bulb
public final static String CHANNEL_LIGHTLEVEL = "lightlevel";
public final static String CHANNEL_LIGHTSTATE = "lightstate";
public final static String CHANNEL_SWITCHSTATE = "switchstate";
public final static String CHANNEL_LOCKSTATE = "lockstate";
public static final String CHANNEL_LIGHTLEVEL = "lightlevel";
public static final String CHANNEL_LIGHTSTATE = "lightstate";
public static final String CHANNEL_SWITCHSTATE = "switchstate";
public static final String CHANNEL_LOCKSTATE = "lockstate";

// REST URI constants
public static final String WINK_URI = "https://api.wink.com/";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.wink.client;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ public IWinkDevice updateDeviceState(IWinkDevice device, Map<String, String> upd
Client winkClient = ClientBuilder.newClient();
WebTarget target = winkClient.target(WINK_URI)
.path(device.getDeviceType().getPath() + "/" + device.getId() + "/desired_state");
String desired_state = new Gson().toJson(updatedState);
String wrapper = "{\"desired_state\":" + desired_state + "}";
String desiredState = new Gson().toJson(updatedState);
String wrapper = "{\"desired_state\":" + desiredState + "}";
JsonElement jsonResult = executePut(target, wrapper);

IWinkDevice ret = new JsonWinkDevice(jsonResult.getAsJsonObject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -47,14 +49,19 @@ public class DelegatedAuthenticationService implements IWinkAuthenticationServic

public DelegatedAuthenticationService(String auth_token) {
this.auth_token = auth_token;
Client client = ClientBuilder.newClient();
ClientConfig configuration = new ClientConfig();
configuration = configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000 * 15);
configuration = configuration.property(ClientProperties.READ_TIMEOUT, 1000 * 15);
Client client = ClientBuilder.newBuilder().withConfig(configuration).build();
WebTarget target = client.target(DELEGATED_AUTH_SERVICE);
WebTarget tokenPath = target.path("/token");
Response response = tokenPath.request(MediaType.APPLICATION_JSON_TYPE)
.header("Authorization", "Token " + this.auth_token).get();
JsonElement json = getResultAsJson(response);
logger.debug("Access Token Response: {}", json);
token = json.getAsJsonObject().get("access_token").getAsString();
if (json.getAsJsonObject().get("access_token") != null) {
token = json.getAsJsonObject().get("access_token").getAsString();
}
client.close();
}

Expand All @@ -73,7 +80,10 @@ public String refreshToken() throws AuthenticationException {
.header("Authorization", "Token " + this.auth_token).get();
client.close();
JsonElement json = getResultAsJson(response);
token = json.getAsJsonObject().get("access_token").getAsString();

if (json.getAsJsonObject().get("access_token") != null) {
token = json.getAsJsonObject().get("access_token").getAsString();
}

return token;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ public class WinkAuthenticationService {
*
* @return
*/
public static IWinkAuthenticationService getInstance() {
public static synchronized IWinkAuthenticationService getInstance() {
if (instance == null) {
synchronized (WinkAuthenticationService.class) {
if (instance == null) {
instance = new DummyService();
}
if (instance == null) {
instance = new DummyService();
}
}
return instance;
Expand All @@ -38,21 +36,19 @@ public static IWinkAuthenticationService getInstance() {
*
* @param service
*/
public static void setInstance(IWinkAuthenticationService service) {
public static synchronized void setInstance(IWinkAuthenticationService service) {
instance = service;
}

private static class DummyService implements IWinkAuthenticationService {

@Override
public String getAuthToken() {
// TODO Auto-generated method stub
return null;
}

@Override
public String refreshToken() throws AuthenticationException {
// TODO Auto-generated method stub
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class WinkClient {
*
* @return
*/
public static IWinkClient getInstance() {
public static synchronized IWinkClient getInstance() {
if (instance == null) {
instance = new CloudRestfulWinkClient();
}
Expand All @@ -35,7 +35,7 @@ public static IWinkClient getInstance() {
*
* @param testClient
*/
public static void setInstance(IWinkClient testClient) {
public static synchronized void setInstance(IWinkClient testClient) {
instance = testClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.slf4j.LoggerFactory;

/**
* TODO: The {@link BinarySwitchHandler} is responsible for handling commands, which are
* The {@link BinarySwitchHandler} is responsible for handling commands, which are
* sent to one of the channels.
*
* @author Scott Hanson - Initial contribution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.slf4j.LoggerFactory;

/**
* TODO: The {@link LightBulbHandler} is responsible for handling commands, which are
* The {@link LightBulbHandler} is responsible for handling commands, which are
* sent to one of the channels.
*
* @author Sebastien Marchand - Initial contribution
Expand Down Expand Up @@ -80,18 +80,18 @@ protected WinkSupportedDevice getDeviceType() {
@Override
protected void updateDeviceState(IWinkDevice device) {

final String desired_brightness = device.getDesiredState().get("brightness");
final String current_brightness = device.getCurrentState().get("brightness");
if (desired_brightness != null && desired_brightness.equals(current_brightness)) {
Float brightness = Float.valueOf(current_brightness) * 100;
final String desiredBrightness = device.getDesiredState().get("brightness");
final String currentBrightness = device.getCurrentState().get("brightness");
if (desiredBrightness != null && desiredBrightness.equals(currentBrightness)) {
Float brightness = Float.valueOf(currentBrightness) * 100;
logger.debug("New brightness state: {}", brightness);
updateState(CHANNEL_LIGHTLEVEL, new PercentType(brightness.intValue()));
}
final String desired_power_state = device.getDesiredState().get("powered");
final String current_power_state = device.getCurrentState().get("powered");
final String desiredPowerState = device.getDesiredState().get("powered");
final String currentPowerState = device.getCurrentState().get("powered");

if (desired_power_state == null || desired_power_state.equals(current_power_state)) {
if (current_power_state.equals("true")) {
if (desiredPowerState == null || desiredPowerState.equals(currentPowerState)) {
if (currentPowerState.equals("true")) {
logger.debug("New Light State: ON");
updateState(CHANNEL_LIGHTSTATE, OnOffType.ON);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public RemoteHandler(Thing thing) {

@Override
public void handleWinkCommand(ChannelUID channelUID, Command command) {
// TODO
}

@Override
Expand All @@ -37,7 +36,6 @@ protected WinkSupportedDevice getDeviceType() {

@Override
protected void updateDeviceState(IWinkDevice device) {
// TODO Auto-generated method stub

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.wink.handler;

import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.wink.handler;

import java.util.HashMap;
Expand Down Expand Up @@ -42,7 +50,6 @@ public void initialize() {
this.scheduler.schedule(new Runnable() {
@Override
public void run() {
// TODO
// connect();
}
}, 0, TimeUnit.SECONDS);
Expand All @@ -51,7 +58,6 @@ public void run() {

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
// TODO Auto-generated method stub
logger.debug("Bridge Handler doesn't supporte any commands");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Copyright (c) 2014-2016 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.wink.internal;

import java.util.Dictionary;
Expand Down Expand Up @@ -33,10 +41,10 @@ public void updated(Dictionary<String, ?> properties) throws ConfigurationExcept

private void configure(Dictionary<String, ?> properties) {
IWinkAuthenticationService service = null;
String auth_service = (String) properties.get("auth_service");
logger.debug("Auth Service: {}", auth_service);
String authService = (String) properties.get("auth_service");
logger.debug("Auth Service: {}", authService);

if ("delegated".equals(auth_service)) {
if ("delegated".equals(authService)) {
String token = (String) properties.get("auth_service_token");
logger.debug("Configuring Delegated Wink Authentication Service on Heroku with token {}", token);
service = new DelegatedAuthenticationService(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public class WinkHandlerFactory extends BaseThingHandlerFactory {

private Logger logger = LoggerFactory.getLogger(WinkHandlerFactory.class);

public final static Set<ThingTypeUID> DISCOVERABLE_DEVICE_TYPES_UIDS = ImmutableSet.of(THING_TYPE_LIGHT_BULB,
public static final Set<ThingTypeUID> DISCOVERABLE_DEVICE_TYPES_UIDS = ImmutableSet.of(THING_TYPE_LIGHT_BULB,
THING_TYPE_REMOTE, THING_TYPE_BINARY_SWITCH, THING_TYPE_LOCK);

private final static Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = ImmutableSet.of(THING_TYPE_WINK_HUB_2);
private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = ImmutableSet.of(THING_TYPE_WINK_HUB_2);

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
Expand Down

0 comments on commit a67e98e

Please sign in to comment.