[Update] Used relation between 'global state' <=> 'chip state' to manage chips

This commit is contained in:
xdrm-brackets 2017-02-23 15:24:05 +01:00
parent 88cd366077
commit a689696a2b
1 changed files with 74 additions and 10 deletions

View File

@ -10,23 +10,83 @@
/* [1] Fetch global state /* [1] Fetch global state
=========================================================*/ =========================================================*/
$GLOBAL_STATE = [];
/* (1) Fetch from file */ /* (1) Fetch from file */
$f_gstate = @file_get_contents(STATE_CONF); $f_gstate = @file_get_contents(STATE_CONF);
/* (2) Manage errors */ /* (2) Manage errors */
if( $f_gstate === false ) if( $f_gstate === false ){
slog('Cannot access GLOBAL STATE file', 'global-state:update');
return 127; return 127;
}
/* (3) Remove surrounding spaces */ /* (3) Remove surrounding spaces */
$f_gstate = preg_replace('@^\s+@', '', $f_gstate); $f_gstate = preg_replace('@^\s+@', '', $f_gstate);
$f_gstate = preg_replace('@\s+$@', '', $f_gstate); $f_gstate = preg_replace('@\s+$@', '', $f_gstate);
/* (4) Extract in array */ /* (4) Extract in array */
$gstate = str_split($f_gstate); $GLOBAL_STATE = $f_gstate;
/* [2] Cache global state to chip states
=========================================================*/
$CHIP_STATE = null;
/* (1) Fetch from file */
$f_cstates = new SplFileObject(STATES_CONF, 'r');
/* (2) Section Title */
if( $f_cstates === false ){
slog('Cannot access GLOBAL/CHIP STATES file', 'global-state:update');
return 127;
}
/* (3) Parse line by line */
while( !$f_cstates->eof() ){
// {1} Try to parse current line //
$parsed = json_decode($f_cstates->fgets(), true);
// {2} If cannot parse, go to next //
if( is_null($parsed) )
continue;
// {3} Check if GLOBAL STATE matches CURRENT state //
$failed = false;
for( $c = 0 ; $c < strlen($GLOBAL_STATE) || $c < strlen($parsed[0]) ; $c++ ){
// {3.1} If 'X' -> whatever state //
if( $parsed[0][$c] == 'X' )
continue;
// {3.2} Else, if different -> fail //
else if( $parsed[0][$c] != $GLOBAL_STATE[$c] ){
$failed = true;
break;
}
}
// {4} If not failed -> state to keep //
if( !$failed ){
slog("Current state '$GLOBAL_STATE' matches '{$parsed[0]}'", 'global-state:update');
$CHIP_STATE = $parsed[1];
break;
}
}
/* (4) If no CHIP_STATE set -> abort */
if( is_null($CHIP_STATE) ){
slog("Cannot find a chip_state for the current global state '{$GLOBAL_STATE}'", 'global-state');
return 127;
}
/* [2] Cache chips
/* [2] Update chips
=========================================================*/ =========================================================*/
/* (1) Get file handler */ /* (1) Get file handler */
$f_chips = new SplFileObject(CHIPS_CONF, 'r'); $f_chips = new SplFileObject(CHIPS_CONF, 'r');
@ -41,16 +101,20 @@
if( is_null($parsed) ) if( is_null($parsed) )
continue; continue;
// {3} Check if position available in GSTATE // // {3} Check if position available in GLOBAL_STATE //
if( isset($gstate[$parsed[0]]) ){ if( isset($CHIP_STATE[$parsed[0]]) ){
/* (1) If according state does not exist -> go to next chip */ /* (1) If CHIP STATE value is 'x' -> do nothing */
if( !isset($parsed[2][$gstate[$parsed[0]]]) ) if( $CHIP_STATE[$parsed[0]] == 'x' )
break; continue;
/* (2) Use more human-readable data */ /* (2) If state does not exist -> go to next chip */
if( !isset($parsed[2][$CHIP_STATE[$parsed[0]]]) )
continue;
/* (3) Use variables to be more human-readable */
$position = $parsed[0]; $position = $parsed[0];
$state = $gstate[$parsed[0]]; $state = $CHIP_STATE[$parsed[0]];
/* (3) Set state */ /* (3) Set state */
$updated = syscall(SOURCE_DIR."/lib/global-state/set {$position} {$state}"); $updated = syscall(SOURCE_DIR."/lib/global-state/set {$position} {$state}");