import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
+import org.osgi.framework.FrameworkUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private @Nullable ScheduledFuture<?> pollingJob;
private @Nullable Future<?> sessionFuture;
private String rtEnabled = "false";
+ private @Nullable String subscriptionURL;
+ private @Nullable String versionString;
public TibberHandler(Thing thing) {
super(thing);
updateStatus(ThingStatus.UNKNOWN);
tibberConfig = getConfigAs(TibberConfiguration.class);
+ versionString = FrameworkUtil.getBundle(this.getClass()).getVersion().toString();
+ logger.debug("Binding version: {}", versionString);
+
getTibberParameters();
startRefresh(tibberConfig.getRefresh());
}
try {
httpHeader.put("cache-control", "no-cache");
httpHeader.put("content-type", JSON_CONTENT_TYPE);
- httpHeader.put("Authorization", "Bearer " + tibberConfig.getToken());
+ httpHeader.put(HttpHeader.USER_AGENT.asString(),
+ "openHAB/Tibber " + versionString + " Tibber driver " + TIBBER_DRIVER);
+ httpHeader.put(HttpHeader.AUTHORIZATION.asString(), "Bearer " + tibberConfig.getToken());
TibberPriceConsumptionHandler tibberQuery = new TibberPriceConsumptionHandler();
InputStream connectionStream = tibberQuery.connectionInputStream(tibberConfig.getHomeid());
if ("true".equals(rtEnabled)) {
logger.debug("Pulse associated with HomeId: Live stream will be started");
+
+ InputStream wsURL = tibberQuery.getWebsocketUrl();
+ String wsResponse = HttpUtil.executeUrl("POST", BASE_URL, httpHeader, wsURL, null, REQUEST_TIMEOUT);
+
+ JsonObject wsobject = (JsonObject) JsonParser.parseString(wsResponse);
+ subscriptionURL = wsobject.getAsJsonObject("data").getAsJsonObject("viewer")
+ .get("websocketSubscriptionUrl").toString().replaceAll("^\"|\"$", "");
+ logger.debug("Subscribing to: {}", subscriptionURL);
+
open();
} else {
logger.debug("No Pulse associated with HomeId: No live stream will be started");
sslContextFactory.setTrustAll(true);
sslContextFactory.setEndpointIdentificationAlgorithm(null);
- client = new WebSocketClient(sslContextFactory);
+ client = new WebSocketClient(new HttpClient(sslContextFactory));
client.setMaxIdleTimeout(30 * 1000);
this.client = client;
}
ClientUpgradeRequest newRequest = new ClientUpgradeRequest();
- newRequest.setHeader("Authorization", "Bearer " + tibberConfig.getToken());
- newRequest.setSubProtocols("graphql-subscriptions");
+ newRequest.setHeader(HttpHeader.USER_AGENT.asString(),
+ "openHAB/Tibber " + versionString + " Tibber driver " + TIBBER_DRIVER);
+ newRequest.setHeader(HttpHeader.AUTHORIZATION.asString(), "Bearer " + tibberConfig.getToken());
+ newRequest.setSubProtocols("graphql-transport-ws");
try {
logger.debug("Starting Websocket connection");
}
try {
logger.debug("Connecting Websocket connection");
- sessionFuture = client.connect(socket, new URI(SUBSCRIPTION_URL), newRequest);
+ sessionFuture = client.connect(socket, new URI(subscriptionURL), newRequest);
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
public void onConnect(Session wssession) {
TibberHandler.this.session = wssession;
TibberWebSocketListener socket = TibberHandler.this.socket;
- String connection = "{\"type\":\"connection_init\", \"payload\":\"token=" + tibberConfig.getToken() + "\"}";
+ String connection = "{\"type\":\"connection_init\", \"payload\":{\"token\":\"" + tibberConfig.getToken()
+ + "\"}}";
try {
if (socket != null) {
logger.debug("Sending websocket connect message");
}
public void startSubscription() {
- String query = "{\"id\":\"1\",\"type\":\"start\",\"payload\":{\"variables\":{},\"extensions\":{},\"operationName\":null,\"query\":\"subscription {\\n liveMeasurement(homeId:\\\""
+ String query = "{\"id\":\"1\",\"type\":\"subscribe\",\"payload\":{\"variables\":{},\"extensions\":{},\"operationName\":null,\"query\":\"subscription {\\n liveMeasurement(homeId:\\\""
+ tibberConfig.getHomeid()
+ "\\\") {\\n timestamp\\n power\\n lastMeterConsumption\\n accumulatedConsumption\\n accumulatedCost\\n currency\\n minPower\\n averagePower\\n maxPower\\n"
+ "voltagePhase1\\n voltagePhase2\\n voltagePhase3\\n currentL1\\n currentL2\\n currentL3\\n powerProduction\\n accumulatedProduction\\n minPowerProduction\\n maxPowerProduction\\n }\\n }\\n\"}}";