38 lines
1.0 KiB
PHP
38 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Mqtt;
|
|
|
|
/**
|
|
* Shelly Gen2+ MQTT topic conventions. Status arrives on `<prefix>/status/<component>`;
|
|
* simple control is `<prefix>/command/<component>`. Vendor specifics stay here (H3).
|
|
*/
|
|
class ShellyTopics
|
|
{
|
|
/** Topics the listener subscribes to. `+` matches any device prefix. */
|
|
public static function subscriptions(): array
|
|
{
|
|
return [
|
|
'+/status/#',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Parse a status topic into [prefix, component], or null if it isn't one.
|
|
* e.g. "shellyplus1-a1b2c3/status/switch:0" => ['shellyplus1-a1b2c3', 'switch:0']
|
|
*/
|
|
public static function parseStatus(string $topic): ?array
|
|
{
|
|
if (! preg_match('#^(?<prefix>[^/]+)/status/(?<component>.+)$#', $topic, $m)) {
|
|
return null;
|
|
}
|
|
|
|
return [$m['prefix'], $m['component']];
|
|
}
|
|
|
|
/** JSON-RPC topic for control (Switch.Set, Light.Set, Shelly.Reboot, …). */
|
|
public static function rpcTopic(string $prefix): string
|
|
{
|
|
return "{$prefix}/rpc";
|
|
}
|
|
}
|