From 7dbac76e2bbd06e4b90ecc4c2ff612e90e954bc3 Mon Sep 17 00:00:00 2001 From: Jordan Martin Date: Mon, 18 Dec 2023 13:20:09 +0100 Subject: [PATCH] Add error message status on fetch fail --- .../thekeys/internal/api/GatewayService.java | 38 +++++++++----- .../internal/api/model/LockerStatusDTO.java | 14 ++++++ .../internal/api/model/SynchronizeDTO.java | 49 +++++++++++++++++++ .../smartlock/TheKeysSmartlockHandler.java | 3 +- 4 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/model/SynchronizeDTO.java diff --git a/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/GatewayService.java b/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/GatewayService.java index 9b11ed71a1868..da7621b794062 100644 --- a/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/GatewayService.java +++ b/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/GatewayService.java @@ -12,28 +12,30 @@ */ package org.openhab.binding.thekeys.internal.api; +import java.io.EOFException; +import java.io.IOException; +import java.net.ConnectException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.time.Instant; +import java.util.Base64; +import java.util.List; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.binding.thekeys.internal.api.model.GatewayInfosDTO; import org.openhab.binding.thekeys.internal.api.model.LockerDTO; import org.openhab.binding.thekeys.internal.api.model.LockerStatusDTO; import org.openhab.binding.thekeys.internal.api.model.LockersDTO; import org.openhab.binding.thekeys.internal.api.model.OpenCloseDTO; +import org.openhab.binding.thekeys.internal.api.model.SynchronizeDTO; import org.openhab.binding.thekeys.internal.gateway.TheKeysGatewayConfiguration; import org.openhab.binding.thekeys.internal.utils.ExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.crypto.Mac; -import javax.crypto.spec.SecretKeySpec; -import java.io.EOFException; -import java.io.IOException; -import java.net.ConnectException; -import java.security.InvalidKeyException; -import java.security.NoSuchAlgorithmException; -import java.time.Instant; -import java.util.Base64; -import java.util.List; - /** * Implement the communication with the Gateway via HTTP * @@ -77,6 +79,20 @@ public LockerStatusDTO getLockStatus(int lockId) throws IOException { return post("/locker_status", LockerStatusDTO.class, lockId); } + /** + * Synchronize the gateway + */ + public SynchronizeDTO synchronizeGateway() throws IOException { + return get("/synchronize", SynchronizeDTO.class); + } + + /** + * Synchronize the gateway + */ + public SynchronizeDTO synchronizeLock() throws IOException { + return get("/locker/synchronize", SynchronizeDTO.class); + } + /** * Open operation of the lock * diff --git a/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/model/LockerStatusDTO.java b/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/model/LockerStatusDTO.java index 08f86a99e83d7..207d24e16db42 100644 --- a/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/model/LockerStatusDTO.java +++ b/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/model/LockerStatusDTO.java @@ -19,11 +19,13 @@ */ public class LockerStatusDTO { + public static final int NEED_RESYNC = 0x26; public static final int CODE_LOCK_CLOSE = 0x31; public static final int CODE_LOCK_OPEN = 0x32; private String status; private int code; + private String cause; private int id; private int version; private int position; @@ -86,6 +88,14 @@ public void setBattery(int battery) { this.battery = battery; } + public String getCause() { + return cause; + } + + public void setCause(String cause) { + this.cause = cause; + } + public boolean isClosed() { return CODE_LOCK_CLOSE == code; } @@ -93,4 +103,8 @@ public boolean isClosed() { public boolean isOpened() { return CODE_LOCK_OPEN == code; } + + public boolean isNeedResync() { + return NEED_RESYNC == code; + } } diff --git a/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/model/SynchronizeDTO.java b/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/model/SynchronizeDTO.java new file mode 100644 index 0000000000000..42c048207e349 --- /dev/null +++ b/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/api/model/SynchronizeDTO.java @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2010-2023 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.thekeys.internal.api.model; + +/** + * Response DTO for /locker_status endpoint + * + * @author Jordan Martin - Initial contribution + */ +public class SynchronizeDTO { + + private String status; + private int code; + private String message; + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/smartlock/TheKeysSmartlockHandler.java b/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/smartlock/TheKeysSmartlockHandler.java index ea8f51ff50bf7..d7a5481392b2c 100644 --- a/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/smartlock/TheKeysSmartlockHandler.java +++ b/bundles/org.openhab.binding.thekeys/src/main/java/org/openhab/binding/thekeys/internal/smartlock/TheKeysSmartlockHandler.java @@ -129,7 +129,8 @@ private boolean fetchAndUpdateLockStatus() { updateState(CHANNEL_SYNC_IN_PROGRESS, OnOffType.ON); LockerStatusDTO lockStatus = gatewayApi().getLockStatus(config.lockId); if ("ko".equals(lockStatus.getStatus())) { - throw new TheKeysError("Request failed with code " + lockStatus.getCode()); + throw new TheKeysError( + "Request failed with code " + lockStatus.getCode() + ". " + lockStatus.getCause()); } updateState(CHANNEL_STATUS, new StringType(lockStatus.getStatus()));