mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[tibber] Only open websocket if live channels are linked (#17188)
Signed-off-by: Anders Alfredsson <andersb86@gmail.com> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
parent
b2957cf1f7
commit
756a089050
@ -47,6 +47,7 @@ import org.openhab.core.library.types.DecimalType;
|
||||
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;
|
||||
@ -99,8 +100,10 @@ public class TibberHandler extends BaseThingHandler {
|
||||
versionString = FrameworkUtil.getBundle(this.getClass()).getVersion().toString();
|
||||
logger.debug("Binding version: {}", versionString);
|
||||
|
||||
scheduler.execute(() -> {
|
||||
getTibberParameters();
|
||||
startRefresh(tibberConfig.getRefresh());
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -112,6 +115,24 @@ public class TibberHandler extends BaseThingHandler {
|
||||
}
|
||||
}
|
||||
|
||||
@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 {
|
||||
@ -148,20 +169,8 @@ public class TibberHandler extends BaseThingHandler {
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
@ -311,20 +320,15 @@ public class TibberHandler extends BaseThingHandler {
|
||||
|
||||
public void updateRequest() throws IOException {
|
||||
getURLInput(BASE_URL);
|
||||
if ("true".equals(rtEnabled) && !isConnected()) {
|
||||
logger.debug("Attempting to reopen Websocket connection");
|
||||
getSubscriptionUrl();
|
||||
if (liveChannelsLinked() && "true".equals(rtEnabled) && !isConnected()) {
|
||||
startLiveStream();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
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();
|
||||
@ -393,6 +397,20 @@ public class TibberHandler extends BaseThingHandler {
|
||||
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()) {
|
||||
|
Loading…
Reference in New Issue
Block a user