This repository has been archived by the owner on May 17, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
TACmiGenericBindingProvider.java
174 lines (156 loc) · 4.57 KB
/
TACmiGenericBindingProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Copyright (c) 2010-2016 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.tacmi.internal;
import org.openhab.binding.tacmi.internal.TACmiMeasureType;
import org.openhab.binding.tacmi.TACmiBindingProvider;
import org.openhab.core.binding.BindingConfig;
import org.openhab.core.items.Item;
import org.openhab.model.item.binding.AbstractGenericBindingProvider;
import org.openhab.model.item.binding.BindingConfigParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class is responsible for parsing the binding configuration.
*
* *
* <p>
* Valid bindings for digital ports are:
* <ul>
* <li><code>{ tacmi="<can_node>#d#<port>" }</code></li>
* <ul>
* <li><code>{ tacmi="50#d#1" }</code></li>
* </ul>
* </li> </ul>
* <p>
* Valid bindings for analog ports are:
* <ul>
* <li>
* <code>{ tacmi="<can_node>#d#<port>#Measuretype" }</code></li>
* <ul>
* <li>
* <code>{ tacmi="50#a#1#Temperature" }</code></li>
* </ul>
* </li> </ul>
*
* @author Timo Wendt
* @since 1.8.0
*/
public class TACmiGenericBindingProvider extends AbstractGenericBindingProvider
implements TACmiBindingProvider {
private static Logger logger = LoggerFactory
.getLogger(TACmiGenericBindingProvider.class);
/**
* {@inheritDoc}
*/
public String getBindingType() {
return "tacmi";
}
/**
* @{inheritDoc
*/
@Override
public void validateItemType(Item item, String bindingConfig)
throws BindingConfigParseException {
}
/**
* {@inheritDoc}
*/
@Override
public void processBindingConfiguration(String context, Item item,
String bindingConfig) throws BindingConfigParseException {
super.processBindingConfiguration(context, item, bindingConfig);
TACmiBindingConfig config = new TACmiBindingConfig();
logger.debug("Processing binding configuration: '{}'", bindingConfig);
final String[] configParts = bindingConfig.split("#");
switch (configParts.length) {
case 3:
config.canNode = configParts[0];
config.portType = configParts[1];
config.portNumber = Integer.parseInt(configParts[2]);
break;
case 4:
config.canNode = configParts[0];
config.portType = configParts[1];
config.portNumber = Integer.parseInt(configParts[2]);
config.measureType = TACmiMeasureType.fromString(configParts[3]);
break;
default:
throw new BindingConfigParseException(
"A TACmi binding configuration must consist of three or four parts - please verify your *.items file");
}
config.configurationString = bindingConfig;
logger.debug("Adding Binding configuration: {}", config);
addBindingConfig(item, config);
}
/**
* This is a helper class holding binding specific configuration details
*
* @author Timo Wendt
* @since 1.7.0
*/
class TACmiBindingConfig implements BindingConfig {
String canNode;
String portType;
int portNumber;
TACmiMeasureType measureType;
String configurationString;
@Override
public String toString() {
return "NetatmoBindingConfig [canNode=" + this.canNode
+ ", portType=" + this.portType + ", portNumber="
+ this.portNumber + ", measure="
+ this.measureType.getMeasure() + "]";
}
}
/**
* {@inheritDoc}
*/
@Override
public String getConfigurationString(String itemName) {
final TACmiBindingConfig config = (TACmiBindingConfig) this.bindingConfigs
.get(itemName);
return config != null ? config.configurationString : null;
}
/**
* {@inheritDoc}
*/
@Override
public int getCanNode(String itemName) {
final TACmiBindingConfig config = (TACmiBindingConfig) this.bindingConfigs
.get(itemName);
return config != null ? Integer.parseInt(config.canNode) : null;
}
/**
* {@inheritDoc}
*/
@Override
public String getPortType(String itemName) {
final TACmiBindingConfig config = (TACmiBindingConfig) this.bindingConfigs
.get(itemName);
return config != null ? config.portType : null;
}
/**
* {@inheritDoc}
*/
@Override
public int getPortNumber(String itemName) {
final TACmiBindingConfig config = (TACmiBindingConfig) this.bindingConfigs
.get(itemName);
return config != null ? config.portNumber : null;
}
/**
* {@inheritDoc}
*/
@Override
public TACmiMeasureType getMeasureType(String itemName) {
final TACmiBindingConfig config = (TACmiBindingConfig) this.bindingConfigs
.get(itemName);
return config != null ? config.measureType : null;
}
}