[Update] Renames \lib\chip to \lib\global-state and implemented 'update' that updated all chips according to the STATE file and the CHIPS list

This commit is contained in:
xdrm-brackets 2017-02-22 10:40:43 +01:00
parent 1500d616a9
commit 07b61d2167
6 changed files with 91 additions and 8 deletions

View File

@ -1,3 +0,0 @@
#!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/state.php $*;

3
lib/global-state/set Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/set.php $*;

View File

@ -5,7 +5,7 @@
require_once __DIR__.'/../../include/php/const'; require_once __DIR__.'/../../include/php/const';
function chip_state($p_id_chip, $p_state){ function globalstate_set($p_id_chip, $p_state){
$g_pins = null; $g_pins = null;
$g_state = null; $g_state = null;
@ -50,8 +50,10 @@
} }
/* (3) If chip not found -> abort */ /* (3) If chip not found -> abort */
if( is_null($g_pins) || is_null($g_state) ) if( is_null($g_pins) || is_null($g_state) ){
slog("Chip $p_id_chip not found", 'global-state:set');
return 127; return 127;
}
@ -61,8 +63,10 @@
foreach($g_pins as $pinNumber) foreach($g_pins as $pinNumber)
// if failed -> propagate error // if failed -> propagate error
if( syscall(SOURCE_DIR."/lib/gpio/out {$pinNumber}") == false ) if( syscall(SOURCE_DIR."/lib/gpio/out {$pinNumber}") == false ){
slog("Chip $p_id_chip cannot be set to OUT mode", 'global-state:set');
return 127; return 127;
}
/* (2) For each pin, set the associated value */ /* (2) For each pin, set the associated value */
foreach($g_state as $pinIndex=>$pinValue){ foreach($g_state as $pinIndex=>$pinValue){
@ -90,7 +94,7 @@
/* [2] Launch main script /* [2] Launch main script
=========================================================*/ =========================================================*/
$exec = chip_state($argv[1], $argv[2]); $exec = globalstate_set($argv[1], $argv[2]);
echo $exec; echo $exec;
die($exec); die($exec);

View File

@ -0,0 +1,76 @@
#!/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 */
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);
?>

3
lib/global-state/update Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/update.php;

View File

@ -292,7 +292,7 @@
if( $f_gstate === false ) if( $f_gstate === false )
return; return;
/* (2) Remove external spaces */ /* (2) 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);