SATS/lib/global-state/source/update.php

77 lines
1.7 KiB
PHP
Raw Normal View History

#!/usr/bin/php
<?php
require_once __DIR__.'/../../include/php/const';
function globalstate_update(){
/* [1] Fetch global state
=========================================================*/
/* (1) Fetch from file */
$f_gstate = @file_get_contents(STATE_CONF);
/* (2) Manage errors */
if( $f_gstate === false )
return 127;
/* (3) Remove surrounding spaces */
$f_gstate = preg_replace('@^\s+@', '', $f_gstate);
$f_gstate = preg_replace('@\s+$@', '', $f_gstate);
/* (4) Extract in array */
$gstate = explode('', $f_gstate);
/* [2] Cache chips
=========================================================*/
/* (1) Get file handler */
$f_chips = new SplFileObject(CHIPS_CONF, 'r');
/* (2) Parse line by line */
while( !$f_chips->eof() ){
// {1} Try to parse current line //
$parsed = json_decode($f_chips->fgets(), true);
// {2} If cannot parse, go to next //
if( is_null($parsed) )
continue;
// {3} Check if position available in GSTATE //
if( isset($gstate[$parsed[0]]) ){
/* (1) If according state does not exist -> go to next chip */
2017-02-23 13:56:11 +00:00
if( !isset($parsed[2][$gstate[$parsed[0]]]) )
break;
/* (2) Use more human-readable data */
$position = $parsed[0];
$state = $gstate[$parsed[0]];
/* (3) Set state */
$updated = syscall(SOURCE_DIR."/lib/global-state/set {$position} {$state}");
/* (4) If error */
if( $updated === false )
slog("Cannot set ${position} chip to state {$state}", 'global-state:update');
}
}
return 0;
}
/* [3] Launch main script
=========================================================*/
$exec = globalstate_update();
echo $exec;
die($exec);
?>