Newer
Older
smart-home-server / server / ControlScripts / TestScript.php
<?php

namespace ControlScripts;

use \SHServ\Entities\Device;

class TestScript extends \SHServ\Middleware\ControlScripts implements \SHServ\Implements\ControlScriptsInterface {
	public function register_events_handlers() {
		$this -> add_event_handler("button.press", function(Device $device, Array $data) {
			$channel = intval($data["channel"]);
			
			$relay = $this -> devices() -> by_alias("test_device_relay");
			$relay -> device_api() -> toggle_channel($channel);

			$relay_channels = ($relay -> device_api() -> get_status())["channels"];

			$device_api = $device -> device_api();
			if($device_api instanceof \SHServ\Tools\DeviceAPI\Button) {
				$device_api -> set_channel_state(
					$relay_channels[$channel]["state"] == "on" ? "enabled" : "disabled", 
					$channel
				);
			}
		});
	}

	public function register_regular_scripts() {

	}
}