2 * Copyright (c) 2010-2023 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.km200.internal.handler;
15 import static org.openhab.binding.km200.internal.KM200BindingConstants.*;
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;
23 import com.google.gson.JsonObject;
24 import com.google.gson.JsonParseException;
27 * The KM200DataHandler is representing the virtual services inside this binding
29 * @author Markus Eckhardt - Initial contribution
32 public class KM200VirtualServiceHandler {
33 private final Logger logger = LoggerFactory.getLogger(KM200VirtualServiceHandler.class);
35 private final KM200Device remoteDevice;
37 public KM200VirtualServiceHandler(KM200Device remoteDevice) {
38 this.remoteDevice = remoteDevice;
42 * This function creates the virtual services
44 public void initVirtualObjects() {
45 KM200ServiceObject newObject = null;
47 for (KM200ServiceObject object : remoteDevice.virtualList) {
48 logger.debug("Full Servicename: {}", object.getFullServiceName());
49 String id = object.getFullServiceName();
50 String type = object.getServiceType();
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());
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);
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);
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);
97 } catch (JsonParseException e) {
98 logger.warn("Parsingexception in JSON: {}", e.getMessage());