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

[bigassfan] Add null annotations #13903

Merged
merged 13 commits into from
Jan 5, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,29 @@
*/
package org.openhab.binding.bigassfan.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link BigAssFanConfig} is responsible for storing the BigAssFan thing configuration.
*
* @author Mark Hilbush - Initial contribution
*/
@NonNullByDefault
public class BigAssFanConfig {
/**
* Name of the device
*/
private String label;
private String label = "";

/**
* IP address of the device
*/
private String ipAddress;
private String ipAddress = "";

/**
* MAC address of the device
*/
private String macAddress;
private String macAddress = "";

public String getLabel() {
return label;
Expand All @@ -58,16 +61,7 @@ public void setMacAddress(String macAddress) {
}

public boolean isValid() {
if (label == null || label.isBlank()) {
return false;
}
if (ipAddress == null || ipAddress.isBlank()) {
return false;
}
if (macAddress == null || macAddress.isBlank()) {
return false;
}
return true;
return label.isBlank() || ipAddress.isBlank() || macAddress.isBlank();
lsiepel marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
protected @Nullable ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
return new BigAssFanHandler(thing, networkAddressService.getPrimaryIpv4HostAddress());
String address = networkAddressService.getPrimaryIpv4HostAddress();
if (address != null) {
return new BigAssFanHandler(thing, address);
}
lsiepel marked this conversation as resolved.
Show resolved Hide resolved
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,44 @@
*/
package org.openhab.binding.bigassfan.internal.discovery;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link BigAssFanDevice} is responsible for storing information about a fan.
*
* @author Mark Hilbush - Initial contribution
*/
@NonNullByDefault
public class BigAssFanDevice {
/**
* Name of device (e.g. Master Bedroom Fan)
*/
private String label;
private String label = "";

/**
* IP address of the device extracted from UDP packet
*/
private String ipAddress;
private String ipAddress = "";

/**
* MAC address of the device extracted from discovery message
*/
private String macAddress;
private String macAddress = "";

/**
* Type of device extracted from discovery message (e.g. FAN or SWITCH)
*/
private String type;
private String type = "";

/**
* Model of device extracted from discovery message (e.g. HSERIES)
*/
private String model;
private String model = "";

/**
* The raw discovery message
*/
private String discoveryMessage;
private String discoveryMessage = "";

public String getLabel() {
return label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.DiscoveryService;
Expand All @@ -44,6 +46,7 @@
*
* @author Mark Hilbush - Initial contribution
*/
@NonNullByDefault
@Component(service = DiscoveryService.class, configurationPid = "discovery.bigassfan")
public class BigAssFanDiscoveryService extends AbstractDiscoveryService {
private final Logger logger = LoggerFactory.getLogger(BigAssFanDiscoveryService.class);
Expand All @@ -53,12 +56,9 @@ public class BigAssFanDiscoveryService extends AbstractDiscoveryService {

// Our own thread pool for the long-running listener job
private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
private ScheduledFuture<?> listenerJob;

DiscoveryListener discoveryListener;

private @Nullable ScheduledFuture<?> listenerJob;
private @Nullable DiscoveryListener discoveryListener;
private boolean terminate;

private final Pattern announcementPattern = Pattern.compile("[(](.*);DEVICE;ID;(.*);(.*)[)]");

private Runnable listenerRunnable = () -> {
Expand All @@ -70,9 +70,9 @@ public class BigAssFanDiscoveryService extends AbstractDiscoveryService {
};

// Frequency (in seconds) with which we poll for new devices
private final long POLL_FREQ = 300L;
private final long POLL_DELAY = 12L;
private ScheduledFuture<?> pollJob;
private static final long POLL_FREQ = 300L;
private static final long POLL_DELAY = 12L;
private @Nullable ScheduledFuture<?> pollJob;

public BigAssFanDiscoveryService() {
super(SUPPORTED_THING_TYPES_UIDS, 0, BACKGROUND_DISCOVERY_ENABLED);
Expand All @@ -84,7 +84,7 @@ public Set<ThingTypeUID> getSupportedThingTypes() {
}

@Override
protected void activate(Map<String, Object> configProperties) {
protected void activate(@Nullable Map<String, Object> configProperties) {
super.activate(configProperties);
logger.trace("BigAssFan discovery service ACTIVATED");
}
Expand All @@ -97,7 +97,7 @@ protected void deactivate() {

@Override
@Modified
protected void modified(Map<String, Object> configProperties) {
protected void modified(@Nullable Map<String, Object> configProperties) {
super.modified(configProperties);
}

Expand All @@ -115,21 +115,25 @@ protected void stopBackgroundDiscovery() {
cancelListenerJob();
}

private void startListenerJob() {
if (listenerJob == null) {
terminate = false;
lsiepel marked this conversation as resolved.
Show resolved Hide resolved
logger.debug("Starting discovery listener job in {} seconds", BACKGROUND_DISCOVERY_DELAY);
listenerJob = scheduledExecutorService.schedule(listenerRunnable, BACKGROUND_DISCOVERY_DELAY,
private synchronized void startListenerJob() {
jlaur marked this conversation as resolved.
Show resolved Hide resolved
logger.debug("Starting discovery listener job in {} seconds", BACKGROUND_DISCOVERY_DELAY);
lsiepel marked this conversation as resolved.
Show resolved Hide resolved

if (this.listenerJob == null) {
this.listenerJob = scheduledExecutorService.schedule(listenerRunnable, BACKGROUND_DISCOVERY_DELAY,
TimeUnit.SECONDS);
}
}

private void cancelListenerJob() {
if (listenerJob != null) {
logger.debug("Canceling discovery listener job");
listenerJob.cancel(true);
terminate = true;
listenerJob = null;
@SuppressWarnings("null")
lsiepel marked this conversation as resolved.
Show resolved Hide resolved
private synchronized void cancelListenerJob() {
logger.debug("Canceling discovery listener job");
lsiepel marked this conversation as resolved.
Show resolved Hide resolved

if (this.listenerJob != null) {
if (!this.listenerJob.isCancelled()) {
lsiepel marked this conversation as resolved.
Show resolved Hide resolved
this.listenerJob.cancel(true);
terminate = true;
listenerJob = null;
lsiepel marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

Expand All @@ -143,9 +147,11 @@ public void stopScan() {

private synchronized void listen() {
logger.info("BigAssFan discovery service is running");
DiscoveryListener localDiscoveryListener;

try {
discoveryListener = new DiscoveryListener();
localDiscoveryListener = new DiscoveryListener();
discoveryListener = localDiscoveryListener;
} catch (SocketException se) {
logger.warn("Got Socket exception creating multicast socket: {}", se.getMessage(), se);
return;
Expand All @@ -158,7 +164,7 @@ private synchronized void listen() {
while (!terminate) {
try {
// Wait for a discovery message
processMessage(discoveryListener.waitForMessage());
processMessage(localDiscoveryListener.waitForMessage());
} catch (SocketTimeoutException e) {
// Read on socket timed out; check for termination
continue;
Expand All @@ -167,11 +173,11 @@ private synchronized void listen() {
break;
}
}
discoveryListener.shutdown();
localDiscoveryListener.shutdown();
logger.debug("DiscoveryListener job is exiting");
}

private void processMessage(BigAssFanDevice device) {
private void processMessage(@Nullable BigAssFanDevice device) {
lsiepel marked this conversation as resolved.
Show resolved Hide resolved
if (device == null) {
return;
}
Expand Down Expand Up @@ -242,22 +248,28 @@ private synchronized void deviceDiscovered(BigAssFanDevice device) {
.withRepresentationProperty(THING_PROPERTY_MAC).withLabel(device.getLabel()).build());
}

private void schedulePollJob() {
logger.debug("Scheduling discovery poll job to run every {} seconds starting in {} sec", POLL_FREQ, POLL_DELAY);
cancelPollJob();
lsiepel marked this conversation as resolved.
Show resolved Hide resolved
pollJob = scheduler.scheduleWithFixedDelay(() -> {
try {
discoveryListener.pollForDevices();
} catch (RuntimeException e) {
logger.warn("Poll job got unexpected exception: {}", e.getMessage(), e);
}
}, POLL_DELAY, POLL_FREQ, TimeUnit.SECONDS);
private synchronized void schedulePollJob() {
if (this.pollJob == null) {
jlaur marked this conversation as resolved.
Show resolved Hide resolved
logger.debug("Scheduling discovery poll job to run every {} seconds starting in {} sec", POLL_FREQ,
POLL_DELAY);
pollJob = scheduler.scheduleWithFixedDelay(() -> {
try {
DiscoveryListener localListener = discoveryListener;
if (localListener != null) {
localListener.pollForDevices();
}
} catch (RuntimeException e) {
logger.warn("Poll job got unexpected exception: {}", e.getMessage(), e);
}
}, POLL_DELAY, POLL_FREQ, TimeUnit.SECONDS);
}
}

private void cancelPollJob() {
if (pollJob != null) {
@SuppressWarnings("null")
private synchronized void cancelPollJob() {
if (this.pollJob != null) {
logger.debug("Canceling poll job");
pollJob.cancel(true);
this.pollJob.cancel(true);
pollJob = null;
}
}
lsiepel marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading