Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Commit

Permalink
[siemensrds] RdsCloudHandler initialization made asynchronous; Fewer …
Browse files Browse the repository at this point in the history
…logger.info()… (#6157)

* RdsCloudHandler initialization made asynchronous
* Fixed bug in logging code; Force refresh of Online status
* Reduced chattiness of state change messages
* Removed unnecessary String.format() calls..
* Log warnings for exceptions should not dump the actual exception

Signed-off-by: Andrew Fiddian-Green <software@whitebear.ch>
  • Loading branch information
andrewfg authored and martinvw committed Oct 5, 2019
1 parent e0d26c5 commit 04a61e0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 33 deletions.
Expand Up @@ -114,7 +114,7 @@ private static String httpGetTokenJson(String apiKey, String user, String passwo
String json = httpGetTokenJson(apiKey, user, password);
return GSON.fromJson(json, RdsAccessToken.class);
} catch (JsonSyntaxException | RdsCloudException | IOException e) {
LOGGER.warn("token creation error \"{}\"", e.getMessage(), e);
LOGGER.warn("create {}: \"{}\"", e.getClass().getName(), e.getMessage());
return null;
}
}
Expand Down
Expand Up @@ -53,19 +53,19 @@ public void initialize() {

if (config == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"missing configuration, status => offline!");
"missing configuration");
return;
}

if (config.userEmail.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"missing email address, status => offline!");
"missing email address");
return;
}

if (config.userPassword.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"missing password, status => offline!");
"missing password");
return;
}

Expand All @@ -74,12 +74,10 @@ public void initialize() {

if (config.pollingInterval < FAST_POLL_INTERVAL || config.pollingInterval > LAZY_POLL_INTERVAL) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
String.format("polling interval out of range [%d..%d], status => offline!", FAST_POLL_INTERVAL,
String.format("polling interval out of range [%d..%d]", FAST_POLL_INTERVAL,
LAZY_POLL_INTERVAL));
return;
}

refreshToken();
}

@Override
Expand All @@ -106,12 +104,13 @@ private synchronized void refreshToken() {

if (accessToken != null) {
if (getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "server responded, status => online..");
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE,
"cloud server responded");
}
} else {
if (getThing().getStatus() == ThingStatus.ONLINE) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"server authentication error, status => offline!");
"cloud server authentication error");
}
}
}
Expand Down
Expand Up @@ -129,9 +129,9 @@ protected static String httpGenericGetJson(String apiKey, String token, String u
LOGGER.trace("create: response={}", json);
}

return GSON.fromJson(json, RdsDataPoints.class);
return GSON.fromJson(json, RdsDataPoints.class);
} catch (JsonSyntaxException | RdsCloudException | IOException e) {
LOGGER.warn("point list creation error \"{}\"", e.getMessage(), e);
LOGGER.warn("create {}: \"{}\"", e.getClass().getName(), e.getMessage());
return null;
}
}
Expand Down Expand Up @@ -300,7 +300,7 @@ public void setValue(String apiKey, String token, String hierarchyName, String v
try {
httpSetPointValueJson(apiKey, token, pointId, json);
} catch (RdsCloudException | IOException e) {
LOGGER.warn("setValue: error \"{}\"", e.getMessage(), e);
LOGGER.warn("setValue {} {}: \"{}\"", hierarchyName, e.getClass().getName(), e.getMessage());
return;
}
} else {
Expand All @@ -324,6 +324,15 @@ public boolean refresh(String apiKey, String token) {
set.add(String.format("\"%s\"", pointId));
}
}

for (Map.Entry<String, BasePoint> entry : points.entrySet()) {
BasePoint point = entry.getValue();
if (point != null && point.memberName != null && point.memberName.equals("Online")) {
set.add(String.format("\"%s\"", entry.getKey()));
break;
}
}

valueFilter = String.join(",", set);
}

