From 07b61d216743c9715303ed759d009619438da858 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 22 Feb 2017 10:40:43 +0100 Subject: [PATCH] [Update] Renames \lib\chip to \lib\global-state and implemented 'update' that updated all chips according to the STATE file and the CHIPS list --- lib/chip/state | 3 - lib/global-state/set | 3 + .../state.php => global-state/source/set.php} | 12 ++- lib/global-state/source/update.php | 76 +++++++++++++++++++ lib/global-state/update | 3 + lib/mfrc522/source/loop.php | 2 +- 6 files changed, 91 insertions(+), 8 deletions(-) delete mode 100755 lib/chip/state create mode 100755 lib/global-state/set rename lib/{chip/source/state.php => global-state/source/set.php} (86%) create mode 100755 lib/global-state/source/update.php create mode 100755 lib/global-state/update diff --git a/lib/chip/state b/lib/chip/state deleted file mode 100755 index 7457f40..0000000 --- a/lib/chip/state +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -/usr/bin/env php $(realpath $(dirname $0))/source/state.php $*; diff --git a/lib/global-state/set b/lib/global-state/set new file mode 100755 index 0000000..d27aec8 --- /dev/null +++ b/lib/global-state/set @@ -0,0 +1,3 @@ +#!/bin/sh + +/usr/bin/env php $(realpath $(dirname $0))/source/set.php $*; diff --git a/lib/chip/source/state.php b/lib/global-state/source/set.php similarity index 86% rename from lib/chip/source/state.php rename to lib/global-state/source/set.php index d8dc0ae..457c706 100755 --- a/lib/chip/source/state.php +++ b/lib/global-state/source/set.php @@ -5,7 +5,7 @@ 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_state = null; @@ -50,8 +50,10 @@ } /* (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; + } @@ -61,8 +63,10 @@ foreach($g_pins as $pinNumber) // 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; + } /* (2) For each pin, set the associated value */ foreach($g_state as $pinIndex=>$pinValue){ @@ -90,7 +94,7 @@ /* [2] Launch main script =========================================================*/ - $exec = chip_state($argv[1], $argv[2]); + $exec = globalstate_set($argv[1], $argv[2]); echo $exec; die($exec); diff --git a/lib/global-state/source/update.php b/lib/global-state/source/update.php new file mode 100755 index 0000000..fb588b4 --- /dev/null +++ b/lib/global-state/source/update.php @@ -0,0 +1,76 @@ +#!/usr/bin/php + +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); +?> + diff --git a/lib/global-state/update b/lib/global-state/update new file mode 100755 index 0000000..0cb51e3 --- /dev/null +++ b/lib/global-state/update @@ -0,0 +1,3 @@ +#!/bin/sh + +/usr/bin/env php $(realpath $(dirname $0))/source/update.php; diff --git a/lib/mfrc522/source/loop.php b/lib/mfrc522/source/loop.php index b97c467..03f4776 100755 --- a/lib/mfrc522/source/loop.php +++ b/lib/mfrc522/source/loop.php @@ -292,7 +292,7 @@ if( $f_gstate === false ) return; - /* (2) Remove external spaces */ + /* (2) Remove surrounding spaces */ $f_gstate = preg_replace('@^\s+@', '', $f_gstate); $f_gstate = preg_replace('@\s+$@', '', $f_gstate);