|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpMqtt\Client; |
| 6 | + |
| 7 | +use Illuminate\Contracts\Container\BindingResolutionException; |
| 8 | +use Illuminate\Contracts\Foundation\Application; |
| 9 | +use PhpMqtt\Client\Contracts\Repository; |
| 10 | +use PhpMqtt\Client\Exceptions\ConnectingToBrokerFailedException; |
| 11 | +use PhpMqtt\Client\Exceptions\ConnectionNotAvailableException; |
| 12 | +use PhpMqtt\Client\Exceptions\DataTransferException; |
| 13 | + |
| 14 | +/** |
| 15 | + * Manages the MQTT connections of the application. |
| 16 | + * |
| 17 | + * @package PhpMqtt\Client |
| 18 | + */ |
| 19 | +class ConnectionManager |
| 20 | +{ |
| 21 | + /** @var Application */ |
| 22 | + protected $application; |
| 23 | + |
| 24 | + /** @var array */ |
| 25 | + protected $config; |
| 26 | + |
| 27 | + /** @var string */ |
| 28 | + protected $defaultConnection; |
| 29 | + |
| 30 | + /** @var MQTTClient[] */ |
| 31 | + protected $connections = []; |
| 32 | + |
| 33 | + /** |
| 34 | + * ConnectionManager constructor. |
| 35 | + * |
| 36 | + * @param Application $application |
| 37 | + * @param array $config |
| 38 | + */ |
| 39 | + public function __construct(Application $application, array $config) |
| 40 | + { |
| 41 | + $this->application = $application; |
| 42 | + $this->config = $config; |
| 43 | + $this->defaultConnection = array_get($config, 'default_connection', 'default'); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the connection with the specified name. |
| 48 | + * |
| 49 | + * @param string|null $name |
| 50 | + * @return MQTTClient |
| 51 | + * @throws BindingResolutionException |
| 52 | + * @throws ConnectingToBrokerFailedException |
| 53 | + * @throws ConnectionNotAvailableException |
| 54 | + */ |
| 55 | + public function connection(string $name = null): MQTTClient |
| 56 | + { |
| 57 | + if ($name === null) { |
| 58 | + $name = $this->defaultConnection; |
| 59 | + } |
| 60 | + |
| 61 | + if (!array_key_exists($name, $this->connections)) { |
| 62 | + $this->connections[$name] = $this->createConnection($name); |
| 63 | + } |
| 64 | + |
| 65 | + return $this->connections[$name]; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Closes the given connection if opened. |
| 70 | + * |
| 71 | + * @param string|null $connection |
| 72 | + * @throws DataTransferException |
| 73 | + */ |
| 74 | + public function close(string $connection = null): void |
| 75 | + { |
| 76 | + if ($connection === null) { |
| 77 | + $connection = $this->defaultConnection; |
| 78 | + } |
| 79 | + |
| 80 | + if (array_key_exists($connection, $this->connections)) { |
| 81 | + $this->connections[$connection]->close(); |
| 82 | + unset($this->connections[$connection]); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Publishes a message on the given connection. The QoS level will be 0 |
| 88 | + * and the message will not be retained by the broker. |
| 89 | + * |
| 90 | + * @param string $topic |
| 91 | + * @param string $message |
| 92 | + * @param string|null $connection |
| 93 | + * @throws BindingResolutionException |
| 94 | + * @throws ConnectingToBrokerFailedException |
| 95 | + * @throws ConnectionNotAvailableException |
| 96 | + * @throws DataTransferException |
| 97 | + */ |
| 98 | + public function publish(string $topic, string $message, string $connection = null): void |
| 99 | + { |
| 100 | + $client = $this->connection($connection); |
| 101 | + |
| 102 | + $client->publish($topic, $message); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Creates a new MQTT client and connects to the specified server. |
| 107 | + * |
| 108 | + * @param string $name |
| 109 | + * @return MQTTClient |
| 110 | + * @throws BindingResolutionException |
| 111 | + * @throws ConnectingToBrokerFailedException |
| 112 | + * @throws ConnectionNotAvailableException |
| 113 | + */ |
| 114 | + protected function createConnection(string $name): MQTTClient |
| 115 | + { |
| 116 | + $config = array_get($this->config, "connections.{$name}"); |
| 117 | + if ($config === null) { |
| 118 | + throw new ConnectionNotAvailableException($name); |
| 119 | + } |
| 120 | + |
| 121 | + $host = array_get($config, 'host'); |
| 122 | + $port = array_get($config, 'port', 1883); |
| 123 | + $username = array_get($config, 'username'); |
| 124 | + $password = array_get($config, 'password'); |
| 125 | + $clientId = array_get($config, 'client_id'); |
| 126 | + $caFile = array_get($config, 'cafile'); |
| 127 | + $cleanSession = array_get($config, 'clean_session', true); |
| 128 | + $loggingEnabled = array_get($config, 'logging_enabled', true); |
| 129 | + $repository = array_get($config, 'repository', Repository::class); |
| 130 | + |
| 131 | + $settings = $this->parseConnectionSettings(array_get($config, 'settings', [])); |
| 132 | + $repository = $this->application->make($repository); |
| 133 | + $logger = $loggingEnabled ? $this->application->make('log') : null; |
| 134 | + |
| 135 | + $client = new MQTTClient($host, $port, $clientId, $caFile, $repository, $logger); |
| 136 | + $client->connect($username, $password, $settings, $cleanSession); |
| 137 | + |
| 138 | + return $client; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Parses the given settings and returns a populated settings object. |
| 143 | + * |
| 144 | + * @param array $settings |
| 145 | + * @return ConnectionSettings |
| 146 | + */ |
| 147 | + protected function parseConnectionSettings(array $settings): ConnectionSettings |
| 148 | + { |
| 149 | + $qos = array_get($settings, 'quality_of_service', 0); |
| 150 | + $blockSocket = array_get($settings, 'block_socket', false); |
| 151 | + $keepAlive = array_get($settings, 'keep_alive', 10); |
| 152 | + $socketTimeout = array_get($settings, 'socket_timeout', 5); |
| 153 | + $resendTimeout = array_get($settings, 'resend_timeout', 10); |
| 154 | + $retain = array_get($settings, 'retain', false); |
| 155 | + $lastWillTopic = array_get($settings, 'last_will_topic'); |
| 156 | + $lastWillMessage = array_get($settings, 'last_will_message'); |
| 157 | + |
| 158 | + return new ConnectionSettings($qos, $retain, $blockSocket, $socketTimeout, $keepAlive, $resendTimeout, $lastWillTopic, $lastWillMessage); |
| 159 | + } |
| 160 | +} |
0 commit comments