Skip to content

Commit

Permalink
Add error message status on fetch fail
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMartin committed Dec 18, 2023
1 parent 9b72f29 commit 7dbac76
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,11 +88,23 @@ 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;
}

public boolean isOpened() {
return CODE_LOCK_OPEN == code;
}

public boolean isNeedResync() {
return NEED_RESYNC == code;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit 7dbac76

Please sign in to comment.