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.*;
17 import java.util.Collections;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.openhab.binding.icalendar.internal.handler.EventFilterHandler;
26 import org.openhab.binding.icalendar.internal.handler.ICalendarHandler;
27 import org.openhab.core.events.EventPublisher;
28 import org.openhab.core.i18n.TimeZoneProvider;
29 import org.openhab.core.io.net.http.HttpClientFactory;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * The {@link ICalendarHandlerFactory} is responsible for creating things and thing
46 * @author Michael Wodniok - Initial contribution
47 * @author Andrew Fiddian-Green - EventPublisher code
48 * @author Michael Wodniok - Added FilteredEvent item type/handler
51 @Component(configurationPid = "binding.icalendar", service = ThingHandlerFactory.class)
52 public class ICalendarHandlerFactory extends BaseThingHandlerFactory {
54 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
55 .of(Collections.singleton(THING_TYPE_CALENDAR), Collections.singleton(THING_TYPE_FILTERED_EVENTS))
56 .flatMap(Set::stream).collect(Collectors.toSet());
57 private final Logger logger = LoggerFactory.getLogger(ICalendarHandlerFactory.class);
59 private final HttpClient sharedHttpClient;
60 private final EventPublisher eventPublisher;
61 private final TimeZoneProvider tzProvider;
64 public ICalendarHandlerFactory(@Reference HttpClientFactory httpClientFactory,
65 @Reference EventPublisher eventPublisher, @Reference TimeZoneProvider tzProvider) {
66 this.eventPublisher = eventPublisher;
67 sharedHttpClient = httpClientFactory.getCommonHttpClient();
68 this.tzProvider = tzProvider;
72 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
73 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
77 protected @Nullable ThingHandler createHandler(Thing thing) {
78 final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
80 if (!supportsThingType(thingTypeUID)) {
83 if (thingTypeUID.equals(THING_TYPE_CALENDAR)) {
84 if (thing instanceof Bridge) {
85 return new ICalendarHandler((Bridge) thing, sharedHttpClient, eventPublisher, tzProvider);
88 "The API of iCalendar has changed. You have to recreate the calendar according to the docs.");
90 } else if (thingTypeUID.equals(THING_TYPE_FILTERED_EVENTS)) {
91 return new EventFilterHandler(thing, tzProvider);