<?php
namespace ControlScripts;
trait Common {
public function btn_on_online(String $alias, Array $muted = []) {
$this -> add_event_handler("button@{$alias}.online", function(Device $btns_block, Array $data) use ($muted) {
$btns_block_api = $btns_block -> device_api();
if($btns_block_api instanceof \SHServ\Tools\DeviceAPI\Button) {
foreach($muted as $ch) {
$btns_block_api -> set_channel_state("mute", $ch);
}
}
$this -> helper() -> sync_btn_channels($this -> sync_map(), $btns_block -> alias);
});
}
protected function set_btns_click_handlers($alias): void {
$self = $this;
$buttons = $this -> helper() -> prepare_sync_map_by_alias($this -> sync_map(), $alias);
foreach($buttons as $btn_channel => $entry) {
if($entry[0]["type"] != "relay") {
continue;
}
$relay_alias = $entry[0]["alias"];
$relay_channel = $entry[0]["channel"];
$this -> add_event_handler("button@{$alias}({$btn_channel}).press", function(Device $btns_block, Array $data) use ($self, $btn_channel, $relay_alias, $relay_channel) {
$btns_block_api = $btns_block -> device_api();
$relay_api = $this -> devices() -> by_alias($relay_alias) -> device_api();
if($relay_api instanceof \SHServ\Tools\DeviceAPI\Relay and $btns_block_api instanceof \SHServ\Tools\DeviceAPI\Button) {
$relay_api -> toggle_channel($relay_channel);
$this -> helper() -> sync_relay_to_btns($this -> sync_map(), $relay_alias);
}
});
}
}
}