1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MQTTClientExtension.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 DI |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 12.03.17 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\MQTTClient\DI; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\DI; |
21
|
|
|
use Nette\PhpGenerator as Code; |
22
|
|
|
|
23
|
|
|
use Kdyby\Console; |
24
|
|
|
|
25
|
|
|
use BinSoul\Net\Mqtt; |
26
|
|
|
|
27
|
|
|
use React; |
28
|
|
|
|
29
|
|
|
use Psr\Log; |
30
|
|
|
|
31
|
|
|
use IPub\MQTTClient; |
32
|
|
|
use IPub\MQTTClient\Client; |
33
|
|
|
use IPub\MQTTClient\Commands; |
34
|
|
|
use IPub\MQTTClient\Logger; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* MQTT client extension container |
38
|
|
|
* |
39
|
|
|
* @package iPublikuj:MQTTClient! |
40
|
|
|
* @subpackage DI |
41
|
|
|
* |
42
|
|
|
* @author Adam Kadlec <[email protected]> |
43
|
|
|
* |
44
|
|
|
* @method DI\ContainerBuilder getContainerBuilder() |
45
|
|
|
* @method array getConfig(array $default) |
46
|
|
|
* @method string prefix($id) |
47
|
|
|
*/ |
48
|
1 |
|
final class MQTTClientExtension extends DI\CompilerExtension |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $defaults = [ |
54
|
|
|
'broker' => [ |
55
|
|
|
'httpHost' => NULL, |
56
|
|
|
'port' => 1883, |
57
|
|
|
'address' => NULL, |
58
|
|
|
'dns' => [ |
59
|
|
|
'enable' => TRUE, |
60
|
|
|
'address' => '8.8.8.8', |
61
|
|
|
], |
62
|
|
|
'secured' => [ |
63
|
|
|
'enable' => FALSE, |
64
|
|
|
'sslSettings' => [], |
65
|
|
|
], |
66
|
|
|
], |
67
|
|
|
'connection' => [ |
68
|
|
|
'username' => '', |
69
|
|
|
'password' => '', |
70
|
|
|
'clientID' => '', |
71
|
|
|
'keepAlive' => 60, |
72
|
|
|
'protocol' => 4, |
73
|
|
|
'clean' => TRUE, |
74
|
|
|
], |
75
|
|
|
'loop' => NULL, |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function loadConfiguration() |
82
|
|
|
{ |
83
|
1 |
|
parent::loadConfiguration(); |
84
|
|
|
|
85
|
|
|
/** @var DI\ContainerBuilder $builder */ |
86
|
1 |
|
$builder = $this->getContainerBuilder(); |
87
|
|
|
/** @var array $configuration */ |
88
|
1 |
|
$configuration = $this->getConfig($this->defaults); |
89
|
|
|
|
90
|
1 |
|
if ($configuration['loop'] === NULL) { |
91
|
|
|
if ($builder->getByType(React\EventLoop\LoopInterface::class) === NULL) { |
92
|
|
|
$loop = $builder->addDefinition($this->prefix('client.loop')) |
93
|
|
|
->setType(React\EventLoop\LoopInterface::class) |
94
|
|
|
->setFactory('React\EventLoop\Factory::create'); |
95
|
|
|
|
96
|
|
|
} else { |
97
|
|
|
$loop = $builder->getDefinitionByType(React\EventLoop\LoopInterface::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
} else { |
101
|
|
|
$loop = $builder->getDefinition(ltrim($configuration['loop'], '@')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$connection = new Mqtt\DefaultConnection( |
105
|
|
|
$configuration['connection']['username'], |
106
|
|
|
$configuration['connection']['password'], |
107
|
|
|
NULL, |
108
|
|
|
$configuration['connection']['clientID'], |
109
|
|
|
$configuration['connection']['keepAlive'], |
110
|
|
|
$configuration['connection']['protocol'], |
111
|
|
|
$configuration['connection']['clean'] |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$configuration = new Client\Configuration( |
115
|
|
|
$configuration['broker']['httpHost'], |
116
|
|
|
$configuration['broker']['port'], |
117
|
|
|
$configuration['broker']['address'], |
118
|
|
|
$configuration['broker']['dns']['enable'], |
119
|
|
|
$configuration['broker']['dns']['address'], |
120
|
|
|
$configuration['broker']['secured']['enable'], |
121
|
|
|
$configuration['broker']['secured']['sslSettings'], |
122
|
|
|
$connection |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
if ($builder->findByType(Log\LoggerInterface::class) === []) { |
126
|
|
|
$builder->addDefinition($this->prefix('server.logger')) |
127
|
|
|
->setType(Logger\Console::class); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$builder->addDefinition($this->prefix('client.client')) |
131
|
|
|
->setType(Client\Client::class) |
132
|
|
|
->setArguments([ |
133
|
|
|
'eventLoop' => $loop, |
134
|
|
|
'configuration' => $configuration, |
135
|
|
|
]); |
136
|
|
|
|
137
|
|
|
// Define all console commands |
138
|
|
|
$commands = [ |
139
|
|
|
'client' => Commands\ClientCommand::class, |
140
|
|
|
]; |
141
|
|
|
|
142
|
|
|
foreach ($commands as $name => $cmd) { |
143
|
|
|
$builder->addDefinition($this->prefix('commands' . lcfirst($name))) |
144
|
|
|
->setType($cmd) |
145
|
|
|
->addTag(Console\DI\ConsoleExtension::TAG_COMMAND); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param Nette\Configurator $config |
151
|
|
|
* @param string $extensionName |
152
|
|
|
* |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public static function register(Nette\Configurator $config, string $extensionName = 'mqttClient') |
156
|
|
|
{ |
157
|
|
|
$config->onCompile[] = function (Nette\Configurator $config, DI\Compiler $compiler) use ($extensionName) { |
158
|
|
|
$compiler->addExtension($extensionName, new MQTTClientExtension); |
159
|
|
|
}; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|