[tapocontrol] fixed cloud connection error (#16822)

Signed-off-by: Christian Wild <christian@wildclan.de>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
Christian Wild 2024-05-31 06:48:00 +02:00 committed by Ciprian Pascu
parent 9b281d905a
commit 422165ca31
5 changed files with 14 additions and 27 deletions

View File

@ -19,7 +19,6 @@ import static org.openhab.binding.tapocontrol.internal.helpers.utils.JsonUtils.*
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.Request;
import org.openhab.binding.tapocontrol.internal.api.protocol.passthrough.PassthroughProtocol; import org.openhab.binding.tapocontrol.internal.api.protocol.passthrough.PassthroughProtocol;
import org.openhab.binding.tapocontrol.internal.devices.bridge.TapoBridgeHandler; import org.openhab.binding.tapocontrol.internal.devices.bridge.TapoBridgeHandler;
import org.openhab.binding.tapocontrol.internal.devices.bridge.dto.TapoCloudLoginData; import org.openhab.binding.tapocontrol.internal.devices.bridge.dto.TapoCloudLoginData;
@ -185,13 +184,4 @@ public class TapoCloudConnector implements TapoConnectorInterface {
} }
return url; return url;
} }
/**
* Set http-headers
*/
public Request setHeaders(Request httpRequest) {
httpRequest.header("content-type", CONTENT_TYPE_JSON);
httpRequest.header("Accept", CONTENT_TYPE_JSON);
return httpRequest;
}
} }

View File

