]> git.basschouten.com Git - openhab-addons.git/blob
b860af1ba49b80693350138ecb1b974f413d18fe
[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.km200.internal.handler;
14
15 import static org.openhab.binding.km200.internal.KM200BindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.km200.internal.KM200Device;
19 import org.openhab.binding.km200.internal.KM200ServiceObject;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParseException;
25
26 /**
27  * The KM200DataHandler is representing the virtual services inside this binding
28  *
29  * @author Markus Eckhardt - Initial contribution
30  */
31 @NonNullByDefault
32 public class KM200VirtualServiceHandler {
33     private final Logger logger = LoggerFactory.getLogger(KM200VirtualServiceHandler.class);
34
35     private final KM200Device remoteDevice;
36
37     public KM200VirtualServiceHandler(KM200Device remoteDevice) {
38         this.remoteDevice = remoteDevice;
39     }
40
41     /**
42      * This function creates the virtual services
43      */
44     public void initVirtualObjects() {
45         KM200ServiceObject newObject = null;
46         try {
47             for (KM200ServiceObject object : remoteDevice.virtualList) {
48                 logger.debug("Full Servicename: {}", object.getFullServiceName());
49                 String id = object.getFullServiceName();
50                 String type = object.getServiceType();
51                 switch (type) {
52                     case DATA_TYPE_SWITCH_PROGRAM:
53                         KM200SwitchProgramServiceHandler sPService = ((KM200SwitchProgramServiceHandler) object
54                                 .getValueParameter());
55                         if (null != sPService) {
56                             if (!sPService.determineSwitchNames(remoteDevice)) {
57                                 logger.info("No references for switch service: {}, this is not supported",
58                                         object.getFullServiceName());
59                                 continue;
60                             }
61                             JsonObject nodeRoot = object.getJSONData();
62                             if (null != nodeRoot) {
63                                 sPService.updateSwitches(nodeRoot, remoteDevice);
64                                 newObject = new KM200ServiceObject(id + "/weekday", type, 1, 1, 0, 1, id);
65                                 object.serviceTreeMap.put("weekday", newObject);
66                                 newObject = new KM200ServiceObject(id + "/nbrCycles", type, 1, 0, 0, 1, id);
67                                 object.serviceTreeMap.put("nbrCycles", newObject);
68                                 newObject = new KM200ServiceObject(id + "/cycle", type, 1, 1, 0, 1, id);
69                                 object.serviceTreeMap.put("cycle", newObject);
70                                 logger.debug("On: {}  Of: {}", id + "/" + sPService.getPositiveSwitch(),
71                                         id + "/" + sPService.getNegativeSwitch());
72                                 newObject = new KM200ServiceObject(id + "/" + sPService.getPositiveSwitch(), type, 1,
73                                         object.getWriteable(), object.getRecordable(), 1, id);
74                                 String posSwitch = sPService.getPositiveSwitch();
75                                 if (null != posSwitch) {
76                                     object.serviceTreeMap.put(posSwitch, newObject);
77                                 }
78                                 newObject = new KM200ServiceObject(id + "/" + sPService.getNegativeSwitch(), type, 1,
79                                         object.getWriteable(), object.getRecordable(), 1, id);
80                                 String negSwitch = sPService.getNegativeSwitch();
81                                 if (null != negSwitch) {
82                                     object.serviceTreeMap.put(negSwitch, newObject);
83                                 }
84                             }
85                         }
86                         break;
87                     case DATA_TYPE_ERROR_LIST:
88                         newObject = new KM200ServiceObject(id + "/nbrErrors", type, 1, 0, 0, 1, id);
89                         object.serviceTreeMap.put("nbrErrors", newObject);
90                         newObject = new KM200ServiceObject(id + "/error", type, 1, 1, 0, 1, id);
91                         object.serviceTreeMap.put("error", newObject);
92                         newObject = new KM200ServiceObject(id + "/errorString", type, 1, 0, 0, 1, id);
93                         object.serviceTreeMap.put("errorString", newObject);
94                         break;
95                 }
96             }
97         } catch (JsonParseException e) {
98             logger.warn("Parsingexception in JSON: {}", e.getMessage());
99         }
100     }
101 }