221 lines
8.1 KiB
PHP
221 lines
8.1 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/libs/WebSocketEndpoint.php';
|
|
require_once __DIR__ . '/libs/ConnectionRegistry.php';
|
|
require_once __DIR__ . '/libs/OCPPFrameRouter.php';
|
|
|
|
class OCPP_Server extends IPSModule
|
|
{
|
|
private const ATTR_CONNECTIONS = 'Connections';
|
|
|
|
public function Create()
|
|
{
|
|
parent::Create();
|
|
|
|
$this->RegisterPropertyBoolean('EnableWebhook', true);
|
|
$this->RegisterPropertyString('HookPath', '/hook/ocpp');
|
|
$this->RegisterPropertyInteger('DefaultTargetInstance', 0);
|
|
$this->RegisterPropertyString('Ladepunkte', json_encode([]));
|
|
$this->RegisterPropertyInteger('HeartbeatSeconds', 30);
|
|
$this->RegisterPropertyInteger('DebugLevel', 0);
|
|
|
|
$this->RegisterAttributeString(self::ATTR_CONNECTIONS, ConnectionRegistry::toJson(ConnectionRegistry::empty()));
|
|
|
|
$this->RegisterVariableString('TransportStatus', 'TransportStatus', '', 10);
|
|
$this->RegisterVariableString('LastInboundFrame', 'LastInboundFrame', '', 20);
|
|
$this->RegisterVariableString('LastOutboundFrame', 'LastOutboundFrame', '', 21);
|
|
$this->RegisterVariableString('LastRouteResult', 'LastRouteResult', '', 22);
|
|
$this->RegisterVariableInteger('ConnectionCount', 'ConnectionCount', '', 30);
|
|
$this->RegisterVariableInteger('LastMessageTime', 'LastMessageTime', '', 31);
|
|
$this->RegisterVariableString('WebSocketSupportStatus', 'WebSocketSupportStatus', '', 40);
|
|
$this->RegisterVariableString('LetzteMeldung', 'LetzteMeldung', '', 41);
|
|
$this->RegisterVariableInteger('LetzteMeldungZeit', 'LetzteMeldungZeit', '', 42);
|
|
|
|
$this->RegisterTimer('Timer_TransportWatchdog', 30000, 'IPS_RequestAction(' . $this->InstanceID . ', "TransportWatchdog", "");');
|
|
|
|
$this->SetValue('TransportStatus', 'Scaffold');
|
|
$this->SetValue('WebSocketSupportStatus', 'Nicht geprueft');
|
|
$this->SetValue('LetzteMeldung', 'OCPP Server Scaffold initialisiert');
|
|
}
|
|
|
|
public function ApplyChanges()
|
|
{
|
|
parent::ApplyChanges();
|
|
|
|
$this->SetTimerInterval('Timer_TransportWatchdog', max(5, $this->ReadPropertyInteger('HeartbeatSeconds')) * 1000);
|
|
$summary = WebSocketEndpoint::supportSummary(method_exists($this, 'RegisterHook'), $this->ReadPropertyString('HookPath'));
|
|
$this->SetValue('WebSocketSupportStatus', $summary['status'] . ': ' . $summary['detail']);
|
|
$this->SetSummary($summary['status']);
|
|
|
|
if ($this->ReadPropertyBoolean('EnableWebhook')) {
|
|
$this->tryRegisterHook();
|
|
}
|
|
|
|
$this->SetStatus(102);
|
|
}
|
|
|
|
public function RequestAction($Ident, $Value)
|
|
{
|
|
switch ($Ident) {
|
|
case 'RegisterHook':
|
|
$this->tryRegisterHook();
|
|
break;
|
|
|
|
case 'QueueOutboundFrame':
|
|
$this->QueueOutboundFrame((string)$Value);
|
|
break;
|
|
|
|
case 'RouteInboundFrame':
|
|
$this->RouteInboundFrame((string)$Value);
|
|
break;
|
|
|
|
case 'TransportWatchdog':
|
|
$this->TransportWatchdog();
|
|
break;
|
|
|
|
case 'ClearBuffers':
|
|
$this->SetValue('LastInboundFrame', '');
|
|
$this->SetValue('LastOutboundFrame', '');
|
|
$this->SetValue('LastRouteResult', '');
|
|
$this->setMessage('Puffer geloescht');
|
|
break;
|
|
|
|
default:
|
|
throw new Exception('Invalid Ident');
|
|
}
|
|
}
|
|
|
|
protected function ProcessHookData($JSONString = '')
|
|
{
|
|
$raw = WebSocketEndpoint::readRawBody();
|
|
if ($raw === '' && is_string($JSONString)) {
|
|
$raw = $JSONString;
|
|
}
|
|
|
|
$path = $_SERVER['REQUEST_URI'] ?? $this->ReadPropertyString('HookPath');
|
|
$chargePointId = (new OCPPFrameRouter())->extractChargePointId((string)$path);
|
|
|
|
$this->RouteInboundFrame(json_encode([
|
|
'ChargePointId' => $chargePointId,
|
|
'Frame' => $raw,
|
|
'Remote' => ($_SERVER['REMOTE_ADDR'] ?? '') . ':' . ($_SERVER['REMOTE_PORT'] ?? '')
|
|
]));
|
|
|
|
header('Content-Type: application/json');
|
|
echo json_encode([
|
|
'status' => 'accepted',
|
|
'note' => 'OCPP transport scaffold. Produktiver WebSocket-Dauerbetrieb muss mit Station verifiziert werden.'
|
|
]);
|
|
}
|
|
|
|
public function QueueOutboundFrame(string $json): void
|
|
{
|
|
$this->SetValue('LastOutboundFrame', $json);
|
|
$this->SetValue('LastMessageTime', time());
|
|
$this->setMessage('Outbound Frame vorgemerkt. Aktiver WebSocket-Sendekanal ist noch Scaffold.');
|
|
}
|
|
|
|
public function RouteInboundFrame(string $json): void
|
|
{
|
|
$data = json_decode($json, true);
|
|
if (!is_array($data)) {
|
|
$data = [
|
|
'ChargePointId' => '',
|
|
'Frame' => $json,
|
|
'Remote' => ''
|
|
];
|
|
}
|
|
|
|
$chargePointId = (string)($data['ChargePointId'] ?? '');
|
|
$frame = (string)($data['Frame'] ?? '');
|
|
$remote = (string)($data['Remote'] ?? '');
|
|
|
|
$this->SetValue('LastInboundFrame', $frame);
|
|
$this->SetValue('LastMessageTime', time());
|
|
|
|
$connections = ConnectionRegistry::touch(
|
|
ConnectionRegistry::fromJson($this->ReadAttributeString(self::ATTR_CONNECTIONS)),
|
|
$chargePointId,
|
|
$remote
|
|
);
|
|
$this->WriteAttributeString(self::ATTR_CONNECTIONS, ConnectionRegistry::toJson($connections));
|
|
$this->SetValue('ConnectionCount', count($connections['connections']));
|
|
|
|
$routes = json_decode($this->ReadPropertyString('Ladepunkte'), true);
|
|
if (!is_array($routes)) {
|
|
$routes = [];
|
|
}
|
|
|
|
$target = (new OCPPFrameRouter())->route(
|
|
$routes,
|
|
$chargePointId,
|
|
1,
|
|
1,
|
|
$this->ReadPropertyInteger('DefaultTargetInstance')
|
|
);
|
|
|
|
$this->SetValue('LastRouteResult', json_encode([
|
|
'chargePointId' => $chargePointId,
|
|
'target' => $target,
|
|
'timestamp' => time()
|
|
]));
|
|
|
|
if ($target > 0 && IPS_InstanceExists($target)) {
|
|
IPS_RequestAction($target, 'HandleInboundFrame', $frame);
|
|
$this->setMessage('Inbound Frame an Zielinstanz ' . $target . ' geroutet.');
|
|
return;
|
|
}
|
|
|
|
$this->setMessage('Inbound Frame empfangen, aber keine Zielinstanz gefunden.');
|
|
}
|
|
|
|
public function TransportWatchdog(): void
|
|
{
|
|
$last = (int)$this->GetValue('LastMessageTime');
|
|
if ($last === 0) {
|
|
$this->SetValue('TransportStatus', 'Wartet auf OCPP Verbindung');
|
|
return;
|
|
}
|
|
|
|
$age = time() - $last;
|
|
if ($age > max(90, 3 * $this->ReadPropertyInteger('HeartbeatSeconds'))) {
|
|
$this->SetValue('TransportStatus', 'Timeout');
|
|
$this->setMessage('Transport-Watchdog Timeout nach ' . $age . ' Sekunden.');
|
|
return;
|
|
}
|
|
|
|
$this->SetValue('TransportStatus', 'Aktiv/Scaffold');
|
|
}
|
|
|
|
private function tryRegisterHook(): void
|
|
{
|
|
$hook = $this->ReadPropertyString('HookPath');
|
|
if (method_exists($this, 'RegisterHook')) {
|
|
try {
|
|
$this->RegisterHook($hook);
|
|
$this->SetValue('WebSocketSupportStatus', 'RegisterHook aufgerufen fuer ' . $hook . '. WebSocket-Dauerbetrieb noch testen.');
|
|
$this->setMessage('Webhook registriert: ' . $hook);
|
|
return;
|
|
} catch (Throwable $e) {
|
|
$this->SetValue('WebSocketSupportStatus', 'RegisterHook Fehler: ' . $e->getMessage());
|
|
$this->setMessage('Webhook konnte nicht registriert werden.');
|
|
return;
|
|
}
|
|
}
|
|
|
|
$this->SetValue('WebSocketSupportStatus', 'RegisterHook nicht verfuegbar. WebHook Control manuell pruefen.');
|
|
$this->setMessage('RegisterHook nicht verfuegbar.');
|
|
}
|
|
|
|
private function setMessage(string $message): void
|
|
{
|
|
$this->SetValue('LetzteMeldung', $message);
|
|
$this->SetValue('LetzteMeldungZeit', time());
|
|
if ($this->ReadPropertyInteger('DebugLevel') > 0) {
|
|
$this->SendDebug('OCPP_Server', $message, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|