2 * Copyright (c) 2010-2024 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.systeminfo.internal;
15 import static org.openhab.binding.systeminfo.internal.SystemInfoBindingConstants.*;
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;
30 * The {@link SystemInfoHandlerFactory} is responsible for creating things and thing
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
39 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.systeminfo")
40 public class SystemInfoHandlerFactory extends BaseThingHandlerFactory {
41 private @NonNullByDefault({}) SystemInfoInterface systeminfo;
42 private @NonNullByDefault({}) SystemInfoThingTypeProvider thingTypeProvider;
45 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
46 return BINDING_ID.equals(thingTypeUID.getBindingId())
47 && thingTypeUID.getId().startsWith(THING_TYPE_COMPUTER_ID);
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.
61 return new SystemInfoHandler(thing, thingTypeProvider, systeminfo);
67 public void bindSystemInfo(SystemInfoInterface systeminfo) {
68 this.systeminfo = systeminfo;
71 public void unbindSystemInfo(SystemInfoInterface systeminfo) {
72 this.systeminfo = null;
76 public void setSystemInfoThingTypeProvider(SystemInfoThingTypeProvider thingTypeProvider) {
77 this.thingTypeProvider = thingTypeProvider;
80 public void unsetSystemInfoThingTypeProvider(SystemInfoThingTypeProvider thingTypeProvider) {
81 this.thingTypeProvider = null;