Repository navigation
Expand file tree
/
Copy pathplaybackDevice.php
More file actions
136 lines (106 loc) · 3.59 KB
/
Copy pathplaybackDevice.php
File metadata and controls
136 lines (106 loc) · 3.59 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
<?php
declare(strict_types=1);
include_once __DIR__ . '/variablePresentation.php';
interface HelperPlaybackValues
{
const PREVIOUS = 0;
const STOP = 1;
const PLAY = 2;
const PAUSE = 3;
const NEXT = 4;
}
trait HelperPlaybackDevice
{
use HelperVariablePresentation;
private static function getPlaybackCompatibility($variableID)
{
if (!IPS_VariableExists($variableID)) {
return 'Missing';
}
$targetVariable = IPS_GetVariable($variableID);
if ($targetVariable['VariableType'] != VARIABLETYPE_INTEGER) {
return 'Integer required';
}
if (!HasAction($variableID)) {
return 'Action required';
}
$presentation = self::resolvePresentation($variableID);
if ($presentation === false) {
return 'Presentation required';
}
switch ($presentation['kind']) {
case 'legacy':
if (!in_array($presentation['profile'], ['~Playback', '~PlaybackPreviousNext'])) {
return '~Playback profile required';
}
break;
case 'enumeration':
if (!self::hasPlaybackOptions($presentation['options'], [HelperPlaybackValues::STOP, HelperPlaybackValues::PLAY, HelperPlaybackValues::PAUSE])) {
return 'Options for Stop, Play and Pause required';
}
break;
default:
return 'Unsupported presentation';
}
return 'OK';
}
private static function activateCommand($variableID, $value)
{
if (!IPS_VariableExists($variableID)) {
return false;
}
if (!HasAction($variableID)) {
return false;
}
$targetVariable = IPS_GetVariable($variableID);
if ($targetVariable['VariableType'] != VARIABLETYPE_INTEGER) {
return false;
}
return RequestActionEx($variableID, $value, 'VoiceControl');
}
private static function activatePrevious($variableID)
{
return self::activateCommand($variableID, HelperPlaybackValues::PREVIOUS);
}
private static function activatePlay($variableID)
{
return self::activateCommand($variableID, HelperPlaybackValues::PLAY);
}
private static function activatePause($variableID)
{
return self::activateCommand($variableID, HelperPlaybackValues::PAUSE);
}
private static function activateStop($variableID)
{
return self::activateCommand($variableID, HelperPlaybackValues::STOP);
}
private static function activateNext($variableID)
{
return self::activateCommand($variableID, HelperPlaybackValues::NEXT);
}
private static function supportsPreviousNext($variableID)
{
$presentation = self::resolvePresentation($variableID);
if ($presentation === false) {
return false;
}
switch ($presentation['kind']) {
case 'legacy':
return $presentation['profile'] === '~PlaybackPreviousNext';
case 'enumeration':
return self::hasPlaybackOptions($presentation['options'], [HelperPlaybackValues::PREVIOUS, HelperPlaybackValues::NEXT]);
default:
return false;
}
}
private static function hasPlaybackOptions($options, $values)
{
$optionValues = array_column($options, 'Value');
foreach ($values as $value) {
if (!in_array($value, $optionValues)) {
return false;
}
}
return true;
}
}