import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.Units;
+import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
versionString = FrameworkUtil.getBundle(this.getClass()).getVersion().toString();
logger.debug("Binding version: {}", versionString);
- getTibberParameters();
- startRefresh(tibberConfig.getRefresh());
+ scheduler.execute(() -> {
+ getTibberParameters();
+ startRefresh(tibberConfig.getRefresh());
+ });
}
@Override
}
}
+ @Override
+ public void channelLinked(ChannelUID channelUID) {
+ if (channelUID.getAsString().contains("live_") && !isConnected() && "true".equals(rtEnabled)) {
+ try {
+ startLiveStream();
+ } catch (IOException e) {
+ logger.debug("Unable to start live data: {}", e.getMessage());
+ }
+ }
+ }
+
+ @Override
+ public void channelUnlinked(ChannelUID channelUID) {
+ if (channelUID.getAsString().contains("live_") && !liveChannelsLinked() && isConnected()) {
+ close();
+ }
+ }
+
public void getTibberParameters() {
String response = "";
try {
}
}
- if ("true".equals(rtEnabled)) {
- logger.debug("Pulse associated with HomeId: Live stream will be started");
- getSubscriptionUrl();
-
- if (subscriptionURL == null || subscriptionURL.isBlank()) {
- logger.debug("Unexpected subscription result from the server");
- updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
- "Unexpected subscription result from the server");
- } else {
- logger.debug("Reconnecting Subscription to: {}", subscriptionURL);
- open();
- }
- } else {
- logger.debug("No Pulse associated with HomeId: No live stream will be started");
+ if (liveChannelsLinked() && "true".equals(rtEnabled)) {
+ startLiveStream();
}
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
public void updateRequest() throws IOException {
getURLInput(BASE_URL);
- if ("true".equals(rtEnabled) && !isConnected()) {
- logger.debug("Attempting to reopen Websocket connection");
- getSubscriptionUrl();
-
- if (subscriptionURL == null || subscriptionURL.isBlank()) {
- logger.debug("Unexpected subscription result from the server");
- updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
- "Unexpected subscription result from the server");
- } else {
- logger.debug("Reconnecting Subscription to: {}", subscriptionURL);
- open();
- }
+ if (liveChannelsLinked() && "true".equals(rtEnabled) && !isConnected()) {
+ startLiveStream();
}
}
+ private boolean liveChannelsLinked() {
+ return getThing().getChannels().stream().map(Channel::getUID)
+ .filter((channelUID -> channelUID.getAsString().contains("live_"))).anyMatch(this::isLinked);
+ };
+
private void getSubscriptionUrl() throws IOException {
TibberPriceConsumptionHandler tibberQuery = new TibberPriceConsumptionHandler();
InputStream wsURL = tibberQuery.getWebsocketUrl();
super.dispose();
}
+ private void startLiveStream() throws IOException {
+ logger.debug("Attempting to open Websocket connection");
+ getSubscriptionUrl();
+
+ if (subscriptionURL == null || subscriptionURL.isBlank()) {
+ logger.debug("Unexpected subscription result from the server");
+ updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
+ "Unexpected subscription result from the server");
+ } else {
+ logger.debug("Connecting Subscription to: {}", subscriptionURL);
+ open();
+ }
+ }
+
public void open() {
WebSocketClient client = this.client;
if (client == null || !client.isRunning() || !isConnected()) {