738 lines
18 KiB
PHP
Executable File
738 lines
18 KiB
PHP
Executable File
<?php
|
|
|
|
namespace api\module;
|
|
use \database\core\DatabaseDriver;
|
|
use \manager\sessionManager;
|
|
use \error\core\Error;
|
|
use \error\core\Err;
|
|
use \database\core\Repo;
|
|
use \api\core\Request;
|
|
|
|
class machineDefault{
|
|
|
|
public function __construct(){}
|
|
public function __destruct(){}
|
|
|
|
|
|
/* CREATION D'UNE NOUVELLE MACHINE DANS LA BDD
|
|
*
|
|
* @name<String> Identifiant de la machine
|
|
*
|
|
* @return status<Boolean> Retourne si oui ou non, tout s'est bien passe
|
|
*
|
|
*/
|
|
public function create($params){
|
|
extract($params);
|
|
|
|
/* [1] Vérification unicité (name)
|
|
=========================================================*/
|
|
/* (1) Check if unique : name */
|
|
$byName_r = new Repo('machine/getByName', [$_SESSION['WAREHOUSE']['id'], $name]);
|
|
$byName = $byName_r->answer();
|
|
|
|
/* (2) If already used -> error */
|
|
if( $byName != false )
|
|
return ['error' => new Error(Err::AlreadyExists, 'name')];
|
|
|
|
|
|
/* [2] Creation de la machine
|
|
=========================================================*/
|
|
$create_machine = new Repo('machine/create', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$name
|
|
]);
|
|
$id_machine = $create_machine->answer();
|
|
|
|
// Si une erreur est retournee, on retourne une erreur
|
|
if( $id_machine === false )
|
|
return ['error' => new Error(Err::ModuleError)];
|
|
|
|
|
|
|
|
/* [3] Gestion du retour
|
|
=========================================================*/
|
|
return [
|
|
'id_machine' => $id_machine
|
|
];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* AJOUTE UNE MACHINE DONNEE A UN GROUPE DONNE
|
|
*
|
|
* @id_cluster<int> UID du groupe
|
|
* @id_machine<int> UID de la machine
|
|
*
|
|
* @return association<int> Renvoie l'UID de l'association cree
|
|
* Renvoie FALSE si une erreur occure
|
|
*
|
|
*/
|
|
public function link($params){
|
|
extract($params);
|
|
|
|
/* Creation de l'association */
|
|
$link_machine = new Repo('machine_cluster/link', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$id_cluster,
|
|
$id_machine
|
|
]);
|
|
|
|
/* (1) On retourne l'erreur du repo */
|
|
return [ 'error' => $link_machine->answer() ];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* RETIRE UNE MACHINE DONNEE A UN GROUPE DONNE
|
|
*
|
|
* @id_cluster<int> UID du groupe
|
|
* @id_machine<int> UID de la machine
|
|
*
|
|
* @return association<int> Renvoie l'UID de l'association cree
|
|
* Renvoie FALSE si une erreur occure
|
|
*
|
|
*/
|
|
public function unlink($params){
|
|
extract($params);
|
|
|
|
/* Suppression de l'association */
|
|
$link_machine = new Repo('machine_cluster/unlink', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$id_cluster,
|
|
$id_machine
|
|
]);
|
|
|
|
/* (1) On retourne l'erreur du repo */
|
|
return [ 'error' => $link_machine->answer() ];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE UNE MACHINE EN FONCTION D'UN MOT CLE
|
|
*
|
|
* @keywords<String> Element de recherche
|
|
*
|
|
* @return machines<Array> Retourne la liste des machines trouvees
|
|
*
|
|
*/
|
|
public function search($params){
|
|
extract($params);
|
|
|
|
// On recupere les donnees
|
|
$machine = new Repo('machine/search', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$keywords
|
|
]);
|
|
|
|
return [ 'machines' => $machine->answer() ];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LA LISTE EXHAUSTIVE DES MACHINES
|
|
*
|
|
* @return machines<Array> Liste des machines
|
|
*
|
|
*/
|
|
public function getAll(){
|
|
// On recupere les donnees
|
|
$machines = new Repo('machine/getAll', [$_SESSION['WAREHOUSE']['id']]);
|
|
|
|
return [ 'machines' => $machines->answer() ];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LA MACHINE D'UID DONNE
|
|
*
|
|
* @id_machine<int> UID de la machine en question
|
|
*
|
|
* @return machine<Array> Machine d'UID donne
|
|
*
|
|
*/
|
|
public function getById($params){
|
|
extract($params);
|
|
|
|
// On recupere les donnees
|
|
$request = new Repo('machine/getById', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$id_machine
|
|
]);
|
|
$answer = $request->answer();
|
|
|
|
// Si aucun resultat, on retourne une erreur
|
|
if( $answer === false )
|
|
return [ 'error' => new Error(Err::NoMatchFound) ];
|
|
|
|
|
|
return [ 'machine' => $answer ];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LA MACHINE DE NAME DONNE
|
|
*
|
|
* @name<String> Name de l'utilisateur en question
|
|
*
|
|
* @return machine<Array> Machine de name donne
|
|
*
|
|
*/
|
|
public function getByName($params){
|
|
extract($params);
|
|
|
|
|
|
// On recupere les donnees
|
|
$request = new Repo('machine/getByName', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$name
|
|
]);
|
|
$answer = $request->answer();
|
|
|
|
// Si aucun resultat, on retourne une erreur
|
|
if( $answer === false )
|
|
return [ 'error' => new Error(Err::NoMatchFound) ];
|
|
|
|
|
|
return [ 'machine' => $answer ];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LES GROUPES D'UNE MACHINE DONNEE
|
|
*
|
|
* @id_machine<int> UID de la machine en question
|
|
*
|
|
* @return clusters<Array> Groupes de la machine donne
|
|
*
|
|
*/
|
|
public function getClusters($params){
|
|
extract($params);
|
|
|
|
// On recupere les donnees
|
|
$request = new Repo('machine/getClusters', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$id_machine
|
|
]);
|
|
$answer = $request->answer();
|
|
|
|
// Si aucun resultat, on retourne une erreur
|
|
if( $answer === false )
|
|
return [ 'error' => new Error(Err::NoMatchFound) ];
|
|
|
|
|
|
return [ 'clusters' => $answer ];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* MODIFIE UNE MACHINE DONNEE
|
|
*
|
|
* @id_machine<int> UID de la machine
|
|
* @name<String> Identifiant l'utilisateur
|
|
*
|
|
* @return status<Boolean> Retourne si oui ou non tout s'est bien deroule
|
|
*
|
|
*/
|
|
public function edit($params){
|
|
extract($params);
|
|
|
|
|
|
/* [1] On verifie l'existence de la machine
|
|
=========================================================*/
|
|
$machine_exists = new Repo('machine/getById', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$id_machine
|
|
]);
|
|
$machine_data = $machine_exists->answer();
|
|
|
|
// Si on a recupere aucune machine, on retourne une erreur
|
|
if( !is_array($machine_data) )
|
|
return ['error' => new Error(Err::NoMatchFound)];
|
|
|
|
|
|
|
|
/* [2] Vérification unicité (name)
|
|
=========================================================*/
|
|
/* (1) Check if unique : name */
|
|
$byName_r = new Repo('machine/getByName', [$_SESSION['WAREHOUSE']['id'], $name]);
|
|
$byName = $byName_r->answer();
|
|
|
|
/* (2) If already used (not current machine) -> error */
|
|
if( $byName != false && $byName['id_machine'] != $id_machine )
|
|
return ['error' => new Error(Err::AlreadyExists, 'name')];
|
|
|
|
|
|
/* [3] Normalisation + verification des donnees
|
|
=========================================================*/
|
|
|
|
/* (1) Verification des parametres (si correct et different)*/
|
|
$correct_param = [
|
|
'name' => !is_null($name) && $machine_data['name'] != $name
|
|
];
|
|
|
|
/* (2) Gestion des parametres optionnels */
|
|
$opt_data = [
|
|
'name' => ($correct_param['name']) ? $name : $machine_data['name']
|
|
];
|
|
|
|
|
|
/* [4] Modification de la machine
|
|
=========================================================*/
|
|
$request = new Repo('machine/edit', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$id_machine,
|
|
$opt_data['name']
|
|
]);
|
|
|
|
|
|
return [ 'status' => $request->answer() ];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRIME UNE MACHINE DONNEE
|
|
*
|
|
* @id_machine<int> UID de la machine en question
|
|
*
|
|
* @return status<Boolean> Retourne si oui ou non tout s'est bien deroule
|
|
*
|
|
*/
|
|
public function delete($params){
|
|
extract($params);
|
|
|
|
// On recupere les donnees
|
|
$del_machine = new Repo('machine/delete', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$id_machine
|
|
]);
|
|
$deleted_machine = $del_machine->answer();
|
|
|
|
|
|
return [ 'status' => $deleted_machine ];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETURN MACHINE STATE
|
|
*
|
|
* @id_machine<int> UID of the machine
|
|
*
|
|
* @return state<string> Machine state
|
|
*
|
|
*/
|
|
public function getState($params){
|
|
extract($params);
|
|
|
|
|
|
/* [1] Get machine info
|
|
=========================================================*/
|
|
/* (1) Write request */
|
|
$machine_req = new Request('machineDefault/getById', ['id_machine' => $id_machine]);
|
|
|
|
/* (2) Execute request */
|
|
$machine_res = $machine_req->dispatch();
|
|
|
|
/* (3) Manage error */
|
|
if( $machine_res->error->get() != Err::Success )
|
|
return [ 'error' => $machine_res->error ];
|
|
|
|
/* (4) Fetch data */
|
|
$machine = $machine_res->get('machine');
|
|
|
|
|
|
/* [2] Get action id=>name
|
|
=========================================================*/
|
|
$action = [];
|
|
|
|
/* (1) Write request */
|
|
$action_req = new Repo('action/getAll', []);
|
|
|
|
/* (2) Manage error */
|
|
if( $action_req->error->get() != Err::Success )
|
|
return [ 'error' => $action_req->error ];
|
|
|
|
/* (3) Create association array */
|
|
foreach($action_req->answer() as $a)
|
|
$action[ strtolower($a['name']) ] = $a['id_action'];
|
|
|
|
|
|
/* [3] Get history for the machine
|
|
=========================================================*/
|
|
/* (1) Write request */
|
|
$history_req = new Repo('history/getByIdMachine', [$id_machine]);
|
|
|
|
/* (2) Manage error */
|
|
if( $history_req->error->get() != Err::Success )
|
|
return [ 'error' => $history_req->error ];
|
|
|
|
/* (3) Extract history */
|
|
$history = $history_req->answer();
|
|
|
|
|
|
/* [4] Process state
|
|
=========================================================*/
|
|
|
|
/* (1) IF DETACHED (never initialized)
|
|
---------------------------------------------------------*/
|
|
/* (1) Check machine data (no token | unlock code) */
|
|
if( !$machine['token'] || $machine['unlock_code'] )
|
|
return [ 'state' => 'detached' ];
|
|
|
|
|
|
/* (2) STOPPED (no history)
|
|
---------------------------------------------------------*/
|
|
if( count($history) <= 0 )
|
|
return [ 'state' => 'stopped' ];
|
|
|
|
/* (3) LOCKED (last = lock)
|
|
---------------------------------------------------------*/
|
|
if( $history[0]['id_action'] == $action['lock'] )
|
|
return [ 'state' => 'locked' ];
|
|
|
|
/* (4) STOPPED (last = unlock | stop)
|
|
---------------------------------------------------------*/
|
|
if( in_array($history[0]['id_action'], [$action['stop'], $action['unlock']]) )
|
|
return [ 'state' => 'stopped' ];
|
|
|
|
|
|
/* (5) SIGNALED (start|stop ..... signal)
|
|
---------------------------------------------------------*/
|
|
for( $c = 0 ; $c < count($history) ; $c++ ){
|
|
|
|
/* (1) If (start|stop), continue to search */
|
|
if( in_array($history[$c]['id_action'] , [$action['start'], $action['stop']]) )
|
|
continue;
|
|
|
|
/* (2) If (signal) found, therefore it is signaled */
|
|
else if( $history[$c]['id_action'] == $action['signal'] )
|
|
return [ 'state' => 'signaled' ];
|
|
|
|
/* (6.1) STARTED (last state)
|
|
---------------------------------------------------------*/
|
|
else
|
|
return [ 'state' => 'started' ];
|
|
|
|
}
|
|
|
|
/* (6.2) STARTED (last state)
|
|
---------------------------------------------------------*/
|
|
return [ 'state' => 'started' ];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETURNS ALL INFORMATION NEEDED BY MACHINES IN ORDER TO WORK
|
|
*
|
|
* @id_machine<int> Machine UID
|
|
*
|
|
* @return data<Array<Mixed>> Useful data
|
|
*
|
|
*/
|
|
private static function getMachineWorkingInformation($id_machine){
|
|
|
|
|
|
/* [1] On envoie les e-tree de la machine
|
|
=========================================================*/ {
|
|
|
|
$etrees = [];
|
|
|
|
/* (1) On essaie de récupérer les etree de la machine*/
|
|
$mc_req = new Request('machineDefault/getClusters', ['id_machine' => $id_machine]);
|
|
|
|
$mc_res = $mc_req->dispatch();
|
|
|
|
/* (2) Si on arrive à récupérer les clusters */
|
|
if( $mc_res->error->get() == Err::Success ){
|
|
|
|
/* (3) Pour chaque cluster, on récupère les e-trees */
|
|
foreach($mc_res->get('clusters') as $cluster){
|
|
|
|
/* (4) On les e-trees du cluster */
|
|
$etree_req = new Request('clusterDefault/getEtrees', [
|
|
'id_machine_cluster' => $cluster['id_machine_cluster']
|
|
]);
|
|
|
|
$etree_res = $etree_req->dispatch();
|
|
|
|
/* (5) Si erreur, on passe */
|
|
if( $etree_res->error->get() != Err::Success )
|
|
continue;
|
|
|
|
/* (6) Sinon -> On entregistre les e-trees */
|
|
foreach($etree_res->get('etrees') as $etree){
|
|
$etree_name = $etree['name'].'-'.$etree['daemon'];
|
|
|
|
/* (7) Si pas déja entregistré -> on l'ajoute */
|
|
if( !in_array($etree_name, $etrees) )
|
|
$etrees[] = $etree_name;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* [2] On récupère les données globales
|
|
=========================================================*/ {
|
|
|
|
/* (1) On récupère la liste des états
|
|
---------------------------------------------------------*/ {
|
|
|
|
$globalStatesReq = new Repo('global_state/getAll');
|
|
$globalStates = ($globalStatesReq->error->get()==Err::Success) ? $globalStatesReq->answer() : [];
|
|
|
|
}
|
|
|
|
|
|
/* (2) On récupère la liste des composants (chip)
|
|
---------------------------------------------------------*/ {
|
|
|
|
/* (1) On récupère les composants disponibles */
|
|
$chipsReq = new Repo('chip/getForMachine', [$_SESSION['WAREHOUSE']['id'], $id_machine]);
|
|
$chips = ($chipsReq->error->get()==Err::Success) ? $chipsReq->answer() : [];
|
|
|
|
|
|
foreach($chips as &$chip){
|
|
|
|
/* (2) On récupére la liste des PINS de chaque COMPOSANT */
|
|
$chip['pins'] = explode(',', $chip['pins']);
|
|
|
|
if( !is_array($chip['pins']) )
|
|
$chip['pins'] = [];
|
|
|
|
/* (3) On récupère valeurs pour chaque état de chaque COMPOSANT */
|
|
$chip['states'] = [];
|
|
|
|
$statesReq = new Repo('state/getForChip', [$chip['id_chip']]);
|
|
$states = ($statesReq->error->get()==Err::Success) ? $statesReq->answer() : [];
|
|
|
|
/* (4) On met en forme les données : "val1,val2,val3" -> [val1, val2, val3] */
|
|
foreach($states as $s=>$state){
|
|
|
|
$chip['states'][$state['state']] = explode(',', $state['value']);
|
|
|
|
foreach($chip['states'][$state['state']] as $s2=>$state2)
|
|
$chip['states'][$state['state']][$s2] = intval($state2);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* [3] On récupère les données de chaque feature
|
|
=========================================================*/
|
|
$features = [];
|
|
|
|
/* (1) Pour chaque etree/feature */
|
|
foreach($etrees as $etree_name){
|
|
|
|
$features[$etree_name] = [];
|
|
|
|
/* (2) On lance le dispatch */
|
|
$dispatch_feat_req = new Request('deployDefault/dispatch', [
|
|
'etree' => $etree_name
|
|
]);
|
|
|
|
/* (3) Exécution + récupération réponse */
|
|
$dispatch_feat_res = $dispatch_feat_req->dispatch();
|
|
|
|
/* (4) Gestion erreur */
|
|
if( $dispatch_feat_res->error->get() != Err::Success )
|
|
$features[$etree_name]['err'] = $dispatch_feat_res->error->get();
|
|
|
|
/* (5) Enregistrement des données */
|
|
$features[$etree_name] = $dispatch_feat_res->get('data');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [N] Retourne les données
|
|
=========================================================*/
|
|
return [
|
|
'etrees' => $etrees,
|
|
'feature' => $features,
|
|
'chips' => $chips,
|
|
'states' => $globalStates
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ENVOI DES DONNEES D'INITIALISATION DU SATS
|
|
*
|
|
* @return id_machine<int> UID de la machine
|
|
* @return token<string> Nouveau token d'identification (hashage cyclique)
|
|
* @return unlock<string> Code de déblocage de la machine
|
|
*
|
|
*/
|
|
public function sync($params){
|
|
extract($params);
|
|
|
|
|
|
/* [1] On essaie de débloquer la machine
|
|
=========================================================*/
|
|
/* (1) On rédige la requête */
|
|
$unlockReq = new Repo('machine/unlock', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$id_machine,
|
|
$unlock,
|
|
$token
|
|
]);
|
|
|
|
/* (2) On gère l'erreur */
|
|
if( $unlockReq->error->get() != Err::Success || !$unlockReq->answer() )
|
|
return [ 'error' => new Error(Err::TokenError) ];
|
|
|
|
|
|
/* [N] Retourne les données
|
|
=========================================================*/
|
|
return self::getMachineWorkingInformation($id_machine);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* MET A JOUR UNE MACHINE
|
|
*
|
|
* @token<String> Token de synchronisation de la machine
|
|
* @data<Array> Données de la synchronisation
|
|
* @renew<String> Renouvellement du token de synchronisation de la machine (opt.)
|
|
*
|
|
* @return data<Array> Données de retour de synchronisation
|
|
*
|
|
*/
|
|
public function update($params){
|
|
extract($params);
|
|
|
|
|
|
/* [1] Initialisation des variables
|
|
=========================================================*/
|
|
$fetched = []; // count of registered logs for each feature
|
|
|
|
|
|
|
|
/* [2] Gestion des données reçues
|
|
=========================================================*/
|
|
/* (1) For each feature data */
|
|
if( isset($data['feature']) && is_array($data['feature']) ){
|
|
|
|
/* (2) Create history entry in db */
|
|
foreach($data['feature'] as $etree_name=>$feat_data){
|
|
|
|
// {1} Use fetchDefault to dispatch the request //
|
|
$dispatch_feat_req = new Request('fetchDefault/dispatch', [
|
|
'etree' => $etree_name,
|
|
'data' => $feat_data
|
|
]);
|
|
|
|
// {2} Process + get response //
|
|
$dispatch_feat_res = $dispatch_feat_req->dispatch();
|
|
|
|
// {3} Exit on failure //
|
|
if( $dispatch_feat_res->error->get() != Err::Success )
|
|
continue;
|
|
|
|
// {4} Register saved code (returned from dispatcher) //
|
|
$fetched[$etree_name] = $dispatch_feat_res->get('saved');
|
|
}
|
|
|
|
}
|
|
|
|
/* (2) Mise à jour de l'identité de la machine (ap-ip) */
|
|
$updateNetId = new Repo('machine/updateNetworkIdentity', [
|
|
$_SESSION['WAREHOUSE']['id'],
|
|
$_SESSION['SATS']['id'],
|
|
isset($data['identity']) && is_array($data['identity']) && isset($data['identity']['ap']) ? $data['identity']['ap'] : null,
|
|
isset($data['identity']) && is_array($data['identity']) && isset($data['identity']['ip']) ? $data['identity']['ip'] : null,
|
|
]);
|
|
|
|
/* (3) Gestion erreur */
|
|
if( $updateNetId->error->get() != Err::Success || !$updateNetId->answer() )
|
|
return [ 'error' => new Error(Err::RepoError) ];
|
|
|
|
|
|
/* [3] Gestion des données à envoyer
|
|
=========================================================*/
|
|
/* (1) Basic working data update
|
|
---------------------------------------------------------*/
|
|
$basis_update = self::getMachineWorkingInformation($_SESSION['SATS']['id']);
|
|
|
|
|
|
|
|
/* [4] Envoi des données
|
|
=========================================================*/
|
|
return array_merge($basis_update, ['saved' => $fetched]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
?>
|