[1] machineDefault/update uses `moduleDefault/dispatch` to manage data for each feature

[2] `moduleDefault/dispatch` is now implemented
[3] `moduleDefault/rfid_read` implemented and caught by the *dispatcher*
[4] Added repo method etree/parse to get useable data from 'moduleName-etreeDaemonName'
This commit is contained in:
xdrm-brackets 2017-09-26 17:28:09 +02:00
parent d1f7f4f18f
commit 978aed690f
6 changed files with 198 additions and 311 deletions

View File

@ -682,57 +682,46 @@
/* [1] Initialisation des variables /* [1] Initialisation des variables
=========================================================*/ =========================================================*/
$fetched = [ $fetched = [ // count of registered logs for each feature
'history' => 0, // count of registered logs
'feature' => [], // count of registered logs for each feature
'identity' => [ // network identity
'ap' => isset($data['identity']) && is_array($data['identity']) && isset($data['identity']['ap']) ? $data['identity']['ap'] : null,
'ip' => isset($data['identity']) && is_array($data['identity']) && isset($data['identity']['ip']) ? $data['identity']['ip'] : null
]
]; ];
/* [2] Gestion des données reçues /* [2] Gestion des données reçues
=========================================================*/ =========================================================*/
/* (1) For each history entry */ /* (1) For each feature data */
if( isset($data['default']) && is_array($data['default']) ){ if( isset($data['feature']) && is_array($data['feature']) ){
/* (2) Create history entry in db */ /* (2) Create history entry in db */
foreach($data['default'] as $entry){ foreach($data['feature'] as $etree_name=>$feat_data){
// {1} Build request // // {1} Use moduleDefault to dispatch the request //
$log_req = new Request('historyDefault/create', [ $dispatch_feat_req = new Request('moduleDefault/dispatch', [
'timestamp' => $entry[0], 'etree' => $etree_name,
'id_user' => $entry[1], 'data' => $feat_data
'id_action' => $entry[2],
'id_machine' => $_SESSION['SATS']['id']
]); ]);
// {2} Process + get response // // {2} Process + get response //
$log_res = $log_req->dispatch(); $dispatch_feat_res = $dispatch_feat_req->dispatch();
// {3} Exit on failure // // {3} Exit on failure //
if( $log_res->error->get() != Err::Success ) if( $dispatch_feat_res->error->get() != Err::Success )
break; break;
// {4} Register count if success // // {4} Register saved code (returned from dispatcher) //
$fetched['history']++; $fetched[$etree_name] = $dispatch_feat_res->get('saved');
} }
} }
/* (2) For each feature's entry */ /* (2) Mise à jour de l'identité de la machine (ap-ip) */
// TODO: Manage feature's entries
/* (3) Mise à jour de l'identité de la machine (ap-ip) */
$updateNetId = new Repo('machine/updateNetworkIdentity', [ $updateNetId = new Repo('machine/updateNetworkIdentity', [
$_SESSION['WAREHOUSE']['id'], $_SESSION['WAREHOUSE']['id'],
$_SESSION['SATS']['id'], $_SESSION['SATS']['id'],
$fetched['identity']['ap'], isset($data['identity']) && is_array($data['identity']) && isset($data['identity']['ap']) ? $data['identity']['ap'] : null,
$fetched['identity']['ip'] isset($data['identity']) && is_array($data['identity']) && isset($data['identity']['ip']) ? $data['identity']['ip'] : null,
]); ]);
/* (2) On gère l'erreur */ /* (3) Gestion erreur */
if( $updateNetId->error->get() != Err::Success || !$updateNetId->answer() ) if( $updateNetId->error->get() != Err::Success || !$updateNetId->answer() )
return [ 'error' => new Error(Err::RepoError) ]; return [ 'error' => new Error(Err::RepoError) ];
@ -747,7 +736,7 @@
/* [4] Envoi des données /* [4] Envoi des données
=========================================================*/ =========================================================*/
return array_merge($basis_update, ['saved' => $fetched, 'id' => $fetched['identity']]); return array_merge($basis_update, ['saved' => $fetched]);
} }

View File

