20
20
21
21
package com .christophecvb .touchportal .helpers ;
22
22
23
- import com .christophecvb .touchportal .annotations .Action ;
24
23
import com .christophecvb .touchportal .annotations .Category ;
25
24
import com .christophecvb .touchportal .annotations .Connector ;
26
25
27
26
import javax .lang .model .element .Element ;
28
27
import java .lang .reflect .Method ;
28
+ import java .util .Map ;
29
29
30
30
/**
31
31
* Touch Portal Plugin Action Helper
@@ -36,6 +36,9 @@ public class ConnectorHelper {
36
36
public static final String TYPE = GenericHelper .TYPE ;
37
37
public static final String DATA = "data" ;
38
38
public static final String FORMAT = "format" ;
39
+ public static final String UPDATE_PREFIX = "pc" ;
40
+ public static final String UPDATE_ID_SEPARATOR = "_" ;
41
+ public static final String UPDATE_DATA_SEPARATOR = "|" ;
39
42
40
43
protected static final String KEY_CONNECTOR = "connector" ;
41
44
@@ -56,28 +59,28 @@ public static String getConnectorId(Element pluginElement, Element categoryEleme
56
59
/**
57
60
* Get the generated Connector Name
58
61
*
59
- * @param actionElement Element
60
- * @param connector {@link Action }
62
+ * @param connectorElement Element
63
+ * @param connector {@link Connector }
61
64
* @return String connectorName
62
65
*/
63
- public static String getConnectorName (Element actionElement , Connector connector ) {
64
- return connector .name ().isEmpty () ? actionElement .getSimpleName ().toString () : connector .name ();
66
+ public static String getConnectorName (Element connectorElement , Connector connector ) {
67
+ return connector .name ().isEmpty () ? connectorElement .getSimpleName ().toString () : connector .name ();
65
68
}
66
69
67
70
/**
68
71
* Get the generated Connector ID
69
72
*
70
- * @param pluginClass Class
71
- * @param actionMethodName String
73
+ * @param pluginClass Class
74
+ * @param connectorMethodName String
72
75
* @return String connectorId
73
76
*/
74
- public static String getConnectorId (Class <?> pluginClass , String actionMethodName ) {
77
+ public static String getConnectorId (Class <?> pluginClass , String connectorMethodName ) {
75
78
String connectorId = "" ;
76
79
77
80
for (Method method : pluginClass .getDeclaredMethods ()) {
78
- if (method .isAnnotationPresent (Action .class ) && method .getName ().equals (actionMethodName )) {
79
- Action action = method .getDeclaredAnnotation (Action .class );
80
- connectorId = ConnectorHelper ._getConnectorId ( CategoryHelper . getCategoryId ( pluginClass , action . categoryId ()), (! action . id (). isEmpty () ? action . id () : actionMethodName ) );
81
+ if (method .isAnnotationPresent (Connector .class ) && method .getName ().equals (connectorMethodName )) {
82
+ Connector connector = method .getDeclaredAnnotation (Connector .class );
83
+ connectorId = ConnectorHelper .getConnectorId ( pluginClass , method , connector );
81
84
}
82
85
}
83
86
@@ -96,20 +99,80 @@ public static String getConnectorId(Class<?> pluginClass, Method connectorMethod
96
99
97
100
if (connectorMethod .isAnnotationPresent (Connector .class )) {
98
101
Connector connector = connectorMethod .getDeclaredAnnotation (Connector .class );
99
- connectorId = ConnectorHelper ._getConnectorId ( CategoryHelper . getCategoryId ( pluginClass , connector . categoryId ()), (! connector . id (). isEmpty () ? connector . id () : connectorMethod . getName ()) );
102
+ connectorId = ConnectorHelper .getConnectorId ( pluginClass , connectorMethod , connector );
100
103
}
101
104
102
105
return connectorId ;
103
106
}
104
107
108
+ /**
109
+ * Get the generated Connector ID
110
+ *
111
+ * @param pluginClass Class
112
+ * @param connectorMethod Method
113
+ * @param connector {@link Connector}
114
+ * @return String connectorId
115
+ */
116
+ public static String getConnectorId (Class <?> pluginClass , Method connectorMethod , Connector connector ) {
117
+ return ConnectorHelper ._getConnectorId (CategoryHelper .getCategoryId (pluginClass , connector .categoryId ()), !connector .id ().isEmpty () ? connector .id () : connectorMethod .getName ());
118
+ }
119
+
105
120
/**
106
121
* Internal - Get the formatted Connector ID
107
122
*
108
- * @param categoryId String
109
- * @param rawActionId String
110
- * @return String actionId
123
+ * @param rawConnectorId String
124
+ * @return String connectorId
111
125
*/
112
- private static String _getConnectorId (String categoryId , String rawActionId ) {
113
- return categoryId + "." + ConnectorHelper .KEY_CONNECTOR + "." + rawActionId ;
126
+ private static String _getConnectorId (String categoryId , String rawConnectorId ) {
127
+ return categoryId + "." + ConnectorHelper .KEY_CONNECTOR + "." + rawConnectorId ;
128
+ }
129
+
130
+ /**
131
+ * Compute a Linear Translate
132
+ *
133
+ * @param inputMin float
134
+ * @param inputMax float
135
+ * @param outputMin float
136
+ * @param outputMax float
137
+ * @param inputValue float
138
+ * @return float computedValue
139
+ */
140
+ public static Float linearTranslate (float inputMin , float inputMax , float outputMin , float outputMax , float inputValue ) {
141
+ float inputRange = inputMax - inputMin ;
142
+ float outputRange = outputMax - outputMin ;
143
+ float computedCross = outputRange / inputRange * (inputValue - inputMin );
144
+ return computedCross + outputMin ;
145
+ }
146
+
147
+ /**
148
+ * Get the constructed Connector Id for Connector Update
149
+ *
150
+ * @param pluginId String
151
+ * @param connectorId String
152
+ * @param value Integer
153
+ * @param data Map<String, Object>
154
+ * @return String constructedConnectorId
155
+ */
156
+ public static String getConstructedId (String pluginId , String connectorId , Integer value , Map <String , Object > data ) {
157
+ String constructedConnectorId = null ;
158
+
159
+ if (pluginId != null && !pluginId .isEmpty () && connectorId != null && !connectorId .isEmpty () && value != null && value >= 0 && value <= 100 ) {
160
+ StringBuilder constructedConnectorIdBuilder = new StringBuilder (ConnectorHelper .UPDATE_PREFIX )
161
+ .append (ConnectorHelper .UPDATE_ID_SEPARATOR )
162
+ .append (pluginId )
163
+ .append (ConnectorHelper .UPDATE_ID_SEPARATOR )
164
+ .append (connectorId );
165
+ if (data != null && data .size () > 0 ) {
166
+ for (String dataKey : data .keySet ()) {
167
+ constructedConnectorIdBuilder .append (ConnectorHelper .UPDATE_DATA_SEPARATOR )
168
+ .append (dataKey )
169
+ .append ("=" )
170
+ .append (data .get (dataKey ));
171
+ }
172
+ }
173
+ constructedConnectorId = constructedConnectorIdBuilder .toString ();
174
+ }
175
+
176
+ return constructedConnectorId ;
114
177
}
115
178
}
0 commit comments