]> git.basschouten.com Git - openhab-addons.git/blob
6dcbb30a5e6caa5dbbc12b2307073724f5436593
[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.samsungtv.internal.service;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.samsungtv.internal.service.api.SamsungTvService;
22 import org.openhab.core.io.transport.upnp.UpnpIOService;
23
24 /**
25  * The {@link ServiceFactory} is helper class for creating Samsung TV related
26  * services.
27  *
28  * @author Pauli Anttila - Initial contribution
29  */
30 @NonNullByDefault
31 public class ServiceFactory {
32
33     @SuppressWarnings("serial")
34     private static final Map<String, Class<? extends SamsungTvService>> SERVICEMAP = Collections
35             .unmodifiableMap(new HashMap<>() {
36                 {
37                     put(MainTVServerService.SERVICE_NAME, MainTVServerService.class);
38                     put(MediaRendererService.SERVICE_NAME, MediaRendererService.class);
39                     put(RemoteControllerService.SERVICE_NAME, RemoteControllerService.class);
40                 }
41             });
42
43     /**
44      * Create Samsung TV service.
45      *
46      * @param type
47      * @param upnpIOService
48      * @param udn
49      * @param host
50      * @param port
51      * @return
52      */
53     public static @Nullable SamsungTvService createService(String type, UpnpIOService upnpIOService, String udn,
54             String host, int port) {
55         SamsungTvService service = null;
56
57         switch (type) {
58             case MainTVServerService.SERVICE_NAME:
59                 service = new MainTVServerService(upnpIOService, udn);
60                 break;
61             case MediaRendererService.SERVICE_NAME:
62                 service = new MediaRendererService(upnpIOService, udn);
63                 break;
64             // will not be created automatically
65             case RemoteControllerService.SERVICE_NAME:
66                 service = RemoteControllerService.createUpnpService(host, port);
67                 break;
68
69         }
70
71         return service;
72     }
73
74     /**
75      * Procedure to query amount of supported services.
76      *
77      * @return Amount of supported services
78      */
79     public static int getServiceCount() {
80         return SERVICEMAP.size();
81     }
82
83     /**
84      * Procedure to get service class by service name.
85      *
86      * @param serviceName Name of the service
87      * @return Class of the service
88      */
89     public static @Nullable Class<? extends SamsungTvService> getClassByServiceName(String serviceName) {
90         return SERVICEMAP.get(serviceName);
91     }
92 }