mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-25 14:55:55 +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.QuantityType;
|
||||||
import org.openhab.core.library.types.StringType;
|
import org.openhab.core.library.types.StringType;
|
||||||
import org.openhab.core.library.unit.Units;
|
import org.openhab.core.library.unit.Units;
|
||||||
|
import org.openhab.core.thing.Channel;
|
||||||
import org.openhab.core.thing.ChannelUID;
|
import org.openhab.core.thing.ChannelUID;
|
||||||
import org.openhab.core.thing.Thing;
|
import org.openhab.core.thing.Thing;
|
||||||
import org.openhab.core.thing.ThingStatus;
|
import org.openhab.core.thing.ThingStatus;
|
||||||
@ -99,8 +100,10 @@ public class TibberHandler extends BaseThingHandler {
|
|||||||
versionString = FrameworkUtil.getBundle(this.getClass()).getVersion().toString();
|
versionString = FrameworkUtil.getBundle(this.getClass()).getVersion().toString();
|
||||||
logger.debug("Binding version: {}", versionString);
|
logger.debug("Binding version: {}", versionString);
|
||||||
|
|
||||||
getTibberParameters();
|
scheduler.execute(() -> {
|
||||||
startRefresh(tibberConfig.getRefresh());
|
getTibberParameters();
|
||||||
|
startRefresh(tibberConfig.getRefresh());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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() {
|
public void getTibberParameters() {
|
||||||
String response = "";
|
String response = "";
|
||||||
try {
|
try {
|
||||||
@ -148,20 +169,8 @@ public class TibberHandler extends BaseThingHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ("true".equals(rtEnabled)) {
|
if (liveChannelsLinked() && "true".equals(rtEnabled)) {
|
||||||
logger.debug("Pulse associated with HomeId: Live stream will be started");
|
startLiveStream();
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
|
||||||
@ -311,21 +320,16 @@ public class TibberHandler extends BaseThingHandler {
|
|||||||
|
|
||||||
public void updateRequest() throws IOException {
|
public void updateRequest() throws IOException {
|
||||||
getURLInput(BASE_URL);
|
getURLInput(BASE_URL);
|
||||||
if ("true".equals(rtEnabled) && !isConnected()) {
|
if (liveChannelsLinked() && "true".equals(rtEnabled) && !isConnected()) {
|
||||||
logger.debug("Attempting to reopen Websocket connection");
|
startLiveStream();
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean liveChannelsLinked() {
|
||||||
|
return getThing().getChannels().stream().map(Channel::getUID)
|
||||||
|
.filter((channelUID -> channelUID.getAsString().contains("live_"))).anyMatch(this::isLinked);
|
||||||
|
};
|
||||||
|
|
||||||
private void getSubscriptionUrl() throws IOException {
|
private void getSubscriptionUrl() throws IOException {
|
||||||
TibberPriceConsumptionHandler tibberQuery = new TibberPriceConsumptionHandler();
|
TibberPriceConsumptionHandler tibberQuery = new TibberPriceConsumptionHandler();
|
||||||
InputStream wsURL = tibberQuery.getWebsocketUrl();
|
InputStream wsURL = tibberQuery.getWebsocketUrl();
|
||||||
@ -393,6 +397,20 @@ public class TibberHandler extends BaseThingHandler {
|
|||||||
super.dispose();
|
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() {
|
public void open() {
|
||||||
WebSocketClient client = this.client;
|
WebSocketClient client = this.client;
|
||||||
if (client == null || !client.isRunning() || !isConnected()) {
|
if (client == null || !client.isRunning() || !isConnected()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user