@ -0,0 +1,120 @@
<?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 moduleDefault{
public function __construct(){}
public function __destruct(){}
/* [0] Dispatch des données d'une feature d'une machine
*
* @etree<String> Nom (complet) de l'e-tree
* @data<mixed> Données à traiter
*
* @return saved<numeric> Code de réception (nombre d'entrées ou autre)
*
=========================================================*/
public function dispatch($params){
extract($params);
/* (1) On parse le nom complet
---------------------------------------------------------*/
/* (1) Requête */
$etree_parse_req = new Repo('etree/parse', [$etree]);
/* (2) On récupère la réponse */
$etree_data = $etree_parse_req->answer();
/* (3) Gestion erreur */
if( $etree_data === false )
return ['error' => new Error(Err::FormatError)];
/* (4) On calcule le nom de la méthode */
$method_name = $etree_data['module_name'].'_'.$etree_data['etree_name'];
/* (2) Gestion du dispatch
---------------------------------------------------------*/
/* (1) Formulation requête */
$request = new Request("moduleDefault/$method_name", [
'data' => $data
]);
/* (2) Gestion erreur */
if( $request->error->get() != Err::Success )
return ['error' => new Error(Err::ModuleError)];
/* (3) Exécution */
$response = $request->dispatch();
/* (4) Gestion erreur */
if( $response->error->get() != Err::Success )
return ['error' => new Error(Err::ModuleError)];
/* (5) Remontée du résultat */
return [
'saved' => $response->get('saved')
];
}
/* [1] Gestion des données historiques de l'etree READ du module RFID
*
* @data<mixed> Données à traiter
*
* @return saved<numeric> Nombre de lignes ajoutées
*
=========================================================*/
public function rfid_read($params){
/* (1) Initialisation du compteur de lignes enregistrées */
$count = 0;
/* (2) Create history entry in db */
foreach($data as $entry){
// {1} Build request to store each entry //
$log_req = new Request('historyDefault/create', [
'timestamp' => $entry[0],
'id_user' => $entry[1],
'id_action' => $entry[2],
'id_machine' => $_SESSION['SATS']['id']
]);
// {2} Process + get response //
$log_res = $log_req->dispatch();
// {3} Exit on failure //
if( $log_res->error->get() != Err::Success )
break;
// {4} Register count if success //
$count++;
}
/* (3) Retourne le nombre d'entrées enregistrées */
return [ 'saved' => $count ];
}
}
?>

View File

@ -43,84 +43,54 @@
} }
/* [2] Parses an e-tree full name (mod_name-etree_name)
/* [4] Adds a module to machine cluster of a warehouse
* *
* @id_warehouse<int> UID of the given warehouse * @full_name<String> Full name to parse
* @id_machine_cluster<int> UID of the given machine_cluster
* @id_module UID of the given module
* *
* @return status<bool> TRUE on success * @return info<Array> The useful data (id_module, id_etree)
* FALSE on error
* *
=========================================================*/ =========================================================*/
public static function link($id_warehouse, $id_machine_cluster, $id_module){ public static function parse($full_name){
/* (1) Check module
---------------------------------------------------------*/ /* (1) On vérifie le format (permet l'extraction des données) */
/* (1) Check if module exists (by its id) */ if( !preg_match('@^(.+)-(.+)$@', $full_name, $matches) )
return false;
/* (2) On recherche le module */
$module = Table::get('module') $module = Table::get('module')
->select('*') ->select('id_module')
->select('name')
->unique() ->unique()
->whereId($id_module); ->whereName($matches[1])
->fetch();
if( !$module->fetch() ) /* (3) Si on ne trouve pas de module -> error */
if( !is_array($module) || !isset($module['id_module']) )
return false; return false;
/* (2) Check if the module is allowed in the warehouse */ /* (5) On cherche l'e-tree */
$module_m = Table::get('module_availability') $etree = Table::get('etree')
->select('*') ->select('id_etree')
->select('daemon')
->whereIdModule($module['id_module'])
->whereDaemon($matches[2])
->unique() ->unique()
->whereIdWarehouse($id_warehouse) ->fetch();
->whereIdModule($id_module);
// if not allowed in the warehouse -> error /* (6) Si erreur */
if( !$module->fetch() ) if( !is_array($etree) || !isset($etree['id_etree']) )
return false; return false;
/* (2) Check machine_cluster /* (7) Si tout ok, on retourne les données */
---------------------------------------------------------*/ return [
/* (1) Check if machine_cluster exists (by its id) */ 'id_module' => $module['id_module'],
$machine_cluster = Table::get('machine_cluster') 'module_name' => $module['name'],
->select('*') 'id_etree' => $etree['id_etree'],
->unique() 'etree_name' => $etree['daemon']
->whereIdWarehouse($id_warehouse) ];
->whereId($id_machine_cluster);
if( !$machine_cluster->fetch() )
return false;
/* (2) Check link does not already exists */
$exists = Table::get('module_merge')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereIdMachineCluster($id_machine_cluster)
->whereIdModule($id_module);
if( !!$exists->fetch() )
return true; // returns all right (even if nothing done)
/* (3) Create link
---------------------------------------------------------*/
$inserted = Table::get('module_merge')->insert([
'id_warehouse' => $id_warehouse,
'id_module' => $id_module,
'id_machine_cluster' => $id_machine_cluster
]);
// return TRUE only if PDO insert successful
return $inserted;
} }
@ -130,68 +100,6 @@
/* [5] Removes a module from machine cluster of a warehouse
*
* @id_warehouse<int> UID of the given warehouse
* @id_machine_cluster<int> UID of the given machine_cluster
* @id_module UID of the given module
*
* @return status<bool> TRUE on success
*
=========================================================*/
public static function unlink($id_warehouse, $id_machine_cluster, $id_module){
/* (1) Check module
---------------------------------------------------------*/
/* (1) Check if module exists (by its id) */
$module = Table::get('module')
->select('*')
->unique()
->whereId($id_module);
if( !$module->fetch() )
return false;
/* (2) Check machine_cluster
---------------------------------------------------------*/
/* (1) Check if machine_cluster exists (by its id) */
$machine_cluster = Table::get('machine_cluster')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereId($id_machine_cluster);
if( !$machine_cluster->fetch() )
return false;
/* (2) Check if link already exists */
$exists = Table::get('module_merge')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereIdMachineCluster($id_machine_cluster)
->whereIdModule($id_module);
if( !$exists->fetch() )
return false; // returns all right (even if nothing done)
/* (3) Remove link
---------------------------------------------------------*/
$deleted = Table::get('module_merge')
->whereIdWarehouse($id_warehouse)
->whereIdModule($id_module)
->whereIdMachineCluster($id_machine_cluster)
->delete();
// return TRUE only if PDO delete successful
return $deleted;
}

