This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
/
DeviceResponse.php
188 lines (166 loc) · 5.15 KB
/
DeviceResponse.php
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
<?php
/**
* @package DFCom
* @subpackage Model
* @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
* @author Cornelius Weiss <c.weiss@metaways.de>
* @copyright Copyright (c) 2018 Metaways Infosystems GmbH (http://www.metaways.de)
*/
/**
* class to hold DF Device Response
*
* @package DFCom
* @subpackage Model
*/
class DFCom_Model_DeviceResponse //extends \Zend\Diactoros\Response
{
const BEEP_OK = 1;
const BEEP_ERROR = 2;
const BEEP_LONG = 3;
const BEEP_SHORTLONG = 4;
const BEEP_SHORTSHORT = 5;
const BEEP_LONGLONG = 6;
const BEEP_SHORTSHORTSHORT = 7;
const BEEP_LONGLONGLONG = 8;
const BEEP_SHORTLONGSHORT = 9;
const BEEP_LONGSHORTLONG = 10;
const BEEP_SOS = 11;
const FONT_STANDARD = 0;
const FONT_7LINES = 1;
const FONT_7LINES_FIXEDWIDTH = 2;
const FONT_6LINES = 3;
const FONT_6LINES_FIXEDWIDTH = 4;
const FONT_5LINES = 5;
const FONT_5LINES_FIXEDWIDTH = 6;
protected $_responseData = [
'df_api' => 1,
];
protected $_headers = [];
// Content-Type: application/x-www-form-urlencoded; charset: iso-8859-1
// protected $_df_api = 1;
// protected $_df_time;
// protected $_df_beep;
// protected $_df_service;
// protected $_df_var;
// protected $_df_ek;
// protected $_df_msg;
// protected $_df_ac2;
// protected $_df_setup_list;
// protected $_df_ac2_list;
/**
* set device time
*
* @param DateTime $time
* @return DFCom_Model_DeviceResponse $this
*/
public function setTime(DateTime $time)
{
$this->_responseData['df_time'] = $time->format('Y-m-d\TH:i:s');
return $this;
}
/**
* beep on device
*
* @param int $signal
* @throws Tinebase_Exception_InvalidArgument
* @return DFCom_Model_DeviceResponse $this
*/
public function beep($signal = self::BEEP_OK)
{
if (! is_int($signal) || $signal < 1 || $signal > 11) {
throw new Tinebase_Exception_InvalidArgument('no such beep');
}
$this->_responseData['df_beep'] = $signal;
return $this;
}
/**
* @TODO: test: one var per response only????
*
* @param string $name
* @param string $value
* @return DFCom_Model_DeviceResponse $this
*/
public function setDeviceVariable($name, $value)
{
if ($name == 'setupVersion') {
throw new Tinebase_Exception_InvalidArgument('setupVersion can only be set in setup!');
}
// NOTE: No urlencode here for some reason device doesn't decode
$this->_responseData['df_var'] = "setup.$name," . $value;
return $this;
}
/**
* switch device to activeMode with optional service connection
*
* @param string $host
* @param string $port
* @return DFCom_Model_DeviceResponse $this
*/
public function setService($host=null, $port=null)
{
$serviceString = '1';
if ($host) {
$serviceString .= ",$host";
}
if ($port) {
$serviceString .= ",$port";
}
$this->_responseData['df_service'] = $serviceString;
$this->_headers['Connection'] = 'close';
return $this;
}
/**
* trigger event chain
*
* @param $name
* @return DFCom_Model_DeviceResponse $this
*/
public function triggerEventChain($name)
{
$this->_responseData['df_ek'] = $name;
return $this;
}
/**
* display message
* NOTE: only possible from user action
*
* @param string $message
* @param int $duration
* @param int $beep
* @param int $font
* @return $this
*/
public function displayMessage($message, $duration=5, $beep=self::BEEP_SHORTLONG, $font=self::FONT_STANDARD)
{
$this->_responseData['df_msg'] = implode(',', [rawurlencode(utf8_decode($message)), $duration, $beep, $font]);
return $this;
}
/**
* direct device to update given list
*
* @param DFCom_Model_DeviceList $deviceList
* @param DFCom_Model_Device $device
*/
public function updateDeviceList(DFCom_Model_DeviceList $deviceList, DFCom_Model_Device $device, $query='')
{
$link = '/' . ltrim(Tinebase_Core::getUrl(Tinebase_Core::GET_URL_PATH) . "/DFCom/v1/device/{$device->getId()}/list/{$deviceList->getId()}/{$device->authKey}", '/');
if ($query) {
$link .= urlencode('?' . ltrim($query, '?'));
}
$this->_responseData['df_setup_list'] = $deviceList->name . ',' . ($link);
}
public function getHTTPResponse()
{
$body = implode('&', array_map(function($value, $key) {
return $key.'='.$value;
}, array_values($this->_responseData), array_keys($this->_responseData)));
$headers = array_merge($this->_headers, [
'Content-Type' => 'application/x-www-form-urlencoded; charset: iso-8859-1',
'Content-Length' => strlen($body),
// 'Connection' => 'close',
]);
$response = new \Zend\Diactoros\Response('php://memory', 200, $headers);
$response->getBody()->write($body);
return $response;
}
}