31 lines
901 B
PHP
31 lines
901 B
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
/**
|
|
* The address smart-home devices should point at to reach this server's MQTT broker.
|
|
*
|
|
* We can't read the Docker host's LAN IP from inside the container reliably, so we derive it
|
|
* from the request host — the address the user opened HomeOS at is, by definition, this server's
|
|
* reachable LAN address. `MQTT_DEVICE_HOST` overrides it only if that ever guesses wrong.
|
|
*/
|
|
class HostAddress
|
|
{
|
|
public static function port(): int
|
|
{
|
|
return (int) config('homeos.mqtt.port', 1883);
|
|
}
|
|
|
|
/** "host:port" for the device's MQTT server field. */
|
|
public static function broker(): string
|
|
{
|
|
$override = config('homeos.mqtt.device_host');
|
|
|
|
if (filled($override)) {
|
|
return str_contains($override, ':') ? $override : $override.':'.self::port();
|
|
}
|
|
|
|
return request()->getHost().':'.self::port();
|
|
}
|
|
}
|