diff --git a/server/ControlScripts/TestScript.php b/server/ControlScripts/TestScript.php index 860f5f0..ab5ea3f 100644 --- a/server/ControlScripts/TestScript.php +++ b/server/ControlScripts/TestScript.php @@ -4,8 +4,24 @@ class TestScript extends \SHServ\Middleware\ControlScripts implements \SHServ\Implements\ControlScriptsInterface { public function register_events_handlers() { - events() -> handler("app:button.click", function(Array $params) { - var_dump($params); + events() -> handler("app:button.press", function(Array $params) { + $device = $params["device"]; + $data = $params["data"]; + $channel = intval($data["channel"]); + + $devices = new \SHServ\Models\Devices(); + $device_relay = $devices -> get_by_device_hard_id("914414"); + $device_relay -> device_api() -> toggle_channel($channel); + + $relay_status = $device_relay -> device_api() -> get_status(); + $relay_channels = $relay_status["data"]["channels"]; + + $indicator = "disabled"; + if($relay_channels[$channel]["state"] == "on") { + $indicator = "enabled"; + } + + $device -> device_api() -> set_channel_state($indicator, $channel); }); } diff --git a/server/SHServ/Controllers/EventsController.php b/server/SHServ/Controllers/EventsController.php index 17b18c6..538f650 100644 --- a/server/SHServ/Controllers/EventsController.php +++ b/server/SHServ/Controllers/EventsController.php @@ -16,6 +16,11 @@ $devices_model = new Devices(); $device = $devices_model -> get_by_device_hard_id($device_id); + ob_start(); + var_dump([$event_name, $device_id, $data]); + file_put_contents("/srv/http/smart-home-serv.local/server/SHServ/Logs/dump.log", ob_get_contents()); + ob_end_clean(); + if(!$device) { return $this -> utils() -> response_error("unknown_device", ["device_id"]); } @@ -24,6 +29,19 @@ return $this -> utils() -> response_error("error_of_device_auth"); } + ignore_user_abort(true); + set_time_limit(2); + + ob_start(); + http_response_code(200); + header("Content-Type: application/json; charset=utf-8"); + echo json_encode(['status' => 'ok']); + + $size = ob_get_length(); + header("Content-Length: {$size}"); + ob_end_flush(); + flush(); + $result = events() -> app_call( "{$device -> device_type}.{$event_name}", [ diff --git a/server/SHServ/Entities/Device.php b/server/SHServ/Entities/Device.php index fd55313..c0a6489 100644 --- a/server/SHServ/Entities/Device.php +++ b/server/SHServ/Entities/Device.php @@ -6,6 +6,7 @@ use \SHServ\Entities\Area; use \SHServ\Tools\DeviceAPI\Base; use \SHServ\Tools\DeviceAPI\Relay; +use \SHServ\Tools\DeviceAPI\Button; class Device extends \SHServ\Middleware\Entity { public static $table_name = "devices"; @@ -60,6 +61,9 @@ case "relay": $this -> device_api_instance = new Relay($this -> device_ip); break; + case "button": + $this -> device_api_instance = new Button($this -> device_ip); + break; default: $this -> device_api_instance = new Base($this -> device_ip); } diff --git a/server/SHServ/Routes.php b/server/SHServ/Routes.php index da603cf..26adaea 100644 --- a/server/SHServ/Routes.php +++ b/server/SHServ/Routes.php @@ -64,7 +64,7 @@ $devices_model = new \SHServ\Models\Devices(); echo "
";
-				var_dump($devices_model -> connect_new_device("192.168.68.3", "test_device_btn", "Test device"));
+				var_dump($devices_model -> connect_new_device("192.168.68.3", "test_device_relay", "Test device"));
 			});
 
 			$this -> router -> uri("/dev/test/model/devices/unregistered_devices.html", function($args){
diff --git a/server/SHServ/Tools/DeviceAPI/TestActions/Base_test.php b/server/SHServ/Tools/DeviceAPI/TestActions/Base_test.php
index 771f1b2..27c6aaf 100644
--- a/server/SHServ/Tools/DeviceAPI/TestActions/Base_test.php
+++ b/server/SHServ/Tools/DeviceAPI/TestActions/Base_test.php
@@ -7,7 +7,7 @@
 
 trait Base_test {
 	public function device_api_base__about() {
-		$device_base_api = new Base("192.168.68.7");
+		$device_base_api = new Base("192.168.68.2");
 		$about = $device_base_api -> get_about();
 
 		echo "
";