Skip to content

Commit

Permalink
makes the my.openhab bundle configurable (openhab#1452)
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan authored and kaikreuzer committed Nov 21, 2016
1 parent 64abc6a commit 55762cf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
23 changes: 23 additions & 0 deletions addons/io/org.openhab.io.myopenhab/ESH-INF/config/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<config-description:config-descriptions
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:config-description="http://eclipse.org/smarthome/schemas/config-description/v1.0.0"
xsi:schemaLocation="http://eclipse.org/smarthome/schemas/config-description/v1.0.0
http://eclipse.org/smarthome/schemas/config-description-1.0.0.xsd">
<config-description uri="io:myopenhab">
<parameter name="mode" type="text" required="true">
<label>Mode</label>
<description>What mode to use with the my.opehab service</description>
<options>
<option value="persistence">Persistence</option>
<option value="none">No persistence</option>
</options>
<default>persistence</default>
</parameter>
<parameter name="baseURL" type="text" required="false">
<label>Base URL for the my.openhab server</label>
<description>Base URL for the my.openhab server</description>
<default>https://my.openhab.org/</default>
</parameter>
</config-description>
</config-description:config-descriptions>
3 changes: 3 additions & 0 deletions addons/io/org.openhab.io.myopenhab/OSGI-INF/myopenhab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
<reference bind="setItemRegistry" cardinality="1..1" interface="org.eclipse.smarthome.core.items.ItemRegistry" name="ItemRegistry" policy="dynamic" unbind="unsetItemRegistry"/>
<reference bind="setEventPublisher" cardinality="0..1" interface="org.eclipse.smarthome.core.events.EventPublisher" name="EventPublisher" policy="dynamic" unbind="unsetEventPublisher"/>
<property name="service.pid" type="String" value="org.openhab.myopenhab"/>
<property name="service.config.description.uri" type="String" value="io:myopenhab"/>
<property name="service.config.label" type="String" value="my.openhab"/>
<property name="service.config.category" type="String" value="io"/>
</scr:component>
1 change: 1 addition & 0 deletions addons/io/org.openhab.io.myopenhab/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ output.. = target/classes
bin.includes = META-INF/,\
.,\
OSGI-INF/,\
ESH-INF/,\
lib/,\
lib/json-20140107.jar,\
lib/engine.io-client-0.6.2-SNAPSHOT.jar,\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ public class MyOpenHABClient {
private static final int HTTP_CLIENT_TIMEOUT = 30000;

/*
* This variable holds base URL for my.openHAB cloud connections, has a default
* value but can be changed
* This variable holds base URL for my.openHAB cloud connections
*/
private String myOpenHABBaseUrl = "https://my.openhab.org/";
private String baseURL;

/*
* This variable holds openHAB's UUID for authenticating and connecting to my.openHAB cloud
Expand Down Expand Up @@ -128,9 +127,10 @@ public class MyOpenHABClient {
* @param secret openHAB's Secret to connect to my.openHAB
*
*/
public MyOpenHABClient(String uuid, String secret) {
public MyOpenHABClient(String uuid, String secret, String baseURL) {
this.uuid = uuid;
this.secret = secret;
this.baseURL = baseURL;
runningRequests = new HashMap<Integer, Request>();
jettyClient = new HttpClient();
jettyClient.setMaxConnectionsPerDestination(HTTP_CLIENT_MAX_CONNECTIONS_PER_DEST);
Expand All @@ -143,7 +143,7 @@ public MyOpenHABClient(String uuid, String secret) {

public void connect() {
try {
socket = IO.socket(myOpenHABBaseUrl);
socket = IO.socket(baseURL);
} catch (URISyntaxException e) {
logger.error("Error creating Socket.IO: {}", e.getMessage());
}
Expand Down Expand Up @@ -295,8 +295,9 @@ private void handleRequestEvent(JSONObject data) {
newPath += queryName;
newPath += "=";
newPath += URLEncoder.encode(requestQueryJson.getString(queryName), "UTF-8");
if (queryIterator.hasNext())
if (queryIterator.hasNext()) {
newPath += "&";
}
}
// Finally get the future request URI
URI requestUri = new URI(newPath);
Expand Down Expand Up @@ -371,8 +372,9 @@ private void handleCancelEvent(JSONObject data) {
private void handleCommandEvent(JSONObject data) {
try {
logger.debug("Received command " + data.getString("command") + " for item " + data.getString("item"));
if (this.listener != null)
if (this.listener != null) {
this.listener.sendCommand(data.getString("item"), data.getString("command"));
}
} catch (JSONException e) {
logger.error(e.getMessage());
}
Expand Down Expand Up @@ -524,28 +526,6 @@ public void shutdown() {
socket.disconnect();
}

/**
* Set base URL of my.openHAB cloud
*
* @param myOHBaseUrl base URL in http://host:port form
*
*/

public void setMyOHBaseUrl(String myOHBaseUrl) {
myOpenHABBaseUrl = myOHBaseUrl;
}

/**
* Set base local URL of openHAB
*
* @param ohBaseUrl base local URL of openHAB in http://host:port form
*
*/

public void setOHBaseUrl(String ohBaseUrl) {
this.localBaseUrl = ohBaseUrl;
}

public String getOpenHABVersion() {
return openHABVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ public class MyOpenHABService implements PersistenceService, ActionService, MyOp
private Logger logger = LoggerFactory.getLogger(MyOpenHABService.class);

private static final String SECRET_FILE_NAME = "myopenhab" + File.separator + "secret";

private static final String DEFAULT_URL = "https://my.openhab.org/";
public static String myohVersion = null;
private MyOpenHABClient myOHClient;
private boolean persistenceEnabled = false;
private String myOpenHABBaseUrl = null;
protected ItemRegistry itemRegistry = null;
protected EventPublisher eventPublisher = null;

Expand Down Expand Up @@ -138,8 +139,20 @@ protected void modified(Map<String, ?> config) {
} else {
logger.debug("config is null");
}

if (config.get("baseURL") != null) {
myOpenHABBaseUrl = (String) config.get("baseURL");
} else {
myOpenHABBaseUrl = DEFAULT_URL;
}

logger.debug("UUID = " + InstanceUUID.get() + ", secret = " + getSecret());
myOHClient = new MyOpenHABClient(InstanceUUID.get(), getSecret());

if (myOHClient != null) {
myOHClient.shutdown();
}

myOHClient = new MyOpenHABClient(InstanceUUID.get(), getSecret(), myOpenHABBaseUrl);
myOHClient.setOpenHABVersion(OpenHAB.getVersion());
myOHClient.connect();
myOHClient.setListener(this);
Expand Down

0 comments on commit 55762cf

Please sign in to comment.