From 0f86cb10a2b7dbdeae15c7c12ca64ba7c6c1b4a4 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 20 Feb 2017 10:46:39 +0100 Subject: [PATCH] [Done] `machineDefault/getState` works [FUNCTIONAL] --- build/api/module/machineDefault.php | 45 ++++++++++++---------- build/database/repo/history.php | 60 +++++++++++++++++++++++++++-- build/database/repo/machine.php | 51 ++++++++++++++++++++---- 3 files changed, 125 insertions(+), 31 deletions(-) diff --git a/build/api/module/machineDefault.php b/build/api/module/machineDefault.php index e97c8fa..7bdea17 100755 --- a/build/api/module/machineDefault.php +++ b/build/api/module/machineDefault.php @@ -339,6 +339,9 @@ 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 =========================================================*/ @@ -372,22 +375,32 @@ /* [4] Process state =========================================================*/ - /* (1) LOCKED (last = lock) + /* (1) IF DETACHED (never initialized) ---------------------------------------------------------*/ - if( count($history) > 0 && $history[0]['id_action'] == $action['lock'] ) - return [ 'state' => 'locked' ]; + /* (1) Check machine data (no token | unlock code) */ + if( !$machine['token'] || $machine['unlock_code'] ) + return [ 'state' => 'detached' ]; - /* (2) STOPPED (last = unlock | stop) + + /* (2) STOPPED (no history) ---------------------------------------------------------*/ - if( count($history) > 0 && in_array($history[0]['id_action'], [$action['stop'], $action['unlock']]) ) + if( count($history) <= 0 ) return [ 'state' => 'stopped' ]; - /* (3) SIGNALED (start|stop ..... signal) + /* (3) LOCKED (last = lock) ---------------------------------------------------------*/ - if( count($history) > 0 && $history[0]['id_action'] == $action['signal'] ) - return [ 'state' => 'signaled' ]; + if( $history[0]['id_action'] == $action['lock'] ) + return [ 'state' => 'locked' ]; - for( $c = 1 ; $c < count($history) ; $c++ ){ + /* (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']]) ) @@ -397,24 +410,16 @@ else if( $history[$c]['id_action'] == $action['signal'] ) return [ 'state' => 'signaled' ]; - /* (4) STARTED (last state) + /* (6.1) STARTED (last state) ---------------------------------------------------------*/ else return [ 'state' => 'started' ]; } - /* (4) STARTED (last state) + /* (6.2) STARTED (last state) ---------------------------------------------------------*/ - if( count($history) > 0 && $history[0]['id_action'] == $action['start'] ) - return [ 'state' => 'started' ]; - - - /* (5) DETACHED (no state) - ---------------------------------------------------------*/ - return [ 'state' => 'detached' ]; - - + return [ 'state' => 'started' ]; } diff --git a/build/database/repo/history.php b/build/database/repo/history.php index 6aad9f0..1464b18 100755 --- a/build/database/repo/history.php +++ b/build/database/repo/history.php @@ -21,6 +21,7 @@ * */ public static function create($id_user, $id_machine, $id_action, $timestamp){ + /* [1] On retourne l'id_history ou FALSE si erreur =========================================================*/ $inserted = Table::get('history')->insert([ @@ -77,6 +78,58 @@ + /* RETOURNE UNE ENTREE SPECIFIQUE + * + * @id_machine UID de la machine + * + * @return entry Données de l'entree + * FALSE si aucun résultat + * + */ + public static function getByIdMachine($id_machine){ + /* [1] On rédige/execute la requête + =========================================================*/ + $machine = Table::get('history') + ->whereIdMachine($id_machine) + ->orderby('timestamp', Rows::ORDER_DESC) + ->select('*'); + + return $machine->fetch(); + } + + + + + + + + + /* RETOURNE UNE ENTREE SPECIFIQUE + * + * @id_user UID de l'utilisateur + * + * @return entry Données de l'entree + * FALSE si aucun résultat + * + */ + public static function getByIdUser($id_user){ + /* [1] On rédige/execute la requête + =========================================================*/ + $user = Table::get('history') + ->whereIdUser($id_user) + ->orderby('timestamp', Rows::ORDER_DESC) + ->select('*'); + + return $user->fetch(); + } + + + + + + + + /* RETOURNE UNE ENTREE SPECIFIQUE * * @id_history UID de l'entree @@ -89,10 +142,9 @@ /* [1] On rédige/execute la requête =========================================================*/ $user = Table::get('user') - ->whereIdWarehouse($id_warehouse) - ->whereIdUser($id_user) - ->select('*') - ->unique(); + ->whereId($id_history) + ->orderby('timestamp', Rows::ORDER_DESC) + ->select('*'); return $user->fetch(); } diff --git a/build/database/repo/machine.php b/build/database/repo/machine.php index efc27b1..c2f432a 100755 --- a/build/database/repo/machine.php +++ b/build/database/repo/machine.php @@ -177,17 +177,30 @@ * */ public static function getById($id_warehouse, $id_machine){ - /* [1] On rédige/execute la requête + /* [1] On récupère la machine =========================================================*/ + /* (1) Write request */ $machine = Table::get('machine') ->whereId($id_machine) ->whereIdWarehouse($id_warehouse) ->select('id_machine') ->select('name') + ->select('token') + ->select('unlock_code') ->orderby('id_machine', Rows::ORDER_ASC) - ->unique(); + ->unique() + ->fetch(); - return $machine->fetch(); + /* (2) Manage error */ + if( $machine === false ) + return false; + + /* (3) On remplace les valeurs 'unlock_code' et 'token' */ + $machine['token'] = strlen($machine['token']) > 0; + $machine['unlock_code'] = strlen($machine['unlock_code']) > 0; + + /* (4) On retourne la réponse */ + return $machine; } @@ -208,17 +221,29 @@ * */ public static function getByName($id_warehouse, $name){ - /* [1] On rédige/execute la requête + /* [1] On récupère la machine =========================================================*/ + /* (1) Write request */ $machine = Table::get('machine') ->whereName($name) ->whereIdWarehouse($id_warehouse) ->select('id_machine') ->select('name') + ->select('token') + ->select('unlock_code') ->orderby('name', Rows::ORDER_ASC) ->unique(); - return $machine->fetch(); + /* (2) Manage error */ + if( $machine === false ) + return false; + + /* (3) On remplace les valeurs 'unlock_code' et 'token' */ + $machine['token'] = strlen($machine['token']) > 0; + $machine['unlock_code'] = strlen($machine['unlock_code']) > 0; + + /* (4) On retourne la réponse */ + return $machine; } @@ -239,17 +264,29 @@ * */ public static function getByToken($id_warehouse, $token){ - /* [1] On rédige/execute la requête + /* [1] On récupère la machine =========================================================*/ + /* (1) Write request */ $machine = Table::get('machine') ->whereToken($token) ->whereIdWarehouse($id_warehouse) ->select('id_machine') ->select('name') + ->select('token') + ->select('unlock_code') ->orderby('name', Rows::ORDER_ASC) ->unique(); - return $machine->fetch(); + /* (2) Manage error */ + if( $machine === false ) + return false; + + /* (3) On remplace les valeurs 'unlock_code' et 'token' */ + $machine['token'] = strlen($machine['token']) > 0; + $machine['unlock_code'] = strlen($machine['unlock_code']) > 0; + + /* (4) On retourne la réponse */ + return $machine; }