Add validateOnly
This commit is contained in:
@@ -6,8 +6,10 @@ use common\components\DateUtil;
|
||||
use common\components\Helper;
|
||||
use common\components\StopWatch;
|
||||
use common\models\Card;
|
||||
use common\models\CardKeyAssignment;
|
||||
use common\models\DoorLog;
|
||||
use common\models\DoorLogForTest;
|
||||
use common\models\Key;
|
||||
use common\models\Log;
|
||||
use common\models\Ticket;
|
||||
use frontend\models\KeyToggleForm;
|
||||
@@ -26,8 +28,7 @@ class DoorManager extends BaseObject
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* @param $cardNumber 00000000: nyomogombos nyitás
|
||||
* @param $cardNumber 00000000: nyomogombos nyitás
|
||||
* @param $device string B(gomb)|Q(qrcode)|C(nfc),E(emergency)
|
||||
* @param $direction string IN|OUT
|
||||
* @param $verifyOnly boolean true: akkor csak lekerdezés, false: megtörtént a mozgás (ilyenkor vonunk le egy alkalmat)
|
||||
@@ -63,6 +64,21 @@ class DoorManager extends BaseObject
|
||||
$dateStr = DateUtil::formatDateUtc($date);
|
||||
|
||||
$doorLog = new DoorLog();
|
||||
if ( $device == 'E'){
|
||||
$direction = DoorLog::$DIRECTION_ALL_EMERGENCY;
|
||||
}else{
|
||||
switch ($direction) {
|
||||
case 'IN':
|
||||
$direction = DoorLog::$DIRECTION_IN ;
|
||||
break;
|
||||
case 'OUT':
|
||||
$direction = DoorLog::$DIRECTION_OUT;
|
||||
break;
|
||||
default:
|
||||
throw new BadRequestHttpException("Direction not supported: ".$direction);
|
||||
}
|
||||
|
||||
}
|
||||
$doorLog->direction = $direction;
|
||||
$doorLog->source_app = $device;
|
||||
$doorLog->created_at = $createdAtStr;
|
||||
@@ -70,31 +86,53 @@ class DoorManager extends BaseObject
|
||||
/**
|
||||
* emergency- no card needed
|
||||
*/
|
||||
if ($direction == DoorLog::$DIRECTION_ALL_EMERGENCY) {
|
||||
$doorLog->save(false);
|
||||
Log::log(
|
||||
[
|
||||
'type' => Log::$TYPE_INFO,
|
||||
'message' => 'Ajtó nyitás: vészhelyzet',
|
||||
'id_door_log' => $doorLog->id_door_log
|
||||
]
|
||||
);
|
||||
\Yii::$app->response->statusCode = 204;
|
||||
\Yii::error("emergency event in sec " . $stopWatch->split());
|
||||
if ($device == 'E' ) {
|
||||
if (!$verifyOnly) {
|
||||
$doorLog->save(false);
|
||||
Log::log(
|
||||
[
|
||||
'type' => Log::$TYPE_INFO,
|
||||
'message' => 'Ajtó nyitás: vészhelyzet',
|
||||
'id_door_log' => $doorLog->id_door_log
|
||||
]
|
||||
);
|
||||
\Yii::$app->response->statusCode = 204;
|
||||
\Yii::error("emergency event in sec " . $stopWatch->split());
|
||||
}
|
||||
\Yii::$app->db->transaction->commit();
|
||||
return;
|
||||
}
|
||||
|
||||
$card = Card::readCard(Helper::fixAsciiChars($cardNumber));
|
||||
\Yii::error("card loaded in sec " . $stopWatch->split());
|
||||
|
||||
/*
|
||||
* in any other cases, the door needs a card
|
||||
*/
|
||||
if (!isset($card)) {
|
||||
throw new BadRequestHttpException('Card not found with number: ' . $cardNumber);
|
||||
|
||||
|
||||
// if device is qr code
|
||||
if ( $device == 'Q'){
|
||||
|
||||
// allow only virtual key
|
||||
$keyAssignment = CardKeyAssignment::findOne(['virtual_key' => $cardNumber]);
|
||||
if (!isset($keyAssignment)){
|
||||
throw new BadRequestHttpException("Virtual key not found: ". $cardNumber);
|
||||
}
|
||||
$card = Card::findOne($keyAssignment->id_card);
|
||||
if ( $card != null ){
|
||||
$card = Card::readCard($card->number);
|
||||
}
|
||||
if ( $card == null ){
|
||||
throw new BadRequestHttpException("Card not found by virtual key: ". $cardNumber.'/'.$keyAssignment->id_card);
|
||||
}
|
||||
\Yii::error("virtual key loaded in sec " . $stopWatch->split());
|
||||
} else{
|
||||
$card = Card::readCard(Helper::fixAsciiChars($cardNumber));
|
||||
\Yii::error("Card loaded in sec " . $stopWatch->split());
|
||||
if (!isset($card)) {
|
||||
throw new BadRequestHttpException('Card not found with number: ' . $cardNumber);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$doorLog->id_card = $card->id_card;
|
||||
$doorLog->card_flag = $card->flag;
|
||||
/**
|
||||
@@ -102,9 +140,11 @@ class DoorManager extends BaseObject
|
||||
* Free to enter/leave
|
||||
*/
|
||||
if ($card->type == Card::TYPE_EMPLOYEE) {
|
||||
$doorLog->save(false);
|
||||
\Yii::$app->response->statusCode = 204;
|
||||
\Yii::error("employee card {$card->id_card} event finished in sec " . $stopWatch->split());
|
||||
if (!$verifyOnly) {
|
||||
$doorLog->save(false);
|
||||
\Yii::$app->response->statusCode = 204;
|
||||
\Yii::error("employee card {$card->id_card} event finished in sec " . $stopWatch->split());
|
||||
}
|
||||
\Yii::$app->db->transaction->commit();
|
||||
return;
|
||||
}
|
||||
@@ -134,8 +174,10 @@ class DoorManager extends BaseObject
|
||||
$doorLog->id_customer = $customer->id_customer;
|
||||
\Yii::error("customer {$customer->id_customer} loaded in sec " . $stopWatch->split());
|
||||
|
||||
// save the door log
|
||||
$doorLog->save(false);
|
||||
if (!$verifyOnly) {
|
||||
// save the door log
|
||||
$doorLog->save(false);
|
||||
}
|
||||
\Yii::error("door log {$doorLog->id_door_log} saved in sec " . $stopWatch->split());
|
||||
|
||||
// if direction is in
|
||||
@@ -154,6 +196,10 @@ class DoorManager extends BaseObject
|
||||
}
|
||||
|
||||
$countDoorLogsForTicketSince = $this->getCountDoorLogsForTicketSince($ticket->id_ticket, DateUtil::utcDate(clone $date));
|
||||
if (!$verifyOnly) {
|
||||
// if verifyOnly, the actual doorLog is not saved, so we hate to increase the count artificially
|
||||
$countDoorLogsForTicketSince = $countDoorLogsForTicketSince + 1;
|
||||
}
|
||||
\Yii::error("count of door logs '{$countDoorLogsForTicketSince}' loaded in sec " . $stopWatch->split());
|
||||
|
||||
// if the current event is the first door log today
|
||||
@@ -161,15 +207,17 @@ class DoorManager extends BaseObject
|
||||
// increase the ticket usage count with 1
|
||||
$usageCount = $ticket->usage_count;
|
||||
$ticket->usage_count += 1;
|
||||
$ticket->save(false);
|
||||
Log::log(
|
||||
[
|
||||
'type' => Log::$TYPE_TICKET_USAGE_FIRST,
|
||||
'message' => 'Bérlet használat(előtte: ' . $usageCount . ' -> utána: ' . $ticket->usage_count,
|
||||
'id_ticket' => $ticket->id_ticket,
|
||||
'id_door_log' => $doorLog->id_door_log
|
||||
]
|
||||
);
|
||||
if (!$verifyOnly) {
|
||||
$ticket->save(false);
|
||||
Log::log(
|
||||
[
|
||||
'type' => Log::$TYPE_TICKET_USAGE_FIRST,
|
||||
'message' => 'Bérlet használat(előtte: ' . $usageCount . ' -> utána: ' . $ticket->usage_count,
|
||||
'id_ticket' => $ticket->id_ticket,
|
||||
'id_door_log' => $doorLog->id_door_log
|
||||
]
|
||||
);
|
||||
}
|
||||
\Yii::error("Ticket usage count increased after first doorlog of day in sec " . $stopWatch->split());
|
||||
|
||||
} else {
|
||||
@@ -223,7 +271,9 @@ class DoorManager extends BaseObject
|
||||
|
||||
if ($logCountInActiveInterval == 1) {
|
||||
$ticket->usage_count = $ticket->usage_count + 1;
|
||||
$ticket->save(false);
|
||||
if (!$verifyOnly) {
|
||||
$ticket->save(false);
|
||||
}
|
||||
\Yii::error("Ticket usage count increased after first IN after first door_log in interval in sec " . $stopWatch->split());
|
||||
|
||||
}
|
||||
@@ -243,27 +293,31 @@ class DoorManager extends BaseObject
|
||||
}
|
||||
|
||||
$ticket->count_move_out = $ticket->usage_count;
|
||||
$ticket->save(false);
|
||||
if (!$verifyOnly) {
|
||||
$ticket->save(false);
|
||||
}
|
||||
\Yii::error("direction_out: ticket count_move_out set after direction_out in sec " . $stopWatch->split());
|
||||
}
|
||||
|
||||
Card::updateCardFlagTicket($ticket->id_ticket);
|
||||
\Yii::error("updateCardFlagTicket: card flag updated in sec " . $stopWatch->split());
|
||||
if (!$verifyOnly) {
|
||||
Card::updateCardFlagTicket($ticket->id_ticket);
|
||||
\Yii::error("updateCardFlagTicket: card flag updated in sec " . $stopWatch->split());
|
||||
|
||||
// reload card after flag is set
|
||||
$card = Card::readCard($cardNumber);
|
||||
if ($card->type != Card::TYPE_EMPLOYEE) {
|
||||
if ($direction == DoorLog::$DIRECTION_OUT_WITHOUT_MOVE || $direction == DoorLog::$DIRECTION_OUT) {
|
||||
$card->flag_out = Helper::setBit($card->flag_out, Card::$FLAG_DOOR, true);
|
||||
$card->flag = Helper::setBit($card->flag, Card::$FLAG_DOOR, false);
|
||||
$card->save(false);
|
||||
\Yii::error("direction_out: Door flag updated in sec " . $stopWatch->split());
|
||||
// reload card after flag is set
|
||||
$card = Card::readCard($cardNumber);
|
||||
if ($card->type != Card::TYPE_EMPLOYEE) {
|
||||
if ($direction == DoorLog::$DIRECTION_OUT_WITHOUT_MOVE || $direction == DoorLog::$DIRECTION_OUT) {
|
||||
$card->flag_out = Helper::setBit($card->flag_out, Card::$FLAG_DOOR, true);
|
||||
$card->flag = Helper::setBit($card->flag, Card::$FLAG_DOOR, false);
|
||||
$card->save(false);
|
||||
\Yii::error("direction_out: Door flag updated in sec " . $stopWatch->split());
|
||||
|
||||
} else if ($direction == DoorLog::$DIRECTION_IN || $direction == DoorLog::$DIRECTION_IN_WITHOUT_MOVE) {
|
||||
$card->flag_out = Helper::setBit($card->flag_out, Card::$FLAG_DOOR, false);
|
||||
$card->flag = Helper::setBit($card->flag, Card::$FLAG_DOOR, true);
|
||||
$card->save(false);
|
||||
\Yii::error("direction_in: Card flag updated in sec " . $stopWatch->split());
|
||||
} else if ($direction == DoorLog::$DIRECTION_IN || $direction == DoorLog::$DIRECTION_IN_WITHOUT_MOVE) {
|
||||
$card->flag_out = Helper::setBit($card->flag_out, Card::$FLAG_DOOR, false);
|
||||
$card->flag = Helper::setBit($card->flag, Card::$FLAG_DOOR, true);
|
||||
$card->save(false);
|
||||
\Yii::error("direction_in: Card flag updated in sec " . $stopWatch->split());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,7 +383,7 @@ class DoorManager extends BaseObject
|
||||
['id_card' => $card->id_card]
|
||||
);
|
||||
// todo: revoke all assigned key
|
||||
$this->revokeKey($cardNumber,"f100");
|
||||
$this->revokeKey($cardNumber, "f100");
|
||||
}
|
||||
|
||||
public function getLogs($cardNumber)
|
||||
|
||||
Reference in New Issue
Block a user