]> git.basschouten.com Git - openhab-addons.git/blob
e21f9dcdd7fab5c328bbe955ab2d177e7dded394
[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.freeathomesystem.internal.datamodel;
14
15 import java.util.Iterator;
16 import java.util.Set;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.gson.JsonObject;
23
24 /**
25  *
26  * @author Andras Uhrin - Initial contribution
27  *
28  */
29 @NonNullByDefault
30 public class FreeAtHomeDatapoint {
31
32     private final Logger logger = LoggerFactory.getLogger(FreeAtHomeDatapoint.class);
33
34     enum DatapointDirection {
35         UNKNOWN,
36         INPUT,
37         OUTPUT,
38         INPUTOUTPUT,
39         INPUT_AS_OUTPUT
40     };
41
42     public String channelId = "";
43     private String datapointId = "";
44
45     DatapointDirection searchForDatapoint(DatapointDirection direction, int neededPairingIDFunction, String channelId,
46             JsonObject jsonObjectOfChannel) {
47         DatapointDirection resultingDirection = DatapointDirection.UNKNOWN;
48         boolean foundId = false;
49         JsonObject localDatapoints = null;
50
51         switch (direction) {
52             case INPUT: {
53                 localDatapoints = jsonObjectOfChannel.getAsJsonObject("inputs");
54                 resultingDirection = DatapointDirection.INPUT;
55                 break;
56             }
57             case INPUT_AS_OUTPUT: {
58                 localDatapoints = jsonObjectOfChannel.getAsJsonObject("inputs");
59                 resultingDirection = DatapointDirection.OUTPUT;
60                 break;
61             }
62             case OUTPUT: {
63                 localDatapoints = jsonObjectOfChannel.getAsJsonObject("outputs");
64                 resultingDirection = DatapointDirection.OUTPUT;
65                 break;
66             }
67             default: {
68                 localDatapoints = jsonObjectOfChannel.getAsJsonObject("outputs");
69                 resultingDirection = DatapointDirection.OUTPUT;
70                 break;
71             }
72         }
73
74         Set<String> keys = localDatapoints.keySet();
75
76         Iterator<String> iter = keys.iterator();
77
78         // Scan datapoints for pairingID IDs
79         while (iter.hasNext() && !foundId) {
80             String datapointId = iter.next();
81
82             JsonObject datapointJsonObject = localDatapoints.getAsJsonObject(datapointId);
83
84             int pairingIDFunction = datapointJsonObject.get("pairingID").getAsInt();
85
86             if (pairingIDFunction == neededPairingIDFunction) {
87                 this.channelId = channelId;
88                 this.datapointId = datapointId;
89
90                 logger.debug("Datapoint is found - channel {} - datapoint {}", this.channelId, this.datapointId);
91
92                 foundId = true;
93             }
94         }
95
96         // id not found, add dummy
97         if (!foundId) {
98             this.channelId = "";
99             this.datapointId = "";
100             resultingDirection = DatapointDirection.UNKNOWN;
101
102             logger.debug("Needed datapoint is not found - channel {} - pairingId {}", channelId,
103                     neededPairingIDFunction);
104         }
105
106         return resultingDirection;
107     }
108
109     public String getChannelIdforDatapoint() {
110         return channelId;
111     }
112
113     public String getDatapointId() {
114         return datapointId;
115     }
116 }