[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:
Christoph Weitkamp 2020-12-13 07:00:16 +01:00 committed by GitHub
parent af3f8774d0
commit 863606e311
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 18 deletions

View File

@ -45,6 +45,7 @@ import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.thing.binding.ThingHandlerCallback; import org.openhab.core.thing.binding.ThingHandlerCallback;
import org.openhab.core.thing.binding.builder.ChannelBuilder; import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.thing.binding.builder.ThingBuilder; 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.Command;
import org.openhab.core.types.RefreshType; import org.openhab.core.types.RefreshType;
import org.openhab.core.types.UnDefType; import org.openhab.core.types.UnDefType;
@ -64,12 +65,10 @@ public class EventFilterHandler extends BaseThingHandler implements CalendarUpda
private final List<ResultChannelSet> resultChannels; private final List<ResultChannelSet> resultChannels;
private final TimeZoneProvider tzProvider; private final TimeZoneProvider tzProvider;
private @Nullable ScheduledFuture<?> updateFuture; private @Nullable ScheduledFuture<?> updateFuture;
private boolean initFinished;
public EventFilterHandler(Thing thing, TimeZoneProvider tzProvider) { public EventFilterHandler(Thing thing, TimeZoneProvider tzProvider) {
super(thing); super(thing);
resultChannels = new CopyOnWriteArrayList<>(); resultChannels = new CopyOnWriteArrayList<>();
initFinished = false;
this.tzProvider = tzProvider; this.tzProvider = tzProvider;
} }
@ -101,8 +100,6 @@ public class EventFilterHandler extends BaseThingHandler implements CalendarUpda
@Override @Override
public void initialize() { public void initialize() {
updateStatus(ThingStatus.UNKNOWN);
Bridge iCalendarBridge = getBridge(); Bridge iCalendarBridge = getBridge();
if (iCalendarBridge == null) { if (iCalendarBridge == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
@ -120,12 +117,12 @@ public class EventFilterHandler extends BaseThingHandler implements CalendarUpda
configuration = config; configuration = config;
updateChannelSet(config); updateChannelSet(config);
initFinished = true;
if (iCalendarBridge.getStatus() != ThingStatus.ONLINE) { if (iCalendarBridge.getStatus() != ThingStatus.ONLINE) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
return; return;
} }
updateStates();
updateStatus(ThingStatus.UNKNOWN);
} }
@Override @Override
@ -254,7 +251,7 @@ public class EventFilterHandler extends BaseThingHandler implements CalendarUpda
* Updates all states and channels. Reschedules an update if no error occurs. * Updates all states and channels. Reschedules an update if no error occurs.
*/ */
private void updateStates() { private void updateStates() {
if (!initFinished) { if (!ThingHandlerHelper.isHandlerInitialized(this)) {
logger.debug("Ignoring call for updating states as this instance is not initialized yet."); logger.debug("Ignoring call for updating states as this instance is not initialized yet.");
return; return;
} }

View File

@ -158,6 +158,7 @@ public class ICalendarHandler extends BaseBridgeHandler implements CalendarUpdat
// reload calendar file asynchronously // reload calendar file asynchronously
if (reloadCalendar()) { if (reloadCalendar()) {
updateStates(); updateStates();
updateChildren();
} else { } else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"The calendar seems to be configured correctly, but the local copy of calendar could not be loaded."); "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 @Override
public void onCalendarUpdated() { public void onCalendarUpdated() {
if (reloadCalendar()) { if (reloadCalendar()) {
updateStates(); updateStates();
for (Thing childThing : getThing().getThings()) { updateChildren();
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);
}
}
}
} else { } else {
logger.trace("Calendar was updated, but loading failed."); logger.trace("Calendar was updated, but loading failed.");
} }
@ -389,4 +388,27 @@ public class ICalendarHandler extends BaseBridgeHandler implements CalendarUpdat
updateStatesLastCalledTime = now; 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);
}
}
}
} }