Files
Symcon_Belevo_Energiemanage…/Ladestation_OCPP/libs/OCPPMessage.php
T

127 lines
4.0 KiB
PHP

<?php
class OCPPMessage
{
public string $version;
public string $direction;
public string $action;
public string $uniqueId;
public array $payload;
public int $timestamp;
public string $chargePointId;
public int $messageTypeId;
public function __construct(
string $version,
string $direction,
string $action,
string $uniqueId,
array $payload,
string $chargePointId = '',
int $messageTypeId = 0
) {
$this->version = $version;
$this->direction = $direction;
$this->action = $action;
$this->uniqueId = $uniqueId;
$this->payload = $payload;
$this->timestamp = time();
$this->chargePointId = $chargePointId;
$this->messageTypeId = $messageTypeId;
}
public static function fromJson(string $json, string $version = 'auto', string $chargePointId = ''): ?self
{
$frame = json_decode($json, true);
if (!is_array($frame) || count($frame) < 3) {
return null;
}
$type = (int)($frame[0] ?? 0);
$uniqueId = (string)($frame[1] ?? '');
if ($type === 2) {
return new self($version, 'in', (string)($frame[2] ?? ''), $uniqueId, (array)($frame[3] ?? []), $chargePointId, $type);
}
if ($type === 3) {
return new self($version, 'in_result', 'CallResult', $uniqueId, (array)($frame[2] ?? []), $chargePointId, $type);
}
if ($type === 4) {
return new self($version, 'in_error', (string)($frame[2] ?? 'CallError'), $uniqueId, [
'errorCode' => (string)($frame[2] ?? ''),
'errorDescription' => (string)($frame[3] ?? ''),
'errorDetails' => (array)($frame[4] ?? [])
], $chargePointId, $type);
}
return null;
}
public static function call(string $action, array $payload, string $version = 'auto', string $chargePointId = ''): self
{
return new self($version, 'out', $action, self::newUniqueId(), $payload, $chargePointId, 2);
}
public static function callResult(string $uniqueId, array $payload, string $version = 'auto', string $chargePointId = ''): self
{
return new self($version, 'out_result', 'CallResult', $uniqueId, $payload, $chargePointId, 3);
}
public static function callError(string $uniqueId, string $code, string $description, array $details = [], string $version = 'auto', string $chargePointId = ''): self
{
return new self($version, 'out_error', $code, $uniqueId, [
'errorCode' => $code,
'errorDescription' => $description,
'errorDetails' => $details
], $chargePointId, 4);
}
public function toFrame(): array
{
if ($this->messageTypeId === 2) {
return [2, $this->uniqueId, $this->action, $this->payload];
}
if ($this->messageTypeId === 3) {
return [3, $this->uniqueId, $this->payload];
}
if ($this->messageTypeId === 4) {
return [
4,
$this->uniqueId,
(string)($this->payload['errorCode'] ?? $this->action),
(string)($this->payload['errorDescription'] ?? ''),
(array)($this->payload['errorDetails'] ?? [])
];
}
return [$this->messageTypeId, $this->uniqueId, $this->action, $this->payload];
}
public function toJson(): string
{
return json_encode($this->toFrame());
}
public function toArray(): array
{
return [
'version' => $this->version,
'direction' => $this->direction,
'action' => $this->action,
'uniqueId' => $this->uniqueId,
'payload' => $this->payload,
'timestamp' => $this->timestamp,
'chargePointId' => $this->chargePointId,
'messageTypeId' => $this->messageTypeId
];
}
private static function newUniqueId(): string
{
return str_replace('.', '', uniqid('ips-', true));
}
}
?>