]> git.basschouten.com Git - openhab-addons.git/blob
bc02959dcf847e4869406846e6c62562f3b33f31
[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.Collections;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
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;
41
42 /**
43  * The {@link ICalendarHandlerFactory} is responsible for creating things and thing
44  * handlers.
45  *
46  * @author Michael Wodniok - Initial contribution
47  * @author Andrew Fiddian-Green - EventPublisher code
48  * @author Michael Wodniok - Added FilteredEvent item type/handler
49  */
50 @NonNullByDefault
51 @Component(configurationPid = "binding.icalendar", service = ThingHandlerFactory.class)
52 public class ICalendarHandlerFactory extends BaseThingHandlerFactory {
53
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);
58
59     private final HttpClient sharedHttpClient;
60     private final EventPublisher eventPublisher;
61     private final TimeZoneProvider tzProvider;
62
63     @Activate
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;
69     }
70
71     @Override
72     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
73         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
74     }
75
76     @Override
77     protected @Nullable ThingHandler createHandler(Thing thing) {
78         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79
80         if (!supportsThingType(thingTypeUID)) {
81             return null;
82         }
83         if (thingTypeUID.equals(THING_TYPE_CALENDAR)) {
84             if (thing instanceof Bridge) {
85                 return new ICalendarHandler((Bridge) thing, sharedHttpClient, eventPublisher, tzProvider);
86             } else {
87                 logger.warn(
88                         "The API of iCalendar has changed. You have to recreate the calendar according to the docs.");
89             }
90         } else if (thingTypeUID.equals(THING_TYPE_FILTERED_EVENTS)) {
91             return new EventFilterHandler(thing, tzProvider);
92         }
93         return null;
94     }
95 }