]> git.basschouten.com Git - openhab-addons.git/blob
13c08ebd18841fd3aa96b1159d4e6ffa74b19968
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.icalendar.internal;
14
15 import static org.openhab.binding.icalendar.internal.ICalendarBindingConstants.*;
16
17 import java.util.Set;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
20
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;
40
41 /**
42  * The {@link ICalendarHandlerFactory} is responsible for creating things and thing
43  * handlers.
44  *
45  * @author Michael Wodniok - Initial contribution
46  * @author Andrew Fiddian-Green - EventPublisher code
47  * @author Michael Wodniok - Added FilteredEvent item type/handler
48  */
49 @NonNullByDefault
50 @Component(configurationPid = "binding.icalendar", service = ThingHandlerFactory.class)
51 public class ICalendarHandlerFactory extends BaseThingHandlerFactory {
52
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);
57
58     private final HttpClient sharedHttpClient;
59     private final EventPublisher eventPublisher;
60     private final TimeZoneProvider tzProvider;
61
62     @Activate
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;
68     }
69
70     @Override
71     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73     }
74
75     @Override
76     protected @Nullable ThingHandler createHandler(Thing thing) {
77         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
78
79         if (!supportsThingType(thingTypeUID)) {
80             return null;
81         }
82         if (thingTypeUID.equals(THING_TYPE_CALENDAR)) {
83             if (thing instanceof Bridge bridge) {
84                 return new ICalendarHandler(bridge, sharedHttpClient, eventPublisher, tzProvider);
85             } else {
86                 logger.warn(
87                         "The API of iCalendar has changed. You have to recreate the calendar according to the docs.");
88             }
89         } else if (thingTypeUID.equals(THING_TYPE_FILTERED_EVENTS)) {
90             return new EventFilterHandler(thing, tzProvider);
91         }
92         return null;
93     }
94 }