Skip to content

Commit

Permalink
Login Authentication corrections to give negative feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dag81 committed Nov 6, 2021
1 parent 6e0aa7a commit 0e5d6e5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.BaseBridgeHandler;
import org.openhab.core.thing.binding.ThingHandler;
Expand Down Expand Up @@ -116,8 +117,8 @@ protected void setBackgroundScanInterval(final int seconds) {
backgroundDiscoveryPollingJob = null;
}
if (seconds > 0) {
backgroundDiscoveryPollingJob = scheduler.scheduleWithFixedDelay(() -> runDeviceScanSequence(),
seconds, seconds, TimeUnit.SECONDS);
backgroundDiscoveryPollingJob = scheduler.scheduleWithFixedDelay(
() -> runDeviceScanSequenceNoAuthErrors(), seconds, seconds, TimeUnit.SECONDS);
}
backgroundScanTime = seconds;
}
Expand All @@ -134,7 +135,17 @@ public void unregisterMetaDataUpdatedHandler(DeviceMetaDataUpdatedHandler dmduh)

private CopyOnWriteArrayList<DeviceMetaDataUpdatedHandler> handlers = new CopyOnWriteArrayList<>();

public void runDeviceScanSequence() {
public void runDeviceScanSequenceNoAuthErrors() {
try {
runDeviceScanSequence();
updateStatus(ThingStatus.ONLINE);
} catch (AuthenticationException ae) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Check login credentials");
return;
}
}

public void runDeviceScanSequence() throws AuthenticationException {
logger.trace("Scanning for new devices / base information now");
api.discoverDevices();
handlers.forEach(x -> x.HandleMetadataRetrieved(this));
Expand Down Expand Up @@ -183,12 +194,13 @@ public void initialize() {

try {
api.login(config.username, passwordMd5, "Europe/London");
updateStatus(ThingStatus.ONLINE);
api.UpdateBridgeData(this);
runDeviceScanSequence();
updateStatus(ThingStatus.ONLINE);
} catch (final AuthenticationException ae) {
updateStatus(ThingStatus.OFFLINE);
setBackgroundScanInterval(DEFAULT_DEVICE_SCAN_DISABLED);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Check login credentials");
// setBackgroundScanInterval(DEFAULT_DEVICE_SCAN_DISABLED); -- Let the system keep checking in case the
// user updates their password externally to match openhab
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import org.openhab.binding.vesync.internal.dto.requests.VesyncRequestManagedDeviceBypassV2;
import org.openhab.binding.vesync.internal.dto.responses.VesyncV2BypassHumidifierStatus;
import org.openhab.core.cache.ExpiringCache;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.types.Command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -75,6 +77,10 @@ public void dispose() {
this.setBackgroundPollInterval(-1);
}

@Override
public void handleCommand(final ChannelUID channelUID, final Command command) {
}

@Override
protected void pollForDeviceData(final ExpiringCache<String> cachedResponse) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.openhab.core.library.items.DateTimeItem;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.types.IncreaseDecreaseType;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ public void setHttpClient(@Nullable HttpClient httpClient) {
return md5Result.toString();
}

public void discoverDevices() {
VesyncRequestManagedDevicesPage reqDevPage = new VesyncRequestManagedDevicesPage(loggedInSession);
public void discoverDevices() throws AuthenticationException {

try {
VesyncRequestManagedDevicesPage reqDevPage = new VesyncRequestManagedDevicesPage(loggedInSession);
boolean finished = false;
int pageNo = 1;
HashMap<String, VesyncManagedDevicesPage.Result.VesyncManagedDeviceBase> generatedMacLookup = new HashMap<>();
Expand Down Expand Up @@ -132,11 +133,15 @@ public void discoverDevices() {
macLookup = Collections.unmodifiableMap(generatedMacLookup);
} catch (final AuthenticationException ae) {
logger.warn("Failed background device scan : {}", ae.getMessage());
throw ae;
}
}

public String reqV2Authorized(final String url, final String macId, final VesyncAuthenticatedRequest requestData)
throws AuthenticationException, DeviceUnknownException {

if (loggedInSession == null)
throw new AuthenticationException("User is not logged in");
// Apply current session authentication data
requestData.ApplyAuthentication(loggedInSession);

Expand Down Expand Up @@ -214,6 +219,7 @@ public synchronized void login(final @Nullable String username, final @Nullable
loggedInSession = processLogin(username, password, timezone).getUserSession();
} catch (final AuthenticationException ae) {
loggedInSession = null;
throw ae;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ protected void startScan() {
// If the bridge is not online no other thing devices can be found, so no reason to scan at this moment.
removeOlderResults(getTimestampOfLastScan());
if (ThingStatus.ONLINE.equals(bridgeHandler.getThing().getStatus())) {
bridgeHandler.runDeviceScanSequence();
// After the meta-data is updated it will call HandleMetadataRetrieved via the registered handler
bridgeHandler.runDeviceScanSequenceNoAuthErrors();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.binding.vesync.internal.dto.requests;

import org.openhab.binding.vesync.internal.AuthenticationException;
import org.openhab.binding.vesync.internal.dto.responses.VesyncLoginResponse;

import com.google.gson.annotations.SerializedName;
Expand All @@ -33,13 +34,18 @@ public VesyncAuthenticatedRequest() {
super();
}

public VesyncAuthenticatedRequest(final VesyncLoginResponse.VesyncUserSession user) {
public VesyncAuthenticatedRequest(final VesyncLoginResponse.VesyncUserSession user) throws AuthenticationException {
super();
if (user == null)
throw new AuthenticationException("User is not logged in");
this.token = user.getToken();
this.accountId = user.getAccountId();
}

public void ApplyAuthentication(final VesyncLoginResponse.VesyncUserSession userSession) {
public void ApplyAuthentication(final VesyncLoginResponse.VesyncUserSession userSession)
throws AuthenticationException {
if (userSession == null)
throw new AuthenticationException("User is not logged in");
this.accountId = userSession.getAccountId();
this.token = userSession.getToken();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.binding.vesync.internal.dto.requests;

import org.openhab.binding.vesync.internal.AuthenticationException;
import org.openhab.binding.vesync.internal.dto.responses.VesyncLoginResponse;

import com.google.gson.annotations.SerializedName;
Expand All @@ -38,12 +39,14 @@ public String getPageSize() {
@SerializedName("pageSize")
public String pageSize;

public VesyncRequestManagedDevicesPage(final VesyncLoginResponse.VesyncUserSession user) {
public VesyncRequestManagedDevicesPage(final VesyncLoginResponse.VesyncUserSession user)
throws AuthenticationException {
super(user);
method = "devices";
}

public VesyncRequestManagedDevicesPage(final VesyncLoginResponse.VesyncUserSession user, int pageNo, int pageSize) {
public VesyncRequestManagedDevicesPage(final VesyncLoginResponse.VesyncUserSession user, int pageNo, int pageSize)
throws AuthenticationException {
this(user);
this.pageNo = String.valueOf(pageNo);
this.pageSize = String.valueOf(pageSize);
Expand Down

0 comments on commit 0e5d6e5

Please sign in to comment.