[icalendar] Ensure Bridge is initialized and calendar is available to update child Things (#9337)
* Ensure Bridge is initialized and calendar is available to update child Things Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
parent
af3f8774d0
commit
863606e311
@ -45,6 +45,7 @@ import org.openhab.core.thing.binding.BaseThingHandler;
|
||||
import org.openhab.core.thing.binding.ThingHandlerCallback;
|
||||
import org.openhab.core.thing.binding.builder.ChannelBuilder;
|
||||
import org.openhab.core.thing.binding.builder.ThingBuilder;
|
||||
import org.openhab.core.thing.util.ThingHandlerHelper;
|
||||
import org.openhab.core.types.Command;
|
||||
import org.openhab.core.types.RefreshType;
|
||||
import org.openhab.core.types.UnDefType;
|
||||
@ -64,12 +65,10 @@ public class EventFilterHandler extends BaseThingHandler implements CalendarUpda
|
||||
private final List<ResultChannelSet> resultChannels;
|
||||
private final TimeZoneProvider tzProvider;
|
||||
private @Nullable ScheduledFuture<?> updateFuture;
|
||||
private boolean initFinished;
|
||||
|
||||
public EventFilterHandler(Thing thing, TimeZoneProvider tzProvider) {
|
||||
super(thing);
|
||||
resultChannels = new CopyOnWriteArrayList<>();
|
||||
initFinished = false;
|
||||
this.tzProvider = tzProvider;
|
||||
}
|
||||
|
||||
@ -101,8 +100,6 @@ public class EventFilterHandler extends BaseThingHandler implements CalendarUpda
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
updateStatus(ThingStatus.UNKNOWN);
|
||||
|
||||
Bridge iCalendarBridge = getBridge();
|
||||
if (iCalendarBridge == null) {
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
|
||||
@ -120,12 +117,12 @@ public class EventFilterHandler extends BaseThingHandler implements CalendarUpda
|
||||
configuration = config;
|
||||
|
||||
updateChannelSet(config);
|
||||
initFinished = true;
|
||||
if (iCalendarBridge.getStatus() != ThingStatus.ONLINE) {
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
|
||||
return;
|
||||
}
|
||||
updateStates();
|
||||
|
||||
updateStatus(ThingStatus.UNKNOWN);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -254,7 +251,7 @@ public class EventFilterHandler extends BaseThingHandler implements CalendarUpda
|
||||
* Updates all states and channels. Reschedules an update if no error occurs.
|
||||
*/
|
||||
private void updateStates() {
|
||||
if (!initFinished) {
|
||||
if (!ThingHandlerHelper.isHandlerInitialized(this)) {
|
||||
logger.debug("Ignoring call for updating states as this instance is not initialized yet.");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -158,6 +158,7 @@ public class ICalendarHandler extends BaseBridgeHandler implements CalendarUpdat
|
||||
// reload calendar file asynchronously
|
||||
if (reloadCalendar()) {
|
||||
updateStates();
|
||||
updateChildren();
|
||||
} else {
|
||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
|
||||
"The calendar seems to be configured correctly, but the local copy of calendar could not be loaded.");
|
||||
@ -176,21 +177,19 @@ public class ICalendarHandler extends BaseBridgeHandler implements CalendarUpdat
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
|
||||
final AbstractPresentableCalendar calendar = runtimeCalendar;
|
||||
if (calendar != null) {
|
||||
updateChild(childHandler);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCalendarUpdated() {
|
||||
if (reloadCalendar()) {
|
||||
updateStates();
|
||||
for (Thing childThing : getThing().getThings()) {
|
||||
ThingHandler handler = childThing.getHandler();
|
||||
if (handler instanceof CalendarUpdateListener) {
|
||||
try {
|
||||
logger.trace("Notifying {} about fresh calendar.", handler.getThing().getUID());
|
||||
((CalendarUpdateListener) handler).onCalendarUpdated();
|
||||
} catch (Exception e) {
|
||||
logger.trace("The update of a child handler failed. Ignoring.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
updateChildren();
|
||||
} else {
|
||||
logger.trace("Calendar was updated, but loading failed.");
|
||||
}
|
||||
@ -389,4 +388,27 @@ public class ICalendarHandler extends BaseBridgeHandler implements CalendarUpdat
|
||||
updateStatesLastCalledTime = now;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates all children of this handler.
|
||||
*/
|
||||
private void updateChildren() {
|
||||
getThing().getThings().forEach(childThing -> updateChild(childThing.getHandler()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a specific child handler.
|
||||
*
|
||||
* @param childHandler the handler to be updated
|
||||
*/
|
||||
private void updateChild(@Nullable ThingHandler childHandler) {
|
||||
if (childHandler instanceof CalendarUpdateListener) {
|
||||
logger.trace("Notifying {} about fresh calendar.", childHandler.getThing().getUID());
|
||||
try {
|
||||
((CalendarUpdateListener) childHandler).onCalendarUpdated();
|
||||
} catch (Exception e) {
|
||||
logger.trace("The update of a child handler failed. Ignoring.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user