Skip to content

Commit

Permalink
[netatmo] Null annotations Part 2 of 3
Browse files Browse the repository at this point in the history
Related to openhab#7913

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo committed Jun 27, 2020
1 parent 9f4f876 commit 194665a
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
*/
package org.openhab.binding.netatmo.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* This class holds various unit/measurement conversion methods
*
* @author Gaël L'hopital - Initial contribution
* @author Rob Nielsen - updated heat index
*/
@NonNullByDefault
public class WeatherUtils {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.List;
import java.util.Optional;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.types.State;
import org.openhab.binding.netatmo.internal.ChannelTypeUtils;
Expand All @@ -33,8 +35,9 @@
* @author Gaël L'hopital - Initial contribution
*
*/
@NonNullByDefault
public class MeasurableChannels {
protected NAMeasureResponse measures;
protected @Nullable NAMeasureResponse measures;
protected List<String> measuredChannels = new ArrayList<>();

/*
Expand All @@ -59,9 +62,10 @@ protected void removeChannel(ChannelUID channelUID) {

protected Optional<State> getNAThingProperty(String channelId) {
int index = measuredChannels.indexOf(channelId);
if (index != -1 && measures != null) {
if (!measures.getBody().isEmpty()) {
List<List<Float>> valueList = measures.getBody().get(0).getValue();
NAMeasureResponse theMeasures = measures;
if (index != -1 && theMeasures != null) {
if (!theMeasures.getBody().isEmpty()) {
List<List<Float>> valueList = theMeasures.getBody().get(0).getValue();
if (!valueList.isEmpty()) {
List<Float> values = valueList.get(0);
if (values.size() >= index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ public void initialize() {

private void connectionSucceed() {
updateStatus(ThingStatus.ONLINE);
WelcomeWebHookServlet webHookServlet = this.webHookServlet;
WelcomeWebHookServlet servlet = webHookServlet;
String webHookURI = getWebHookURI();
WelcomeApi welcomeApi = getWelcomeApi();
if (welcomeApi != null && webHookServlet != null && webHookURI != null) {
webHookServlet.activate(this);
if (welcomeApi != null && servlet != null && webHookURI != null) {
servlet.activate(this);
logger.debug("Setting up Netatmo Welcome WebHook");
welcomeApi.addwebhook(webHookURI, WEBHOOK_APP);
}
Expand Down Expand Up @@ -226,39 +226,39 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}

public @Nullable PartnerApi getPartnerApi() {
APIMap apiMap = this.apiMap;
return apiMap != null ? (PartnerApi) apiMap.get(PartnerApi.class) : null;
APIMap map = apiMap;
return map != null ? (PartnerApi) map.get(PartnerApi.class) : null;
}

private @Nullable StationApi getStationApi() {
APIMap apiMap = this.apiMap;
return apiMap != null ? (StationApi) apiMap.get(StationApi.class) : null;
APIMap map = apiMap;
return map != null ? (StationApi) map.get(StationApi.class) : null;
}

private @Nullable HealthyhomecoachApi getHomeCoachApi() {
APIMap apiMap = this.apiMap;
return apiMap != null ? (HealthyhomecoachApi) apiMap.get(HealthyhomecoachApi.class) : null;
APIMap map = apiMap;
return map != null ? (HealthyhomecoachApi) map.get(HealthyhomecoachApi.class) : null;
}

public @Nullable ThermostatApi getThermostatApi() {
APIMap apiMap = this.apiMap;
return apiMap != null ? (ThermostatApi) apiMap.get(ThermostatApi.class) : null;
APIMap map = apiMap;
return map != null ? (ThermostatApi) map.get(ThermostatApi.class) : null;
}

public @Nullable WelcomeApi getWelcomeApi() {
APIMap apiMap = this.apiMap;
return apiMap != null ? (WelcomeApi) apiMap.get(WelcomeApi.class) : null;
APIMap map = apiMap;
return map != null ? (WelcomeApi) map.get(WelcomeApi.class) : null;
}

@Override
public void dispose() {
logger.debug("Running dispose()");

WelcomeWebHookServlet webHookServlet = this.webHookServlet;
WelcomeWebHookServlet servlet = webHookServlet;
WelcomeApi welcomeApi = getWelcomeApi();
if (welcomeApi != null && webHookServlet != null && getWebHookURI() != null) {
if (welcomeApi != null && servlet != null && getWebHookURI() != null) {
logger.debug("Releasing Netatmo Welcome WebHook");
webHookServlet.deactivate();
servlet.deactivate();
welcomeApi.dropwebhook(WEBHOOK_APP);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.library.types.DecimalType;
Expand All @@ -47,16 +47,16 @@
*
* @author Gaël L'hopital - Initial contribution
*/
@NonNullByDefault
public abstract class NetatmoDeviceHandler<DEVICE> extends AbstractNetatmoThingHandler {

private static final int MIN_REFRESH_INTERVAL = 2000;
private static final int DEFAULT_REFRESH_INTERVAL = 300000;

private Logger logger = LoggerFactory.getLogger(NetatmoDeviceHandler.class);
private ScheduledFuture<?> refreshJob;
private RefreshStrategy refreshStrategy;
@Nullable
protected DEVICE device;
private final Logger logger = LoggerFactory.getLogger(NetatmoDeviceHandler.class);
private @Nullable ScheduledFuture<?> refreshJob;
private @Nullable RefreshStrategy refreshStrategy;
protected @Nullable DEVICE device;
protected Map<String, Object> childs = new ConcurrentHashMap<>();

public NetatmoDeviceHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
Expand All @@ -71,13 +71,18 @@ protected void initializeThing() {
}

private void scheduleRefreshJob() {
long delay = refreshStrategy.nextRunDelayInS();
RefreshStrategy strategy = refreshStrategy;
if (strategy == null) {
return;
}
long delay = strategy.nextRunDelayInS();
logger.debug("Scheduling update channel thread in {} s", delay);
refreshJob = scheduler.schedule(() -> {
updateChannels();
if (refreshJob != null && !refreshJob.isCancelled()) {
ScheduledFuture<?> job = refreshJob;
if (job != null) {
logger.debug("cancel refresh job");
refreshJob.cancel(false);
job.cancel(false);
refreshJob = null;
}
scheduleRefreshJob();
Expand All @@ -87,9 +92,10 @@ private void scheduleRefreshJob() {
@Override
public void dispose() {
logger.debug("Running dispose()");
if (refreshJob != null && !refreshJob.isCancelled()) {
ScheduledFuture<?> job = refreshJob;
if (job != null) {
logger.debug("cancel refresh job");
refreshJob.cancel(true);
job.cancel(true);
refreshJob = null;
}
}
Expand All @@ -101,12 +107,14 @@ protected void updateProperties(DEVICE deviceData) {

@Override
protected void updateChannels() {
if (refreshStrategy != null) {
logger.debug("Data aged of {} s", refreshStrategy.dataAge() / 1000);
if (refreshStrategy.isDataOutdated()) {
RefreshStrategy strategy = refreshStrategy;
if (strategy != null) {
logger.debug("Data aged of {} s", strategy.dataAge() / 1000);
if (strategy.isDataOutdated()) {
logger.debug("Trying to update channels on device {}", getId());
childs.clear();

@Nullable
DEVICE newDeviceReading = null;
try {
newDeviceReading = updateReadings();
Expand All @@ -123,14 +131,14 @@ protected void updateChannels() {
if (newDeviceReading != null) {
updateStatus(ThingStatus.ONLINE);
logger.debug("Successfully updated device {} readings! Now updating channels", getId());
DEVICE device = newDeviceReading;
this.device = device;
updateProperties(device);
DEVICE theDevice = newDeviceReading;
this.device = theDevice;
updateProperties(theDevice);
Integer dataTimeStamp = getDataTimestamp();
if (dataTimeStamp != null) {
refreshStrategy.setDataTimeStamp(dataTimeStamp, timeZoneProvider.getTimeZone());
strategy.setDataTimeStamp(dataTimeStamp, timeZoneProvider.getTimeZone());
}
radioHelper.ifPresent(helper -> helper.setModule(device));
radioHelper.ifPresent(helper -> helper.setModule(theDevice));
NetatmoBridgeHandler handler = getBridgeHandler();
if (handler != null) {
handler.checkForNewThings(newDeviceReading);
Expand All @@ -154,21 +162,23 @@ protected void updateChannels() {
}

@Override
protected State getNAThingProperty(@NonNull String channelId) {
protected State getNAThingProperty(String channelId) {
try {
@Nullable
DEVICE theDevice = device;
switch (channelId) {
case CHANNEL_LAST_STATUS_STORE:
if (device != null) {
Method getLastStatusStore = device.getClass().getMethod("getLastStatusStore");
Integer lastStatusStore = (Integer) getLastStatusStore.invoke(device);
if (theDevice != null) {
Method getLastStatusStore = theDevice.getClass().getMethod("getLastStatusStore");
Integer lastStatusStore = (Integer) getLastStatusStore.invoke(theDevice);
return ChannelTypeUtils.toDateTimeType(lastStatusStore, timeZoneProvider.getTimeZone());
} else {
return UnDefType.UNDEF;
}
case CHANNEL_LOCATION:
if (device != null) {
Method getPlace = device.getClass().getMethod("getPlace");
NAPlace place = (NAPlace) getPlace.invoke(device);
if (theDevice != null) {
Method getPlace = theDevice.getClass().getMethod("getPlace");
NAPlace place = (NAPlace) getPlace.invoke(theDevice);
PointType point = new PointType(new DecimalType(place.getLocation().get(1)),
new DecimalType(place.getLocation().get(0)));
if (place.getAltitude() != null) {
Expand Down Expand Up @@ -231,6 +241,9 @@ private void defineRefreshInterval() {
protected abstract @Nullable Integer getDataTimestamp();

public void expireData() {
refreshStrategy.expireData();
RefreshStrategy strategy = refreshStrategy;
if (strategy != null) {
strategy.expireData();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import static org.openhab.binding.netatmo.internal.ChannelTypeUtils.*;
import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.thing.Thing;
Expand All @@ -32,14 +32,15 @@
* @author Michael Svinth - Initial contribution OH2 version
*
*/
@NonNullByDefault
public class NAHealthyHomeCoachHandler extends NetatmoDeviceHandler<NAHealthyHomeCoach> {

public NAHealthyHomeCoachHandler(@NonNull Thing thing, final TimeZoneProvider timeZoneProvider) {
public NAHealthyHomeCoachHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
super(thing, timeZoneProvider);
}

@Override
protected NAHealthyHomeCoach updateReadings() {
protected @Nullable NAHealthyHomeCoach updateReadings() {
NAHealthyHomeCoach result = null;
NAHealthyHomeCoachDataBody homecoachDataBody = getBridgeHandler().getHomecoachDataBody(getId());
if (homecoachDataBody != null) {
Expand All @@ -54,9 +55,10 @@ protected void updateProperties(NAHealthyHomeCoach deviceData) {
}

@Override
protected State getNAThingProperty(@NonNull String channelId) {
if (device != null) {
NADashboardData dashboardData = device.getDashboardData();
protected State getNAThingProperty(String channelId) {
NAHealthyHomeCoach healthyHomeCoachDevice = device;
if (healthyHomeCoachDevice != null) {
NADashboardData dashboardData = healthyHomeCoachDevice.getDashboardData();
switch (channelId) {
case CHANNEL_CO2:
return toQuantityType(dashboardData.getCO2(), API_CO2_UNIT);
Expand Down Expand Up @@ -91,7 +93,7 @@ protected State getNAThingProperty(@NonNull String channelId) {
return super.getNAThingProperty(channelId);
}

private String toHealthIndexString(Integer healthIndex) {
private @Nullable String toHealthIndexString(@Nullable Integer healthIndex) {
if (healthIndex == null) {
return null;
}
Expand All @@ -113,8 +115,9 @@ private String toHealthIndexString(Integer healthIndex) {

@Override
protected @Nullable Integer getDataTimestamp() {
if (device != null) {
Integer lastStored = device.getLastStatusStore();
NAHealthyHomeCoach healthyHomeCoachDevice = device;
if (healthyHomeCoachDevice != null) {
Integer lastStored = healthyHomeCoachDevice.getLastStatusStore();
if (lastStored != null) {
return lastStored;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.i18n.TimeZoneProvider;
import org.eclipse.smarthome.core.thing.Thing;
Expand All @@ -43,6 +43,7 @@
* @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules
*
*/
@NonNullByDefault
public class NAMainHandler extends NetatmoDeviceHandler<NAMain> {
private Map<String, Float> channelMeasurements = new ConcurrentHashMap<>();

Expand All @@ -51,7 +52,7 @@ public NAMainHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
}

@Override
protected NAMain updateReadings() {
protected @Nullable NAMain updateReadings() {
NAMain result = null;
NAStationDataBody stationDataBody = getBridgeHandler().getStationsDataBody(getId());
if (stationDataBody != null) {
Expand Down Expand Up @@ -167,9 +168,10 @@ private void updateMonthMeasurements() {
}

@Override
protected State getNAThingProperty(@NonNull String channelId) {
if (device != null) {
NADashboardData dashboardData = device.getDashboardData();
protected State getNAThingProperty(String channelId) {
NAMain mainDevice = device;
if (mainDevice != null) {
NADashboardData dashboardData = mainDevice.getDashboardData();
if (dashboardData != null) {
switch (channelId) {
case CHANNEL_CO2:
Expand Down Expand Up @@ -288,8 +290,9 @@ protected State getNAThingProperty(@NonNull String channelId) {

@Override
protected @Nullable Integer getDataTimestamp() {
if (device != null) {
Integer lastStored = device.getLastStatusStore();
NAMain mainDevice = device;
if (mainDevice != null) {
Integer lastStored = mainDevice.getLastStatusStore();
if (lastStored != null) {
return lastStored;
}
Expand Down
Loading

0 comments on commit 194665a

Please sign in to comment.