Repository navigation
Expand file tree
/
Copy pathvariablePresentation.php
More file actions
115 lines (100 loc) · 4.5 KB
/
Copy pathvariablePresentation.php
File metadata and controls
115 lines (100 loc) · 4.5 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
<?php
declare(strict_types=1);
trait HelperVariablePresentation
{
//Normalizes the legacy profile and all variable presentations into one common structure:
//kind: 'legacy', 'slider', 'valuePresentation', 'shutter', 'color', 'enumeration' or 'unsupported'
//Returns false if the variable is missing or has no presentation at all
private static function resolvePresentation($variableID)
{
if (!IPS_VariableExists($variableID)) {
return false;
}
$legacyPresentation = function ($profileName)
{
$presentation = [
'kind' => 'legacy',
'profile' => $profileName,
'profileExists' => ($profileName != '') && IPS_VariableProfileExists($profileName),
'min' => null,
'max' => null,
'digits' => null,
'stepSize' => null,
'reversed' => (bool) preg_match('/\.Reversed$/', $profileName),
'encoding' => null,
'options' => null,
];
if ($presentation['profileExists']) {
$profile = IPS_GetVariableProfile($profileName);
$presentation['min'] = $profile['MinValue'];
$presentation['max'] = $profile['MaxValue'];
$presentation['digits'] = $profile['Digits'];
$presentation['stepSize'] = $profile['StepSize'];
$presentation['options'] = $profile['Associations'];
}
return $presentation;
};
//Handling for versions prior to presentations being supported
if (!function_exists('IPS_GetVariablePresentation')) {
$targetVariable = IPS_GetVariable($variableID);
if ($targetVariable['VariableCustomProfile'] != '') {
return $legacyPresentation($targetVariable['VariableCustomProfile']);
}
return $legacyPresentation($targetVariable['VariableProfile']);
}
$presentation = IPS_GetVariablePresentation($variableID);
if (empty($presentation)) {
return false;
}
$normalized = [
'kind' => 'unsupported',
'profile' => '',
'profileExists' => false,
'min' => null,
'max' => null,
'digits' => null,
'stepSize' => null,
'reversed' => false,
'encoding' => null,
'options' => null,
];
switch ($presentation['PRESENTATION']) {
case VARIABLE_PRESENTATION_LEGACY:
return $legacyPresentation($presentation['PROFILE']);
case VARIABLE_PRESENTATION_SLIDER:
$normalized['kind'] = 'slider';
$normalized['min'] = $presentation['MIN'];
$normalized['max'] = $presentation['MAX'];
$normalized['digits'] = $presentation['DIGITS'] ?? null;
return $normalized;
case VARIABLE_PRESENTATION_VALUE_PRESENTATION:
$normalized['kind'] = 'valuePresentation';
$normalized['min'] = $presentation['MIN'];
$normalized['max'] = $presentation['MAX'];
$normalized['digits'] = $presentation['DIGITS'] ?? null;
return $normalized;
case VARIABLE_PRESENTATION_SHUTTER:
$normalized['kind'] = 'shutter';
$normalized['min'] = $presentation['OPEN_OUTSIDE_VALUE'];
$normalized['max'] = $presentation['CLOSE_INSIDE_VALUE'];
//A closed value below the open value is simply reversed
if ($normalized['min'] > $normalized['max']) {
$normalized['reversed'] = true;
$k = $normalized['min'];
$normalized['min'] = $normalized['max'];
$normalized['max'] = $k;
}
return $normalized;
case VARIABLE_PRESENTATION_COLOR:
$normalized['kind'] = 'color';
$normalized['encoding'] = $presentation['ENCODING'] ?? 0;
return $normalized;
case VARIABLE_PRESENTATION_ENUMERATION:
$normalized['kind'] = 'enumeration';
$normalized['options'] = json_decode($presentation['OPTIONS'], true);
return $normalized;
default:
return $normalized;
}
}
}