Skip to content

Commit

Permalink
Implementation of refresh command and better multi-thread handling
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Polihronov <[email protected]>
  • Loading branch information
theater committed Nov 25, 2023
1 parent 24f4234 commit 9756bbf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@NonNullByDefault
public class SolaxBindingConstants {

private static final String BINDING_ID = "solax";
protected static final String BINDING_ID = "solax";
private static final String THING_LOCAL_CONNECT_INVERTER_ID = "local-connect-inverter";

// List of all Thing Type UIDs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import javax.measure.Quantity;
import javax.measure.Unit;
Expand All @@ -42,6 +43,7 @@
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.UnDefType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -69,6 +71,8 @@ public class SolaxLocalAccessHandler extends BaseThingHandler {

private final Set<String> unsupportedExistingChannels = new HashSet<String>();

private final ReentrantLock retrieveDataCallLock = new ReentrantLock();

public SolaxLocalAccessHandler(Thing thing) {
super(thing);
}
Expand All @@ -89,18 +93,26 @@ public void initialize() {

private void retrieveData() {
try {
String rawJsonData = localHttpConnector.retrieveData();
logger.debug("Raw data retrieved = {}", rawJsonData);

if (rawJsonData != null && !rawJsonData.isEmpty()) {
updateFromData(rawJsonData);
if (retrieveDataCallLock.tryLock()) {
String rawJsonData = localHttpConnector.retrieveData();
logger.debug("Raw data retrieved = {}", rawJsonData);

if (rawJsonData != null && !rawJsonData.isEmpty()) {
updateFromData(rawJsonData);
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
SolaxBindingConstants.I18N_KEY_OFFLINE_COMMUNICATION_ERROR_JSON_CANNOT_BE_RETRIEVED);
}
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
SolaxBindingConstants.I18N_KEY_OFFLINE_COMMUNICATION_ERROR_JSON_CANNOT_BE_RETRIEVED);
logger.debug("Unable to retrieve data because a request is already in progress.");
}
} catch (IOException e) {
logger.debug("Exception received while attempting to retrieve data via HTTP", e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
} finally {
if (retrieveDataCallLock.isHeldByCurrentThread()) {
retrieveDataCallLock.unlock();
}
}
}

Expand Down Expand Up @@ -283,7 +295,11 @@ private void logRemovedChannels(List<Channel> channelsToRemove) {

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
// Nothing to do here as of now. Maybe implement a REFRESH command in the future.
if (command instanceof RefreshType) {
scheduler.execute(this::retrieveData);
} else {
logger.debug("Binding {} only supports refresh command", SolaxBindingConstants.BINDING_ID);
}
}

@Override
Expand Down

0 comments on commit 9756bbf

Please sign in to comment.