View File

@ -83,160 +83,6 @@
} }
/* [4] Adds a module to machine cluster of a warehouse
*
* @id_warehouse<int> UID of the given warehouse
* @id_machine_cluster<int> UID of the given machine_cluster
* @id_module UID of the given module
*
* @return status<bool> TRUE on success
*
=========================================================*/
public static function link($id_warehouse, $id_machine_cluster, $id_module){
/* (1) Check module
---------------------------------------------------------*/
/* (1) Check if module exists (by its id) */
$module = Table::get('module')
->select('*')
->unique()
->whereId($id_module);
if( !$module->fetch() )
return false;
/* (2) Check if the module is allowed in the warehouse */
$module_m = Table::get('module_availability')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereIdModule($id_module);
// if not allowed in the warehouse -> error
if( !$module->fetch() )
return false;
/* (2) Check machine_cluster
---------------------------------------------------------*/
/* (1) Check if machine_cluster exists (by its id) */
$machine_cluster = Table::get('machine_cluster')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereId($id_machine_cluster);
if( !$machine_cluster->fetch() )
return false;
/* (2) Check link does not already exists */
$exists = Table::get('module_merge')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereIdMachineCluster($id_machine_cluster)
->whereIdModule($id_module);
if( !!$exists->fetch() )
return true; // returns all right (even if nothing done)
/* (3) Create link
---------------------------------------------------------*/
$inserted = Table::get('module_merge')->insert([
'id_warehouse' => $id_warehouse,
'id_module' => $id_module,
'id_machine_cluster' => $id_machine_cluster
]);
// return TRUE only if PDO insert successful
return $inserted;
}
/* [5] Removes a module from machine cluster of a warehouse
*
* @id_warehouse<int> UID of the given warehouse
* @id_machine_cluster<int> UID of the given machine_cluster
* @id_module UID of the given module
*
* @return status<bool> TRUE on success
*
=========================================================*/
public static function unlink($id_warehouse, $id_machine_cluster, $id_module){
/* (1) Check module
---------------------------------------------------------*/
/* (1) Check if module exists (by its id) */
$module = Table::get('module')
->select('*')
->unique()
->whereId($id_module);
if( !$module->fetch() )
return false;
/* (2) Check machine_cluster
---------------------------------------------------------*/
/* (1) Check if machine_cluster exists (by its id) */
$machine_cluster = Table::get('machine_cluster')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereId($id_machine_cluster);
if( !$machine_cluster->fetch() )
return false;
/* (2) Check if link already exists */
$exists = Table::get('module_merge')
->select('*')
->unique()
->whereIdWarehouse($id_warehouse)
->whereIdMachineCluster($id_machine_cluster)
->whereIdModule($id_module);
if( !$exists->fetch() )
return false; // returns all right (even if nothing done)
/* (3) Remove link
---------------------------------------------------------*/
$deleted = Table::get('module_merge')
->whereIdWarehouse($id_warehouse)
->whereIdModule($id_module)
->whereIdMachineCluster($id_machine_cluster)
->delete();
// return TRUE only if PDO delete successful
return $deleted;
}
} }

View File

@ -524,6 +524,31 @@
"output": {} "output": {}
} }
},
"moduleDefault": {
"POST::dispatch": {
"description": "Dispatche la gestion des données reçues par un SATS",
"permissions": [["sats"]],
"parameters": {
"etree": { "description": "Etree full name", "type": "text" },
"data": { "descripton": "Données reçues", "type": "mixed" }
},
"output": {
"saved": { "description": "Code de reception (nombre de lignes recues ou autre)", "type": "numeric" }
}
},
"POST::rfid_read": {
"description": "Enregistrement de l'historique de l'e-tree READ du module RFID",
"permissions": [["sats"]],
"parameters": {
"data": { "descripton": "Historique reçu", "type": "mixed" }
},
"output": {
"saved": { "description": "Nombre d'entrées enregistrées", "type": "id" }
}
}
} }
} }

View File

@ -3,13 +3,12 @@
"module": [ "module": [
"getAll", "getAll",
"getForWarehouse", "getForWarehouse",
"getForMachineCluster", "getForMachineCluster"
"link",
"unlink"
], ],
"etree": [ "etree": [
"getForMachineCluster" "getForMachineCluster",
"parse"
], ],
"history": [ "history": [