SATS/lib/api/source/deploy.php

191 lines
4.4 KiB
PHP
Raw Normal View History

<?php
require_once __DIR__.'/../../include/php/const';
2017-05-08 20:07:20 +00:00
function api_deploy($init){
/* [1] Fetch api response
=========================================================*/
/* (1) Try to read response */
$file_r = @file_get_contents(TMP_DIR.'/api.response');
/* (2) Check response */
2017-05-09 09:47:16 +00:00
if( $file_r === false ){
slog('Cannot find api.response file', 'api:deploy', 'update');
return 127;
2017-05-09 09:47:16 +00:00
}
/* (3) Try to parse response */
$arr_r = json_decode($file_r, true);
/* (4) Check parse error */
2017-05-09 09:47:16 +00:00
if( is_null($arr_r) ){
slog('Cannot parse api.response file', 'api:deploy', 'update');
return 127;
2017-05-09 09:47:16 +00:00
}
2017-05-08 20:07:20 +00:00
/* [2] Get system states (global_state)
=========================================================*/
/* (1) Check data */
2017-05-09 09:47:16 +00:00
if( !isset($arr_r['states']) || !is_array($arr_r['states']) ){
slog('No \'states\' found in api.response file', 'api:deploy', 'update');
return 127;
2017-05-09 09:47:16 +00:00
}
/* (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 */
2017-05-09 09:47:16 +00:00
if( !isset($arr_r['chips']) || !is_array($arr_r['chips']) ){
slog('No \'chips\' found in api.response file', 'api:deploy', 'update');
return 127;
2017-05-09 09:47:16 +00:00
}
/* (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([
2017-05-08 20:07:20 +00:00
(int) $chip['position'],
$chip['pins'],
$chip['states']
]).PHP_EOL );
$f = null;
/* [4] Get auth list (permissions)
=========================================================*/
/* (1) Check data */
2017-05-09 09:47:16 +00:00
if( !isset($arr_r['permissions']) || !is_array($arr_r['permissions']) ){
slog('No \'permissions\' found in api.response file', 'api:deploy', 'update');
return 127;
2017-05-09 09:47:16 +00:00
}
/* (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([
2017-05-08 20:07:20 +00:00
$code,
(int) $perm['id_user'],
$perm['actions']
]).PHP_EOL );
$f = null;
/* [5] Get action list (with timeout)
=========================================================*/
/* (1) Check data */
2017-05-09 09:47:16 +00:00
if( !isset($arr_r['actions']) || !is_array($arr_r['actions']) ){
slog('No \'actions\' found in api.response file', 'api:deploy', 'update');
return 127;
2017-05-09 09:47:16 +00:00
}
/* (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([
2017-05-08 20:07:20 +00:00
(int) $timeout,
(int) $action['id_action'],
$action['previous'],
$action['action']
]).PHP_EOL );
$f = null;
/* [6] Remove history entries
=========================================================*/
2017-05-08 20:07:20 +00:00
/* (0) We are done if @init */
2017-05-09 09:47:16 +00:00
if( $init ){
slog('Post-init deployment -> no history truncate', 'api:deploy', 'update');
2017-05-08 20:07:20 +00:00
return 0;
2017-05-09 09:47:16 +00:00
}
2017-05-08 20:07:20 +00:00
/* (1) Check history entries count */
2017-05-09 09:47:16 +00:00
if( !isset($arr_r['saved']) || !is_array($arr_r['saved']) ){
slog('No \'saved\' found in api.response file', 'api:deploy', 'update');
return 127;
2017-05-09 09:47:16 +00:00
}
/* (2) Check history count */
2017-05-09 09:47:16 +00:00
if( !isset($arr_r['saved']['history']) || !is_numeric($arr_r['saved']['history']) ){
slog('No \'actions\'.\'history\' found in api.response file', 'api:deploy', 'update');
return 127;
2017-05-09 09:47:16 +00:00
}
/* (3) Fetch number of entries */
$saved = intval($arr_r['saved']['history']);
$saved = $saved < 0 ? 0 : $saved; // prevent negative
/* (4) Truncate the log file */
2017-05-09 10:24:27 +00:00
if( $saved > 0 )
$saved++; // because need to be excluded
$truncated = syscall(SOURCE_DIR."/lib/file/truncate mfrc522 {$saved}");
/* (5) Manage error */
2017-05-09 09:47:16 +00:00
if( $truncated === false ){
2017-05-09 10:10:37 +00:00
slog('History cannot be truncated', 'api:deploy', 'update');
return 127;
2017-05-09 09:47:16 +00:00
}
2017-05-09 10:10:37 +00:00
slog('History succesfully truncated', 'api:deploy', 'update');
return 0;
}
2017-05-08 20:07:20 +00:00
/* Manage init vs. sync */
$is_init = false;
if( $argc > 1 && $argv[1] == 'init' )
$is_init = true;
$exec = api_deploy($is_init);
2017-05-09 10:27:09 +00:00
if( $exec == 0 ) slog('Success', 'api:deploy', 'update');
else slog('Failure', 'api:deploy', 'update');
2017-05-09 09:47:16 +00:00
echo $exec;
die($exec);
2017-05-08 20:07:20 +00:00
?>