[Done] `machineDefault/getState` works [FUNCTIONAL]

This commit is contained in:
xdrm-brackets 2017-02-20 10:46:39 +01:00
parent 0771694be8
commit 0f86cb10a2
3 changed files with 125 additions and 31 deletions

View File

@ -339,6 +339,9 @@
if( $machine_res->error->get() != Err::Success ) if( $machine_res->error->get() != Err::Success )
return [ 'error' => $machine_res->error ]; return [ 'error' => $machine_res->error ];
/* (4) Fetch data */
$machine = $machine_res->get('machine');
/* [2] Get action id=>name /* [2] Get action id=>name
=========================================================*/ =========================================================*/
@ -372,22 +375,32 @@
/* [4] Process state /* [4] Process state
=========================================================*/ =========================================================*/
/* (1) LOCKED (last = lock) /* (1) IF DETACHED (never initialized)
---------------------------------------------------------*/ ---------------------------------------------------------*/
if( count($history) > 0 && $history[0]['id_action'] == $action['lock'] ) /* (1) Check machine data (no token | unlock code) */
return [ 'state' => 'locked' ]; 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' ]; return [ 'state' => 'stopped' ];
/* (3) SIGNALED (start|stop ..... signal) /* (3) LOCKED (last = lock)
---------------------------------------------------------*/ ---------------------------------------------------------*/
if( count($history) > 0 && $history[0]['id_action'] == $action['signal'] ) if( $history[0]['id_action'] == $action['lock'] )
return [ 'state' => 'signaled' ]; 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 */ /* (1) If (start|stop), continue to search */
if( in_array($history[$c]['id_action'] , [$action['start'], $action['stop']]) ) if( in_array($history[$c]['id_action'] , [$action['start'], $action['stop']]) )
@ -397,24 +410,16 @@
else if( $history[$c]['id_action'] == $action['signal'] ) else if( $history[$c]['id_action'] == $action['signal'] )
return [ 'state' => 'signaled' ]; return [ 'state' => 'signaled' ];
/* (4) STARTED (last state) /* (6.1) STARTED (last state)
---------------------------------------------------------*/ ---------------------------------------------------------*/
else else
return [ 'state' => 'started' ]; 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' ];
return [ 'state' => 'started' ];
/* (5) DETACHED (no state)
---------------------------------------------------------*/
return [ 'state' => 'detached' ];
} }

View File

@ -21,6 +21,7 @@
* *
*/ */
public static function create($id_user, $id_machine, $id_action, $timestamp){ public static function create($id_user, $id_machine, $id_action, $timestamp){
/* [1] On retourne l'id_history ou FALSE si erreur /* [1] On retourne l'id_history ou FALSE si erreur
=========================================================*/ =========================================================*/
$inserted = Table::get('history')->insert([ $inserted = Table::get('history')->insert([
@ -77,6 +78,58 @@
/* RETOURNE UNE ENTREE SPECIFIQUE
*
* @id_machine<int> UID de la machine
*
* @return entry<Array> 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<int> UID de l'utilisateur
*
* @return entry<Array> 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 /* RETOURNE UNE ENTREE SPECIFIQUE
* *
* @id_history<int> UID de l'entree * @id_history<int> UID de l'entree
@ -89,10 +142,9 @@
/* [1] On rédige/execute la requête /* [1] On rédige/execute la requête
=========================================================*/ =========================================================*/
$user = Table::get('user') $user = Table::get('user')
->whereIdWarehouse($id_warehouse) ->whereId($id_history)
->whereIdUser($id_user) ->orderby('timestamp', Rows::ORDER_DESC)
->select('*') ->select('*');
->unique();
return $user->fetch(); return $user->fetch();
} }

View File

@ -177,17 +177,30 @@
* *
*/ */
public static function getById($id_warehouse, $id_machine){ public static function getById($id_warehouse, $id_machine){
/* [1] On dige/execute la requête /* [1] On cupère la machine
=========================================================*/ =========================================================*/
/* (1) Write request */
$machine = Table::get('machine') $machine = Table::get('machine')
->whereId($id_machine) ->whereId($id_machine)
->whereIdWarehouse($id_warehouse) ->whereIdWarehouse($id_warehouse)
->select('id_machine') ->select('id_machine')
->select('name') ->select('name')
->select('token')
->select('unlock_code')
->orderby('id_machine', Rows::ORDER_ASC) ->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){ public static function getByName($id_warehouse, $name){
/* [1] On dige/execute la requête /* [1] On cupère la machine
=========================================================*/ =========================================================*/
/* (1) Write request */
$machine = Table::get('machine') $machine = Table::get('machine')
->whereName($name) ->whereName($name)
->whereIdWarehouse($id_warehouse) ->whereIdWarehouse($id_warehouse)
->select('id_machine') ->select('id_machine')
->select('name') ->select('name')
->select('token')
->select('unlock_code')
->orderby('name', Rows::ORDER_ASC) ->orderby('name', Rows::ORDER_ASC)
->unique(); ->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){ public static function getByToken($id_warehouse, $token){
/* [1] On dige/execute la requête /* [1] On cupère la machine
=========================================================*/ =========================================================*/
/* (1) Write request */
$machine = Table::get('machine') $machine = Table::get('machine')
->whereToken($token) ->whereToken($token)
->whereIdWarehouse($id_warehouse) ->whereIdWarehouse($id_warehouse)
->select('id_machine') ->select('id_machine')
->select('name') ->select('name')
->select('token')
->select('unlock_code')
->orderby('name', Rows::ORDER_ASC) ->orderby('name', Rows::ORDER_ASC)
->unique(); ->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;
} }