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

namespace SHServ\Controllers;

use \SHServ\Models\Devices;
use \SHServ\Tools\DeviceScanner;
use \SHServ\Middleware\ControlScripts;
use \SHServ\Models\Scripts;

class CronController extends \SHServ\Middleware\Controller {
	public function run_regular_cron_scripts() {
		$scripts_model = new Scripts();
		$regular_scripts = ControlScripts::get_regular_scripts();

		foreach($regular_scripts as $alias => $script) {
			if(!$scripts_model -> script_state("regular", $alias)) {
				continue;
			}
			
			$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();
		}
	}
}