Motheure-simple 1st implementation

This commit is contained in:
xdrm-brackets 2017-10-12 19:19:46 +02:00
parent a5115fe37d
commit cb4a0e5921
4 changed files with 209 additions and 6 deletions

View File

@ -0,0 +1,3 @@
#!/bin/bash
sudo -u sats /usr/bin/env php $(realpath $(dirname $0))/source/loop.php;

View File

@ -0,0 +1,194 @@
#!/usr/bin/php
<?php
require_once __DIR__.'/../../../lib/include/php/const';
$f_data;
$start_ts;
$FEATURE = basename(dirname(__DIR__));
$MOTOR_PIN = 29;
$LOOP_FREQ = 100; // check frequency in ms
$THRESHOLD = 4; // maximum number of reads to wait for motor to be active back
/* READ MOTOR STATE
*
*/
function motheure_read(){
/* [1] Export global caches + variables
=========================================================*/
/* (1) Motor pin */
global $MOTOR_PIN;
/* [2] Try to read motor state
=========================================================*/
/* (1) Get its state */
$STATE = syscall( SOURCE_DIR."/lib/gpio/in $MOTOR_PIN" );
/* (2) If invalid */
if( is_bool($STATE) )
return false;
/* (3) If inactive, pass */
if( $STATE == 0 )
return false;
/* (4) Return TRUE means motor active */
return true;
}
/* MAIN LOOP
*
*/
function motheure_loop(){
/* [1] Export global caches + variables
=========================================================*/
/* (1) Log history file descriptor */
global $f_data;
/* (2) Motor pin */
global $MOTOR_PIN;
/* (3) Loop data */
global $start_ts;
global $LOOP_FREQ;
global $THRESHOLD;
/* [2] Try to read motor state
=========================================================*/
/* (1) Read motor pin */
if( !motheure_read() ) // if not active -> continue watching
return false;
slog('Motor active detected', 'motheure-simple:loop', 'motheure');
/* [3] Wait for machine to stop
=========================================================*/
/* (0) Userful variables */
$offset = 0; // offset (number of 0 reads)
$loop_ts = 0; // loop start time
$stop_ts = microtime(true); // last active time for now
while( $offset <= $THRESHOLD ){ // exit when motor active for $threshold cycles
/* (1) Start loop timer */
$loop_ts = microtime(true);
/* (2) If stopped -> increment offset */
if( !motheure_read() ){
$offset++;
/* (3) If not stopped */
}else{
$offset = 0;
$stop_ts = microtime(true); // last active time for now
}
/* (4) Wait for loop to last @LOOP_FREQ seconds (if not already elapsed) */
while( microtime(true) - $loop_ts < ($LOOP_FREQ/1000) );
}
/* [2] Main loop
=========================================================*/
/* (1) Log action to default log file */
$f_data->fwrite( json_encode([
$stop_ts - $start_ts
]).PHP_EOL );
/* (2) Return status */
return true;
}
function motheure_setup(){
/* [0] Initialize global variables
=========================================================*/
/* (1) File descriptiors */
global $f_data;
/* (3) Useful data */
global $FEATURE;
global $MOTOR_PIN;
/* [1] Open file descriptors on useful files
=========================================================*/
/* (1) Append accesses (logs) */
if( !file_exists(DATA_DIR."/$FEATURE") )
file_put_contents(DATA_DIR."/$FEATURE", '');
$f_data = new SplFileObject(DATA_DIR."/$FEATURE", 'a');
/* [2] Set up GPIO pin
=========================================================*/
/* (1) Set as input */
if( !syscall(SOURCE_DIR."/lib/gpio/in $MOTOR_PIN") ){
slog('Cannot set up motor pin as INPUT', 'motheure-simple:setup');
return 127;
}
return 0;
}
/* [1] Set up daemon
=========================================================*/
/* (1) Set up */
$exec = motheure_setup();
/* (2) Manage error */
if( $exec != 0 ){
slog('cannot set up the daemon', 'motheure-simple:loop');
die($exec);
}
/* (3) Success message */
slog('daemon started (loop)', 'motheure-simple:loop');
/* [2] Daemon loop
=========================================================*/
while( true ){
/* (1) Store time */
$start_ts = microtime(true);
/* (2) Execute loop code */
motheure_loop();
}
?>

View File

@ -30,14 +30,16 @@ test -d /sys/class/gpio/gpio$1 || echo $1 > /sys/class/gpio/export;
# [4] Set mode to OUT # [4] Set mode to IN
#========================================================# #========================================================#
cat /sys/class/gpio/gpio$1/direction | grep -v "in" && echo "in" > /sys/class/gpio/gpio$1/direction; cat /sys/class/gpio/gpio$1/direction | grep -v "in" && echo "in" > /sys/class/gpio/gpio$1/direction;
# [5] EXIT_CODE # [5] RETURN VALUE
#========================================================# #========================================================#
echo 0; VALUE="`cat /sys/class/gpio/gpio$1/value`";
exit 0;
echo $VALUE;
exit $VALUE;

View File

@ -19,8 +19,12 @@
$out = preg_replace('/\s+$/', '', $out); $out = preg_replace('/\s+$/', '', $out);
/* (4) Manage result */ /* (4) Manage result */
if( is_numeric($out) ) return ("$out"==="0"); if( is_numeric($out) && $out === "0" )
else return $out; return false;
elseif( is_numeric($out) && $out === "127" )
return true;
else
return $out;
} }
/* [2] Log management /* [2] Log management