Skip to content

Commit

Permalink
Addressed feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan committed Dec 24, 2020
1 parent cf367e0 commit aff25a9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.generacmobilelink.internal.GeneracMobileLinkBindingConstants;
import org.openhab.binding.generacmobilelink.internal.dto.GeneratorStatusDTO;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;

/**
* The {@link GeneracMobileLinkDiscoveryService} is responsible for discovering generator things
Expand Down Expand Up @@ -48,8 +52,13 @@ public boolean isBackgroundDiscoveryEnabled() {
return false;
}

@Override
public void thingDiscovered(DiscoveryResult result) {
super.thingDiscovered(result);
public void generatorDiscovered(GeneratorStatusDTO generator, ThingUID bridgeUID) {
DiscoveryResult result = DiscoveryResultBuilder
.create(new ThingUID(GeneracMobileLinkBindingConstants.THING_TYPE_GENERATOR, bridgeUID,
String.valueOf(generator.gensetID)))
.withLabel("MobileLink Generator " + generator.generatorName)
.withProperty("generatorId", String.valueOf(generator.gensetID))
.withRepresentationProperty("generatorId").withBridge(bridgeUID).build();
thingDiscovered(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import org.openhab.binding.generacmobilelink.internal.dto.GeneratorStatusResponseDTO;
import org.openhab.binding.generacmobilelink.internal.dto.LoginRequestDTO;
import org.openhab.binding.generacmobilelink.internal.dto.LoginResponseDTO;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
Expand Down Expand Up @@ -125,60 +123,56 @@ private void restartPoll() {
}

private void poll() {
if (authToken == null) {
logger.debug("Attempting Login");
login();
try {
if (authToken == null) {
logger.debug("Attempting Login");
login();
}
getStatuses(true);
} catch (InterruptedException e) {
}
getStatuses(true);
}

private synchronized void login() {
private synchronized void login() throws InterruptedException {
GeneracMobileLinkAccountConfiguration config = getConfigAs(GeneracMobileLinkAccountConfiguration.class);
refreshIntervalSeconds = config.refreshInterval;
try {
HTTPResult result = sendRequest(BASE_URL + "/Users/login", HttpMethod.POST, null,
new StringContentProvider(
gson.toJson(new LoginRequestDTO(SHARED_KEY, config.username, config.password))),
"application/json");
if (result.responseCode == HttpStatus.OK_200) {
LoginResponseDTO loginResponse = gson.fromJson(result.content, LoginResponseDTO.class);
if (loginResponse != null) {
authToken = loginResponse.authToken;
updateStatus(ThingStatus.ONLINE);
}
} else {
handleErrorResponse(result);
if (thing.getStatusInfo().getStatusDetail() == ThingStatusDetail.CONFIGURATION_ERROR) {
// bad credentials, stop trying to login
stopPoll();
}
HTTPResult result = sendRequest(BASE_URL + "/Users/login", HttpMethod.POST, null,
new StringContentProvider(
gson.toJson(new LoginRequestDTO(SHARED_KEY, config.username, config.password))),
"application/json");
if (result.responseCode == HttpStatus.OK_200) {
LoginResponseDTO loginResponse = gson.fromJson(result.content, LoginResponseDTO.class);
if (loginResponse != null) {
authToken = loginResponse.authToken;
updateStatus(ThingStatus.ONLINE);
}
} else {
handleErrorResponse(result);
if (thing.getStatusInfo().getStatusDetail() == ThingStatusDetail.CONFIGURATION_ERROR) {
// bad credentials, stop trying to login
stopPoll();
}
} catch (InterruptedException e) {
}
}

private void getStatuses(boolean retry) {
private void getStatuses(boolean retry) throws InterruptedException {
if (authToken == null) {
return;
}
try {
HTTPResult result = sendRequest(BASE_URL + "/Generator/GeneratorStatus", HttpMethod.GET, authToken, null,
null);
if (result.responseCode == HttpStatus.OK_200) {
generators = gson.fromJson(result.content, GeneratorStatusResponseDTO.class);
updateGeneratorThings();
if (getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE);
}
HTTPResult result = sendRequest(BASE_URL + "/Generator/GeneratorStatus", HttpMethod.GET, authToken, null, null);
if (result.responseCode == HttpStatus.OK_200) {
generators = gson.fromJson(result.content, GeneratorStatusResponseDTO.class);
updateGeneratorThings();
if (getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE);
}
} else {
if (retry) {
logger.debug("Retrying status request");
getStatuses(false);
} else {
if (retry) {
logger.debug("Retrying status request");
getStatuses(false);
} else {
handleErrorResponse(result);
}
handleErrorResponse(result);
}
} catch (InterruptedException e) {
}
}

Expand Down Expand Up @@ -234,7 +228,7 @@ private void updateGeneratorThings() {
Thing thing = getThing().getThing(new ThingUID(GeneracMobileLinkBindingConstants.THING_TYPE_GENERATOR,
getThing().getUID(), String.valueOf(generator.gensetID)));
if (thing == null) {
generatorDiscovered(generator);
discoveryService.generatorDiscovered(generator, getThing().getUID());
} else {
ThingHandler handler = thing.getHandler();
if (handler != null) {
Expand All @@ -245,16 +239,6 @@ private void updateGeneratorThings() {
}
}

private void generatorDiscovered(GeneratorStatusDTO generator) {
DiscoveryResult result = DiscoveryResultBuilder
.create(new ThingUID(GeneracMobileLinkBindingConstants.THING_TYPE_GENERATOR, getThing().getUID(),
String.valueOf(generator.gensetID)))
.withLabel("MobileLink Generator " + generator.generatorName)
.withProperty("generatorId", String.valueOf(generator.gensetID))
.withRepresentationProperty("generatorId").withBridge(getThing().getUID()).build();
discoveryService.thingDiscovered(result);
}

public static class HTTPResult {
public @Nullable String content;
public final int responseCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<bridge-type-ref id="account"/>
</supported-bridge-type-refs>
<label>MobileLink Generator</label>
<description>Generator</description>
<description>MobileLink Generator</description>
<channels>
<channel id="connected" typeId="connected"/>
<channel id="greenLight" typeId="greenLight"/>
Expand Down

0 comments on commit aff25a9

Please sign in to comment.