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; use yii\base\BaseObject;
class DoorMoveContext extends BaseObject { class DoorMoveContext extends BaseObject {
public $id; public $requestId;
public $identifier; public $identifier;
public $device; public $device;
public $direction; public $direction;
public $originalDirection; public $originalDirection;
public $verifyOnly; public $verifyOnly;
public $created_at; public $createdAt;
public $date; public $date;
public $card; public $card;

View File

@ -44,8 +44,8 @@ class DoorManager extends BaseObject
{ {
$requestId = uniqid("", false); $requestId = uniqid("", false);
$context = new DoorMoveContext([ $context = new DoorMoveContext([
'id' => $requestId, 'requestId' => $requestId,
'$identifier' => $identifier, 'identifier' => $identifier,
'device' => $device, 'device' => $device,
'direction' => $direction, 'direction' => $direction,
'originalDirection' => $direction, 'originalDirection' => $direction,
@ -70,7 +70,7 @@ class DoorManager extends BaseObject
$createdAt = DateUtil::utcDateTime(); $createdAt = DateUtil::utcDateTime();
} }
$context->created_at = $createdAt; $context->createdAt = $createdAt;
if (isset($date)) { if (isset($date)) {
$date = DateUtil::parseDateTime($date); $date = DateUtil::parseDateTime($date);
@ -153,14 +153,25 @@ class DoorManager extends BaseObject
$context->error = true; $context->error = true;
$context->exception = $e->originalException; $context->exception = $e->originalException;
$context->errorCode = $e->errorCode; $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) { } catch (\Exception $e) {
$context->error = true; $context->error = true;
$context->exception = $e; $context->exception = $e;
$context->errorCode = "UNKNOWN"; $context->errorCode = "UNKNOWN";
throw $e;
} finally { } finally {
// do logging // do logging
$this->logContext($context); $this->logContext($context);
} }
} }
/** /**
@ -170,7 +181,7 @@ class DoorManager extends BaseObject
function logContext($ctx){ function logContext($ctx){
try{ try{
$result = [ $result = [
'requestId' => $ctx->id, 'requestId' => $ctx->requestId,
'identifier' => $ctx->identifier, 'identifier' => $ctx->identifier,
'verifyOnly' => $ctx->verifyOnly, 'verifyOnly' => $ctx->verifyOnly,
'device' => $ctx->device, 'device' => $ctx->device,
@ -185,7 +196,7 @@ class DoorManager extends BaseObject
'customerName' => isset($ctx->customer) ? $ctx->customer->name : null, 'customerName' => isset($ctx->customer) ? $ctx->customer->name : null,
'customerEmail' => isset($ctx->customer) ? $ctx->customer->email : null, 'customerEmail' => isset($ctx->customer) ? $ctx->customer->email : null,
'key' => isset($ctx->key) ? $ctx->key->name : 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, 'kind' => $ctx->kind,
'actions' => implode(",",$ctx->actions), 'actions' => implode(",",$ctx->actions),
'error' => $ctx->error, 'error' => $ctx->error,
@ -195,14 +206,14 @@ class DoorManager extends BaseObject
if ( $ctx->error === true ){ if ( $ctx->error === true ){
if ( isset($ctx->exception )){ 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)); \Yii::info("door log: " . implode(";", $result));
}catch (\Exception $e){ }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($ctx->virtualKey)) {
\Yii::info("Move OUT: Virtual key set");
if (!isset($virtualKey->direction_in_at)) { if (!isset($ctx->virtualKey->direction_in_at)) {
throw new FitnessException( throw new FitnessException(
"$ctx->requestId: Virtual key: move out without move in", "$ctx->requestId: Virtual key: move out without move in",
FitnessException::TYPE_BAD_REQUEST, 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( throw new FitnessException(
"$ctx->requestId: Virtual key: already move out", "$ctx->requestId: Virtual key: already move out",
FitnessException::TYPE_BAD_REQUEST, 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) { if (!$ctx->verifyOnly) {
$virtualKey->save(false); $ctx->virtualKey->save(false);
} }
} }
@ -606,11 +617,11 @@ class DoorManager extends BaseObject
\Yii::$app->db->transaction->commit(); \Yii::$app->db->transaction->commit();
\Yii::info("$ctx->requestId: Commited"); \Yii::info("$ctx->requestId: Commited");
} catch (FitnessException $fe) { } catch (FitnessException $fe) {
\Yii::$app->db->transaction->rollBack(); $this->doRollback();
\Yii::info("$ctx->requestId: rollbacked"); \Yii::info("$ctx->requestId: rollbacked");
throw $fe; throw $fe;
} catch (\Exception $e) { } catch (\Exception $e) {
\Yii::$app->db->transaction->rollBack(); $this->doRollback();
\Yii::info("$ctx->requestId: rollbacked"); \Yii::info("$ctx->requestId: rollbacked");
throw new FitnessException( throw new FitnessException(
"UNKNOWN_ERROR", "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) function getActiveInterval($intervals, $date)
{ {
foreach ($intervals as $interval) { foreach ($intervals as $interval) {

View File

@ -1,2 +1,2 @@
FITNESS_REST_ALLOW_VERIFY_ONLY=true 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>