From 7c610b50ab18fae965bf55d1648ce57446c99b35 Mon Sep 17 00:00:00 2001 From: Schneider Roland Date: Mon, 20 Feb 2023 18:45:00 +0100 Subject: [PATCH] add door manager logs --- common/components/DoorMoveContext.php | 1 + common/manager/DoorManager.php | 78 ++++++++++---- common/models/DoorManagerLog.php | 67 ++++++++++++ ...9_200020_create_table_door_manager_log.php | 102 ++++++++++++++++++ door-api.http | 2 +- 5 files changed, 230 insertions(+), 20 deletions(-) create mode 100644 common/models/DoorManagerLog.php create mode 100644 console/migrations/m230209_200020_create_table_door_manager_log.php diff --git a/common/components/DoorMoveContext.php b/common/components/DoorMoveContext.php index d270b28..a26a11c 100644 --- a/common/components/DoorMoveContext.php +++ b/common/components/DoorMoveContext.php @@ -21,6 +21,7 @@ public $virtualKey; public $key; public $customer; public $ticket; +public $increasedTicketUsageCount; public $kind; diff --git a/common/manager/DoorManager.php b/common/manager/DoorManager.php index f2c9a48..a87728b 100644 --- a/common/manager/DoorManager.php +++ b/common/manager/DoorManager.php @@ -11,6 +11,7 @@ use common\models\Card; use common\models\CardKeyAssignment; use common\models\DoorLog; use common\models\DoorLogForTest; +use common\models\DoorManagerLog; use common\models\Key; use common\models\Log; use common\models\Ticket; @@ -138,6 +139,10 @@ class DoorManager extends BaseObject $context->virtualKey = $virtualKey; $context->card = $card; + $keyAssignment = CardKeyAssignment::findOne(['id_card' =>$card->id_card]); + if ( isset($keyAssignment)){ + $context->key = Key::findOne($keyAssignment->id_key); + } $context->cardNumber = $cardNumber; \Yii::info("$requestId: Card number " . $card->number); @@ -181,36 +186,67 @@ class DoorManager extends BaseObject function logContext($ctx){ try{ $result = [ - 'requestId' => $ctx->requestId, +//datetime $updated_at -- +//datetime $created_at -- +//string $ticket_usage + //string $request_id + 'request_id' => $ctx->requestId, + //string $identifier 'identifier' => $ctx->identifier, - 'verifyOnly' => $ctx->verifyOnly, + //bool $verify_only + 'verify_only' => $ctx->verifyOnly, + //string $device 'device' => $ctx->device, + //string $direction 'direction' => $ctx->direction, - 'originalDirection' => $ctx->originalDirection, - 'idCard' => isset($ctx->card)? $ctx->card->id_card : null, - 'cardNumber' => isset($ctx->card) ? $ctx->card->number : null, - 'idTicket' => isset($ctx->ticket) ? $ctx->ticket->id_ticket : null, - 'ticketType' => isset($ctx->ticket) ? $ctx->ticket->ticketType->name : null, - 'ticketUsageCount' => isset($ctx->ticket) ? $ctx->ticket->usage_count : null, - 'idCustomer' => isset($ctx->customer) ? $ctx->customer->id_customer : null, - 'customerName' => isset($ctx->customer) ? $ctx->customer->name : null, - 'customerEmail' => isset($ctx->customer) ? $ctx->customer->email : null, - 'key' => isset($ctx->key) ? $ctx->key->name : null, - 'idVirtualKey' => isset($ctx->virtualKey) ? $ctx->virtualKey->id : null, - 'kind' => $ctx->kind, - 'actions' => implode(",",$ctx->actions), + //string $original_direction + 'original_direction' => $ctx->originalDirection, + //integer $card_id_card + 'card_id_card' => isset($ctx->card)? $ctx->card->id_card : null, + //string $card_number + 'card_number' => isset($ctx->card) ? $ctx->card->number : null, + //string $ticket_id_ticket + 'ticket_id_ticket' => isset($ctx->ticket) ? $ctx->ticket->id_ticket : null, + //string $ticket_type_name + 'ticket_type_name' => isset($ctx->ticket) ? $ctx->ticket->ticketType->name : null, + //string $ticket_usage_count + 'ticket_usage_count' => isset($ctx->ticket) ? $ctx->ticket->usage_count : null, + //string $customer_id_customer + 'customer_id_customer' => isset($ctx->customer) ? $ctx->customer->id_customer : null, + //string $customer_name + 'customer_name' => isset($ctx->customer) ? $ctx->customer->name : null, + //string $customer_email + 'customer_email' => isset($ctx->customer) ? $ctx->customer->email : null, + //integer $key_id_key + 'key_id_key' => isset($ctx->key) ? $ctx->key->id_key : null, + //string $key_number + 'key_number' => isset($ctx->key) ? $ctx->key->number : null, + //integer $virtual_key_id + 'virtual_key_id' => isset($ctx->virtualKey) ? $ctx->virtualKey->id : null, + //string $validation_kind + 'validation_kind' => $ctx->kind, + //bool $error 'error' => $ctx->error, - 'errorCode' => $ctx->errorCode, - 'ticketUsage' => 1, + //string $error_code + 'error_code' => $ctx->errorCode, + 'ticket_usage' => $ctx->increasedTicketUsageCount, +// 'actions' => implode(",",$ctx->actions), ]; if ( $ctx->error === true ){ if ( isset($ctx->exception )){ - $result['errorMessage'] = substr( $ctx->exception->getMessage(),0,100); + // string $error_message + $result['error_message'] = substr( $ctx->exception->getMessage(),0,100); } } - \Yii::info("door log: " . implode(";", $result)); + \Yii::info("door log: " . implode(";", $result)); + + \Yii::$app->db->beginTransaction(); + $log = new DoorManagerLog($result); + $log->save(false); + \Yii::$app->db->transaction->commit(); + }catch (\Exception $e){ \Yii::error("Failed to log door context:". $e->getMessage()); @@ -444,6 +480,7 @@ class DoorManager extends BaseObject if ($countDoorLogsForTicketSince == 1) { // increase the ticket usage count with 1 $usageCount = $ticket->usage_count; + $ctx->increasedTicketUsageCount = true; $ticket->usage_count += 1; $ticket->save(false); \Yii::info("$ctx->requestId: First ticket usage today, increasing usage count for card: " . $ctx->card->id_card); @@ -515,6 +552,7 @@ class DoorManager extends BaseObject $logCountInActiveInterval = count($activeInterval['logs']); if ($logCountInActiveInterval == 1) { + $ctx->increasedTicketUsageCount = true; $ticket->usage_count = $ticket->usage_count + 1; $ticket->save(false); \Yii::info("$ctx->requestId: Ticket usage count increased after first IN after first door_log in interval in sec " . $stopWatch->split()); @@ -558,6 +596,8 @@ class DoorManager extends BaseObject ); } + + if (isset($ctx->virtualKey)) { \Yii::info("Move OUT: Virtual key set"); if (!isset($ctx->virtualKey->direction_in_at)) { diff --git a/common/models/DoorManagerLog.php b/common/models/DoorManagerLog.php new file mode 100644 index 0000000..afeb6da --- /dev/null +++ b/common/models/DoorManagerLog.php @@ -0,0 +1,67 @@ + TimestampBehavior::className(), + 'value' => function ($event) { + if ( isset($event->sender->created_at) ){ + return $event->sender->created_at; + } + return date('Y-m-d H:i:s'); + }, + 'updatedAtAttribute' => false, + ] + ], parent::behaviors()); + } + + + +} diff --git a/console/migrations/m230209_200020_create_table_door_manager_log.php b/console/migrations/m230209_200020_create_table_door_manager_log.php new file mode 100644 index 0000000..e3c893e --- /dev/null +++ b/console/migrations/m230209_200020_create_table_door_manager_log.php @@ -0,0 +1,102 @@ +db->driverName === 'mysql') { + // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $this->createTable('door_manager_log', [ + 'id_door_manager_log' => $this->primaryKey(), + 'created_at' => $this->datetime(), + 'updated_at' => $this->dateTime()->notNull(), + 'request_id' => $this->string(50), + 'identifier' => $this->string(100), + 'device' => $this->string(10), + 'direction' => $this->string(10), + 'original_direction' => $this->string(10), + 'verify_only' => $this->boolean(), + 'card_id_card' => $this->integer(), + 'card_number' => $this->string(50), + 'virtual_key_id' => $this->integer(), + 'key_id_key' => $this->integer(), + 'key_number' => $this->string(), + 'customer_id_customer' => $this->integer(), + 'customer_name' => $this->string(), + 'customer_email' => $this->string(), + 'ticket_id_ticket' => $this->string(), + 'ticket_usage_count' => $this->string(), + 'ticket_usage' => $this->integer(), + 'ticket_type_name' => $this->string(), + 'validation_kind' => $this->string(50), + 'error' => $this->boolean()->defaultValue(false), + 'error_code' => $this->string(50), + 'error_message' => $this->string(), + ], + $tableOptions + ); + + + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + echo "m230126_202055_create_table_door_card_pass cannot be reverted.\n"; + + return false; + } + + /* + // Use up()/down() to run migration code without a transaction. + public function up() + { + + } + + public function down() + { + echo "m230126_202055_create_table_door_card_pass cannot be reverted.\n"; + + return false; + } + */ +} diff --git a/door-api.http b/door-api.http index 2953ed3..41fbadf 100644 --- a/door-api.http +++ b/door-api.http @@ -61,7 +61,7 @@ Authorization: Basic ZG9vcl9zeXN0ZW06ZG9vcnN5c3RlbTE= "cardNumber": "123456", "direction": "IN", "device": "C", - "validateOnly": false + "validateOnly": true } ###