@ -112,7 +112,7 @@ public class SecurePassthrough implements TapoProtocolInterface {
logger.trace("({}) encrypted request: '{}' with cookie '{}'", uid, tapoRequest, session.getCookie()); logger.trace("({}) encrypted request: '{}' with cookie '{}'", uid, tapoRequest, session.getCookie());
} }
Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST.toString()); Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST);
/* set header */ /* set header */
httpRequest = setHeaders(httpRequest); httpRequest = setHeaders(httpRequest);
@ -141,7 +141,7 @@ public class SecurePassthrough implements TapoProtocolInterface {
TapoRequest encryptedRequest = session.encryptRequest(tapoRequest); TapoRequest encryptedRequest = session.encryptRequest(tapoRequest);
logger.trace("({}) sending encrypted request to '{}' with cookie '{}'", uid, url, session.getCookie()); logger.trace("({}) sending encrypted request to '{}' with cookie '{}'", uid, url, session.getCookie());
Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST.toString()); Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST);
/* set header */ /* set header */
httpRequest = setHeaders(httpRequest); httpRequest = setHeaders(httpRequest);
@ -192,7 +192,7 @@ public class SecurePassthrough implements TapoProtocolInterface {
*/ */
@Override @Override
public void responseReceived(ContentResponse response, String command) throws TapoErrorHandler { public void responseReceived(ContentResponse response, String command) throws TapoErrorHandler {
logger.trace("({}) recived response: {}", uid, response.getContentAsString()); logger.trace("({}) received response: {}", uid, response.getContentAsString());
TapoResponse tapoResponse = getTapoResponse(response); TapoResponse tapoResponse = getTapoResponse(response);
httpDelegator.handleResponse(tapoResponse, command); httpDelegator.handleResponse(tapoResponse, command);
httpDelegator.responsePasstrough(response.getContentAsString(), command); httpDelegator.responsePasstrough(response.getContentAsString(), command);
@ -266,7 +266,6 @@ public class SecurePassthrough implements TapoProtocolInterface {
* Set HTTP-Headers * Set HTTP-Headers
*/ */
protected Request setHeaders(Request httpRequest) { protected Request setHeaders(Request httpRequest) {
httpRequest.header("content-type", CONTENT_TYPE_JSON);
httpRequest.header("Accept", CONTENT_TYPE_JSON); httpRequest.header("Accept", CONTENT_TYPE_JSON);
if (session.isHandshakeComplete()) { if (session.isHandshakeComplete()) {
httpRequest.header(HTTP_AUTH_TYPE_COOKIE, session.getCookie()); httpRequest.header(HTTP_AUTH_TYPE_COOKIE, session.getCookie());

View File

@ -94,7 +94,7 @@ public class KlapProtocol implements org.openhab.binding.tapocontrol.internal.ap
String command = tapoRequest.method(); String command = tapoRequest.method();
logger.trace("({}) sending unencrypted request: '{}' to '{}' ", uid, tapoRequest, url); logger.trace("({}) sending unencrypted request: '{}' to '{}' ", uid, tapoRequest, url);
Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST.toString()); Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST);
/* set header */ /* set header */
httpRequest = setHeaders(httpRequest); httpRequest = setHeaders(httpRequest);
@ -126,7 +126,7 @@ public class KlapProtocol implements org.openhab.binding.tapocontrol.internal.ap
Integer ivSequence = session.getIvSequence(); Integer ivSequence = session.getIvSequence();
logger.trace("({}) encrypted request is '{}' with sequence '{}'", uid, encrypteString, ivSequence); logger.trace("({}) encrypted request is '{}' with sequence '{}'", uid, encrypteString, ivSequence);
Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST.toString()); Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST);
/* set header and params */ /* set header and params */
httpRequest = setHeaders(httpRequest); httpRequest = setHeaders(httpRequest);
@ -263,7 +263,6 @@ public class KlapProtocol implements org.openhab.binding.tapocontrol.internal.ap
*/ */
protected Request setHeaders(Request httpRequest) { protected Request setHeaders(Request httpRequest) {
if (!session.isHandshakeComplete()) { if (!session.isHandshakeComplete()) {
httpRequest.header("content-type", CONTENT_TYPE_JSON);
httpRequest.header("Accept", CONTENT_TYPE_JSON); httpRequest.header("Accept", CONTENT_TYPE_JSON);
} }
if (!session.getCookie().isBlank()) { if (!session.getCookie().isBlank()) {

View File

@ -95,7 +95,7 @@ public class PassthroughProtocol implements TapoProtocolInterface {
logger.trace("({}) sending encrypted request to '{}' ", uid, url); logger.trace("({}) sending encrypted request to '{}' ", uid, url);
logger.trace("({}) unencrypted request: '{}'", uid, tapoRequest); logger.trace("({}) unencrypted request: '{}'", uid, tapoRequest);
Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST.toString()); Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST);
/* set header */ /* set header */
httpRequest = setHeaders(httpRequest); httpRequest = setHeaders(httpRequest);
@ -131,7 +131,7 @@ public class PassthroughProtocol implements TapoProtocolInterface {
logger.trace("({}) sendAsncRequest to '{}'", uid, url); logger.trace("({}) sendAsncRequest to '{}'", uid, url);
logger.trace("({}) command/payload: '{}''{}'", uid, command, tapoRequest.params()); logger.trace("({}) command/payload: '{}''{}'", uid, command, tapoRequest.params());
Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST.toString()); Request httpRequest = httpDelegator.getHttpClient().newRequest(url).method(HttpMethod.POST);
/* set header */ /* set header */
httpRequest = setHeaders(httpRequest); httpRequest = setHeaders(httpRequest);
@ -180,7 +180,7 @@ public class PassthroughProtocol implements TapoProtocolInterface {
*/ */
@Override @Override
public void responseReceived(ContentResponse response, String command) throws TapoErrorHandler { public void responseReceived(ContentResponse response, String command) throws TapoErrorHandler {
logger.trace("({}) recived response: {}", uid, response.getContentAsString()); logger.trace("({}) received response: {}", uid, response.getContentAsString());
TapoResponse tapoResponse = getTapoResponse(response); TapoResponse tapoResponse = getTapoResponse(response);
if (!tapoResponse.hasError()) { if (!tapoResponse.hasError()) {
switch (command) { switch (command) {
@ -222,8 +222,9 @@ public class PassthroughProtocol implements TapoProtocolInterface {
if (response.getStatus() == 200) { if (response.getStatus() == 200) {
return getTapoResponse(response.getContentAsString()); return getTapoResponse(response.getContentAsString());
} else { } else {
logger.debug("({}) invalid response received", uid); String reason = response.getStatus() + " " + response.getReason();
throw new TapoErrorHandler(ERR_BINDING_HTTP_RESPONSE, "invalid response receicved"); logger.debug("({}) invalid response received - {}", uid, reason);
throw new TapoErrorHandler(ERR_BINDING_HTTP_RESPONSE, reason);
} }
} }
@ -250,7 +251,6 @@ public class PassthroughProtocol implements TapoProtocolInterface {
* Set HTTP-Headers * Set HTTP-Headers
*/ */
public Request setHeaders(Request httpRequest) { public Request setHeaders(Request httpRequest) {
httpRequest.header("content-type", CONTENT_TYPE_JSON);
httpRequest.header("Accept", CONTENT_TYPE_JSON); httpRequest.header("Accept", CONTENT_TYPE_JSON);
return httpRequest; return httpRequest;
} }

View File

@ -13,12 +13,11 @@
package org.openhab.binding.tapocontrol.internal.dto; package org.openhab.binding.tapocontrol.internal.dto;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import io.reactivex.annotations.Nullable;
/** /**
* Holds data sent to device * Holds data sent to device
* *
@ -31,7 +30,7 @@ public record TapoRequest(@Expose String method, @Expose @Nullable Object params
/** /**
* Create request with command (method) and data (params) sent to device * Create request with command (method) and data (params) sent to device
*/ */
public TapoRequest(String method, Object params) { public TapoRequest(String method, @Nullable Object params) {
this(method, params, System.currentTimeMillis()); this(method, params, System.currentTimeMillis());
} }
@ -39,7 +38,7 @@ public record TapoRequest(@Expose String method, @Expose @Nullable Object params
* Create request with command (method) sent to device * Create request with command (method) sent to device
*/ */
public TapoRequest(String method) { public TapoRequest(String method) {
this(method, "", System.currentTimeMillis()); this(method, null, System.currentTimeMillis());
} }
/*********************************************** /***********************************************