Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tellstick] Add null annotations #17974

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.binding.tellstick.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.tellstick.device.TellstickException;

/**
Expand All @@ -20,6 +22,7 @@
*
* @author Jarle Hjortland - Initial contribution
*/
@NonNullByDefault
public class TelldusBindingException extends TellstickException {

private static final long serialVersionUID = 30671795474333158L;
Expand All @@ -32,7 +35,7 @@ public TelldusBindingException(String message) {
}

@Override
public String getMessage() {
public @Nullable String getMessage() {
return msg;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*/
package org.openhab.binding.tellstick.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Runtime exception in tellstick binding.
*
* @author Jarle Hjortland - Initial contribution
*/
@NonNullByDefault
public class TellstickRuntimeException extends RuntimeException {

private static final long serialVersionUID = -1644730263645760297L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
*/
package org.openhab.binding.tellstick.internal.conf;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Configuration class for Tellstick bridge used to connect to the
* Telldus Live service.
*
* @author Jarle Hjortland - Initial contribution
*/
@NonNullByDefault
public class TelldusLiveConfiguration {
public String publicKey;
public String privateKey;
public String token;
public String tokenSecret;
public long refreshInterval;
public String publicKey = "";
public String privateKey = "";
public String token = "";
public String tokenSecret = "";
public long refreshInterval = 60000;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@
*/
package org.openhab.binding.tellstick.internal.conf;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Configuration class for Tellstick bridge used to connect to the
* Telldus local API.
*
* @author Jan Gustafsson - Initial contribution
*/
@NonNullByDefault
public class TelldusLocalConfiguration {
public String ipAddress;
public String accessToken;
public long refreshInterval;
public String ipAddress = "";
public String accessToken = "";
public long refreshInterval = 60000;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
*/
package org.openhab.binding.tellstick.internal.conf;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Configuration class for Tellstick bridge used to connect to the
* Telldus Core service on the local machine.
*
* @author Jarle Hjortland - Initial contribution
*/
@NonNullByDefault
public class TellstickBridgeConfiguration {
public int resendInterval;
public String libraryPath;
public String libraryPath = "C:/Program Files/Telldus/;C:/Program Files (x86)/Telldus/";
public int resendInterval = 100;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.tellstick.internal.conf.TellstickBridgeConfiguration;
import org.openhab.binding.tellstick.internal.handler.DeviceStatusListener;
import org.openhab.binding.tellstick.internal.handler.TelldusBridgeHandler;
Expand Down Expand Up @@ -54,6 +56,7 @@
*
* @author Jarle Hjortland - Initial contribution
*/
@NonNullByDefault
public class TelldusCoreBridgeHandler extends BaseBridgeHandler
implements DeviceChangeListener, SensorListener, TelldusBridgeHandler {

Expand All @@ -62,10 +65,10 @@ public TelldusCoreBridgeHandler(Bridge br) {
}

private Logger logger = LoggerFactory.getLogger(TelldusCoreBridgeHandler.class);
private TelldusDeviceController deviceController = null;
private @Nullable TelldusDeviceController deviceController;
private List<TellstickDevice> deviceList = new Vector<>();
private List<TellstickSensor> sensorList = new Vector<>();
private TellstickEventHandler eventHandler;
private @Nullable TellstickEventHandler eventHandler;
private static boolean initialized = false;
private Set<DeviceStatusListener> deviceStatusListeners = ConcurrentHashMap.newKeySet();

Expand All @@ -82,21 +85,24 @@ public void handleCommand(ChannelUID channelUID, Command command) {
@Override
public void dispose() {
logger.debug("Telldus Core Handler disposed.");
TelldusDeviceController deviceController = this.deviceController;
if (deviceController != null) {
deviceController.dispose();
deviceController = null;
this.deviceController = null;
}

TellstickEventHandler eventHandler = this.eventHandler;
if (eventHandler != null) {
eventHandler.remove();
eventHandler = null;
this.eventHandler = null;
}
clearDeviceList();
initialized = false;
JNA.CLibrary.INSTANCE.tdClose();
super.dispose();
}

private String init(String libraryPath) {
private @Nullable String init(@Nullable String libraryPath) {
if (!initialized) {
if (libraryPath != null) {
logger.info("Loading {} from {}", JNA.nativeLibrary, libraryPath);
Expand Down Expand Up @@ -131,7 +137,10 @@ public void initialize() {
private void setupDeviceController(TellstickBridgeConfiguration configuration) {
deviceController = new TelldusCoreDeviceController(configuration.resendInterval,
"OH-binding-" + getThing().getUID() + "-worker");
eventHandler.addListener((TelldusCoreDeviceController) deviceController);
TellstickEventHandler eventHandler = this.eventHandler;
if (eventHandler != null) {
eventHandler.addListener((TelldusCoreDeviceController) deviceController);
}
}

@Override
Expand Down Expand Up @@ -168,8 +177,9 @@ public void rescanTelldusDevices() {
}

private synchronized void setupListeners() {
eventHandler = new TellstickEventHandler(deviceList);
TellstickEventHandler eventHandler = new TellstickEventHandler(deviceList);
eventHandler.addListener(this);
this.eventHandler = eventHandler;
}

public void onConnectionLost() {
Expand All @@ -183,7 +193,7 @@ public void onConnection() {
}

@Override
public boolean registerDeviceStatusListener(DeviceStatusListener deviceStatusListener) {
public boolean registerDeviceStatusListener(@Nullable DeviceStatusListener deviceStatusListener) {
if (deviceStatusListener == null) {
throw new IllegalArgumentException("It's not allowed to pass a null deviceStatusListener.");
}
Expand All @@ -200,7 +210,7 @@ public void clearDeviceList() {
sensorList.clear();
}

private Device getDevice(String id, List<TellstickDevice> devices) {
private @Nullable Device getDevice(String id, List<TellstickDevice> devices) {
for (Device device : devices) {
if (device.getId() == Integer.valueOf(id)) {
return device;
Expand All @@ -210,12 +220,12 @@ private Device getDevice(String id, List<TellstickDevice> devices) {
}

@Override
public Device getDevice(String serialNumber) {
public @Nullable Device getDevice(String serialNumber) {
return getDevice(serialNumber, deviceList);
}

@Override
public void onRequest(TellstickSensorEvent newEvent) {
public void onRequest(@NonNullByDefault({}) TellstickSensorEvent newEvent) {
String uuid = TellstickSensor.createUUId(newEvent.getSensorId(), newEvent.getModel(), newEvent.getProtocol());
Device device = getSensor(uuid);
logger.debug("Sensor Event for {} event {}", device, newEvent);
Expand Down Expand Up @@ -248,7 +258,7 @@ public void onRequest(TellstickSensorEvent newEvent) {
}

@Override
public void onRequest(TellstickDeviceEvent newEvent) {
public void onRequest(@NonNullByDefault({}) TellstickDeviceEvent newEvent) {
if (newEvent.getChangeType() == ChangeType.ADDED) {
for (DeviceStatusListener listener : deviceStatusListeners) {
listener.onDeviceAdded(getThing(), newEvent.getDevice());
Expand All @@ -265,7 +275,7 @@ public void onRequest(TellstickDeviceEvent newEvent) {
}

@Override
public Device getSensor(String deviceUUId) {
public @Nullable Device getSensor(String deviceUUId) {
for (Device device : sensorList) {
if (device.getUUId().equals(deviceUUId)) {
return device;
Expand All @@ -275,7 +285,7 @@ public Device getSensor(String deviceUUId) {
}

@Override
public TelldusDeviceController getController() {
public @Nullable TelldusDeviceController getController() {
return this.deviceController;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.SortedMap;
import java.util.TreeMap;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.tellstick.internal.TelldusBindingException;
import org.openhab.binding.tellstick.internal.handler.TelldusDeviceController;
import org.openhab.core.library.types.IncreaseDecreaseType;
Expand Down Expand Up @@ -45,6 +47,7 @@
*
* @author Jarle Hjortland, Elias Gabrielsson - Initial contribution
*/
@NonNullByDefault
public class TelldusCoreDeviceController implements DeviceChangeListener, SensorListener, TelldusDeviceController {
private final Logger logger = LoggerFactory.getLogger(TelldusCoreDeviceController.class);
private long lastSend = 0;
Expand Down Expand Up @@ -82,7 +85,7 @@ public void handleSendEvent(Device device, int resendCount, boolean isDimmer, Co
}

@Override
public State calcState(Device dev) {
public @Nullable State calcState(Device dev) {
TellstickDevice device = (TellstickDevice) dev;
State st = null;
switch (device.getStatus()) {
Expand Down Expand Up @@ -137,12 +140,12 @@ public void setLastSend(long currentTimeMillis) {
}

@Override
public void onRequest(TellstickSensorEvent newDevices) {
public void onRequest(@NonNullByDefault({}) TellstickSensorEvent newDevices) {
setLastSend(newDevices.getTimestamp());
}

@Override
public void onRequest(TellstickDeviceEvent newDevices) {
public void onRequest(@NonNullByDefault({}) TellstickDeviceEvent newDevices) {
setLastSend(newDevices.getTimestamp());
}

Expand Down Expand Up @@ -179,10 +182,12 @@ private void sendEvent(Device device, int resendCount, boolean isdimmer, Command
}

private void increaseDecrease(Device dev, IncreaseDecreaseType increaseDecreaseType) throws TellstickException {
String strValue = ((TellstickDevice) dev).getData();
double value = 0;
if (strValue != null) {
value = Double.valueOf(strValue);
if (dev instanceof TellstickDevice device) {
String strValue = device.getData();
if (strValue != null) {
value = Double.valueOf(strValue);
}
}
int percent = (int) Math.round((value / 255) * 100);
if (IncreaseDecreaseType.INCREASE == increaseDecreaseType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Map;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.tellstick.internal.TellstickBindingConstants;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
Expand All @@ -34,6 +35,7 @@
* @author Jarle Hjortland - Initial contribution
*
*/
@NonNullByDefault
@Component(service = DiscoveryService.class, configurationPid = "discovery.tellstick")
public class TellstickBridgeDiscovery extends AbstractDiscoveryService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
import java.util.Set;
import java.util.Vector;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.tellstick.internal.TellstickBindingConstants;
import org.openhab.binding.tellstick.internal.handler.DeviceStatusListener;
import org.openhab.binding.tellstick.internal.handler.TelldusBridgeHandler;
import org.openhab.binding.tellstick.internal.live.xml.LiveDataType;
import org.openhab.binding.tellstick.internal.live.xml.TellstickNetDevice;
import org.openhab.binding.tellstick.internal.live.xml.TellstickNetSensor;
import org.openhab.binding.tellstick.internal.live.dto.LiveDataType;
import org.openhab.binding.tellstick.internal.live.dto.TellstickNetDevice;
import org.openhab.binding.tellstick.internal.live.dto.TellstickNetSensor;
import org.openhab.binding.tellstick.internal.local.dto.TellstickLocalDeviceDTO;
import org.openhab.binding.tellstick.internal.local.dto.TellstickLocalSensorDTO;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
Expand All @@ -46,6 +48,7 @@
*
* @author Jarle Hjortland - Initial contribution
*/
@NonNullByDefault
public class TellstickDiscoveryService extends AbstractDiscoveryService implements DeviceStatusListener {
private static final long DEFAULT_TTL = 60 * 60; // 1 Hour

Expand Down Expand Up @@ -121,7 +124,7 @@ public void onDeviceRemoved(Bridge bridge, Device device) {
}
}

private ThingUID getThingUID(Bridge bridge, Device device) {
private @Nullable ThingUID getThingUID(Bridge bridge, Device device) {
ThingUID thingUID = null;
switch (device.getDeviceType()) {
case SENSOR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.binding.tellstick.internal.handler;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.thing.Bridge;
import org.tellstick.device.iface.Device;
import org.tellstick.device.iface.TellstickEvent;
Expand All @@ -22,12 +23,13 @@
*
* @author Jarle Hjortland - Initial contribution
*/
@NonNullByDefault
public interface DeviceStatusListener {

/**
* This method is called whenever the state of the given device has changed.
* The new state can be obtained by
* {@link org.openhab.binding.tellstick.internal.live.xml.TellstickNetDevice#getState()} /
* {@link org.openhab.binding.tellstick.internal.live.dto.TellstickNetDevice#getState()} /
* {@link org.openhab.binding.tellstick.internal.local.dto.TellstickLocalDeviceDTO#getState()}.
*
* @param bridge
Expand Down
Loading