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