diff --git a/database_dump/smart-home-server.sql.zip b/database_dump/smart-home-server.sql.zip new file mode 100644 index 0000000..34b3f05 --- /dev/null +++ b/database_dump/smart-home-server.sql.zip Binary files differ diff --git a/database_dump/smart-home-server_device_auth.sql b/database_dump/smart-home-server_device_auth.sql deleted file mode 100644 index b464248..0000000 --- a/database_dump/smart-home-server_device_auth.sql +++ /dev/null @@ -1,63 +0,0 @@ --- phpMyAdmin SQL Dump --- version 5.2.1 --- https://www.phpmyadmin.net/ --- --- Хост: localhost --- Время создания: Дек 07 2025 г., 08:41 --- Версия сервера: 12.1.2-MariaDB --- Версия PHP: 8.5.0 - -SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; -START TRANSACTION; -SET time_zone = "+00:00"; - - -/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; -/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; -/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; -/*!40101 SET NAMES utf8mb4 */; - --- --- База данных: `smart-home-server` --- - --- -------------------------------------------------------- - --- --- Структура таблицы `device_auth` --- - -CREATE TABLE `device_auth` ( - `id` int(11) NOT NULL, - `device_id` int(11) NOT NULL, - `device_token` varchar(32) NOT NULL, - `server_token` varchar(32) NOT NULL COMMENT 'dont using yet', - `state` varchar(16) NOT NULL DEFAULT 'active' COMMENT 'expire | killed | active', - `update_at` timestamp NOT NULL DEFAULT current_timestamp(), - `create_at` timestamp NOT NULL -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - --- --- Индексы сохранённых таблиц --- - --- --- Индексы таблицы `device_auth` --- -ALTER TABLE `device_auth` - ADD PRIMARY KEY (`id`); - --- --- AUTO_INCREMENT для сохранённых таблиц --- - --- --- AUTO_INCREMENT для таблицы `device_auth` --- -ALTER TABLE `device_auth` - MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; -COMMIT; - -/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; -/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; -/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/database_dump/smart-home-server_table_device_auth.sql b/database_dump/smart-home-server_table_device_auth.sql new file mode 100644 index 0000000..b464248 --- /dev/null +++ b/database_dump/smart-home-server_table_device_auth.sql @@ -0,0 +1,63 @@ +-- phpMyAdmin SQL Dump +-- version 5.2.1 +-- https://www.phpmyadmin.net/ +-- +-- Хост: localhost +-- Время создания: Дек 07 2025 г., 08:41 +-- Версия сервера: 12.1.2-MariaDB +-- Версия PHP: 8.5.0 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +START TRANSACTION; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + +-- +-- База данных: `smart-home-server` +-- + +-- -------------------------------------------------------- + +-- +-- Структура таблицы `device_auth` +-- + +CREATE TABLE `device_auth` ( + `id` int(11) NOT NULL, + `device_id` int(11) NOT NULL, + `device_token` varchar(32) NOT NULL, + `server_token` varchar(32) NOT NULL COMMENT 'dont using yet', + `state` varchar(16) NOT NULL DEFAULT 'active' COMMENT 'expire | killed | active', + `update_at` timestamp NOT NULL DEFAULT current_timestamp(), + `create_at` timestamp NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- +-- Индексы сохранённых таблиц +-- + +-- +-- Индексы таблицы `device_auth` +-- +ALTER TABLE `device_auth` + ADD PRIMARY KEY (`id`); + +-- +-- AUTO_INCREMENT для сохранённых таблиц +-- + +-- +-- AUTO_INCREMENT для таблицы `device_auth` +-- +ALTER TABLE `device_auth` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; +COMMIT; + +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/database_dump/smart-home-server_table_meta.sql b/database_dump/smart-home-server_table_meta.sql new file mode 100644 index 0000000..5de6ba7 --- /dev/null +++ b/database_dump/smart-home-server_table_meta.sql @@ -0,0 +1,63 @@ +-- phpMyAdmin SQL Dump +-- version 5.2.1 +-- https://www.phpmyadmin.net/ +-- +-- Хост: localhost +-- Время создания: Дек 17 2025 г., 22:12 +-- Версия сервера: 12.1.2-MariaDB +-- Версия PHP: 8.5.0 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +START TRANSACTION; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + +-- +-- База данных: `smart-home-server` +-- + +-- -------------------------------------------------------- + +-- +-- Структура таблицы `meta` +-- + +CREATE TABLE `meta` ( + `id` int(11) NOT NULL, + `ent_id` int(11) NOT NULL, + `assignment` varchar(32) NOT NULL, + `name` varchar(64) NOT NULL, + `value` text NOT NULL, + `update_at` timestamp NOT NULL DEFAULT current_timestamp(), + `create_at` timestamp NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- +-- Индексы сохранённых таблиц +-- + +-- +-- Индексы таблицы `meta` +-- +ALTER TABLE `meta` + ADD PRIMARY KEY (`id`); + +-- +-- AUTO_INCREMENT для сохранённых таблиц +-- + +-- +-- AUTO_INCREMENT для таблицы `meta` +-- +ALTER TABLE `meta` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; +COMMIT; + +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/server/ControlScripts/TestScriptsScope.php b/server/ControlScripts/TestScriptsScope.php index d2b39c2..23285b6 100644 --- a/server/ControlScripts/TestScriptsScope.php +++ b/server/ControlScripts/TestScriptsScope.php @@ -42,19 +42,36 @@ protected function stand_btn_pressed_to_stand_relay() { $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"]; + switch($channel) { + case 0: + $relay = $this -> devices() -> by_alias("test_device_relay"); + $relay -> device_api() -> toggle_channel($channel); - $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 - ); + $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 + ); + } + case 1: + $relay = $this -> devices() -> by_alias("test_back_street_light"); + $relay -> device_api() -> toggle_channel(0); + $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[0]["state"] == "on" ? "enabled" : "disabled", + $channel + ); + } + break; } + }); } } \ No newline at end of file diff --git a/server/SHServ/Controllers/CronController.php b/server/SHServ/Controllers/CronController.php index 634003e..845879f 100644 --- a/server/SHServ/Controllers/CronController.php +++ b/server/SHServ/Controllers/CronController.php @@ -3,6 +3,7 @@ namespace SHServ\Controllers; use \SHServ\Models\Devices; +use \SHServ\Tools\DeviceScanner; class CronController extends \SHServ\Middleware\Controller { public function run_regular_cron_scripts() { @@ -17,4 +18,46 @@ } } } + + public function status_update_scanning() { + $device_scanner = new DeviceScanner(); + $found_devices = $device_scanner -> scan_range(FCONF["device_ip_range"][0], FCONF["device_ip_range"][1]); + + $devices_model = new Devices(); + $active_devices = $devices_model -> get_device_list(); + + foreach($active_devices as $active_device) { + $is_active = false; + $is_changed = false; + + foreach($found_devices as $found_device) { + if($active_device -> device_hard_id != $found_device["device_id"]) { + continue; + } + + $is_active = true; + + if($active_device -> connection_state == "lost") { + $active_device -> connection_state = "active"; + $is_changed = true; + } + + if($active_device -> device_ip != $found_device["ip_address"]) { + $active_device -> device_ip = $found_device["ip_address"]; + $is_changed = true; + } + + break; + } + + if(!$is_active) { + if($active_device -> connection_state == "active") { + $active_device -> connection_state = "lost"; + $is_changed = true; + } + } + + $is_changed and $active_device -> update(); + } + } } \ No newline at end of file diff --git a/server/SHServ/Controllers/EventsController.php b/server/SHServ/Controllers/EventsController.php index 022c559..c331e96 100644 --- a/server/SHServ/Controllers/EventsController.php +++ b/server/SHServ/Controllers/EventsController.php @@ -16,11 +16,6 @@ $devices_model = new Devices(); $device = $devices_model -> by_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"]); } @@ -30,7 +25,7 @@ } ignore_user_abort(true); - set_time_limit(2); + set_time_limit(10); ob_start(); http_response_code(200); diff --git a/server/SHServ/Entities/Meta.php b/server/SHServ/Entities/Meta.php index c65ad8e..8035ae8 100644 --- a/server/SHServ/Entities/Meta.php +++ b/server/SHServ/Entities/Meta.php @@ -14,7 +14,7 @@ self::$table_name, $id, $data - ); + ); } public function __toString() { diff --git a/server/SHServ/Models/Meta.php b/server/SHServ/Models/Meta.php new file mode 100644 index 0000000..a9572b5 --- /dev/null +++ b/server/SHServ/Models/Meta.php @@ -0,0 +1,77 @@ + thin_builder() -> select( + Meta::$table_name, + Meta::get_fields(), + [ + [ "assignment", "=", $assignment ], + "AND", + [ "ent_id", "=", $ent_id ] + ] + ); + + if(!$result) { + return []; + } + + return array_map(function ($item) { + return new Meta($item["id"], $item); + }, $result); + } + + public function get_one_by_name(String $name, String $assignment, int $ent_id): Meta | null { + $result = $this -> thin_builder() -> select( + Meta::$table_name, + Meta::get_fields(), + [ + [ "assignment", "=", $assignment ], + "AND", + [ "ent_id", "=", $ent_id ], + "AND", + [ "name", "=", $name ] + ] + ); + + if(!$result) { + return null; + } + + return new Meta($ent_id, $result[0]); + } + + public function remove_all_by_entity(String $assignment, int $ent_id): bool { + return $this -> thin_builder() -> delete( + Meta::$table_name, + [ + [ "assignment", "=", $assignment ], + "AND", + [ "ent_id", "=", $ent_id ] + ] + ); + } + + public function create(String $name, String $value, String $assignment, int $ent_id): int { + $result = $this -> thin_builder() -> insert( + Meta::$table_name, + [ + "name" => $name, + "value" => $value, + "assignment" => $assignment, + "ent_id" => $ent_id, + "create_at" => date("Y-m-d H:i:s") + ] + ); + + return $result ? $result : 0; + } + + public function create_or_update(String $name, String $value, String $assignment, int $ent_id): Meta | bool { + // ??? + } +} \ No newline at end of file diff --git a/server/SHServ/Routes.php b/server/SHServ/Routes.php index 54f8bf8..55663c9 100644 --- a/server/SHServ/Routes.php +++ b/server/SHServ/Routes.php @@ -52,6 +52,7 @@ // ); $this -> router -> uri("/cron/regular-scripts.html", "{$this -> cn}\\CronController@run_regular_cron_scripts"); + $this -> router -> uri("/cron/status-update-scanning.html", "{$this -> cn}\\CronController@status_update_scanning"); if(FCONF["devmode"]) { $this -> router -> uri("/dev/test/device-scanner.html", "{$this -> cn}\\DevTestController@device_scanner"); @@ -66,7 +67,7 @@ $devices_model = new \SHServ\Models\Devices(); echo "
";
-				var_dump($devices_model -> connect_new_device("192.168.68.3", "test_device_relay", "Test device"));
+				var_dump($devices_model -> connect_new_device("192.168.68.2", "test_back_street_light", "Test device. Back street light"));
 			});
 
 			$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 27c6aaf..cc68edd 100644
--- a/server/SHServ/Tools/DeviceAPI/TestActions/Base_test.php
+++ b/server/SHServ/Tools/DeviceAPI/TestActions/Base_test.php
@@ -15,7 +15,7 @@
 	}
 
 	public function device_api_base__set_token() {
-		$device_base_api = new Base("192.168.68.7");
+		$device_base_api = new Base("192.168.68.5");
 		$result = $device_base_api -> remote_set_token("mytoken123");
 
 		echo "
";
@@ -47,7 +47,7 @@
 	}
 
 	public function device_api_base__reset() {
-		$device_base_api = new Base("192.168.68.3", "38b545dcb18ddbca");
+		$device_base_api = new Base("192.168.68.5", "5e1627a855b0a140");
 		$result = $device_base_api -> reset();
 
 		echo "
";