Skip to content

Commit

Permalink
[smsmodem] Apply review
Browse files Browse the repository at this point in the history
Signed-off-by: Gwendal Roulleau <[email protected]>
  • Loading branch information
dalgwen committed Oct 14, 2022
1 parent 86d3991 commit 6216419
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,32 +115,9 @@ public void initialize() {
config = getConfigAs(SMSConversationConfiguration.class);
try {
checkBridgeHandler();
setStatusByBridgeStatus();
updateStatus(ThingStatus.ONLINE);
} catch (ConfigurationException confe) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, confe.getMessage());
}
}

private void setStatusByBridgeStatus() {
SMSModemBridgeHandler bridgeHandlerFinal = bridgeHandler;
if (bridgeHandlerFinal != null) {
switch (bridgeHandlerFinal.getThing().getStatus()) {
case ONLINE:
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
break;
case INITIALIZING:
case OFFLINE:
case REMOVED:
case REMOVING:
case UNINITIALIZED:
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
break;
case UNKNOWN:
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.BRIDGE_OFFLINE);
break;
}
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,60 +83,71 @@ public class SMSModemBridgeHandler extends BaseBridgeHandler
/**
* The smslib object responsible for the serial communication with the modem
*/
private @NonNullByDefault({}) Modem modem;
private @Nullable Modem modem;

/**
* A scheduled watchdog check
*/
private @NonNullByDefault({}) ScheduledFuture<?> checkScheduled;
private @Nullable ScheduledFuture<?> checkScheduled;

// we keep a list of msisdn sender for autodiscovery
private Set<String> senderMsisdn = new HashSet<String>();
private @Nullable SMSConversationDiscoveryService discoveryService;

private boolean shouldRun = false;

public SMSModemBridgeHandler(Bridge bridge, SerialPortManager serialPortManager) {
super(bridge);
this.serialPortManager = serialPortManager;
}

@Override
public void dispose() {
shouldRun = false;
checkScheduled.cancel(true);
scheduler.execute(modem::stop);
modem.registerStatusListener(null);
modem.registerMessageListener(null);
modem.registerInformationListener(null);
ScheduledFuture<?> checkScheduledFinal = checkScheduled;
if (checkScheduledFinal != null) {
checkScheduledFinal.cancel(true);
}
Modem finalModem = modem;
if (finalModem != null) {
scheduler.execute(finalModem::stop);
finalModem.registerStatusListener(null);
finalModem.registerMessageListener(null);
finalModem.registerInformationListener(null);
}
modem = null;
}

public SMSModemBridgeHandler(Bridge bridge, SerialPortManager serialPortManager) {
super(bridge);
this.serialPortManager = serialPortManager;
}

@Override
protected void updateConfiguration(Configuration configuration) {
super.updateConfiguration(configuration);
scheduler.execute(() -> {
modem.stop();
Modem finalModem = modem;
if (finalModem != null) {
finalModem.stop();
}
checkAndStartModemIfNeeded();
});
}

@Override
public void initialize() {
shouldRun = true;
if (checkScheduled == null || (checkScheduled.isDone()) && this.shouldRun) {
ScheduledFuture<?> checkScheduledFinal = checkScheduled;
if (checkScheduledFinal == null || (checkScheduledFinal.isDone()) && this.shouldRun) {
checkScheduled = scheduler.scheduleWithFixedDelay(this::checkAndStartModemIfNeeded, 0, 15,
TimeUnit.SECONDS);
}
}

private void checkAndStartModemIfNeeded() {
private synchronized void checkAndStartModemIfNeeded() {
try {
if (shouldRun && !isRunning()) {
logger.debug("Initializing smsmodem");
// ensure the underlying modem is stopped before trying to (re)starting it :
if (modem != null) {
modem.stop();
Modem finalModem = modem;
if (finalModem != null) {
finalModem.stop();
}
String logName;
if (getThing().getThingTypeUID().equals(SMSModemBindingConstants.SMSMODEMBRIDGE_THING_TYPE)) {
Expand All @@ -158,10 +169,13 @@ private void checkAndStartModemIfNeeded() {
throw new IllegalArgumentException("Invalid thing type");
}
logger.debug("Now trying to start SMSModem {}", logName);
modem.registerStatusListener(this);
modem.registerMessageListener(this);
modem.registerInformationListener(this);
modem.start();
finalModem = modem;
if (finalModem != null) {
finalModem.registerStatusListener(this);
finalModem.registerMessageListener(this);
finalModem.registerInformationListener(this);
finalModem.start();
}
logger.debug("SMSModem {} started", logName);
}
} catch (ModemConfigurationException e) {
Expand Down Expand Up @@ -210,7 +224,9 @@ private String resolveEventualSymbolicLink(String serialPortOrIp) {
}

public boolean isRunning() {
return modem != null && (modem.getStatus() == Status.Started || modem.getStatus() == Status.Starting);
Modem finalModem = modem;
return finalModem != null
&& (finalModem.getStatus() == Status.Started || finalModem.getStatus() == Status.Starting);
}

@Override
Expand Down Expand Up @@ -258,7 +274,10 @@ public void messageReceived(InboundMessage message) {
finalDiscoveryService.buildByAutoDiscovery(sender);
}
try { // delete message on the sim
modem.delete(message);
Modem finalModem = modem;
if (finalModem != null) {
finalModem.delete(message);
}
} catch (CommunicationException e) {
logger.warn("Cannot delete message after receiving it !", e);
}
Expand All @@ -283,7 +302,10 @@ public void send(String recipient, String text, boolean deliveryReport, @Nullabl
}
out.setRequestDeliveryReport(deliveryReport);
logger.debug("Sending message to {}", recipient);
modem.queue(out);
Modem finalModem = modem;
if (finalModem != null) {
finalModem.queue(out);
}
}

/**
Expand All @@ -304,8 +326,13 @@ public Collection<Class<? extends ThingHandlerService>> getServices() {
public boolean processStatusCallback(Modem.Status oldStatus, Modem.Status newStatus) {
switch (newStatus) {
case Error:
String finalDescription = "unknown";
Modem finalModem = modem;
if (finalModem != null) {
finalDescription = finalModem.getDescription();
}
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"SMSLib reported an error on the underlying modem " + modem.getDescription());
"SMSLib reported an error on the underlying modem " + finalDescription);
break;
case Started:
updateStatus(ThingStatus.ONLINE);
Expand Down Expand Up @@ -389,7 +416,10 @@ public void messageDelivered(DeliveryReportMessage message) {
}
}
try {
modem.delete(message);
Modem finalModem = modem;
if (finalModem != null) {
finalModem.delete(message);
}
} catch (CommunicationException e) {
logger.warn("Cannot delete delivery report after receiving it !", e);
}
Expand Down

0 comments on commit 6216419

Please sign in to comment.