]> git.basschouten.com Git - openhab-addons.git/blob
7f92c8d5bdc7f6f31c800440bf649d98e4735e36
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.systeminfo.internal;
14
15 import static org.openhab.binding.systeminfo.internal.SystemInfoBindingConstants.*;
16
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.systeminfo.internal.handler.SystemInfoHandler;
22 import org.openhab.binding.systeminfo.internal.model.SystemInfoInterface;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.openhab.core.thing.binding.ThingHandlerFactory;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Reference;
30
31 /**
32  * The {@link SystemInfoHandlerFactory} is responsible for creating things and thing
33  * handlers.
34  *
35  * @author Svilen Valkanov - Initial contribution
36  * @author Lyubomir Papazov - Pass systeminfo service to the SystemInfoHandler constructor
37  * @author Wouter Born - Add null annotations
38  * @author Mark Herwege - Add dynamic creation of extra channels
39  */
40 @NonNullByDefault
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.systeminfo")
42 public class SystemInfoHandlerFactory extends BaseThingHandlerFactory {
43     private @NonNullByDefault({}) SystemInfoInterface systeminfo;
44     private @NonNullByDefault({}) SystemInfoThingTypeProvider thingTypeProvider;
45
46     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_COMPUTER,
47             THING_TYPE_COMPUTER_IMPL);
48
49     @Override
50     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
51         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
52     }
53
54     @Override
55     protected @Nullable ThingHandler createHandler(Thing thing) {
56         if (THING_TYPE_COMPUTER.equals(thing.getThingTypeUID())) {
57             if (thingTypeProvider.getThingType(THING_TYPE_COMPUTER_IMPL, null) == null) {
58                 thingTypeProvider.createThingType(THING_TYPE_COMPUTER_IMPL);
59                 // Save the current channels configs, will be restored after thing type change.
60                 thingTypeProvider.storeChannelsConfig(thing);
61             }
62             return new SystemInfoHandler(thing, thingTypeProvider, systeminfo);
63         } else if (THING_TYPE_COMPUTER_IMPL.equals(thing.getThingTypeUID())) {
64             return new SystemInfoHandler(thing, thingTypeProvider, systeminfo);
65         }
66         return null;
67     }
68
69     @Reference
70     public void bindSystemInfo(SystemInfoInterface systeminfo) {
71         this.systeminfo = systeminfo;
72     }
73
74     public void unbindSystemInfo(SystemInfoInterface systeminfo) {
75         this.systeminfo = null;
76     }
77
78     @Reference
79     public void setSystemInfoThingTypeProvider(SystemInfoThingTypeProvider thingTypeProvider) {
80         this.thingTypeProvider = thingTypeProvider;
81     }
82
83     public void unsetSystemInfoThingTypeProvider(SystemInfoThingTypeProvider thingTypeProvider) {
84         this.thingTypeProvider = null;
85     }
86 }