Repository navigation
Expand file tree
/
Copy pathassociationDevice.php
More file actions
236 lines (189 loc) · 6.81 KB
/
Copy pathassociationDevice.php
File metadata and controls
236 lines (189 loc) · 6.81 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
declare(strict_types=1);
include_once __DIR__ . '/numberDevice.php';
include_once __DIR__ . '/variablePresentation.php';
trait HelperAssociationDevice
{
use HelperNumberDevice;
use HelperVariablePresentation;
private static function getAssociationCompatibility($variableID)
{
if (!IPS_VariableExists($variableID)) {
return 'Missing';
}
if (!HasAction($variableID)) {
return 'Action required';
}
$targetVariable = IPS_GetVariable($variableID);
if ($targetVariable['VariableType'] != VARIABLETYPE_INTEGER) {
return 'Integer required';
}
$presentation = self::resolvePresentation($variableID);
if ($presentation === false) {
return 'Presentation required';
}
$checkEnumerated = function ($options, $negativeError, $enumerationError)
{
// Initialize minimum and maximum one above/below legal maximum
$minimumOption = count($options) + 1;
$maximumOption = -1;
foreach ($options as $option) {
if ($option['Value'] < 0) {
return $negativeError;
}
if ($option['Value'] > $maximumOption) {
$maximumOption = $option['Value'];
}
if ($option['Value'] < $minimumOption) {
$minimumOption = $option['Value'];
}
}
if (($maximumOption - $minimumOption + 1) != count($options)) {
return $enumerationError;
}
};
switch ($presentation['kind']) {
case 'legacy':
if (!$presentation['profileExists']) {
return 'Profile required';
}
if (($presentation['stepSize'] != 0) || (count($presentation['options']) == 0)) {
return 'No association profile';
}
$result = $checkEnumerated($presentation['options'], 'Negative associations not allowed', 'Associations not enumerated');
if (!empty($result)) {
return $result;
}
break;
case 'enumeration':
$result = $checkEnumerated($presentation['options'], 'Negative option not allowed', 'Options not enumerated');
if (!empty($result)) {
return $result;
}
break;
default:
return 'Unsupported presentation';
}
return 'OK';
}
private static function getAssociationNumber($variableID)
{
return self::getNumberValue($variableID);
}
private static function getAssociationString($variableID)
{
if (!IPS_VariableExists($variableID)) {
return false;
}
return GetValueFormatted($variableID);
}
private static function setAssociationNumber($variableID, $value)
{
return self::setNumberValue($variableID, $value);
}
private static function setAssociationString($variableID, $value)
{
if (!IPS_VariableExists($variableID)) {
return false;
}
$associations = self::getAssociations($variableID);
if (($associations === false) || empty($associations)) {
return false;
}
$field = self::getAssociationField($variableID);
foreach ($associations as $association) {
if (strcasecmp($association[$field], $value) == 0) {
return self::setAssociationNumber($variableID, intval($association['Value']));
}
}
// Fail, if no association was found
return false;
}
private static function isValidAssociation($variableID, $value, $field)
{
if (!IPS_VariableExists($variableID)) {
return false;
}
$associations = self::getAssociations($variableID);
if ($associations === false) {
return false;
}
if (empty($associations)) {
return false;
}
foreach ($associations as $association) {
if (is_string($association[$field])) {
if (strcasecmp($association[$field], $value) == 0) {
return true;
}
} else {
if ($association[$field] == $value) {
return true;
}
}
}
// Fail, if no association was found
return false;
}
private static function isValidAssociationNumber($variableID, $value)
{
return self::isValidAssociation($variableID, $value, 'Value');
}
private static function isValidAssociationString($variableID, $value)
{
return self::isValidAssociation($variableID, $value, self::getAssociationField($variableID));
}
private static function incrementAssociation($variableID, $increment)
{
if (!IPS_VariableExists($variableID)) {
return false;
}
$associations = self::getAssociations($variableID);
if ($associations === false) {
return false;
}
if (empty($associations)) {
return false;
}
$oneBased = true; // Is the first association a 1 or a 0?
foreach ($associations as $association) {
if ($association['Value'] == 0) {
$oneBased = false;
break;
}
}
$currentValue = GetValue($variableID);
// Convert one-based to zero-based
if ($oneBased) {
$currentValue--;
}
// Double modulo, so negative increments wrap around properly as well
$count = count($associations);
$newValue = ((($currentValue + $increment) % $count) + $count) % $count;
if ($oneBased) {
$newValue++;
}
return self::setAssociationNumber($variableID, $newValue);
}
//Legacy associations use 'Name' as their caption field, while enumeration options use 'Caption'
private static function getAssociationField($variableID)
{
$presentation = self::resolvePresentation($variableID);
if (($presentation === false) || ($presentation['kind'] == 'legacy')) {
return 'Name';
}
return 'Caption';
}
// Legacy associations and options have the same structure so we can handle them basically the same way
private static function getAssociations($variableID)
{
$presentation = self::resolvePresentation($variableID);
if ($presentation === false) {
return false;
}
if (!in_array($presentation['kind'], ['legacy', 'enumeration']) || ($presentation['options'] === null)) {
return false;
}
return $presentation['options'];
}
}