| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 | <?php// 策略指令下发脚本require_once './DB.php';date_default_timezone_set('Asia/Shanghai');$config = [    'hostname' => 'rm-wz98r5cn33zq4ou980o.mysql.rds.aliyuncs.com',	'username' => 'lampmanager',	'password' => 'lampmanager@2019',	'dbname' => 'lampmanager',];function send_cmd($cmd,$type = 0, $timeout = 30, $ms_timeout = 200) {	$ip = '123.57.20.89';    $port = '6800';	$client = @stream_socket_client("tcp://{$ip}:{$port}", $errno, $errstr, 30);	if ($client) {		fwrite($client, $cmd);		stream_set_timeout($client, $timeout, $ms_timeout);		$result = fread($client, 8196);		fclose($client);		return $result;	} else {        sleep(1);        send_cmd($cmd,$type, $timeout, $ms_timeout);	}}$begin = date('H:i',time());while (1) {	$end = date('H:i',time());	if ($end == $begin) {		sleep(1);		continue;	}	$begin = $end;	$db = new Db($config);	$sql = 'SELECT P.id,T.value FROM project AS P left join lampinfo as L on L.projectid = P.id left join timezone AS T on P.timezone = T.id where L.policyid != 0';	$list = $db->query($sql);	foreach ($list as $value) {		$nowTime = time() - 8 * 3600 + $value['value'] * 3600;		$nowDate = date('Y-m-d',$nowTime);		$nowH = date('H:i',$nowTime);		$sql = 'SELECT L.id as lampid,PI.* FROM lampinfo AS L join (SELECT * from policy_cmd where starttime <= "'.$nowDate.'") as PI on L.policyid = PI.policyid where PI.policyid != 0';		$policyList = $db->query($sql);		foreach ($policyList as $policy) {			$index = 1;			while ($index <= 10) {				if (empty($policy['time'.$index])) {					break;				}				if ($policy['time'.$index] == $nowH) {					$data = array();					$data['cmdtype'] = 0;			        $data['relateid'] = $policy['lampid'];			        $data['statuscmd'] = $policy['value'.$index];			        $data['updatetime'] = date("Y-m-d H:i:s");			        $sql = 'SELECT id from batch_switch_cmd where relateid = '.$policy['lampid'].' AND cmdtype = 0';			        $cmdList = $db->query($sql);			       	if (empty($cmdList) || empty($cmdList[0]['id'])) {			       		$cmdid = $db->table('batch_switch_cmd')->insert($data);					}else{						$db->table('batch_switch_cmd')->where('id='.$cmdList[0]['id'])->update($data);						$cmdid = $cmdList[0]['id'];					}			        $cmd = '{"cmd_type":"batch_switch_cmd","cmd_id":'.$cmdid.',"broadcast":0}';	                send_cmd($cmd,1,0);	                break;				}				$index ++;			}		}	}	$db->close();}
 |