Skip to content

Commit

Permalink
[netatmo] Null annotations Part 1 (openhab#8012)
Browse files Browse the repository at this point in the history
Related to openhab#7913

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
Signed-off-by: CSchlipp <christian@schlipp.de>
  • Loading branch information
lolodomo authored and CSchlipp committed Jul 26, 2020
1 parent a8e37fd commit 6ba7c1d
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.netatmo")
public class NetatmoHandlerFactory extends BaseThingHandlerFactory {
private final Logger logger = LoggerFactory.getLogger(NetatmoHandlerFactory.class);
private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
private final Map<ThingUID, ServiceRegistration<?>> webHookServiceRegs = new HashMap<>();
private final Map<ThingUID, @Nullable ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
private final Map<ThingUID, @Nullable ServiceRegistration<?>> webHookServiceRegs = new HashMap<>();
private final HttpService httpService;
private final NATherm1StateDescriptionProvider stateDescriptionProvider;
private final TimeZoneProvider timeZoneProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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.library.types.OnOffType;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.types.UnDefType;
Expand All @@ -34,11 +36,12 @@
* @author Gaël L'hopital - Initial contribution
*
*/
@NonNullByDefault
public class BatteryHelper {
private Logger logger = LoggerFactory.getLogger(BatteryHelper.class);
private final Logger logger = LoggerFactory.getLogger(BatteryHelper.class);
private int batteryLow;

private Object module;
private @Nullable Object module;

public BatteryHelper(String batteryLevels) {
List<String> thresholds = Arrays.asList(batteryLevels.split(","));
Expand All @@ -50,6 +53,7 @@ public void setModule(Object module) {
}

public Optional<State> getNAThingProperty(String channelId) {
Object module = this.module;
if (module != null) {
try {
if (CHANNEL_BATTERY_LEVEL.equalsIgnoreCase(channelId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.types.State;
import org.eclipse.smarthome.core.types.UnDefType;
Expand All @@ -34,10 +36,11 @@
* @author Gaël L'hopital - Initial contribution
*
*/
@NonNullByDefault
public class RadioHelper {
private Logger logger = LoggerFactory.getLogger(RadioHelper.class);
private final Logger logger = LoggerFactory.getLogger(RadioHelper.class);
private final List<Integer> signalThresholds;
private Object module;
private @Nullable Object module;

public RadioHelper(String signalLevels) {
signalThresholds = Stream.of(signalLevels.split(",")).map(Integer::parseInt).collect(Collectors.toList());
Expand All @@ -58,6 +61,7 @@ public void setModule(Object module) {
}

public Optional<State> getNAThingProperty(String channelId) {
Object module = this.module;
if (module != null) {
try {
switch (channelId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@
*/
package org.openhab.binding.netatmo.internal.config;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* The {@link NetatmoBridgeConfiguration} is responsible for holding
* configuration informations needed to access Netatmo API
*
* @author Gaël L'hopital - Initial contribution
*/
@NonNullByDefault
public class NetatmoBridgeConfiguration {
public String clientId;
public String clientSecret;
public String username;
public String password;
public Boolean readStation;
public Boolean readThermostat;
public Boolean readHealthyHomeCoach;
public Boolean readWelcome;
public Boolean readPresence;
public String webHookUrl;
public Integer reconnectInterval;
public @Nullable String clientId;
public @Nullable String clientSecret;
public @Nullable String username;
public @Nullable String password;
public boolean readStation = true;
public boolean readThermostat = false;
public boolean readHealthyHomeCoach = false;
public boolean readWelcome = false;
public boolean readPresence = false;
public @Nullable String webHookUrl;
public int reconnectInterval = 5400;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService;
import org.eclipse.smarthome.config.discovery.DiscoveryResult;
Expand Down Expand Up @@ -45,18 +45,18 @@
* @author Ing. Peter Weiss - Welcome camera implementation
*
*/
@NonNullByDefault
public class NetatmoModuleDiscoveryService extends AbstractDiscoveryService implements NetatmoDataListener {
private static final int SEARCH_TIME = 2;
@NonNull
private final NetatmoBridgeHandler netatmoBridgeHandler;

public NetatmoModuleDiscoveryService(@NonNull NetatmoBridgeHandler netatmoBridgeHandler) {
public NetatmoModuleDiscoveryService(NetatmoBridgeHandler netatmoBridgeHandler) {
super(SUPPORTED_DEVICE_THING_TYPES_UIDS, SEARCH_TIME);
this.netatmoBridgeHandler = netatmoBridgeHandler;
}

@Override
public void activate(@Nullable Map<@NonNull String, @Nullable Object> configProperties) {
public void activate(@Nullable Map<String, @Nullable Object> configProperties) {
super.activate(configProperties);
netatmoBridgeHandler.registerDataListener(this);
}
Expand Down Expand Up @@ -157,7 +157,8 @@ private void discoverWelcomeHome(NAWelcomeHome home) {
}
}

private void onDeviceAddedInternal(String id, String parentId, String type, String name, Integer firmwareVersion) {
private void onDeviceAddedInternal(String id, @Nullable String parentId, String type, String name,
@Nullable Integer firmwareVersion) {
ThingUID thingUID = findThingUID(type, id);
Map<String, Object> properties = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.util.stream.Stream;

import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.ChannelUID;
Expand Down Expand Up @@ -73,16 +73,16 @@
* @author Rob Nielsen - Added day, week, and month measurements to the weather station and modules
*
*/
@NonNullByDefault
public class NetatmoBridgeHandler extends BaseBridgeHandler {
private Logger logger = LoggerFactory.getLogger(NetatmoBridgeHandler.class);
private final Logger logger = LoggerFactory.getLogger(NetatmoBridgeHandler.class);

public NetatmoBridgeConfiguration configuration;
private ScheduledFuture<?> refreshJob;
private APIMap apiMap;
private WelcomeWebHookServlet webHookServlet;
public NetatmoBridgeConfiguration configuration = new NetatmoBridgeConfiguration();
private @Nullable ScheduledFuture<?> refreshJob;
private @Nullable APIMap apiMap;
private @Nullable WelcomeWebHookServlet webHookServlet;
private List<NetatmoDataListener> dataListeners = new CopyOnWriteArrayList<>();

@NonNullByDefault
private class APIMap extends HashMap<Class<?>, Object> {
private static final long serialVersionUID = -2024031764691952343L;
private ApiClient apiClient;
Expand All @@ -101,7 +101,7 @@ public Object get(Class<?> apiClass) {
}
}

public NetatmoBridgeHandler(@NonNull Bridge bridge, WelcomeWebHookServlet webHookServlet) {
public NetatmoBridgeHandler(Bridge bridge, @Nullable WelcomeWebHookServlet webHookServlet) {
super(bridge);
this.webHookServlet = webHookServlet;
}
Expand All @@ -116,11 +116,13 @@ public void initialize() {

private void connectionSucceed() {
updateStatus(ThingStatus.ONLINE);
WelcomeWebHookServlet webHookServlet = this.webHookServlet;
String webHookURI = getWebHookURI();
if (webHookURI != null) {
WelcomeApi welcomeApi = getWelcomeApi();
if (welcomeApi != null && webHookServlet != null && webHookURI != null) {
webHookServlet.activate(this);
logger.debug("Setting up Netatmo Welcome WebHook");
getWelcomeApi().addwebhook(webHookURI, WEBHOOK_APP);
welcomeApi.addwebhook(webHookURI, WEBHOOK_APP);
}
}

Expand Down Expand Up @@ -223,71 +225,88 @@ public void handleCommand(ChannelUID channelUID, Command command) {
logger.debug("Netatmo Bridge is read-only and does not handle commands");
}

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

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

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

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

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

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

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

if (refreshJob != null && !refreshJob.isCancelled()) {
refreshJob.cancel(true);
ScheduledFuture<?> job = refreshJob;
if (job != null) {
job.cancel(true);
refreshJob = null;
}
}

public NAStationDataBody getStationsDataBody(String equipmentId) {
NAStationDataBody data = getStationApi().getstationsdata(equipmentId, false).getBody();
public @Nullable NAStationDataBody getStationsDataBody(@Nullable String equipmentId) {
StationApi stationApi = getStationApi();
NAStationDataBody data = stationApi == null ? null : stationApi.getstationsdata(equipmentId, false).getBody();
updateStatus(ThingStatus.ONLINE);
return data;
}

public List<Float> getStationMeasureResponses(String equipmentId, String moduleId, String scale,
List<@NonNull String> types) {
List<NAMeasureBodyElem> data = getStationApi()
.getmeasure(equipmentId, scale, new CSVParams(types), moduleId, null, "last", 1, true, false).getBody();
public List<Float> getStationMeasureResponses(String equipmentId, @Nullable String moduleId, String scale,
List<String> types) {
StationApi stationApi = getStationApi();
List<NAMeasureBodyElem> data = stationApi == null ? null
: stationApi
.getmeasure(equipmentId, scale, new CSVParams(types), moduleId, null, "last", 1, true, false)
.getBody();
updateStatus(ThingStatus.ONLINE);
NAMeasureBodyElem element = data.get(0);
NAMeasureBodyElem element = (data != null && data.size() > 0) ? data.get(0) : null;
return element != null ? element.getValue().get(0) : Collections.emptyList();
}

public NAHealthyHomeCoachDataBody getHomecoachDataBody(String equipmentId) {
NAHealthyHomeCoachDataBody data = getHomeCoachApi().gethomecoachsdata(equipmentId).getBody();
public @Nullable NAHealthyHomeCoachDataBody getHomecoachDataBody(@Nullable String equipmentId) {
HealthyhomecoachApi healthyhomecoachApi = getHomeCoachApi();
NAHealthyHomeCoachDataBody data = healthyhomecoachApi == null ? null
: healthyhomecoachApi.gethomecoachsdata(equipmentId).getBody();
updateStatus(ThingStatus.ONLINE);
return data;
}

public NAThermostatDataBody getThermostatsDataBody(String equipmentId) {
NAThermostatDataBody data = getThermostatApi().getthermostatsdata(equipmentId).getBody();
public @Nullable NAThermostatDataBody getThermostatsDataBody(@Nullable String equipmentId) {
ThermostatApi thermostatApi = getThermostatApi();
NAThermostatDataBody data = thermostatApi == null ? null
: thermostatApi.getthermostatsdata(equipmentId).getBody();
updateStatus(ThingStatus.ONLINE);
return data;
}

public NAWelcomeHomeData getWelcomeDataBody(String homeId) {
NAWelcomeHomeData data = getWelcomeApi().gethomedata(homeId, null).getBody();
public @Nullable NAWelcomeHomeData getWelcomeDataBody(@Nullable String homeId) {
WelcomeApi welcomeApi = getWelcomeApi();
NAWelcomeHomeData data = welcomeApi == null ? null : welcomeApi.gethomedata(homeId, null).getBody();
updateStatus(ThingStatus.ONLINE);
return data;
}
Expand All @@ -297,7 +316,7 @@ public NAWelcomeHomeData getWelcomeDataBody(String homeId) {
*
* @return Url of the picture or UnDefType.UNDEF
*/
public String getPictureUrl(String id, String key) {
public String getPictureUrl(@Nullable String id, @Nullable String key) {
StringBuilder ret = new StringBuilder();
if (id != null && key != null) {
ret.append(WELCOME_PICTURE_URL).append("?").append(WELCOME_PICTURE_IMAGEID).append("=").append(id)
Expand All @@ -306,7 +325,7 @@ public String getPictureUrl(String id, String key) {
return ret.toString();
}

public Optional<AbstractNetatmoThingHandler> findNAThing(String searchedId) {
public Optional<AbstractNetatmoThingHandler> findNAThing(@Nullable String searchedId) {
List<Thing> things = getThing().getThings();
Stream<AbstractNetatmoThingHandler> naHandlers = things.stream().map(Thing::getHandler)
.filter(AbstractNetatmoThingHandler.class::isInstance).map(AbstractNetatmoThingHandler.class::cast)
Expand Down Expand Up @@ -351,20 +370,21 @@ public void webHookEvent(NAWebhookCameraEvent event) {
}
}

private String getWebHookURI() {
private @Nullable String getWebHookURI() {
String webHookURI = null;
WelcomeWebHookServlet webHookServlet = this.webHookServlet;
if (configuration.webHookUrl != null && (configuration.readWelcome || configuration.readPresence)
&& webHookServlet != null) {
webHookURI = configuration.webHookUrl + webHookServlet.getPath();
}
return webHookURI;
}

public boolean registerDataListener(@NonNull NetatmoDataListener dataListener) {
public boolean registerDataListener(NetatmoDataListener dataListener) {
return dataListeners.add(dataListener);
}

public boolean unregisterDataListener(@NonNull NetatmoDataListener dataListener) {
public boolean unregisterDataListener(NetatmoDataListener dataListener) {
return dataListeners.remove(dataListener);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
*/
package org.openhab.binding.netatmo.internal.handler;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link NetatmoDataListener} allows receiving notification when any netatmo device thing handler
* is getting refreshed data from the netatmo server.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public interface NetatmoDataListener {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ protected void updateChannels() {
if (newDeviceReading != null) {
updateStatus(ThingStatus.ONLINE);
logger.debug("Successfully updated device {} readings! Now updating channels", getId());
this.device = newDeviceReading;
DEVICE device = newDeviceReading;
this.device = device;
updateProperties(device);
Integer dataTimeStamp = getDataTimestamp();
if (dataTimeStamp != null) {
Expand Down
Loading

0 comments on commit 6ba7c1d

Please sign in to comment.