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 api.response file', 'api: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 api.response file', 'api: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 api.response file', 'api: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['previous'], $action['action'] ]).PHP_EOL ); $f = null; /* [6] Get etrees list =========================================================*/ /* (1) Check data */ if( !isset($arr_r['etrees']) || !is_array($arr_r['etrees']) ){ slog('No \'etree\' found in api.response file', 'api:deploy', 'update'); return 127; } /* (2) Reset file */ file_put_contents(ETREE_CONF, ''); /* (3) Replace content */ $f = new SplFileObject(ETREE_CONF, 'w'); foreach($arr_r['etrees'] as $etree) $f->fwrite( $etree.PHP_EOL ); $f = null; /* [6] Remove history entries =========================================================*/ /* (0) We are done if @sync */ if( $sync ){ slog('Post-sync deployment -> no history truncate', 'api:deploy', 'update'); return 0; } /* (1) Check history entries count */ if( !isset($arr_r['saved']) || !is_array($arr_r['saved']) ){ slog('No \'saved\' found in api.response file', 'api:deploy', 'update'); return 127; } /* (2) Check history count */ 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; } /* (3) Fetch number of entries */ $saved = intval($arr_r['saved']['history']); $saved = $saved < 0 ? 0 : $saved; // prevent negative /* (4) Truncate the log file */ $truncated = syscall(SOURCE_DIR."/lib/file/truncate mfrc522 {$saved}"); /* (5) Manage error */ if( $truncated === false ){ slog('History cannot be truncated', 'api:deploy', 'update'); return 127; } slog('History succesfully truncated', 'api:deploy', 'update'); return 0; /* [7] Remove features' entries =========================================================*/ /* (0) We are done if @sync */ if( $sync ){ slog('Post-sync deployment -> no history truncate', 'api:deploy', 'update'); return 0; } if( isset($arr_r['saved']) && is_array($arr_r['saved']) ){ foreach($arr_r['saved'] as $feat_name=>$count){ /* (2) Check history count */ if( !is_numeric($count) ){ slog("No \'history\' found for '$feat_name' in api.response file", 'api:deploy', 'update'); return 127; } /* (3) Fetch number of entries */ $saved = intval($count); $saved = $saved < 0 ? 0 : $saved; // prevent negative /* (4) Truncate the log file */ $truncated = syscall(SOURCE_DIR."/lib/file/truncate $feat_name {$saved}"); /* (5) Manage error */ if( $truncated === false ){ slog('History cannot be truncated', 'api:deploy', 'update'); return 127; } slog("History '$feat_name' succesfully truncated", 'api:deploy', 'update'); } return 0; } /* Manage sync vs. sync */ $is_sync = false; if( $argc > 1 && $argv[1] == 'sync' ) $is_sync = true; $exec = api_deploy($is_sync); if( $exec == 0 ) slog('Success', 'api:deploy', 'update'); else slog('Failure', 'api:deploy', 'update'); echo $exec; die($exec); ?>