<?php
header('Content-type: application/json');
include '../../config/database.php';
function _command(){
	$conn = _connectDB();
	$sql = "SELECT * FROM monitor";
	$result = $conn->query($sql);
	if ($result->num_rows > 0) {
	    while($row = $result->fetch_assoc()) {
			if($row['Timeout'] <= date("Y-m-d H:i:s")){
				print_r($row);
				$sql = 'UPDATE `monitor` SET Status="OFFLINE" WHERE  Code="'.$row['Code'].'" AND Version="'.$row['Version'].'" ';
				$conn->query($sql);
			}
	    }
	}
	// Array
	// (
	//     [Code] => HUAYLIKE
	//     [Version] => V1
	//     [Status] => RUN
	//     [Timeout] => 2020-09-18 14:05:03
	//     [Comment] => 
	// )
}

// UPDATE `05z3l_linebot_demo`.`monitor` SET `Timeout`='2020-09-18 04:52:34' WHERE  `Code`='HUAYLIKE' AND `Version`='V1';
// INSERT INTO `monitor` (`Code`, `SCode`, `Version`, `Status`, `Timeout`, `Comment`, `Exprie`, `Counter`) VALUES ('LOTTORICH28', 'LR', 'V1', 'RUN', '2020-09-18 11:51:08', 'LR', '2020-09-18', '1');

// function _getTime(){
// 	$current = date("Y-m-d H:i:s");  
// 	$current =  date("Y-m-d H:i:s", strtotime("+5 min",strtotime($current)));
// 	return $current;
// }

while (true){
	_command();
	sleep(20);
}