mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-25 14:55:55 +01:00
[solax] Implementation of refresh command and improved thread safety (#15958)
* Implementation of refresh command and better multi-thread handling --------- Signed-off-by: Konstantin Polihronov <polychronov@gmail.com>
This commit is contained in:
parent
4d13a6d20e
commit
f4aa1c6d59
@ -26,7 +26,7 @@ import org.openhab.core.thing.ThingTypeUID;
|
|||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class SolaxBindingConstants {
|
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";
|
private static final String THING_LOCAL_CONNECT_INVERTER_ID = "local-connect-inverter";
|
||||||
|
|
||||||
// List of all Thing Type UIDs
|
// List of all Thing Type UIDs
|
||||||
|
@ -19,6 +19,7 @@ import java.util.List;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
import javax.measure.Quantity;
|
import javax.measure.Quantity;
|
||||||
import javax.measure.Unit;
|
import javax.measure.Unit;
|
||||||
@ -42,6 +43,7 @@ import org.openhab.core.thing.ThingStatus;
|
|||||||
import org.openhab.core.thing.ThingStatusDetail;
|
import org.openhab.core.thing.ThingStatusDetail;
|
||||||
import org.openhab.core.thing.binding.BaseThingHandler;
|
import org.openhab.core.thing.binding.BaseThingHandler;
|
||||||
import org.openhab.core.types.Command;
|
import org.openhab.core.types.Command;
|
||||||
|
import org.openhab.core.types.RefreshType;
|
||||||
import org.openhab.core.types.UnDefType;
|
import org.openhab.core.types.UnDefType;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -69,6 +71,8 @@ public class SolaxLocalAccessHandler extends BaseThingHandler {
|
|||||||
|
|
||||||
private final Set<String> unsupportedExistingChannels = new HashSet<String>();
|
private final Set<String> unsupportedExistingChannels = new HashSet<String>();
|
||||||
|
|
||||||
|
private final ReentrantLock retrieveDataCallLock = new ReentrantLock();
|
||||||
|
|
||||||
public SolaxLocalAccessHandler(Thing thing) {
|
public SolaxLocalAccessHandler(Thing thing) {
|
||||||
super(thing);
|
super(thing);
|
||||||
}
|
}
|
||||||
@ -88,19 +92,25 @@ public class SolaxLocalAccessHandler extends BaseThingHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void retrieveData() {
|
private void retrieveData() {
|
||||||
try {
|
if (retrieveDataCallLock.tryLock()) {
|
||||||
String rawJsonData = localHttpConnector.retrieveData();
|
try {
|
||||||
logger.debug("Raw data retrieved = {}", rawJsonData);
|
String rawJsonData = localHttpConnector.retrieveData();
|
||||||
|
logger.debug("Raw data retrieved = {}", rawJsonData);
|
||||||
|
|
||||||
if (rawJsonData != null && !rawJsonData.isEmpty()) {
|
if (rawJsonData != null && !rawJsonData.isEmpty()) {
|
||||||
updateFromData(rawJsonData);
|
updateFromData(rawJsonData);
|
||||||
} else {
|
} else {
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
||||||
SolaxBindingConstants.I18N_KEY_OFFLINE_COMMUNICATION_ERROR_JSON_CANNOT_BE_RETRIEVED);
|
SolaxBindingConstants.I18N_KEY_OFFLINE_COMMUNICATION_ERROR_JSON_CANNOT_BE_RETRIEVED);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.debug("Exception received while attempting to retrieve data via HTTP", e);
|
||||||
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
|
||||||
|
} finally {
|
||||||
|
retrieveDataCallLock.unlock();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} else {
|
||||||
logger.debug("Exception received while attempting to retrieve data via HTTP", e);
|
logger.debug("Unable to retrieve data because a request is already in progress.");
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,7 +293,11 @@ public class SolaxLocalAccessHandler extends BaseThingHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleCommand(ChannelUID channelUID, Command command) {
|
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
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user