]> git.basschouten.com Git - openhab-addons.git/blob
e283badcd48bfffd775b4c9408ded820e376d62d
[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.yeelight.internal.lib.device;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.openhab.binding.yeelight.internal.lib.enums.DeviceType;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * The {@link DeviceFactory} creates device handler classes.
24  *
25  * @author Coaster Li - Initial contribution
26  * @author Nikita Pogudalov - Added CeilingDeviceWithNightDevice for Ceiling 1
27  */
28 public class DeviceFactory {
29     private static final Logger LOGGER = LoggerFactory.getLogger(DeviceFactory.class);
30
31     private static final String TAG = DeviceFactory.class.getSimpleName();
32
33     public static DeviceBase build(String model, String id) {
34         DeviceType type = DeviceType.valueOf(model);
35         switch (type) {
36             case ceiling:
37                 return new CeilingDevice(id);
38             case ceiling1:
39             case ceiling3:
40                 return new CeilingDeviceWithNightDevice(id);
41             case ceiling4:
42                 return new CeilingDeviceWithAmbientDevice(id);
43             case color:
44             case color4:
45                 return new WonderDevice(id);
46             case mono:
47                 return new MonoDevice(id);
48             case ct_bulb:
49                 return new CtBulbDevice(id);
50             case stripe:
51             case strip6:
52                 return new PitayaDevice(id);
53             case desklamp:
54                 return new DesklampDevice(id);
55             default:
56                 return null;
57         }
58     }
59
60     public static DeviceBase build(Map<String, String> bulbInfo) {
61         DeviceBase device = build(bulbInfo.get("model"), bulbInfo.get("id"));
62         if (null == device) {
63             return null;
64         }
65
66         Map<String, Object> infos = new HashMap<>(bulbInfo);
67         device.setBulbInfo(infos);
68         LOGGER.debug("{}: DeviceFactory Device = {}", TAG, bulbInfo.get("Location"));
69         // TODO enhancement!!!
70         String[] addressInfo = bulbInfo.get("Location").split(":");
71         device.setAddress(addressInfo[1].substring(2));
72         device.setPort(Integer.parseInt(addressInfo[2]));
73         device.setOnline(true);
74         LOGGER.debug("{}: DeviceFactory Device info = {}, port = {}", TAG, device.getAddress(), device.getPort());
75         return device;
76     }
77 }