Skip to content

Commit

Permalink
[siemensrds] null annotations; JUnit; UoM; enhancements; bug; refacto…
Browse files Browse the repository at this point in the history
…ring; logging (openhab#7769)

* [siemensrds] null annotations; JUnit; U-o-M; enhancements; bug; refactoring; logging

Signed-off-by: Andrew Fiddian-Green <[email protected]>
Signed-off-by: CSchlipp <[email protected]>
  • Loading branch information
andrewfg authored and CSchlipp committed Jul 26, 2020
1 parent 2586f70 commit 8452247
Show file tree
Hide file tree
Showing 27 changed files with 5,033 additions and 673 deletions.
1 change: 1 addition & 0 deletions bundles/org.openhab.binding.siemensrds/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@

import javax.net.ssl.HttpsURLConnection;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;

/**
Expand All @@ -44,27 +44,27 @@
* @author Andrew Fiddian-Green - Initial contribution
*
*/
class RdsAccessToken {
@NonNullByDefault
public class RdsAccessToken {

/*
* NOTE: requires a static logger because the class has static methods
*/
protected static final Logger LOGGER = LoggerFactory.getLogger(RdsAccessToken.class);
protected final Logger logger = LoggerFactory.getLogger(RdsAccessToken.class);

private static final Gson GSON = new Gson();

@SerializedName("access_token")
private String accessToken;
private @Nullable String accessToken;
@SerializedName(".expires")
private String expires;
private @Nullable String expires;

private Date expDate = null;
private @Nullable Date expDate = null;

/*
* private method: execute the HTTP POST on the server
* public static method: execute the HTTP POST on the server
*/
private static String httpGetTokenJson(String apiKey, String user, String password)
throws RdsCloudException, IOException {
public static String httpGetTokenJson(String apiKey, String payload) throws RdsCloudException, IOException {
/*
* NOTE: this class uses JAVAX HttpsURLConnection library instead of the
* preferred JETTY library; the reason is that JETTY does not allow sending the
Expand All @@ -85,12 +85,12 @@ private static String httpGetTokenJson(String apiKey, String user, String passwo

try (OutputStream outputStream = https.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream)) {
dataOutputStream.writeBytes(String.format(TOKEN_REQUEST, user, password));
dataOutputStream.writeBytes(payload);
dataOutputStream.flush();
}

if (https.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new RdsCloudException("invalid HTTP response");
throw new IOException(https.getResponseMessage());
}

try (InputStream inputStream = https.getInputStream();
Expand All @@ -106,35 +106,33 @@ private static String httpGetTokenJson(String apiKey, String user, String passwo
}

/*
* public method: execute a POST on the cloud server, parse the JSON, and create
* a class that encapsulates the data
* public method: parse the JSON, and create a class that encapsulates the data
*/
public static @Nullable RdsAccessToken create(String apiKey, String user, String password) {
try {
String json = httpGetTokenJson(apiKey, user, password);
return GSON.fromJson(json, RdsAccessToken.class);
} catch (JsonSyntaxException | RdsCloudException | IOException e) {
LOGGER.warn("create {}: \"{}\"", e.getClass().getName(), e.getMessage());
return null;
}
public static @Nullable RdsAccessToken createFromJson(String json) {
return GSON.fromJson(json, RdsAccessToken.class);
}

/*
* public method: return the access token
*/
public String getToken() {
return accessToken;
public String getToken() throws RdsCloudException {
String accessToken = this.accessToken;
if (accessToken != null) {
return accessToken;
}
throw new RdsCloudException("no access token");
}

/*
* public method: check if the token has expired
*/
public Boolean isExpired() {
public boolean isExpired() {
Date expDate = this.expDate;
if (expDate == null) {
try {
expDate = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").parse(expires);
} catch (ParseException e) {
LOGGER.debug("isExpired: expiry date parsing exception");
logger.debug("isExpired: expiry date parsing exception");

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
*/
package org.openhab.binding.siemensrds.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.thing.ThingTypeUID;

/**
* The {@link RdsBindingConstants} contains the constants used by the binding
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
public class RdsBindingConstants {

/*
Expand Down Expand Up @@ -101,50 +103,54 @@ public class RdsBindingConstants {
public static final String PROP_PLANT_ID = "plantId";

/*
* ==================== USED DATA POINTS ========================== where: HIE_
* = the Hierarchy Name in the ClimatixIc server CHA_ = the Channel ID in the
* OpenHAB binding
* ==================== USED DATA POINTS ==========================
*
* where: HIE_xxx = the point class suffix part of the hierarchy name in the
* ClimatixIc server, and CHA_xxx = the Channel ID in the OpenHAB binding
*
*/

// device name
protected static final String HIE_DESCRIPTION = "R(1)'Description";
public static final String HIE_DESCRIPTION = "'Description";

// online state
public static final String HIE_ONLINE = "#Online";

// room (actual) temperature (read-only)
protected static final String CHA_ROOM_TEMP = "roomTemperature";
private static final String HIE_ROOM_TEMP = "RTemp";
public static final String HIE_ROOM_TEMP = "'RTemp";

// room relative humidity (read-only)
protected static final String CHA_ROOM_HUMIDITY = "roomHumidity";
private static final String HIE_ROOM_HUMIDITY = "RHuRel";
public static final String HIE_ROOM_HUMIDITY = "'RHuRel";

// room air quality (low/med/high) (read-only)
protected static final String CHA_ROOM_AIR_QUALITY = "roomAirQuality";
private static final String HIE_ROOM_AIR_QUALITY = "RAQualInd";
public static final String HIE_ROOM_AIR_QUALITY = "'RAQualInd";

// energy savings level (green leaf) (poor..excellent) (read-write)
// note: writing the value "5" forces the device to green leaf mode
protected static final String CHA_ENERGY_SAVINGS_LEVEL = "energySavingsLevel";
protected static final String HIE_ENERGY_SAVINGS_LEVEL = "REei";
public static final String HIE_ENERGY_SAVINGS_LEVEL = "'REei";

// outside air temperature (read-only)
protected static final String CHA_OUTSIDE_TEMP = "outsideTemperature";
private static final String HIE_OUTSIDE_TEMP = "TOa";
public static final String HIE_OUTSIDE_TEMP = "'TOa";

// set-point override (read-write)
protected static final String CHA_TARGET_TEMP = "targetTemperature";
private static final String HIE_TARGET_TEMP = "SpTR";
public static final String HIE_TARGET_TEMP = "'SpTR";

// heating/cooling state (read-only)
protected static final String CHA_OUTPUT_STATE = "thermostatOutputState";
private static final String HIE_OUTPUT_STATE = "HCSta";
public static final String HIE_OUTPUT_STATE = "'HCSta";

/*
* thermostat occupancy state (absent, present) (read-write) NOTE: uses
* different parameters as follows.. OccMod = 2, 3 to read, and command to, the
* absent, present states
*/
protected static final String CHA_STAT_OCC_MODE_PRESENT = "occupancyModePresent";
protected static final String HIE_STAT_OCC_MODE_PRESENT = "OccMod";
public static final String HIE_STAT_OCC_MODE_PRESENT = "'OccMod";

/*
* thermostat program mode (read-write) NOTE: uses different parameters as
Expand All @@ -153,16 +159,16 @@ public class RdsBindingConstants {
* to command to the auto mode
*/
protected static final String CHA_STAT_AUTO_MODE = "thermostatAutoMode";
private static final String HIE_PR_OP_MOD_RSN = "PrOpModRsn";
protected static final String HIE_STAT_CMF_BTN = "CmfBtn";
public static final String HIE_PR_OP_MOD_RSN = "'PrOpModRsn";
public static final String HIE_STAT_CMF_BTN = "'CmfBtn";

/*
* domestic hot water state (off, on) (read-write) NOTE: uses different
* parameters as follows.. DhwMod = 1, 2 to read, and command to, the off, on
* states
*/
protected static final String CHA_DHW_OUTPUT_STATE = "hotWaterOutputState";
private static final String HIE_DHW_OUTPUT_STATE = "DhwMod";
public static final String HIE_DHW_OUTPUT_STATE = "'DhwMod";

/*
* domestic hot water program mode (manual, auto) (read-write) NOTE: uses
Expand Down Expand Up @@ -215,17 +221,38 @@ public class RdsBindingConstants {
* private static final String HIE_COMFORT_BUTTON = "CmfBtn";
*
*/

/*
* logger strings
*/
public static final String LOG_HTTP_COMMAND = "{} for url {} characters long";
public static final String LOG_CONTENT_LENGTH = "{} {} characters..";
public static final String LOG_PAYLOAD_FMT = "{} {}";

public static final String LOG_HTTP_COMMAND_ABR = "{} for url {} characters long (set log level to TRACE to see full url)..";
public static final String LOG_CONTENT_LENGTH_ABR = "{} {} characters (set log level to TRACE to see full string)..";
public static final String LOG_PAYLOAD_FMT_ABR = "{} {} ...";

public static final String LOG_RECEIVED_MSG = "received";
public static final String LOG_RECEIVED_MARK = "<<";

public static final String LOG_SENDING_MSG = "sending";
public static final String LOG_SENDING_MARK = ">>";

public static final String LOG_SYSTEM_EXCEPTION = "system exception in {}, type={}, message=\"{}\"";
public static final String LOG_RUNTIME_EXCEPTION = "runtime exception in {}, type={}, message=\"{}\"";
}

/**
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
class ChannelMap {
public String channelId;
public String hierarchyName;
public String id;
public String clazz;

public ChannelMap(String channelId, String hierarchyName) {
this.channelId = channelId;
this.hierarchyName = hierarchyName;
public ChannelMap(String channelId, String pointClass) {
this.id = channelId;
this.clazz = pointClass;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
*/
package org.openhab.binding.siemensrds.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link RdsCloudConfiguration} class contains the thing configuration
* parameters for the Climatix IC cloud user account
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
public class RdsCloudConfiguration {

public String userEmail;
public String userPassword;
public String userEmail = "";
public String userPassword = "";
public int pollingInterval;
public String apiKey;
public String apiKey = "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
*/
package org.openhab.binding.siemensrds.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Custom Cloud Server communication exception
*
* @author Andrew Fiddian-Green - Initial contribution
*
*/
public class RdsCloudException extends RuntimeException {
@NonNullByDefault
public class RdsCloudException extends Exception {

private static final long serialVersionUID = -7048044632627280917L;

Expand Down
Loading

0 comments on commit 8452247

Please sign in to comment.