doormanager fixes after refactoring

This commit is contained in:
Schneider Roland 2023-02-19 13:31:42 +01:00
parent fe0dd00145
commit b40d0ccf05
5 changed files with 96 additions and 18 deletions

View File

@ -4,14 +4,14 @@ namespace common\components;
use yii\base\BaseObject;
class DoorMoveContext extends BaseObject {
public $id;
public $requestId;
public $identifier;
public $device;
public $direction;
public $originalDirection;
public $verifyOnly;
public $created_at;
public $createdAt;
public $date;
public $card;

View File

@ -44,8 +44,8 @@ class DoorManager extends BaseObject
{
$requestId = uniqid("", false);
$context = new DoorMoveContext([
'id' => $requestId,
'$identifier' => $identifier,
'requestId' => $requestId,
'identifier' => $identifier,
'device' => $device,
'direction' => $direction,
'originalDirection' => $direction,
@ -70,7 +70,7 @@ class DoorManager extends BaseObject
$createdAt = DateUtil::utcDateTime();
}
$context->created_at = $createdAt;
$context->createdAt = $createdAt;
if (isset($date)) {
$date = DateUtil::parseDateTime($date);
@ -153,14 +153,25 @@ class DoorManager extends BaseObject
$context->error = true;
$context->exception = $e->originalException;
$context->errorCode = $e->errorCode;
if ( $e->type == FitnessException::TYPE_BAD_REQUEST){
throw new BadRequestHttpException($e->getMessage());
} else {
throw new ServerErrorHttpException($e->getMessage());
}
// throw $e->originalException;
// throw new BadRequestHttpException();
} catch (\Exception $e) {
$context->error = true;
$context->exception = $e;
$context->errorCode = "UNKNOWN";
throw $e;
} finally {
// do logging
$this->logContext($context);
}
}
/**
@ -170,7 +181,7 @@ class DoorManager extends BaseObject
function logContext($ctx){
try{
$result = [
'requestId' => $ctx->id,
'requestId' => $ctx->requestId,
'identifier' => $ctx->identifier,
'verifyOnly' => $ctx->verifyOnly,
'device' => $ctx->device,
@ -185,7 +196,7 @@ class DoorManager extends BaseObject
'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->idVirtualKey : null,
'idVirtualKey' => isset($ctx->virtualKey) ? $ctx->virtualKey->id : null,
'kind' => $ctx->kind,
'actions' => implode(",",$ctx->actions),
'error' => $ctx->error,
@ -195,14 +206,14 @@ class DoorManager extends BaseObject
if ( $ctx->error === true ){
if ( isset($ctx->exception )){
$result['errorMessage'] = substr( $ctx->exception->message,0,100);
$result['errorMessage'] = substr( $ctx->exception->getMessage(),0,100);
}
}
\Yii::info("door log: " . implode(";", $result));
}catch (\Exception $e){
\Yii::error("Failed to log door context");
\Yii::error("Failed to log door context:". $e->getMessage());
}
}
@ -547,9 +558,9 @@ class DoorManager extends BaseObject
);
}
if (isset($virtualKey)) {
if (!isset($virtualKey->direction_in_at)) {
if (isset($ctx->virtualKey)) {
\Yii::info("Move OUT: Virtual key set");
if (!isset($ctx->virtualKey->direction_in_at)) {
throw new FitnessException(
"$ctx->requestId: Virtual key: move out without move in",
FitnessException::TYPE_BAD_REQUEST,
@ -558,7 +569,7 @@ class DoorManager extends BaseObject
);
}
if (isset($virtualKey->direction_out_at)) {
if (isset($ctx->virtualKey->direction_out_at)) {
throw new FitnessException(
"$ctx->requestId: Virtual key: already move out",
FitnessException::TYPE_BAD_REQUEST,
@ -567,10 +578,10 @@ class DoorManager extends BaseObject
);
}
$virtualKey->direction_out_at = Helper::getDateTimeString();
$ctx->virtualKey->direction_out_at = Helper::getDateTimeString();
if (!$ctx->verifyOnly) {
$virtualKey->save(false);
$ctx->virtualKey->save(false);
}
}
@ -606,11 +617,11 @@ class DoorManager extends BaseObject
\Yii::$app->db->transaction->commit();
\Yii::info("$ctx->requestId: Commited");
} catch (FitnessException $fe) {
\Yii::$app->db->transaction->rollBack();
$this->doRollback();
\Yii::info("$ctx->requestId: rollbacked");
throw $fe;
} catch (\Exception $e) {
\Yii::$app->db->transaction->rollBack();
$this->doRollback();
\Yii::info("$ctx->requestId: rollbacked");
throw new FitnessException(
"UNKNOWN_ERROR",
@ -623,6 +634,12 @@ class DoorManager extends BaseObject
}
}
function doRollback(){
if ( isset(\Yii::$app->db->transaction ) && \Yii::$app->db->transaction->isActive ){
\Yii::$app->db->transaction->rollBack();
}
}
function getActiveInterval($intervals, $date)
{
foreach ($intervals as $interval) {

View File

@ -1,2 +1,2 @@
FITNESS_REST_ALLOW_VERIFY_ONLY=true
DOOR_ENTRY_STRATEGY=door_pass
DOOR_ENTRY_STRATEGY=door_pass111

View File

@ -0,0 +1,61 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Ajtó teszt</h1>
<div>
<h2>Bejelentkezés</h2>
Kártyaszám: <input id="loginCardNumber">
Eszköznév: <input id="loginCardNumber">
</div>
<div>
<h2>Mozgás</h2>
<form>
<div>
Token
<input value="" id="token">
</div>
<div>
Irány:
<select name="direction">
<option value="out">Out</option>
<option value="in">In</option>
</select>
</div>
<div>
Eszköz:
<select name="device">
<option value="c">Card</option>
<option value="q">Qrcode</option>
</select>
</div>
<div>
Eszköz azonosító:
<input>
</div>
</form>
<button onclick="move()">
Mozgás
</button>
<div id="response">
</div>
</div>
<script>
function move(){
alert('ok');
const token = document.getElementById('token').value;
const dir = document.getElementById('direction').value;
}
</script>
</body>
</html>