Newer
Older
smart-home-server / server / SHServ / Controllers / CronController.php
<?php

namespace SHServ\Controllers;

use \SHServ\Models\Devices;
use \SHServ\Tools\DeviceScanner;

class CronController extends \SHServ\Middleware\Controller {
	public function run_regular_cron_scripts() {
		// В будущем: фильтруем список, отсеивая те скрипты, что выключены
		
		$scopes = app() -> control_scripts_instances;
		foreach($scopes as $scope_name => $scope) {
			$scripts = $scope -> get_regular_scripts();

			foreach($scripts as $script_alias => $script) {
				$script();
			}
		}
	}

	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();
		}
	}
}