From fb80f176918aa8fa8be54cc39a0c7e3a59a1cfb1 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sun, 28 Jun 2020 08:40:42 +0200 Subject: [PATCH] [netatmo] Null annotations Part 2 of 3 (#8019) Related to #7913 Signed-off-by: Laurent Garnier --- .../netatmo/internal/WeatherUtils.java | 3 + .../handler/AbstractNetatmoThingHandler.java | 9 +- .../internal/handler/MeasurableChannels.java | 12 ++- .../handler/NetatmoBridgeHandler.java | 32 +++--- .../handler/NetatmoDeviceHandler.java | 102 ++++++++++-------- .../homecoach/NAHealthyHomeCoachHandler.java | 26 +++-- .../internal/station/NAMainHandler.java | 39 ++++--- .../internal/station/NAModule1Handler.java | 17 +-- .../internal/station/NAModule2Handler.java | 10 +- .../internal/station/NAModule3Handler.java | 20 ++-- .../internal/station/NAModule4Handler.java | 17 +-- .../internal/thermostat/NAPlugHandler.java | 29 +++-- .../internal/thermostat/NATherm1Handler.java | 63 ++++++----- .../webhook/WelcomeWebHookServlet.java | 18 +++- 14 files changed, 236 insertions(+), 161 deletions(-) diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/WeatherUtils.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/WeatherUtils.java index 4d04bff509011..32d1fce9b3e71 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/WeatherUtils.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/WeatherUtils.java @@ -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 { /** diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/AbstractNetatmoThingHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/AbstractNetatmoThingHandler.java index e6dc8117da4bf..aaa8e1d4fc47f 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/AbstractNetatmoThingHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/AbstractNetatmoThingHandler.java @@ -236,8 +236,13 @@ protected void updateProperties(Integer firmware, String modelId) { public void updateMeasurements() { } - public void getMeasurements(NetatmoBridgeHandler handler, String device, @Nullable String module, String scale, - List types, List channels, Map channelMeasurements) { + public void getMeasurements(String device, @Nullable String module, String scale, List types, + List channels, Map channelMeasurements) { + NetatmoBridgeHandler handler = getBridgeHandler(); + if (handler == null) { + return; + } + if (types.size() != channels.size()) { throw new IllegalArgumentException("types and channels lists are different sizes."); } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/MeasurableChannels.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/MeasurableChannels.java index 2ac60a287d918..b98055e715806 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/MeasurableChannels.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/MeasurableChannels.java @@ -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; @@ -33,8 +35,9 @@ * @author Gaël L'hopital - Initial contribution * */ +@NonNullByDefault public class MeasurableChannels { - protected NAMeasureResponse measures; + protected @Nullable NAMeasureResponse measures; protected List measuredChannels = new ArrayList<>(); /* @@ -59,9 +62,10 @@ protected void removeChannel(ChannelUID channelUID) { protected Optional getNAThingProperty(String channelId) { int index = measuredChannels.indexOf(channelId); - if (index != -1 && measures != null) { - if (!measures.getBody().isEmpty()) { - List> valueList = measures.getBody().get(0).getValue(); + NAMeasureResponse theMeasures = measures; + if (index != -1 && theMeasures != null) { + if (!theMeasures.getBody().isEmpty()) { + List> valueList = theMeasures.getBody().get(0).getValue(); if (!valueList.isEmpty()) { List values = valueList.get(0); if (values.size() >= index) { diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoBridgeHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoBridgeHandler.java index 7c81f16b44b0e..da0fbc5868c8d 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoBridgeHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoBridgeHandler.java @@ -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); } @@ -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); } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java index ab31d0d807891..37f2ef5769fd8 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/handler/NetatmoDeviceHandler.java @@ -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; @@ -47,16 +47,16 @@ * * @author Gaël L'hopital - Initial contribution */ +@NonNullByDefault public abstract class NetatmoDeviceHandler 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 childs = new ConcurrentHashMap<>(); public NetatmoDeviceHandler(Thing thing, final TimeZoneProvider timeZoneProvider) { @@ -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(); @@ -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; } } @@ -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(); @@ -120,31 +128,33 @@ protected void updateChannels() { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Unable to connect Netatmo API : " + e.getLocalizedMessage()); } + NetatmoBridgeHandler bridgeHandler = getBridgeHandler(); 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)); - NetatmoBridgeHandler handler = getBridgeHandler(); - if (handler != null) { - handler.checkForNewThings(newDeviceReading); + radioHelper.ifPresent(helper -> helper.setModule(theDevice)); + if (bridgeHandler != null) { + bridgeHandler.checkForNewThings(newDeviceReading); } } else { logger.debug("Failed to update device {} readings! Skip updating channels", getId()); } // Be sure that all channels for the modules will be updated with refreshed data - childs.forEach((childId, moduleData) -> { - Optional childHandler = getBridgeHandler().findNAThing(childId); - childHandler.map(NetatmoModuleHandler.class::cast).ifPresent(naChildModule -> { - naChildModule.setRefreshRequired(true); + if (bridgeHandler != null) { + childs.forEach((childId, moduleData) -> { + Optional childHandler = bridgeHandler.findNAThing(childId); + childHandler.map(NetatmoModuleHandler.class::cast).ifPresent(naChildModule -> { + naChildModule.setRefreshRequired(true); + }); }); - }); + } } else { logger.debug("Data still valid for device {}", getId()); } @@ -154,21 +164,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) { @@ -188,14 +200,17 @@ protected State getNAThingProperty(@NonNull String channelId) { } private void updateChildModules() { - logger.debug("Updating child modules of {}", getId()); - childs.forEach((childId, moduleData) -> { - Optional childHandler = getBridgeHandler().findNAThing(childId); - childHandler.map(NetatmoModuleHandler.class::cast).ifPresent(naChildModule -> { - logger.debug("Updating child module {}", naChildModule.getId()); - naChildModule.updateChannels(moduleData); + NetatmoBridgeHandler bridgeHandler = getBridgeHandler(); + if (bridgeHandler != null) { + logger.debug("Updating child modules of {}", getId()); + childs.forEach((childId, moduleData) -> { + Optional childHandler = bridgeHandler.findNAThing(childId); + childHandler.map(NetatmoModuleHandler.class::cast).ifPresent(naChildModule -> { + logger.debug("Updating child module {}", naChildModule.getId()); + naChildModule.updateChannels(moduleData); + }); }); - }); + } } /* @@ -231,6 +246,9 @@ private void defineRefreshInterval() { protected abstract @Nullable Integer getDataTimestamp(); public void expireData() { - refreshStrategy.expireData(); + RefreshStrategy strategy = refreshStrategy; + if (strategy != null) { + strategy.expireData(); + } } } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/homecoach/NAHealthyHomeCoachHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/homecoach/NAHealthyHomeCoachHandler.java index ad28e769347d1..a8f78bd4aec59 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/homecoach/NAHealthyHomeCoachHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/homecoach/NAHealthyHomeCoachHandler.java @@ -15,11 +15,12 @@ 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; import org.eclipse.smarthome.core.types.State; +import org.openhab.binding.netatmo.internal.handler.NetatmoBridgeHandler; import org.openhab.binding.netatmo.internal.handler.NetatmoDeviceHandler; import io.swagger.client.model.NADashboardData; @@ -32,16 +33,19 @@ * @author Michael Svinth - Initial contribution OH2 version * */ +@NonNullByDefault public class NAHealthyHomeCoachHandler extends NetatmoDeviceHandler { - 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()); + NetatmoBridgeHandler bridgeHandler = getBridgeHandler(); + NAHealthyHomeCoachDataBody homecoachDataBody = bridgeHandler == null ? null + : bridgeHandler.getHomecoachDataBody(getId()); if (homecoachDataBody != null) { result = homecoachDataBody.getDevices().get(0); } @@ -54,9 +58,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); @@ -91,7 +96,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; } @@ -113,8 +118,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; } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAMainHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAMainHandler.java index 8f4643e2f3664..b0e07fb3c85f9 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAMainHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAMainHandler.java @@ -21,13 +21,14 @@ 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; import org.eclipse.smarthome.core.types.State; import org.openhab.binding.netatmo.internal.WeatherUtils; import org.openhab.binding.netatmo.internal.handler.AbstractNetatmoThingHandler; +import org.openhab.binding.netatmo.internal.handler.NetatmoBridgeHandler; import org.openhab.binding.netatmo.internal.handler.NetatmoDeviceHandler; import org.openhab.binding.netatmo.internal.handler.NetatmoModuleHandler; @@ -43,6 +44,7 @@ * @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules * */ +@NonNullByDefault public class NAMainHandler extends NetatmoDeviceHandler { private Map channelMeasurements = new ConcurrentHashMap<>(); @@ -51,9 +53,10 @@ public NAMainHandler(Thing thing, final TimeZoneProvider timeZoneProvider) { } @Override - protected NAMain updateReadings() { + protected @Nullable NAMain updateReadings() { NAMain result = null; - NAStationDataBody stationDataBody = getBridgeHandler().getStationsDataBody(getId()); + NetatmoBridgeHandler bridgeHandler = getBridgeHandler(); + NAStationDataBody stationDataBody = bridgeHandler == null ? null : bridgeHandler.getStationsDataBody(getId()); if (stationDataBody != null) { result = stationDataBody.getDevices().stream().filter(device -> device.getId().equalsIgnoreCase(getId())) .findFirst().orElse(null); @@ -64,12 +67,14 @@ protected NAMain updateReadings() { updateMeasurements(); - childs.keySet().forEach((childId) -> { - Optional childHandler = getBridgeHandler().findNAThing(childId); - childHandler.map(NetatmoModuleHandler.class::cast).ifPresent(naChildModule -> { - naChildModule.updateMeasurements(); + if (bridgeHandler != null) { + childs.keySet().forEach((childId) -> { + Optional childHandler = bridgeHandler.findNAThing(childId); + childHandler.map(NetatmoModuleHandler.class::cast).ifPresent(naChildModule -> { + naChildModule.updateMeasurements(); + }); }); - }); + } return result; } @@ -106,7 +111,7 @@ private void updateDayMeasurements() { addMeasurement(channels, types, CHANNEL_DATE_MIN_PRESSURE, DATE_MIN_PRESSURE); addMeasurement(channels, types, CHANNEL_DATE_MAX_PRESSURE, DATE_MAX_PRESSURE); if (!channels.isEmpty()) { - getMeasurements(getBridgeHandler(), getId(), null, ONE_DAY, types, channels, channelMeasurements); + getMeasurements(getId(), null, ONE_DAY, types, channels, channelMeasurements); } } @@ -134,7 +139,7 @@ private void updateWeekMeasurements() { addMeasurement(channels, types, CHANNEL_DATE_MIN_TEMP_THIS_WEEK, DATE_MIN_TEMP); addMeasurement(channels, types, CHANNEL_DATE_MAX_TEMP_THIS_WEEK, DATE_MAX_TEMP); if (!channels.isEmpty()) { - getMeasurements(getBridgeHandler(), getId(), null, ONE_WEEK, types, channels, channelMeasurements); + getMeasurements(getId(), null, ONE_WEEK, types, channels, channelMeasurements); } } @@ -162,14 +167,15 @@ private void updateMonthMeasurements() { addMeasurement(channels, types, CHANNEL_DATE_MIN_TEMP_THIS_MONTH, DATE_MIN_TEMP); addMeasurement(channels, types, CHANNEL_DATE_MAX_TEMP_THIS_MONTH, DATE_MAX_TEMP); if (!channels.isEmpty()) { - getMeasurements(getBridgeHandler(), getId(), null, ONE_MONTH, types, channels, channelMeasurements); + getMeasurements(getId(), null, ONE_MONTH, types, channels, channelMeasurements); } } @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: @@ -288,8 +294,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; } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule1Handler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule1Handler.java index 42f534ac53ffe..80c814d521c1f 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule1Handler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule1Handler.java @@ -20,7 +20,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.smarthome.core.i18n.TimeZoneProvider; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.types.State; @@ -38,6 +38,7 @@ * @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules * */ +@NonNullByDefault public class NAModule1Handler extends NetatmoModuleHandler { private Map channelMeasurements = new ConcurrentHashMap<>(); @@ -65,7 +66,7 @@ private void updateDayMeasurements() { addMeasurement(channels, types, CHANNEL_DATE_MIN_HUMIDITY, DATE_MIN_HUM); addMeasurement(channels, types, CHANNEL_DATE_MAX_HUMIDITY, DATE_MAX_HUM); if (!channels.isEmpty()) { - getMeasurements(getBridgeHandler(), getParentId(), getId(), ONE_DAY, types, channels, channelMeasurements); + getMeasurements(getParentId(), getId(), ONE_DAY, types, channels, channelMeasurements); } } @@ -81,7 +82,7 @@ private void updateWeekMeasurements() { addMeasurement(channels, types, CHANNEL_DATE_MIN_TEMP_THIS_WEEK, DATE_MIN_TEMP); addMeasurement(channels, types, CHANNEL_DATE_MAX_TEMP_THIS_WEEK, DATE_MAX_TEMP); if (!channels.isEmpty()) { - getMeasurements(getBridgeHandler(), getParentId(), getId(), ONE_WEEK, types, channels, channelMeasurements); + getMeasurements(getParentId(), getId(), ONE_WEEK, types, channels, channelMeasurements); } } @@ -97,15 +98,15 @@ private void updateMonthMeasurements() { addMeasurement(channels, types, CHANNEL_DATE_MIN_TEMP_THIS_MONTH, DATE_MIN_TEMP); addMeasurement(channels, types, CHANNEL_DATE_MAX_TEMP_THIS_MONTH, DATE_MAX_TEMP); if (!channels.isEmpty()) { - getMeasurements(getBridgeHandler(), getParentId(), getId(), ONE_MONTH, types, channels, - channelMeasurements); + getMeasurements(getParentId(), getId(), ONE_MONTH, types, channels, channelMeasurements); } } @Override - protected State getNAThingProperty(@NonNull String channelId) { - if (module != null) { - NADashboardData dashboardData = module.getDashboardData(); + protected State getNAThingProperty(String channelId) { + NAStationModule stationModule = module; + if (stationModule != null) { + NADashboardData dashboardData = stationModule.getDashboardData(); if (dashboardData != null) { switch (channelId) { case CHANNEL_TEMP_TREND: diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule2Handler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule2Handler.java index 130ddda1ae640..459f36c566270 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule2Handler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule2Handler.java @@ -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.smarthome.core.i18n.TimeZoneProvider; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.types.State; @@ -30,6 +30,7 @@ * * @author Gaël L'hopital - Initial contribution */ +@NonNullByDefault public class NAModule2Handler extends NetatmoModuleHandler { public NAModule2Handler(Thing thing, final TimeZoneProvider timeZoneProvider) { @@ -42,9 +43,10 @@ protected void updateProperties(NAStationModule moduleData) { } @Override - protected State getNAThingProperty(@NonNull String channelId) { - if (module != null) { - NADashboardData dashboardData = module.getDashboardData(); + protected State getNAThingProperty(String channelId) { + NAStationModule stationModule = module; + if (stationModule != null) { + NADashboardData dashboardData = stationModule.getDashboardData(); if (dashboardData != null) { switch (channelId) { case CHANNEL_WIND_ANGLE: diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule3Handler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule3Handler.java index 614a2e4a27a12..556440bfd720c 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule3Handler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule3Handler.java @@ -20,7 +20,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.smarthome.core.i18n.TimeZoneProvider; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.types.State; @@ -37,6 +37,7 @@ * @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules * */ +@NonNullByDefault public class NAModule3Handler extends NetatmoModuleHandler { private Map channelMeasurements = new ConcurrentHashMap<>(); @@ -51,23 +52,24 @@ protected void updateProperties(NAStationModule moduleData) { @Override public void updateMeasurements() { - List<@NonNull String> types = Arrays.asList(SUM_RAIN); + List types = Arrays.asList(SUM_RAIN); if (isLinked(CHANNEL_SUM_RAIN_THIS_WEEK)) { - getMeasurements(getBridgeHandler(), getParentId(), getId(), ONE_WEEK, types, - Arrays.asList(CHANNEL_SUM_RAIN_THIS_WEEK), channelMeasurements); + getMeasurements(getParentId(), getId(), ONE_WEEK, types, Arrays.asList(CHANNEL_SUM_RAIN_THIS_WEEK), + channelMeasurements); } if (isLinked(CHANNEL_SUM_RAIN_THIS_MONTH)) { - getMeasurements(getBridgeHandler(), getParentId(), getId(), ONE_MONTH, types, - Arrays.asList(CHANNEL_SUM_RAIN_THIS_MONTH), channelMeasurements); + getMeasurements(getParentId(), getId(), ONE_MONTH, types, Arrays.asList(CHANNEL_SUM_RAIN_THIS_MONTH), + channelMeasurements); } } @Override - protected State getNAThingProperty(@NonNull String channelId) { - if (module != null) { - NADashboardData dashboardData = module.getDashboardData(); + protected State getNAThingProperty(String channelId) { + NAStationModule stationModule = module; + if (stationModule != null) { + NADashboardData dashboardData = stationModule.getDashboardData(); if (dashboardData != null) { switch (channelId) { case CHANNEL_RAIN: diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule4Handler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule4Handler.java index 1ab0402116173..a86f5a3fd663a 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule4Handler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/station/NAModule4Handler.java @@ -20,7 +20,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.smarthome.core.i18n.TimeZoneProvider; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.types.State; @@ -38,6 +38,7 @@ * @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules * */ +@NonNullByDefault public class NAModule4Handler extends NetatmoModuleHandler { private Map channelMeasurements = new ConcurrentHashMap<>(); @@ -69,7 +70,7 @@ private void updateDayMeasurements() { addMeasurement(channels, types, CHANNEL_DATE_MIN_HUMIDITY, DATE_MIN_HUM); addMeasurement(channels, types, CHANNEL_DATE_MAX_HUMIDITY, DATE_MAX_HUM); if (!channels.isEmpty()) { - getMeasurements(getBridgeHandler(), getParentId(), getId(), ONE_DAY, types, channels, channelMeasurements); + getMeasurements(getParentId(), getId(), ONE_DAY, types, channels, channelMeasurements); } } @@ -89,7 +90,7 @@ private void updateWeekMeasurements() { addMeasurement(channels, types, CHANNEL_DATE_MIN_TEMP_THIS_WEEK, DATE_MIN_TEMP); addMeasurement(channels, types, CHANNEL_DATE_MAX_TEMP_THIS_WEEK, DATE_MAX_TEMP); if (!channels.isEmpty()) { - getMeasurements(getBridgeHandler(), getParentId(), getId(), ONE_WEEK, types, channels, channelMeasurements); + getMeasurements(getParentId(), getId(), ONE_WEEK, types, channels, channelMeasurements); } } @@ -109,15 +110,15 @@ private void updateMonthMeasurements() { addMeasurement(channels, types, CHANNEL_DATE_MIN_TEMP_THIS_MONTH, DATE_MIN_TEMP); addMeasurement(channels, types, CHANNEL_DATE_MAX_TEMP_THIS_MONTH, DATE_MAX_TEMP); if (!channels.isEmpty()) { - getMeasurements(getBridgeHandler(), getParentId(), getId(), ONE_MONTH, types, channels, - channelMeasurements); + getMeasurements(getParentId(), getId(), ONE_MONTH, types, channels, channelMeasurements); } } @Override - protected State getNAThingProperty(@NonNull String channelId) { - if (module != null) { - NADashboardData dashboardData = module.getDashboardData(); + protected State getNAThingProperty(String channelId) { + NAStationModule stationModule = module; + if (stationModule != null) { + NADashboardData dashboardData = stationModule.getDashboardData(); if (dashboardData != null) { switch (channelId) { case CHANNEL_TEMP_TREND: diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NAPlugHandler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NAPlugHandler.java index e49e88c60da5e..0ab3a2e0d2cb4 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NAPlugHandler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NAPlugHandler.java @@ -18,12 +18,13 @@ import java.time.ZonedDateTime; import java.time.temporal.TemporalAdjusters; -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; import org.eclipse.smarthome.core.types.State; import org.eclipse.smarthome.core.types.UnDefType; +import org.openhab.binding.netatmo.internal.handler.NetatmoBridgeHandler; import org.openhab.binding.netatmo.internal.handler.NetatmoDeviceHandler; import io.swagger.client.model.NAPlug; @@ -37,16 +38,19 @@ * @author Gaël L'hopital - Initial contribution OH2 version * */ +@NonNullByDefault public class NAPlugHandler extends NetatmoDeviceHandler { - public NAPlugHandler(@NonNull Thing thing, final TimeZoneProvider timeZoneProvider) { + public NAPlugHandler(Thing thing, final TimeZoneProvider timeZoneProvider) { super(thing, timeZoneProvider); } @Override - protected NAPlug updateReadings() { + protected @Nullable NAPlug updateReadings() { NAPlug result = null; - NAThermostatDataBody thermostatDataBody = getBridgeHandler().getThermostatsDataBody(getId()); + NetatmoBridgeHandler bridgeHandler = getBridgeHandler(); + NAThermostatDataBody thermostatDataBody = bridgeHandler == null ? null + : bridgeHandler.getThermostatsDataBody(getId()); if (thermostatDataBody != null) { result = thermostatDataBody.getDevices().stream().filter(device -> device.getId().equalsIgnoreCase(getId())) .findFirst().orElse(null); @@ -63,12 +67,13 @@ protected void updateProperties(NAPlug deviceData) { } @Override - protected State getNAThingProperty(@NonNull String channelId) { + protected State getNAThingProperty(String channelId) { + NAPlug plugDevice = device; switch (channelId) { case CHANNEL_CONNECTED_BOILER: - return device != null ? toOnOffType(device.getPlugConnectedBoiler()) : UnDefType.UNDEF; + return plugDevice != null ? toOnOffType(plugDevice.getPlugConnectedBoiler()) : UnDefType.UNDEF; case CHANNEL_LAST_PLUG_SEEN: - return device != null ? toDateTimeType(device.getLastPlugSeen(), timeZoneProvider.getTimeZone()) + return plugDevice != null ? toDateTimeType(plugDevice.getLastPlugSeen(), timeZoneProvider.getTimeZone()) : UnDefType.UNDEF; case CHANNEL_LAST_BILAN: return toDateTimeType(getLastBilan()); @@ -77,8 +82,9 @@ protected State getNAThingProperty(@NonNull String channelId) { } public @Nullable ZonedDateTime getLastBilan() { - if (device != null) { - NAYearMonth lastBilan = device.getLastBilan(); + NAPlug plugDevice = device; + if (plugDevice != null) { + NAYearMonth lastBilan = plugDevice.getLastBilan(); if (lastBilan != null) { ZonedDateTime zonedDT = ZonedDateTime.of(lastBilan.getY(), lastBilan.getM(), 1, 0, 0, 0, 0, ZonedDateTime.now().getZone()); @@ -90,8 +96,9 @@ protected State getNAThingProperty(@NonNull String channelId) { @Override protected @Nullable Integer getDataTimestamp() { - if (device != null) { - Integer lastStored = device.getLastStatusStore(); + NAPlug plugDevice = device; + if (plugDevice != null) { + Integer lastStored = plugDevice.getLastStatusStore(); if (lastStored != null) { return lastStored; } diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java index bc14a079d1ea1..843af819ec97a 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/thermostat/NATherm1Handler.java @@ -25,7 +25,7 @@ import javax.measure.quantity.Temperature; -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.OnOffType; @@ -61,11 +61,12 @@ * @author Gaël L'hopital - Initial contribution OH2 version * */ +@NonNullByDefault public class NATherm1Handler extends NetatmoModuleHandler { private final Logger logger = LoggerFactory.getLogger(NATherm1Handler.class); private final NATherm1StateDescriptionProvider stateDescriptionProvider; - public NATherm1Handler(@NonNull Thing thing, NATherm1StateDescriptionProvider stateDescriptionProvider, + public NATherm1Handler(Thing thing, NATherm1StateDescriptionProvider stateDescriptionProvider, final TimeZoneProvider timeZoneProvider) { super(thing, timeZoneProvider); this.stateDescriptionProvider = stateDescriptionProvider; @@ -82,7 +83,7 @@ protected void updateProperties(NAThermostat moduleData) { } @Override - public void updateChannels(Object moduleObject) { + public void updateChannels(@Nullable Object moduleObject) { if (isRefreshRequired()) { measurableChannels.getAsCsv().ifPresent(csvParams -> { ThermostatApi thermostatApi = getThermostatApi(); @@ -96,8 +97,9 @@ public void updateChannels(Object moduleObject) { } super.updateChannels(moduleObject); - if (module != null) { - updateStateDescription(module); + NAThermostat thermostat = module; + if (thermostat != null) { + updateStateDescription(thermostat); } } @@ -109,30 +111,32 @@ private void updateStateDescription(NAThermostat thermostat) { stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), CHANNEL_PLANNING), options); } - @SuppressWarnings("null") @Override - protected State getNAThingProperty(@NonNull String channelId) { + protected State getNAThingProperty(String channelId) { + NAThermostat thermostat = module; switch (channelId) { case CHANNEL_THERM_ORIENTATION: - return module != null ? toDecimalType(module.getThermOrientation()) : UnDefType.UNDEF; + return thermostat != null ? toDecimalType(thermostat.getThermOrientation()) : UnDefType.UNDEF; case CHANNEL_THERM_RELAY: - return module != null ? module.getThermRelayCmd() == 100 ? OnOffType.ON : OnOffType.OFF + return thermostat != null ? thermostat.getThermRelayCmd() == 100 ? OnOffType.ON : OnOffType.OFF : UnDefType.UNDEF; case CHANNEL_TEMPERATURE: - return module != null ? toQuantityType(module.getMeasured().getTemperature(), API_TEMPERATURE_UNIT) + return thermostat != null + ? toQuantityType(thermostat.getMeasured().getTemperature(), API_TEMPERATURE_UNIT) : UnDefType.UNDEF; case CHANNEL_SETPOINT_TEMP: return getCurrentSetpoint(); case CHANNEL_TIMEUTC: - return module != null ? toDateTimeType(module.getMeasured().getTime(), timeZoneProvider.getTimeZone()) + return thermostat != null + ? toDateTimeType(thermostat.getMeasured().getTime(), timeZoneProvider.getTimeZone()) : UnDefType.UNDEF; case CHANNEL_SETPOINT_END_TIME: { - if (module != null) { - NASetpoint setpoint = module.getSetpoint(); + if (thermostat != null) { + NASetpoint setpoint = thermostat.getSetpoint(); if (setpoint != null) { Integer endTime = setpoint.getSetpointEndtime(); if (endTime == null) { - endTime = getNextProgramTime(module.getThermProgramList()); + endTime = getNextProgramTime(thermostat.getThermProgramList()); } return toDateTimeType(endTime, timeZoneProvider.getTimeZone()); } @@ -144,8 +148,8 @@ protected State getNAThingProperty(@NonNull String channelId) { return getSetpoint(); case CHANNEL_PLANNING: { String currentPlanning = "-"; - if (module != null) { - for (NAThermProgram program : module.getThermProgramList()) { + if (thermostat != null) { + for (NAThermProgram program : thermostat.getThermProgramList()) { if (program.getSelected() == Boolean.TRUE) { currentPlanning = program.getProgramId(); } @@ -157,20 +161,21 @@ protected State getNAThingProperty(@NonNull String channelId) { return super.getNAThingProperty(channelId); } - @SuppressWarnings("null") private State getSetpoint() { - return module != null - ? module.getSetpoint() != null ? toStringType(module.getSetpoint().getSetpointMode()) : UnDefType.NULL + NAThermostat thermostat = module; + return thermostat != null + ? thermostat.getSetpoint() != null ? toStringType(thermostat.getSetpoint().getSetpointMode()) + : UnDefType.NULL : UnDefType.UNDEF; } - @SuppressWarnings("null") private State getCurrentSetpoint() { - if (module != null && module.getSetpoint() != null) { - NASetpoint setPoint = module.getSetpoint(); + NAThermostat thermostat = module; + if (thermostat != null && thermostat.getSetpoint() != null) { + NASetpoint setPoint = thermostat.getSetpoint(); String currentMode = setPoint.getSetpointMode(); - NAThermProgram currentProgram = module.getThermProgramList().stream() + NAThermProgram currentProgram = thermostat.getThermProgramList().stream() .filter(p -> p.getSelected() != null && p.getSelected()).findFirst().get(); switch (currentMode) { case CHANNEL_SETPOINT_MODE_MANUAL: @@ -182,7 +187,7 @@ private State getCurrentSetpoint() { NAZone zone1 = getZone(currentProgram.getZones(), 3); return toDecimalType(zone1.getTemp()); case CHANNEL_SETPOINT_MODE_PROGRAM: - NATimeTableItem currentProgramMode = getCurrentProgramMode(module.getThermProgramList()); + NATimeTableItem currentProgramMode = getCurrentProgramMode(thermostat.getThermProgramList()); if (currentProgramMode != null) { NAZone zone2 = getZone(currentProgram.getZones(), currentProgramMode.getId()); return toDecimalType(zone2.getTemp()); @@ -208,7 +213,7 @@ private long getNetatmoProgramBaseTime() { return mondayZero.getTimeInMillis(); } - private NATimeTableItem getCurrentProgramMode(List thermProgramList) { + private @Nullable NATimeTableItem getCurrentProgramMode(List thermProgramList) { NATimeTableItem lastProgram = null; Calendar now = Calendar.getInstance(); long diff = (now.getTimeInMillis() - getNetatmoProgramBaseTime()) / 1000 / 60; @@ -219,7 +224,10 @@ private NATimeTableItem getCurrentProgramMode(List thermProgramL if (currentProgram.isPresent()) { Stream pastPrograms = currentProgram.get().getTimetable().stream() .filter(t -> t.getMOffset() < diff); - lastProgram = pastPrograms.reduce((first, second) -> second).orElse(null); + Optional program = pastPrograms.reduce((first, second) -> second); + if (program.isPresent()) { + lastProgram = program.get(); + } } return lastProgram; @@ -300,7 +308,8 @@ public void handleCommand(ChannelUID channelUID, Command command) { } } - private void pushSetpointUpdate(String target_mode, Integer setpointEndtime, Float setpointTemp) { + private void pushSetpointUpdate(String target_mode, @Nullable Integer setpointEndtime, + @Nullable Float setpointTemp) { ThermostatApi thermostatApi = getThermostatApi(); if (thermostatApi != null) { thermostatApi.setthermpoint(getParentId(), getId(), target_mode, setpointEndtime, setpointTemp); diff --git a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/webhook/WelcomeWebHookServlet.java b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/webhook/WelcomeWebHookServlet.java index 2364ae3e22d57..8929af01255db 100644 --- a/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/webhook/WelcomeWebHookServlet.java +++ b/bundles/org.openhab.binding.netatmo/src/main/java/org/openhab/binding/netatmo/internal/webhook/WelcomeWebHookServlet.java @@ -20,6 +20,8 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.netatmo.internal.handler.NetatmoBridgeHandler; import org.osgi.service.http.HttpService; import org.osgi.service.http.NamespaceException; @@ -33,17 +35,19 @@ * * @author Gaël L'hopital - Initial contribution */ +@NonNullByDefault public class WelcomeWebHookServlet extends HttpServlet { private static final long serialVersionUID = 1288539782077957954L; private static final String PATH = "/netatmo/%s/camera"; private static final String APPLICATION_JSON = "application/json"; private static final String CHARSET = "utf-8"; + private final Gson gson = new Gson(); private final Logger logger = LoggerFactory.getLogger(WelcomeWebHookServlet.class); private HttpService httpService; - private NetatmoBridgeHandler bridgeHandler; + private @Nullable NetatmoBridgeHandler bridgeHandler; private String path; public WelcomeWebHookServlet(HttpService httpService, String id) { @@ -76,12 +80,18 @@ public void deactivate() { } @Override - protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + protected void service(@Nullable HttpServletRequest req, @Nullable HttpServletResponse resp) + throws ServletException, IOException { + if (req == null || resp == null) { + return; + } + String data = inputStreamToString(req); - if (data != null && bridgeHandler != null) { + NetatmoBridgeHandler handler = bridgeHandler; + if (!data.isEmpty() && handler != null) { NAWebhookCameraEvent event = gson.fromJson(data, NAWebhookCameraEvent.class); logger.debug("Event transmitted from restService"); - bridgeHandler.webHookEvent(event); + handler.webHookEvent(event); } setHeaders(resp);