Skip to content

Commit

Permalink
More review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Hilbrand Bouwkamp <[email protected]>
  • Loading branch information
Hilbrand committed Nov 27, 2019
1 parent d25a6bd commit d24b8bf
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public InnogyWebSocket(EventListener eventListener, URI webSocketURI, int maxIdl
*/
public synchronized void start() throws Exception {
final SslContextFactory sslContextFactory = new SslContextFactory();
// sslContextFactory.setTrustAll(true); // The magic

if (client == null || client.isStopped()) {
client = new WebSocketClient(sslContextFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -41,7 +40,7 @@
import org.eclipse.smarthome.core.auth.client.oauth2.OAuthResponseException;
import org.openhab.binding.innogysmarthome.internal.client.entity.StatusResponse;
import org.openhab.binding.innogysmarthome.internal.client.entity.action.Action;
import org.openhab.binding.innogysmarthome.internal.client.entity.action.SetStateAction;
import org.openhab.binding.innogysmarthome.internal.client.entity.action.StateActionSetter;
import org.openhab.binding.innogysmarthome.internal.client.entity.capability.Capability;
import org.openhab.binding.innogysmarthome.internal.client.entity.capability.CapabilityState;
import org.openhab.binding.innogysmarthome.internal.client.entity.device.Device;
Expand Down Expand Up @@ -117,26 +116,43 @@ public InnogyClient(final OAuthClientService oAuthService, final HttpClient http
*/
public void refreshStatus() throws IOException, ApiException, AuthenticationException {
logger.debug("Get innogy SmartHome status...");
final ContentResponse response = executeGet(API_URL_STATUS);
final StatusResponse status = executeGet(API_URL_STATUS, StatusResponse.class);

final StatusResponse status = gson.fromJson(response.getContentAsString(), StatusResponse.class);
bridgeDetails = status.gateway;
configVersion = bridgeDetails.getConfigVersion();

logger.debug("innogy SmartHome Status loaded. Configuration version is {}.", configVersion);
}

/**
* Executes a HTTP GET request with default headers.
* Executes a HTTP GET request with default headers and returns data as object of type T.
*
* @param url
* @param clazz type of data to return
* @return
* @throws IOException
* @throws AuthenticationException
* @throws ApiException
*/
private ContentResponse executeGet(final String url) throws IOException, AuthenticationException, ApiException {
return request(httpClient.newRequest(url).method(HttpMethod.GET));
private <T> T executeGet(final String url, final Class<T> clazz)
throws IOException, AuthenticationException, ApiException {
final ContentResponse response = request(httpClient.newRequest(url).method(HttpMethod.GET));

return gson.fromJson(response.getContentAsString(), clazz);
}

/**
* Executes a HTTP GET request with default headers and returns data as List of type T.
*
* @param url
* @param clazz array type of data to return as list
* @throws IOException
* @throws AuthenticationException
* @throws ApiException
*/
private <T> List<T> executeGetList(final String url, final Class<T[]> clazz)
throws IOException, AuthenticationException, ApiException {
return executeGetList(url, clazz);
}

/**
Expand Down Expand Up @@ -260,7 +276,7 @@ private void handleResponseErrors(final ContentResponse response, final URI uri)
*/
public void setSwitchActuatorState(final String capabilityId, final boolean state)
throws IOException, ApiException, AuthenticationException {
executePost(API_URL_ACTION, new SetStateAction(capabilityId, Capability.TYPE_SWITCHACTUATOR, state));
executePost(API_URL_ACTION, new StateActionSetter(capabilityId, Capability.TYPE_SWITCHACTUATOR, state));
}

/**
Expand All @@ -273,7 +289,7 @@ public void setSwitchActuatorState(final String capabilityId, final boolean stat
*/
public void setDimmerActuatorState(final String capabilityId, final int dimLevel)
throws IOException, ApiException, AuthenticationException {
executePost(API_URL_ACTION, new SetStateAction(capabilityId, Capability.TYPE_DIMMERACTUATOR, dimLevel));
executePost(API_URL_ACTION, new StateActionSetter(capabilityId, Capability.TYPE_DIMMERACTUATOR, dimLevel));
}

/**
Expand All @@ -287,7 +303,7 @@ public void setDimmerActuatorState(final String capabilityId, final int dimLevel
public void setRollerShutterActuatorState(final String capabilityId, final int rollerShutterLevel)
throws IOException, ApiException, AuthenticationException {
executePost(API_URL_ACTION,
new SetStateAction(capabilityId, Capability.TYPE_ROLLERSHUTTERACTUATOR, rollerShutterLevel));
new StateActionSetter(capabilityId, Capability.TYPE_ROLLERSHUTTERACTUATOR, rollerShutterLevel));
}

/**
Expand All @@ -300,7 +316,7 @@ public void setRollerShutterActuatorState(final String capabilityId, final int r
*/
public void setVariableActuatorState(final String capabilityId, final boolean state)
throws IOException, ApiException, AuthenticationException {
executePost(API_URL_ACTION, new SetStateAction(capabilityId, Capability.TYPE_VARIABLEACTUATOR, state));
executePost(API_URL_ACTION, new StateActionSetter(capabilityId, Capability.TYPE_VARIABLEACTUATOR, state));
}

/**
Expand All @@ -314,7 +330,7 @@ public void setVariableActuatorState(final String capabilityId, final boolean st
public void setPointTemperatureState(final String capabilityId, final double pointTemperature)
throws IOException, ApiException, AuthenticationException {
executePost(API_URL_ACTION,
new SetStateAction(capabilityId, Capability.TYPE_THERMOSTATACTUATOR, pointTemperature));
new StateActionSetter(capabilityId, Capability.TYPE_THERMOSTATACTUATOR, pointTemperature));
}

/**
Expand All @@ -328,7 +344,7 @@ public void setPointTemperatureState(final String capabilityId, final double poi
public void setOperationMode(final String capabilityId, final boolean autoMode)
throws IOException, ApiException, AuthenticationException {
executePost(API_URL_ACTION,
new SetStateAction(capabilityId, Capability.TYPE_THERMOSTATACTUATOR,
new StateActionSetter(capabilityId, Capability.TYPE_THERMOSTATACTUATOR,
autoMode ? CapabilityState.STATE_VALUE_OPERATION_MODE_AUTO
: CapabilityState.STATE_VALUE_OPERATION_MODE_MANUAL));
}
Expand All @@ -343,7 +359,7 @@ public void setOperationMode(final String capabilityId, final boolean autoMode)
*/
public void setAlarmActuatorState(final String capabilityId, final boolean alarmState)
throws IOException, ApiException, AuthenticationException {
executePost(API_URL_ACTION, new SetStateAction(capabilityId, Capability.TYPE_ALARMACTUATOR, alarmState));
executePost(API_URL_ACTION, new StateActionSetter(capabilityId, Capability.TYPE_ALARMACTUATOR, alarmState));
}

/**
Expand All @@ -355,9 +371,7 @@ public void setAlarmActuatorState(final String capabilityId, final boolean alarm
*/
public List<Device> getDevices() throws IOException, ApiException, AuthenticationException {
logger.debug("Loading innogy devices...");
final ContentResponse response = executeGet(API_URL_DEVICE);

return Arrays.asList(gson.fromJson(response.getContentAsString(), Device[].class));
return executeGetList(API_URL_DEVICE, Device[].class);
}

/**
Expand All @@ -370,9 +384,7 @@ public List<Device> getDevices() throws IOException, ApiException, Authenticatio
*/
public Device getDeviceById(final String deviceId) throws IOException, ApiException, AuthenticationException {
logger.debug("Loading device with id {}...", deviceId);
final ContentResponse response = executeGet(API_URL_DEVICE_ID.replace("{id}", deviceId));

return gson.fromJson(response.getContentAsString(), Device.class);
return executeGet(API_URL_DEVICE_ID.replace("{id}", deviceId), Device.class);
}

/**
Expand Down Expand Up @@ -507,18 +519,16 @@ public Device getFullDeviceById(final String deviceId) throws IOException, ApiEx
deviceState.setId(deviceId);
deviceState.setState(state);

// deviceState.setStateList(deviceStateList);

// MESSAGES
final List<Message> messageList = getMessages();

final List<Message> ml = new ArrayList<>();
final String deviceIdPath = "/device/" + deviceId;

for (final Message m : messageList) {
logger.trace("Message Type {} with ID {}", m.getType(), m.getId());
if (m.getDeviceLinkList() != null && !m.getDeviceLinkList().isEmpty()) {
for (final String li : m.getDeviceLinkList()) {
if (li.equals("/device/" + deviceId)) {
if (deviceIdPath.equals(li)) {
ml.add(m);
}
}
Expand Down Expand Up @@ -574,9 +584,7 @@ public Device getFullDeviceById(final String deviceId) throws IOException, ApiEx
*/
public List<DeviceState> getDeviceStates() throws IOException, ApiException, AuthenticationException {
logger.debug("Loading device states...");
final ContentResponse response = executeGet(API_URL_DEVICE_STATES);

return Arrays.asList(gson.fromJson(response.getContentAsString(), DeviceState[].class));
return executeGetList(API_URL_DEVICE_STATES, DeviceState[].class);
}

/**
Expand All @@ -590,9 +598,7 @@ public List<DeviceState> getDeviceStates() throws IOException, ApiException, Aut
public State getDeviceStateByDeviceId(final String deviceId)
throws IOException, ApiException, AuthenticationException {
logger.debug("Loading device states for device id {}...", deviceId);
final ContentResponse response = executeGet(API_URL_DEVICE_ID_STATE.replace("{id}", deviceId));

return gson.fromJson(response.getContentAsString(), State.class);
return executeGet(API_URL_DEVICE_ID_STATE.replace("{id}", deviceId), State.class);
}

/**
Expand All @@ -604,9 +610,7 @@ public State getDeviceStateByDeviceId(final String deviceId)
*/
public List<Location> getLocations() throws IOException, ApiException, AuthenticationException {
logger.debug("Loading locations...");
final ContentResponse response = executeGet(API_URL_LOCATION);

return Arrays.asList(gson.fromJson(response.getContentAsString(), Location[].class));
return executeGetList(API_URL_LOCATION, Location[].class);
}

/**
Expand All @@ -620,9 +624,7 @@ public List<Location> getLocations() throws IOException, ApiException, Authentic
public List<Capability> getCapabilitiesForDevice(final String deviceId)
throws IOException, ApiException, AuthenticationException {
logger.debug("Loading capabilities for device {}...", deviceId);
final ContentResponse response = executeGet(API_URL_DEVICE_CAPABILITIES.replace("{id}", deviceId));

return Arrays.asList(gson.fromJson(response.getContentAsString(), Capability[].class));
return executeGetList(API_URL_DEVICE_CAPABILITIES.replace("{id}", deviceId), Capability[].class);
}

/**
Expand All @@ -634,9 +636,7 @@ public List<Capability> getCapabilitiesForDevice(final String deviceId)
*/
public List<Capability> getCapabilities() throws IOException, ApiException, AuthenticationException {
logger.debug("Loading capabilities...");
final ContentResponse response = executeGet(API_URL_CAPABILITY);

return Arrays.asList(gson.fromJson(response.getContentAsString(), Capability[].class));
return executeGetList(API_URL_CAPABILITY, Capability[].class);
}

/**
Expand All @@ -648,9 +648,7 @@ public List<Capability> getCapabilities() throws IOException, ApiException, Auth
*/
public List<CapabilityState> getCapabilityStates() throws IOException, ApiException, AuthenticationException {
logger.debug("Loading capability states...");
final ContentResponse response = executeGet(API_URL_CAPABILITY_STATES);

return Arrays.asList(gson.fromJson(response.getContentAsString(), CapabilityState[].class));
return executeGetList(API_URL_CAPABILITY_STATES, CapabilityState[].class);
}

/**
Expand All @@ -662,9 +660,7 @@ public List<CapabilityState> getCapabilityStates() throws IOException, ApiExcept
*/
public List<Message> getMessages() throws IOException, ApiException, AuthenticationException {
logger.debug("Loading messages...");
final ContentResponse response = executeGet(API_URL_MESSAGE);

return Arrays.asList(gson.fromJson(response.getContentAsString(), Message[].class));
return executeGetList(API_URL_MESSAGE, Message[].class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
*
* @author Oliver Kuhl - Initial contribution
*/
public class SetStateAction extends Action {
public class StateActionSetter extends Action {

private static final String CONSTANT = "Constant";

/**
* Constructs a new {@link SetStateAction}.
* Constructs a new {@link StateActionSetter}.
*
* @param capabilityId String of the 32 character capability id
* @param capabilityType the type of the {@link Capability}, {@link Capability#TYPE_SWITCHACTUATOR} or
* {@link Capability#TYPE_VARIABLEACTUATOR}
* @param state the new state as boolean (true=on, false=off)
*/
public SetStateAction(String capabilityId, String capabilityType, boolean state) {
public StateActionSetter(String capabilityId, String capabilityType, boolean state) {
setType(ACTION_TYPE_SETSTATE);
setTargetCapabilityById(capabilityId);
final ActionParams params = new ActionParams();
Expand All @@ -47,13 +47,13 @@ public SetStateAction(String capabilityId, String capabilityType, boolean state)
}

/**
* Constructs a new {@link SetStateAction}.
* Constructs a new {@link StateActionSetter}.
*
* @param capabilityId String of the 32 character capability id
* @param capabilityType the type of the {@link Capability}, {@link Capability#TYPE_THERMOSTATACTUATOR}
* @param newValue the new double value
*/
public SetStateAction(String capabilityId, String capabilityType, double newValue) {
public StateActionSetter(String capabilityId, String capabilityType, double newValue) {
setType(ACTION_TYPE_SETSTATE);
setTargetCapabilityById(capabilityId);
final ActionParams params = new ActionParams();
Expand All @@ -65,13 +65,13 @@ public SetStateAction(String capabilityId, String capabilityType, double newValu
}

/**
* Constructs a new {@link SetStateAction}.
* Constructs a new {@link StateActionSetter}.
*
* @param capabilityId String of the 32 character capability id
* @param capabilityType the type of the {@link Capability}, {@link Capability#TYPE_DIMMERACTUATOR}
* @param newValue the new int value
*/
public SetStateAction(String capabilityId, String capabilityType, int newValue) {
public StateActionSetter(String capabilityId, String capabilityType, int newValue) {
setType(ACTION_TYPE_SETSTATE);
setTargetCapabilityById(capabilityId);
final ActionParams params = new ActionParams();
Expand All @@ -86,13 +86,13 @@ public SetStateAction(String capabilityId, String capabilityType, int newValue)
}

/**
* Constructs a new {@link SetStateAction}.
* Constructs a new {@link StateActionSetter}.
*
* @param capabilityId String of the 32 character capability id
* @param capabilityType the type of the {@link Capability}, {@link Capability#TYPE_THERMOSTATACTUATOR}
* @param newValue the new string value
*/
public SetStateAction(String capabilityId, String capabilityType, String newValue) {
public StateActionSetter(String capabilityId, String capabilityType, String newValue) {
setType(ACTION_TYPE_SETSTATE);
setTargetCapabilityById(capabilityId);
final ActionParams params = new ActionParams();
Expand Down
Loading

0 comments on commit d24b8bf

Please sign in to comment.