Expand Down Expand Up @@ -371,7 +380,7 @@ public boolean refresh(String apiKey, String token) {

return true;
} catch (JsonSyntaxException | RdsCloudException | IOException e) {
LOGGER.warn("refresh: error \"{}\"", e.getMessage());
LOGGER.warn("refresh {}: \"{}\"", e.getClass().getName(), e.getMessage());
return false;
}
}
Expand Down
Expand Up @@ -65,15 +65,17 @@ public void deactivate() {

@Override
protected void startScan() {
if (cloud.getThing().getStatus() != ThingStatus.ONLINE) {
cloud.getToken();
}
if (cloud.getThing().getStatus() == ThingStatus.ONLINE) {
discoverPlants();
}
}

@Override
protected void startBackgroundDiscovery() {
String msg = "start background discovery..";
logger.info(msg);
logger.debug("start background discovery..");

if (discoveryScheduler == null || discoveryScheduler.isCancelled()) {
discoveryScheduler = scheduler.scheduleWithFixedDelay(this::startScan, 10,
Expand All @@ -83,8 +85,7 @@ protected void startBackgroundDiscovery() {

@Override
protected void stopBackgroundDiscovery() {
String msg = "stop background discovery..";
logger.info(msg);
logger.debug("stop background discovery..");

if (discoveryScheduler != null && !discoveryScheduler.isCancelled()) {
discoveryScheduler.cancel(true);
Expand Down
Expand Up @@ -74,27 +74,27 @@ public void handleCommand(ChannelUID channelUID, Command command) {

@Override
public void initialize() {
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING, "status => unknown..");
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.CONFIGURATION_PENDING);

config = getConfigAs(RdsConfiguration.class);

if (config == null || config.plantId == null || config.plantId.isEmpty()) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"missing Plant Id, status => offline!");
"missing Plant Id");
return;
}

RdsCloudHandler cloud = getCloudHandler();

if (cloud == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"missing cloud handler, status => offline!");
"missing cloud server handler");
return;
}

if (cloud.getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
"cloud handler not online, status => offline!");
"cloud server offline");
return;
}

Expand All @@ -108,8 +108,6 @@ public void initializePolling() {
int pollInterval = cloud.getPollInterval();

if (pollInterval > 0) {
logger.info("creating polling timers..");

// create a "lazy" polling scheduler
if (lazyPollingScheduler == null || lazyPollingScheduler.isCancelled()) {
lazyPollingScheduler = scheduler.scheduleWithFixedDelay(this::lazyPollingSchedulerExecute,
Expand Down Expand Up @@ -139,8 +137,6 @@ public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {

@Override
public void dispose() {
logger.info("disposing polling timers..");

// clean up the lazy polling scheduler
if (lazyPollingScheduler != null && !lazyPollingScheduler.isCancelled()) {
lazyPollingScheduler.cancel(true);
Expand Down Expand Up @@ -192,18 +188,19 @@ private void doPollNow() {

if (cloud == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"missing cloud handler, status => offline!");
"missing cloud server handler");
return;
}

String token = cloud.getToken();

if (cloud.getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
"cloud handler offline, status => offline!");
"cloud server offline");
return;
}

String apiKey = cloud.getApiKey();
String token = cloud.getToken();

if (points == null || (!points.refresh(apiKey, token))) {
points = RdsDataPoints.create(apiKey, token, config.plantId);
Expand All @@ -212,22 +209,22 @@ private void doPollNow() {
if (points == null) {
if (getThing().getStatus() == ThingStatus.ONLINE) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
String.format("server has no info for %s, status => offline!", getThing().getLabel()));
"cloud server has no info this device");
}
return;
}

if (!points.isOnline()) {
if (getThing().getStatus() == ThingStatus.ONLINE) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
String.format("server reports %s offline, status => offline!", getThing().getLabel()));
"cloud server reports device offline");
}
return;
}

if (getThing().getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE,
String.format("received info for %s from cloud server, status => online..", getThing().getLabel()));
"received info from cloud server");
}

for (ChannelMap chan : CHAN_MAP) {
Expand Down Expand Up @@ -352,7 +349,7 @@ private synchronized void doHandleCommand(String channelId, Command command) {
}

/*
* private method: returns the cloud handler
* private method: returns the cloud server handler
*/
private RdsCloudHandler getCloudHandler() {
@Nullable Bridge b;
Expand Down
Expand Up @@ -66,7 +66,7 @@ public Boolean isOnline() {
String json = RdsDataPoints.httpGenericGetJson(apiKey, token, URL_PLANTS);
return GSON.fromJson(json, RdsPlants.class);
} catch (JsonSyntaxException | RdsCloudException | IOException e) {
LOGGER.warn("plant list creation error \"{}\"", e.getMessage(), e);
LOGGER.warn("create {}: \"{}\"", e.getClass().getName(), e.getMessage());
return null;
}
}
Expand Down

0 comments on commit 04a61e0

Please sign in to comment.