From 978aed690f1c28b0befd440b3b8386134559d49b Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 26 Sep 2017 17:28:09 +0200 Subject: [PATCH] [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' --- build/api/module/machineDefault.php | 45 +++----- build/api/module/moduleDefault.php | 120 +++++++++++++++++++++ build/database/repo/etree.php | 158 ++++++---------------------- build/database/repo/module.php | 154 --------------------------- config/modules.json | 25 +++++ config/repositories.json | 7 +- 6 files changed, 198 insertions(+), 311 deletions(-) create mode 100644 build/api/module/moduleDefault.php diff --git a/build/api/module/machineDefault.php b/build/api/module/machineDefault.php index 13c6d19..10f0e8f 100755 --- a/build/api/module/machineDefault.php +++ b/build/api/module/machineDefault.php @@ -682,57 +682,46 @@ /* [1] Initialisation des variables =========================================================*/ - $fetched = [ - '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 - ] + $fetched = [ // count of registered logs for each feature ]; /* [2] Gestion des données reçues =========================================================*/ - /* (1) For each history entry */ - if( isset($data['default']) && is_array($data['default']) ){ + /* (1) For each feature data */ + if( isset($data['feature']) && is_array($data['feature']) ){ /* (2) Create history entry in db */ - foreach($data['default'] as $entry){ + foreach($data['feature'] as $etree_name=>$feat_data){ - // {1} Build request // - $log_req = new Request('historyDefault/create', [ - 'timestamp' => $entry[0], - 'id_user' => $entry[1], - 'id_action' => $entry[2], - 'id_machine' => $_SESSION['SATS']['id'] + // {1} Use moduleDefault to dispatch the request // + $dispatch_feat_req = new Request('moduleDefault/dispatch', [ + 'etree' => $etree_name, + 'data' => $feat_data ]); // {2} Process + get response // - $log_res = $log_req->dispatch(); + $dispatch_feat_res = $dispatch_feat_req->dispatch(); // {3} Exit on failure // - if( $log_res->error->get() != Err::Success ) + if( $dispatch_feat_res->error->get() != Err::Success ) break; - // {4} Register count if success // - $fetched['history']++; + // {4} Register saved code (returned from dispatcher) // + $fetched[$etree_name] = $dispatch_feat_res->get('saved'); } } - /* (2) For each feature's entry */ - // TODO: Manage feature's entries - - /* (3) Mise à jour de l'identité de la machine (ap-ip) */ + /* (2) Mise à jour de l'identité de la machine (ap-ip) */ $updateNetId = new Repo('machine/updateNetworkIdentity', [ $_SESSION['WAREHOUSE']['id'], $_SESSION['SATS']['id'], - $fetched['identity']['ap'], - $fetched['identity']['ip'] + 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, ]); - /* (2) On gère l'erreur */ + /* (3) Gestion erreur */ if( $updateNetId->error->get() != Err::Success || !$updateNetId->answer() ) return [ 'error' => new Error(Err::RepoError) ]; @@ -747,7 +736,7 @@ /* [4] Envoi des données =========================================================*/ - return array_merge($basis_update, ['saved' => $fetched, 'id' => $fetched['identity']]); + return array_merge($basis_update, ['saved' => $fetched]); } diff --git a/build/api/module/moduleDefault.php b/build/api/module/moduleDefault.php new file mode 100644 index 0000000..e45ef70 --- /dev/null +++ b/build/api/module/moduleDefault.php @@ -0,0 +1,120 @@ + Nom (complet) de l'e-tree + * @data Données à traiter + * + * @return saved 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 Données à traiter + * + * @return saved 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 ]; + + } + + + } + + +?> diff --git a/build/database/repo/etree.php b/build/database/repo/etree.php index 459843c..7a6535f 100644 --- a/build/database/repo/etree.php +++ b/build/database/repo/etree.php @@ -43,84 +43,54 @@ } - - - - - - - - - - /* [4] Adds a module to machine cluster of a warehouse + /* [2] Parses an e-tree full name (mod_name-etree_name) * - * @id_warehouse UID of the given warehouse - * @id_machine_cluster UID of the given machine_cluster - * @id_module UID of the given module + * @full_name Full name to parse * - * @return status TRUE on success + * @return info 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) Check if module exists (by its id) */ + + /* (1) On vérifie le format (permet l'extraction des données) */ + if( !preg_match('@^(.+)-(.+)$@', $full_name, $matches) ) + return false; + + /* (2) On recherche le module */ $module = Table::get('module') - ->select('*') + ->select('id_module') + ->select('name') ->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; - /* (2) Check if the module is allowed in the warehouse */ - $module_m = Table::get('module_availability') - ->select('*') + /* (5) On cherche l'e-tree */ + $etree = Table::get('etree') + ->select('id_etree') + ->select('daemon') + ->whereIdModule($module['id_module']) + ->whereDaemon($matches[2]) ->unique() - ->whereIdWarehouse($id_warehouse) - ->whereIdModule($id_module); + ->fetch(); - // if not allowed in the warehouse -> error - if( !$module->fetch() ) + /* (6) Si erreur */ + if( !is_array($etree) || !isset($etree['id_etree']) ) 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; + /* (7) Si tout ok, on retourne les données */ + return [ + 'id_module' => $module['id_module'], + 'module_name' => $module['name'], + 'id_etree' => $etree['id_etree'], + 'etree_name' => $etree['daemon'] + ]; } @@ -130,68 +100,6 @@ - /* [5] Removes a module from machine cluster of a warehouse - * - * @id_warehouse UID of the given warehouse - * @id_machine_cluster UID of the given machine_cluster - * @id_module UID of the given module - * - * @return status 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; - - } - diff --git a/build/database/repo/module.php b/build/database/repo/module.php index 1fb697e..5719ed0 100644 --- a/build/database/repo/module.php +++ b/build/database/repo/module.php @@ -83,160 +83,6 @@ } - - - - - - - - - - - /* [4] Adds a module to machine cluster of a warehouse - * - * @id_warehouse UID of the given warehouse - * @id_machine_cluster UID of the given machine_cluster - * @id_module UID of the given module - * - * @return status 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 UID of the given warehouse - * @id_machine_cluster UID of the given machine_cluster - * @id_module UID of the given module - * - * @return status 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; - - } - - - - - } diff --git a/config/modules.json b/config/modules.json index 4a252f3..a81706b 100755 --- a/config/modules.json +++ b/config/modules.json @@ -524,6 +524,31 @@ "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" } + } + } } } diff --git a/config/repositories.json b/config/repositories.json index c7f483d..3a13be1 100755 --- a/config/repositories.json +++ b/config/repositories.json @@ -3,13 +3,12 @@ "module": [ "getAll", "getForWarehouse", - "getForMachineCluster", - "link", - "unlink" + "getForMachineCluster" ], "etree": [ - "getForMachineCluster" + "getForMachineCluster", + "parse" ], "history": [