Skip to content

Commit

Permalink
Oceanic : Improve serial port handling + distribution of read requests (
Browse files Browse the repository at this point in the history
openhab#1916)

Signed-Off-By: Karel Goderis <[email protected]>
  • Loading branch information
kgoderis authored and kaikreuzer committed Feb 18, 2017
1 parent f394325 commit 13f6be3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ private synchronized void onUpdate() {
int polling_interval = ((BigDecimal) getConfig().get(INTERVAL)).intValue();
pollingJob = scheduler.scheduleWithFixedDelay(pollingRunnable, 1, polling_interval, TimeUnit.SECONDS);
}

// Read out the properties
try {
if (getThing().getStatus() == ThingStatus.ONLINE) {
for (Channel aChannel : getThing().getChannels()) {
try {
OceanicChannelSelector selector = OceanicChannelSelector.getValueSelector(
aChannel.getUID().getId(), OceanicChannelSelector.ValueSelectorType.GET);

if (selector != null && selector.isProperty()) {
scheduler.schedule(new RequestRunnable(aChannel.getUID(), selector), 0,
TimeUnit.MILLISECONDS);
}
} catch (Exception e) {
}
}
}
} catch (Exception e) {
logger.error("An exception occurred while reading the properties the Oceanic Water Softener: '{}'",
e.getMessage());
}
}

@Override
Expand All @@ -113,14 +134,23 @@ public void onDataReceived(String line) {
public void run() {
try {
if (getThing().getStatus() == ThingStatus.ONLINE) {

long interval = (((BigDecimal) getConfig().get(INTERVAL)).intValue() * 1000)
/ getThing().getChannels().size();
logger.trace("Sending a request every {} milliseconds", interval);
long delay = 0;

for (Channel aChannel : getThing().getChannels()) {
for (OceanicChannelSelector selector : OceanicChannelSelector.values()) {
ChannelUID theChannelUID = new ChannelUID(getThing().getUID(), selector.toString());
if (aChannel.getUID().equals(theChannelUID)
&& selector.getTypeValue() == OceanicChannelSelector.ValueSelectorType.GET) {
scheduler.schedule(new RequestRunnable(theChannelUID, selector), 0,
try {
OceanicChannelSelector selector = OceanicChannelSelector.getValueSelector(
aChannel.getUID().getId(), OceanicChannelSelector.ValueSelectorType.GET);

if (selector != null) {
scheduler.schedule(new RequestRunnable(aChannel.getUID(), selector), delay,
TimeUnit.MILLISECONDS);
delay = delay + interval;
}
} catch (Exception e) {
}
}
}
Expand All @@ -144,6 +174,7 @@ public RequestRunnable(ChannelUID theChannelUID, OceanicChannelSelector selector
public void run() {
try {
if (getThing().getStatus() == ThingStatus.ONLINE) {
logger.trace("Requesting a response for {}", selector.name());
String response = requestResponse(selector.name());
if (response != "") {
if (selector.isProperty()) {
Expand Down Expand Up @@ -234,11 +265,12 @@ private State createStateForType(OceanicChannelSelector selector, String value)
}

private String requestResponse(String commandAsString) {
synchronized (this) {
SerialPortThrottler.lock(port);

SerialPortThrottler.lock(port);

String response = null;
try {
lastLineReceived = "";
String response = null;
writeString(commandAsString + "\r");
long timeStamp = System.currentTimeMillis();
while (lastLineReceived.equals("")) {
Expand All @@ -253,10 +285,11 @@ private String requestResponse(String commandAsString) {
}
}
response = lastLineReceived;

} finally {
SerialPortThrottler.unlock(port);

return response;
}

return response;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ public void dispose() {
}

if (readerThread != null) {
readerThread.interrupt();
try {
readerThread.interrupt();
readerThread.join();
} catch (InterruptedException e) {
}
}
}

Expand Down Expand Up @@ -302,13 +306,13 @@ public void run() {
} catch (InterruptedException e) {
}
}
} else {
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
// quietly exit
}
}

try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
}

}
} catch (InterruptedIOException e) {
Thread.currentThread().interrupt();
Expand Down

0 comments on commit 13f6be3

Please sign in to comment.