1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Broker.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:MQTTClient! |
9
|
|
|
* @subpackage Client |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 14.03.17 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\MQTTClient\Configuration; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
|
21
|
|
|
use BinSoul\Net\Mqtt; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* MQTT client connection configuration |
25
|
|
|
* |
26
|
|
|
* @package iPublikuj:MQTTClient! |
27
|
|
|
* @subpackage Client |
28
|
|
|
* |
29
|
|
|
* @author Adam Kadlec <[email protected]> |
30
|
|
|
*/ |
31
|
1 |
|
final class Connection implements Mqtt\Connection |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Implement nette smart magic |
35
|
|
|
*/ |
36
|
1 |
|
use Nette\SmartObject; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $username; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $password; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Mqtt\Message|NULL |
50
|
|
|
*/ |
51
|
|
|
private $will = NULL; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $clientID; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var int |
60
|
|
|
*/ |
61
|
|
|
private $keepAlive; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var int |
65
|
|
|
*/ |
66
|
|
|
private $protocol; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var bool |
70
|
|
|
*/ |
71
|
|
|
private $clean; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $username |
75
|
|
|
* @param string $password |
76
|
|
|
* @param Mqtt\Message|NULL $will |
77
|
|
|
* @param string $clientID |
78
|
|
|
* @param int $keepAlive |
79
|
|
|
* @param int $protocol |
80
|
|
|
* @param bool $clean |
81
|
|
|
*/ |
82
|
|
|
public function __construct( |
83
|
|
|
string $username = '', |
84
|
|
|
string $password = '', |
85
|
|
|
Mqtt\Message $will = NULL, |
86
|
|
|
string $clientID = '', |
87
|
|
|
int $keepAlive = 60, |
88
|
|
|
int $protocol = 4, |
89
|
|
|
bool $clean = TRUE |
90
|
|
|
) { |
91
|
1 |
|
$this->username = $username; |
92
|
1 |
|
$this->password = $password; |
93
|
1 |
|
$this->will = $will; |
94
|
1 |
|
$this->clientID = $clientID; |
95
|
1 |
|
$this->keepAlive = $keepAlive; |
96
|
1 |
|
$this->protocol = $protocol; |
97
|
1 |
|
$this->clean = $clean; |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return int |
102
|
|
|
*/ |
103
|
|
|
public function getProtocol() : int |
104
|
|
|
{ |
105
|
|
|
return $this->protocol; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $clientID |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public function setClientID(string $clientID) : void |
114
|
|
|
{ |
115
|
|
|
$this->clientID = $clientID; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getClientID() : string |
122
|
|
|
{ |
123
|
|
|
return $this->clientID; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function isCleanSession() : bool |
130
|
|
|
{ |
131
|
|
|
return $this->clean; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $username |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function setUsername(string $username) : void |
140
|
|
|
{ |
141
|
|
|
$this->username = $username; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getUsername() : string |
148
|
|
|
{ |
149
|
|
|
return $this->username; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $password |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function setPassword(string $password) : void |
158
|
|
|
{ |
159
|
|
|
$this->password = $password; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public function getPassword() : string |
166
|
|
|
{ |
167
|
|
|
return $this->password; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param Mqtt\Message|NULL $will |
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
|
|
public function setWill(?Mqtt\Message $will) : void |
176
|
|
|
{ |
177
|
|
|
$this->will = $will; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return Mqtt\Message|NULL |
182
|
|
|
*/ |
183
|
|
|
public function getWill() : ?Mqtt\Message |
184
|
|
|
{ |
185
|
|
|
return $this->will; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return int |
190
|
|
|
*/ |
191
|
|
|
public function getKeepAlive() : int |
192
|
|
|
{ |
193
|
|
|
return $this->keepAlive; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param string $clientID |
198
|
|
|
* |
199
|
|
|
* @return self |
200
|
|
|
*/ |
201
|
|
|
public function withClientID($clientID) : self |
202
|
|
|
{ |
203
|
|
|
$this->clientID = $clientID; |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param string $username |
210
|
|
|
* @param string $password |
211
|
|
|
* |
212
|
|
|
* @return self |
213
|
|
|
*/ |
214
|
|
|
public function withCredentials($username, $password) : self |
215
|
|
|
{ |
216
|
|
|
$this->username = $username; |
217
|
|
|
$this->password = $password; |
218
|
|
|
|
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param int $timeout |
224
|
|
|
* |
225
|
|
|
* @return self |
226
|
|
|
*/ |
227
|
|
|
public function withKeepAlive($timeout) : self |
228
|
|
|
{ |
229
|
|
|
$this->keepAlive = $timeout; |
230
|
|
|
|
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param int $protocol |
236
|
|
|
* |
237
|
|
|
* @return self |
238
|
|
|
*/ |
239
|
|
|
public function withProtocol($protocol) : self |
240
|
|
|
{ |
241
|
|
|
$this->protocol = $protocol; |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param Mqtt\Message $will |
248
|
|
|
* |
249
|
|
|
* @return self |
250
|
|
|
*/ |
251
|
|
|
public function withWill(Mqtt\Message $will) : self |
252
|
|
|
{ |
253
|
|
|
$this->will = $will; |
254
|
|
|
|
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|