2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.icalendar.internal;
15 import static org.openhab.binding.icalendar.internal.ICalendarBindingConstants.*;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.icalendar.internal.handler.EventFilterHandler;
25 import org.openhab.binding.icalendar.internal.handler.ICalendarHandler;
26 import org.openhab.core.events.EventPublisher;
27 import org.openhab.core.i18n.TimeZoneProvider;
28 import org.openhab.core.io.net.http.HttpClientFactory;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link ICalendarHandlerFactory} is responsible for creating things and thing
45 * @author Michael Wodniok - Initial contribution
46 * @author Andrew Fiddian-Green - EventPublisher code
47 * @author Michael Wodniok - Added FilteredEvent item type/handler
50 @Component(configurationPid = "binding.icalendar", service = ThingHandlerFactory.class)
51 public class ICalendarHandlerFactory extends BaseThingHandlerFactory {
53 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
54 .of(Set.of(THING_TYPE_CALENDAR), Set.of(THING_TYPE_FILTERED_EVENTS)).flatMap(Set::stream)
55 .collect(Collectors.toSet());
56 private final Logger logger = LoggerFactory.getLogger(ICalendarHandlerFactory.class);
58 private final HttpClient sharedHttpClient;
59 private final EventPublisher eventPublisher;
60 private final TimeZoneProvider tzProvider;
63 public ICalendarHandlerFactory(@Reference HttpClientFactory httpClientFactory,
64 @Reference EventPublisher eventPublisher, @Reference TimeZoneProvider tzProvider) {
65 this.eventPublisher = eventPublisher;
66 sharedHttpClient = httpClientFactory.getCommonHttpClient();
67 this.tzProvider = tzProvider;
71 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
76 protected @Nullable ThingHandler createHandler(Thing thing) {
77 final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79 if (!supportsThingType(thingTypeUID)) {
82 if (thingTypeUID.equals(THING_TYPE_CALENDAR)) {
83 if (thing instanceof Bridge bridge) {
84 return new ICalendarHandler(bridge, sharedHttpClient, eventPublisher, tzProvider);
87 "The API of iCalendar has changed. You have to recreate the calendar according to the docs.");
89 } else if (thingTypeUID.equals(THING_TYPE_FILTERED_EVENTS)) {
90 return new EventFilterHandler(thing, tzProvider);