From 474d60394d52bc8d643d0abe31aca2a4564b3f1c Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 13 Oct 2017 16:46:30 +0200 Subject: [PATCH] Dev. motheure::increment + fetchDefault::motheure_simple calling motheure::increment --- build/api/module/fetchDefault.php | 52 +++++++++++++++++++++-- build/api/module/motheure.php | 68 ++++++++++++++++++++++++++++++- config/modules.json | 10 ++++- 3 files changed, 124 insertions(+), 6 deletions(-) diff --git a/build/api/module/fetchDefault.php b/build/api/module/fetchDefault.php index f8ffa31..306a137 100644 --- a/build/api/module/fetchDefault.php +++ b/build/api/module/fetchDefault.php @@ -100,9 +100,9 @@ // {2} Process + get response // $log_res = $log_req->dispatch(); - // {3} Exit on failure // - if( $log_res->error->get() != Err::Success ) - break; + // {3} Ignore failure // + // if( $log_res->error->get() != Err::Success ) + // continue; // {4} Register count if success // $count++; @@ -115,6 +115,52 @@ } + + + + /* [2] Gestion des données historiques de l'etree SIMPLE du module MOTHEURE + * + * @data Données à traiter + * + * @return saved Nombre de lignes ajoutées + * + =========================================================*/ + public function motheure_simple($params){ + extract($params); + + /* (1) Initialize data + ---------------------------------------------------------*/ + /* (1) Initialisation du compteur de lignes enregistrées */ + $count = 0; + + /* (2) Calcl the total to add */ + $total = 0; + + foreach($data as $entry){ + + // Only if positive numeric + if( is_numeric($entry) && $entry > 0 ) + $total += intval($entry); + + $count++; + } + + /* (2) Update the table in the database + ---------------------------------------------------------*/ + /* (1) Build request to store each entry */ + $upd_req = new Request('motheure/increment', [ + 'count' => $total + ]); + + // {2} Process + get response // + $upd_res = $log_req->dispatch(); + + /* (3) Retourne le nombre d'entrées enregistrées */ + return [ 'saved' => $count ]; + + } + + } diff --git a/build/api/module/motheure.php b/build/api/module/motheure.php index f4e770e..6afcedc 100644 --- a/build/api/module/motheure.php +++ b/build/api/module/motheure.php @@ -16,13 +16,13 @@ public function __destruct(){} - /* RETURNS THE TOTAL COUNT FOR A MACHINE + /* (1) RETURNS THE TOTAL COUNT FOR A MACHINE * * @id_machine Identifiant de la machine * * @return count Retourne le compte horaire * - */ + ---------------------------------------------------------*/ public function getCount($params){ extract($params); @@ -70,6 +70,70 @@ } + + + + + /* (2) Increment the motor count + * + * @count Count to add to motor count of the machine + * + ---------------------------------------------------------*/ + public function increment(){ + extract($params); + + /* (1) Check if entry exists + ---------------------------------------------------------*/ + /* (1) Check if there is an entry for the current machine */ + $already_exists = is_array(Table::get('motheure') + ->unique() + ->select('id_motheure') + ->select('id_machine') + ->whereIdMachine($_SESSION['SATS']['id']) + ->fetch()); + + + /* (2) If does not exist, create entry + ---------------------------------------------------------*/ + if( !$already_exists ){ + + /* (1) Insert new entry */ + $inserted = Table::get('motheure') + ->insert([ + 'id_motheure' => Rows::INSERT_DEFAULT, + 'id_machine' => $_SESSION['SATS']['id'], + 'count' => $count + ]); + + /* (2) Propagate error */ + return [ 'error' => ($inserted) ? new Error(Err::Success) : new Error(Err::RepoError) ]; + + + /* (3) If already exists, update entry + ---------------------------------------------------------*/ + }else{ + + /* (1) Update entry */ + $updated = Table::get('motheure') + ->unique() + ->whereIdMachine($_SESSION['SATS']['id']) + ->edit([ + 'count' => $count + ]); + + /* (2) Propagate error */ + return [ 'error' => ($updated) ? new Error(Err::Success) : new Error(Err::RepoError) ]; + + } + + /* (x) Default error */ + return [ 'error' => new Error(Err::Success) ]; + + + } + + + } diff --git a/config/modules.json b/config/modules.json index ed9e25c..229d1dc 100755 --- a/config/modules.json +++ b/config/modules.json @@ -23,6 +23,14 @@ "output": { "count": { "description": "Compte horaire (en secondes)", "type": "int" } } + }, + "POST::increment": { + "description": "Incrémente le comptheure horaire d'une machine.", + "permissions": [["sats"]], + "parameters": { + "data": { "description": "Données du module", "type": "array" } + }, + "output": {} } }, @@ -193,7 +201,7 @@ "machineDefault": { "POST::update": { - "description": "Mise à jour d'un SAYS++TS.", + "description": "Mise à jour d'un SATS.", "permissions": [["sats"]], "parameters": { "data": { "description": "Données (dépendent des modules).", "type": "array" }