From dbb3a7c93e19f5a0fbe1fa8247585a7d5a8ee732 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 23 Feb 2017 18:13:11 +0100 Subject: [PATCH] [Update] Added feature to truncate history after sync --- lib/api/source/deploy.php | 24 +++++++ lib/file/source/set.php | 102 +++++++++++++++++++++++++++ lib/file/source/update.php | 140 +++++++++++++++++++++++++++++++++++++ lib/file/truncate | 41 +++++++++++ 4 files changed, 307 insertions(+) create mode 100755 lib/file/source/set.php create mode 100755 lib/file/source/update.php create mode 100755 lib/file/truncate diff --git a/lib/api/source/deploy.php b/lib/api/source/deploy.php index eb12000..c4b44a5 100755 --- a/lib/api/source/deploy.php +++ b/lib/api/source/deploy.php @@ -118,6 +118,30 @@ $f = null; + + /* [6] Remove history entries + =========================================================*/ + /* (1) Check history entries count */ + if( !isset($arr_r['saved']) || !is_array($arr_r['saved']) ) + return 127; + + /* (2) Check history count */ + if( !isset($arr_r['saved']['history']) || !is_numeric($arr_r['saved']['history']) ) + return 127; + + /* (3) Fetch number of entries */ + $saved = intval($arr_r['saved']['history']); + $saved = $saved < 0 ? 0 : $saved; // prevent negative + + /* (4) Truncate the log file */ + $saved++; // because need to be excluded + $truncated = syscall(SOURCE_DIR."/lib/file/truncate mfrc522 {$saved}"); + + /* (5) Manage error */ + if( $truncated === false ) + return 127; + + return 0; } diff --git a/lib/file/source/set.php b/lib/file/source/set.php new file mode 100755 index 0000000..457c706 --- /dev/null +++ b/lib/file/source/set.php @@ -0,0 +1,102 @@ +#!/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 choosen pin // + if( $parsed[0] == $p_id_chip ){ + + // store pin list + $g_pins = $parsed[1]; + + /* (3) Check if choosen state */ + foreach($parsed[2] as $state=>$pinValues){ + + if( "$state" === "$p_state" ){ + + // store state values + $g_state = $pinValues; + break; + } + + } + + break; + } + } + + /* (3) If chip not found -> abort */ + if( is_null($g_pins) || is_null($g_state) ){ + slog("Chip $p_id_chip not found", 'global-state:set'); + return 127; + } + + + + /* [2] Set chip to choosen state + =========================================================*/ + /* (1) Activate pin + set OUT mode */ + foreach($g_pins as $pinNumber) + + // if failed -> propagate error + 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){ + + // set value to HIGH if not 0 // + if( $pinValue !== 0 ) + syscall(SOURCE_DIR."/lib/gpio/high ".$g_pins[$pinIndex]); + else + syscall(SOURCE_DIR."/lib/gpio/low ".$g_pins[$pinIndex]); + + } + + + return 0; + + } + + + /* [1] Check argument (chip position, chip state) + =========================================================*/ + if( $argc < 3 || !preg_match('@^\d+$@', $argv[1]) || !preg_match('@^[A-Za-z0-9]{1}$@', $argv[2]) ){ + echo 127; + die(127); + } + + /* [2] Launch main script + =========================================================*/ + $exec = globalstate_set($argv[1], $argv[2]); + + echo $exec; + die($exec); +?> + diff --git a/lib/file/source/update.php b/lib/file/source/update.php new file mode 100755 index 0000000..13688e7 --- /dev/null +++ b/lib/file/source/update.php @@ -0,0 +1,140 @@ +#!/usr/bin/php + +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] Update 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 GLOBAL_STATE // + if( isset($CHIP_STATE[$parsed[0]]) ){ + + /* (1) If CHIP STATE value is 'x' -> do nothing */ + if( $CHIP_STATE[$parsed[0]] == 'x' ) + continue; + + /* (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]; + $state = $CHIP_STATE[$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/file/truncate b/lib/file/truncate new file mode 100755 index 0000000..7dd4030 --- /dev/null +++ b/lib/file/truncate @@ -0,0 +1,41 @@ +#!/bin/bash + +__DIR__=$(dirname $(realpath $0)); + +source $__DIR__/../../lib/include/bash/const; +source $__DIR__/../../lib/include/bash/func; + + +# [1] Check arguments +#========================================================# + +# (1) If argument missing # +if [ $# -lt 2 ]; then + echo 127; + exit 127; +fi; + +# (2) Check argument #1 # +if [ ! -e "$DATA_DIR/$1" ]; then + echo 127; + exit 127; +fi; + +# (3) Check number of lines to truncate # +if [ $2 -gt $(wc -l "$DATA_DIR/$1" | awk '{print $1}') ]; then + echo 127; + exit 127; +fi; + + +# [2] Truncate file +#========================================================# +# (1) Increment by 1 (because exlusive) # +incr=$(expr $2 + 1); + +# (2) Truncate file # +tail -n +$incr "$DATA_DIR/$1" | tee "$DATA_DIR/$1" > /dev/null; + + +echo 0; +exit 0;