Skip to content

Commit

Permalink
Merge pull request openhab#141 from digitaldan/omnilink-binding-cleanup
Browse files Browse the repository at this point in the history
Omnilink binding cleanup
  • Loading branch information
craigham authored Apr 13, 2019
2 parents d22e97d + 3af0fe0 commit e4b3074
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 1,077 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2
Bundle-Name: Omnilink Binding
Bundle-SymbolicName: org.openhab.binding.omnilink;singleton:=true
Bundle-Vendor: openHAB
Bundle-Version: 2.4.0.qualifier
Bundle-Version: 2.5.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ClassPath: .,
lib/gson-2.8.2.jar,
Expand Down

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion addons/binding/org.openhab.binding.omnilink/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.openhab.binding</groupId>
<artifactId>pom</artifactId>
<version>2.4.0-SNAPSHOT</version>
<version>2.5.0-SNAPSHOT</version>
</parent>

<groupId>org.openhab.binding</groupId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package org.openhab.binding.omnilink.handler;

import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusInfo;
Expand All @@ -21,7 +22,12 @@ public AbstractOmnilinkHandler(Thing thing) {
}

public OmnilinkBridgeHandler getOmnilinkBridgeHander() {
return (OmnilinkBridgeHandler) getBridge().getHandler();
Bridge bridge = getBridge();
if (bridge != null) {
return (OmnilinkBridgeHandler) bridge.getHandler();
} else {
return null;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

import java.util.Optional;

import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,9 +37,7 @@ public AbstractOmnilinkStatusHandler(Thing thing) {

@Override
public void initialize() {
Optional<T> status = retrieveStatus();
handleStatus(status.orElse(null)); // handle status will process null.
updateStatus(ThingStatus.ONLINE);
updateHandlerStatus();
}

/**
Expand Down Expand Up @@ -75,4 +75,19 @@ public void channelLinked(ChannelUID channelUID) {
updateChannels(status.get());
}
}

@Override
public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
super.bridgeStatusChanged(bridgeStatusInfo);
updateHandlerStatus();
}

private void updateHandlerStatus() {
Bridge bridge = getBridge();
if (bridge != null && bridge.getStatus() == ThingStatus.ONLINE) {
Optional<T> status = retrieveStatus();
handleStatus(status.orElse(null)); // handle status will process null.
updateStatus(ThingStatus.ONLINE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
package org.openhab.binding.omnilink.handler;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

Expand All @@ -32,35 +30,16 @@
public class AudioSourceHandler extends AbstractOmnilinkHandler {

private final static Logger logger = LoggerFactory.getLogger(AudioSourceHandler.class);

private final static long POLL_DELAY = 5; // 5 Second polling

private static ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

private ScheduledFuture<?> scheduledPolling = null;

public static synchronized void shutdownExecutor() {
logger.debug("Shutting down audio polling executor service");
executorService.shutdownNow();
}

private static synchronized void startExecutor() {
if (executorService.isShutdown()) {
logger.debug("Starting audio polling executor service");
executorService = Executors.newSingleThreadScheduledExecutor();
}
}

public AudioSourceHandler(Thing thing) {
super(thing);
}

@Override
public void initialize() {
if (scheduledPolling != null) {
scheduledPolling.cancel(false);
}

cancelPolling();
boolean autoStart = ((Boolean) getThing().getConfiguration()
.get(OmnilinkBindingConstants.THING_PROPERTIES_AUTO_START)).booleanValue();
int sourceNumber = getThingNumber();
Expand All @@ -81,28 +60,23 @@ public synchronized void dispose() {
}

private synchronized void cancelPolling() {
if (scheduledPolling != null) {
if (scheduledPolling.isDone() == false) {
int sourceNumber = getThingNumber();
logger.debug("Cancelling polling for Audio Source {}", sourceNumber);
scheduledPolling.cancel(false);
}
if (scheduledPolling != null && !scheduledPolling.isDone()) {
logger.debug("Cancelling polling for Audio Source {}", getThingNumber());
scheduledPolling.cancel(false);
}
}

private synchronized void schedulePolling() {
cancelPolling();
int sourceNumber = getThingNumber();
logger.debug("Scheduling polling for Audio Source {}", sourceNumber);
startExecutor();
scheduledPolling = executorService.scheduleWithFixedDelay(new PollAudioSource(getThingNumber()), 0, POLL_DELAY,
scheduledPolling = super.scheduler.scheduleWithFixedDelay(() -> pollAudioSource(), 0, POLL_DELAY,
TimeUnit.SECONDS);
}

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
String channelID = channelUID.getId();

switch (channelID) {
case OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_POLLING:
if (command == RefreshType.REFRESH) {
Expand All @@ -124,61 +98,49 @@ public void handleCommand(ChannelUID channelUID, Command command) {
default:
logger.warn("Channel ID ({}) not processed", channelID);
break;

}

}

private class PollAudioSource implements Runnable {

private final int sourceNumber;

private PollAudioSource(int sourceNumber) {
this.sourceNumber = sourceNumber;
}

@Override
public void run() {
logger.debug("Polling Audio Source {} Status", sourceNumber);
try {
int position = 0;
Message message;
while ((message = getOmnilinkBridgeHander().requestAudioSourceStatus(sourceNumber, position))
.getMessageType() == Message.MESG_TYPE_AUDIO_SOURCE_STATUS) {
AudioSourceStatus audioSourceStatus = (AudioSourceStatus) message;
position = audioSourceStatus.getPosition();
switch (position) {
case 1:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT1,
new StringType(audioSourceStatus.getSourceData()));
break;
case 2:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT2,
new StringType(audioSourceStatus.getSourceData()));
break;
case 3:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT3,
new StringType(audioSourceStatus.getSourceData()));
break;
case 4:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT4,
new StringType(audioSourceStatus.getSourceData()));
break;
case 5:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT5,
new StringType(audioSourceStatus.getSourceData()));
break;
case 6:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT6,
new StringType(audioSourceStatus.getSourceData()));
break;
}

public void pollAudioSource() {
int sourceNumber = getThingNumber();
logger.debug("Polling Audio Source {} Status", sourceNumber);
try {
int position = 0;
Message message;
while ((message = getOmnilinkBridgeHander().requestAudioSourceStatus(sourceNumber, position))
.getMessageType() == Message.MESG_TYPE_AUDIO_SOURCE_STATUS) {
AudioSourceStatus audioSourceStatus = (AudioSourceStatus) message;
position = audioSourceStatus.getPosition();
switch (position) {
case 1:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT1,
new StringType(audioSourceStatus.getSourceData()));
break;
case 2:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT2,
new StringType(audioSourceStatus.getSourceData()));
break;
case 3:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT3,
new StringType(audioSourceStatus.getSourceData()));
break;
case 4:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT4,
new StringType(audioSourceStatus.getSourceData()));
break;
case 5:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT5,
new StringType(audioSourceStatus.getSourceData()));
break;
case 6:
updateState(OmnilinkBindingConstants.CHANNEL_AUDIO_SOURCE_TEXT6,
new StringType(audioSourceStatus.getSourceData()));
break;
}
} catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
logger.warn("Exception Polling Audio Status", e);
}

}
} catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
logger.warn("Exception Polling Audio Status", e);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/
package org.openhab.binding.omnilink.handler;

import java.util.Optional;

import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.types.Command;
Expand All @@ -22,14 +20,13 @@
import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
import com.digitaldan.jomnilinkII.MessageTypes.statuses.Status;

/**
*
* @author Craig Hamilton
*
*/
public class ButtonHandler extends AbstractOmnilinkStatusHandler {
public class ButtonHandler extends AbstractOmnilinkHandler {
private Logger logger = LoggerFactory.getLogger(ButtonHandler.class);

public ButtonHandler(Thing thing) {
Expand All @@ -55,14 +52,4 @@ public void buttonActivated() {
OmnilinkBindingConstants.TRIGGER_CHANNEL_BUTTON_ACTIVATED_EVENT);
triggerChannel(activateChannel);
}

@Override
protected Optional retrieveStatus() {
return Optional.empty();
}

@Override
protected void updateChannels(Status t) {
// No links for buttons
}
}
Loading

0 comments on commit e4b3074

Please sign in to comment.