Skip to content

Commit

Permalink
workaround for "Potential null pointer access" warning
Browse files Browse the repository at this point in the history
  • Loading branch information
sibbi77 committed Mar 21, 2021
1 parent b44b798 commit fe1d721
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ private Map<String, String> getValues(String url) throws Exception {
BufferedReader reader = null;
try {
URLConnection connection = new URL(url).openConnection();
if (cookies != null) {
for (String cookie : cookies) {
var localCookies = cookies;
if (localCookies != null) {
for (String cookie : localCookies) {
connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
}
Expand Down Expand Up @@ -264,8 +265,9 @@ public int setValue(String tag, int value) throws Exception {
BufferedReader reader = null;
try {
URLConnection connection = new URL(url).openConnection();
if (cookies != null) {
for (String cookie : cookies) {
var localCookies = cookies;
if (localCookies != null) {
for (String cookie : localCookies) {
connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,20 @@ private void updateChannel(String tag, String value_str) {

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
if (connector == null) {
var localConnector = connector;
if (localConnector == null) {
// the binding was not initialized, yet
return;
}
if (command instanceof RefreshType) {
// request a refresh of a channel
try {
EcoTouchTags tag = EcoTouchTags.fromString(channelUID.getId());
String valueStr = connector.getValue(tag.getTagName());
updateChannel(tag.getTagName(), valueStr);
updateStatus(ThingStatus.ONLINE);
if (tag != null) {
String valueStr = localConnector.getValue(tag.getTagName());
updateChannel(tag.getTagName(), valueStr);
updateStatus(ThingStatus.ONLINE);
}
} catch (Exception e) {
}
} else {
Expand All @@ -108,7 +111,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
// this type needs special treatment
QuantityType<?> value = (QuantityType<?>) command;
int raw = Math.round(value.floatValue() * 2 + 4);
connector.setValue(ecoTouchTag.getTagName(), raw);
localConnector.setValue(ecoTouchTag.getTagName(), raw);
} else {
if (ecoTouchTag.getUnit() != ONE) {
if (command instanceof QuantityType) {
Expand All @@ -118,7 +121,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
if (rawUnit != null) {
int raw = rawUnit.intValue();
raw *= ecoTouchTag.getDivisor();
connector.setValue(ecoTouchTag.getTagName(), raw);
localConnector.setValue(ecoTouchTag.getTagName(), raw);
}
} else {
logger.debug("handleCommand: requires a QuantityType");
Expand All @@ -130,7 +133,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
BigDecimal decimal = decimalType.toBigDecimal();
decimal = decimal.multiply(new BigDecimal(ecoTouchTag.getDivisor()));
int raw = decimal.intValue();
connector.setValue(ecoTouchTag.getTagName(), raw);
localConnector.setValue(ecoTouchTag.getTagName(), raw);
} else {
logger.debug("cannot convert {} to a DecimalType", state);
}
Expand All @@ -146,12 +149,23 @@ public void handleCommand(ChannelUID channelUID, Command command) {
public void initialize() {
config = getConfigAs(EcoTouchConfiguration.class);

connector = new EcoTouchConnector(config.ip, config.username, config.password);
var localConfig = config;
if (localConfig == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
return;
}

connector = new EcoTouchConnector(localConfig.ip, localConfig.username, localConfig.password);

scheduler.execute(() -> {
try {
// try to get a single value
connector.getValue("A1");
var localConnector = connector;
if (localConnector == null) {
updateStatus(ThingStatus.OFFLINE);
return;
}
localConnector.getValue("A1");
} catch (IOException io) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, io.toString());
return;
Expand All @@ -168,7 +182,8 @@ public void initialize() {
}

private void startAutomaticRefresh() {
if (refreshJob == null || refreshJob.isCancelled()) {
var localRefreshJob = refreshJob;
if (localRefreshJob == null || localRefreshJob.isCancelled()) {
Runnable runnable = () -> {
try {
Set<String> tags = new HashSet<String>();
Expand All @@ -178,12 +193,15 @@ private void startAutomaticRefresh() {
if (linked)
tags.add(ecoTouchTag.getTagName());
}
Map<String, String> result = connector.getValues(tags);

Iterator<Map.Entry<String, String>> it = result.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pair = it.next();
updateChannel(pair.getKey(), pair.getValue());
var localConnector = connector;
if (localConnector != null) {
Map<String, String> result = localConnector.getValues(tags);

Iterator<Map.Entry<String, String>> it = result.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pair = it.next();
updateChannel(pair.getKey(), pair.getValue());
}
}
} catch (IOException io) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, io.toString());
Expand All @@ -198,17 +216,21 @@ private void startAutomaticRefresh() {
}
};

refreshJob = scheduler.scheduleWithFixedDelay(runnable, 10, config.refresh, TimeUnit.SECONDS);
var localConfig = config;
if (localConfig != null)
refreshJob = scheduler.scheduleWithFixedDelay(runnable, 10, localConfig.refresh, TimeUnit.SECONDS);
}
}

@Override
public void dispose() {
if (refreshJob != null && !refreshJob.isCancelled()) {
refreshJob.cancel(true);
refreshJob = null;
var localRefreshJob = refreshJob;
if (localRefreshJob != null && !localRefreshJob.isCancelled()) {
localRefreshJob.cancel(true);
localRefreshJob = null;
}
if (connector != null)
connector.logout();
var localConnector = connector;
if (localConnector != null)
localConnector.logout();
}
}

0 comments on commit fe1d721

Please sign in to comment.