Skip to content

Commit

Permalink
[bigassfan] Null annotations (openhab#13903)
Browse files Browse the repository at this point in the history
* Null annotations and some refactoring
* Fix synchronized block
* Fix remaining warnings

Signed-off-by: Leo Siepel <[email protected]>
  • Loading branch information
lsiepel authored and renescherer committed Mar 23, 2023
1 parent 865bd6d commit c08704e
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 157 deletions.
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();
}

@Override
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,22 @@ protected void stopBackgroundDiscovery() {
cancelListenerJob();
}

private void startListenerJob() {
if (listenerJob == null) {
terminate = false;
private synchronized void startListenerJob() {
if (this.listenerJob == null) {
logger.debug("Starting discovery listener job in {} seconds", BACKGROUND_DISCOVERY_DELAY);
listenerJob = scheduledExecutorService.schedule(listenerRunnable, BACKGROUND_DISCOVERY_DELAY,
terminate = false;
this.listenerJob = scheduledExecutorService.schedule(listenerRunnable, BACKGROUND_DISCOVERY_DELAY,
TimeUnit.SECONDS);
}
}

private void cancelListenerJob() {
if (listenerJob != null) {
ScheduledFuture<?> localListenerJob = this.listenerJob;
if (localListenerJob != null) {
logger.debug("Canceling discovery listener job");
listenerJob.cancel(true);
localListenerJob.cancel(true);
terminate = true;
listenerJob = null;
this.listenerJob = null;
}
}

Expand All @@ -143,9 +144,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 +161,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,14 +170,11 @@ private synchronized void listen() {
break;
}
}
discoveryListener.shutdown();
localDiscoveryListener.shutdown();
logger.debug("DiscoveryListener job is exiting");
}

private void processMessage(BigAssFanDevice device) {
if (device == null) {
return;
}
Matcher matcher = announcementPattern.matcher(device.getDiscoveryMessage());
if (matcher.find()) {
logger.debug("Match: grp1={}, grp2={}, grp(3)={}", matcher.group(1), matcher.group(2), matcher.group(3));
Expand Down Expand Up @@ -242,23 +242,30 @@ 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);
private synchronized void schedulePollJob() {
cancelPollJob();
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);
if (this.pollJob == null) {
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) {
ScheduledFuture<?> localPollJob = pollJob;
if (localPollJob != null) {
logger.debug("Canceling poll job");
pollJob.cancel(true);
pollJob = null;
localPollJob.cancel(true);
this.pollJob = null;
}
}
}
Loading

0 comments on commit c08704e

Please sign in to comment.