SATS/feature/rfid-read/source/deploy.php

156 lines
3.4 KiB
PHP

<?php
require_once __DIR__.'/../../../lib/include/php/const';
$FEATURE = basename(dirname(__DIR__));
function rfid_read_deploy($sync){
global $FEATURE;
/* [1] Fetch api response data
=========================================================*/
/* (1) Try to read response */
$file_r = @file_get_contents(TMP_DIR."/$FEATURE");
/* (2) Check response */
if( $file_r === false ){
slog("Cannot find $FEATURE's response data file", "$FEATURE:deploy", 'update');
return 127;
}
/* (3) Try to parse response */
$arr_r = json_decode($file_r, true);
/* (4) Check parse error */
if( is_null($arr_r) || !is_array($arr_r) ){
slog("Cannot parse $FEATURE's response data file", "$FEATURE:deploy", 'update');
return 127;
}
/* [2] Get system states (global_state)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['states']) || !is_array($arr_r['states']) ){
slog('No \'states\' found in response file', "$FEATURE:deploy", 'update');
return 127;
}
/* (2) Reset file */
file_put_contents(STATES_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(STATES_CONF, 'w');
foreach($arr_r['states'] as $state)
$f->fwrite( json_encode([
$state['global_state'],
$state['chips']
]).PHP_EOL );
$f = null;
/* [3] Get chip list (chips)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['chips']) || !is_array($arr_r['chips']) ){
slog('No \'chips\' found in response file', "$FEATURE:deploy", 'update');
return 127;
}
/* (2) Reset file */
file_put_contents(CHIPS_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(CHIPS_CONF, 'w');
foreach($arr_r['chips'] as $chip)
$f->fwrite( json_encode([
(int) $chip['position'],
$chip['pins'],
$chip['states']
]).PHP_EOL );
$f = null;
/* [4] Get auth list (permissions)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['permissions']) || !is_array($arr_r['permissions']) ){
slog('No \'permissions\' found in response file', "$FEATURE:deploy", 'update');
return 127;
}
/* (2) Reset file */
file_put_contents(AUTH_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(AUTH_CONF, 'w');
foreach($arr_r['permissions'] as $code=>$perm)
$f->fwrite( json_encode([
$code,
(int) $perm['id_user'],
$perm['actions']
]).PHP_EOL );
$f = null;
/* [5] Get action list (with timeout)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['actions']) || !is_array($arr_r['actions']) ){
slog('No \'actions\' found in response file', "$FEATURE:deploy", 'update');
return 127;
}
/* (2) Reset file */
file_put_contents(ACTIONS_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(ACTIONS_CONF, 'w');
foreach($arr_r['actions'] as $timeout=>$actions)
foreach($actions as $action)
$f->fwrite( json_encode([
(int) $timeout,
(int) $action['id_action'],
$action['required'],
$action['action']
]).PHP_EOL );
$f = null;
return 0;
}
/* Manage sync vs. sync */
$is_sync = false;
if( $argc > 1 && $argv[1] == 'sync' )
$is_sync = true;
$exec = rfid_read_deploy($is_sync);
if( $exec == 0 ) slog('Success', "$FEATURE:deploy", 'update');
else slog('Failure', "$FEATURE:deploy", 'update');
echo $exec;
die($exec);
?>