|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Jobs; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Log; |
| 6 | +use SolutionForest\OcppPhp\Ocpp\Exceptions\NotImplementedError; |
| 7 | +use SolutionForest\OcppPhp\Ocpp\JsonSchemaValidator; |
| 8 | +use SolutionForest\OcppPhp\Ocpp\Messages\CallError; |
| 9 | +use VladimirYuldashev\LaravelQueueRabbitMQ\Queue\Jobs\RabbitMQJob; |
| 10 | + |
| 11 | +class OcppMessageProxy extends RabbitMQJob |
| 12 | +{ |
| 13 | + protected $actionName = ''; |
| 14 | + |
| 15 | + /** |
| 16 | + * Fire the job - dispatches to specific OCPP action jobs. |
| 17 | + * |
| 18 | + * @return void |
| 19 | + */ |
| 20 | + public function fire() |
| 21 | + { |
| 22 | + $payload = $this->payload(); |
| 23 | + |
| 24 | + dump('OcppMessageJob received:', $payload); |
| 25 | + |
| 26 | + // OCPP CALL format: [messageTypeId, messageId, action, payload] |
| 27 | + if (! is_array($payload) || count($payload) < 3) { |
| 28 | + Log::error('Invalid OCPP message format', ['payload' => $payload]); |
| 29 | + $this->delete(); |
| 30 | + return false; |
| 31 | + } |
| 32 | + |
| 33 | + [$messageType, $messageId, $action] = $payload; |
| 34 | + $data = $payload[3] ?? []; |
| 35 | + |
| 36 | + // Only handle CALL messages (2); ignore CALLRESULT/CALLERROR |
| 37 | + if ($messageType !== 2) { |
| 38 | + Log::debug('Ignoring non-CALL message', ['messageType' => $messageType]); |
| 39 | + $this->delete(); |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + // Resolve Call Class from Library |
| 44 | + $callClass = "SolutionForest\\OcppPhp\\Ocpp\\v16\\Calls\\{$action}"; |
| 45 | + |
| 46 | + if (!class_exists($callClass)) { |
| 47 | + Log::warning("Unknown OCPP Action (Library class not found): {$action}"); |
| 48 | + $error = new NotImplementedError($messageId); |
| 49 | + $response = $error->toArray(); |
| 50 | + dump('OcppMessageJob response (NotImplemented - Unknown Action):', $response); |
| 51 | + Log::info('OCPP Response ready to send', ['response' => $response]); |
| 52 | + $this->delete(); |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + // Instantiate and hydrate Call object for validation |
| 57 | + /** @var \SolutionForest\OcppPhp\Ocpp\Messages\Call $callObject */ |
| 58 | + $callObject = new $callClass(); |
| 59 | + $callObject->messageId = $messageId; |
| 60 | + foreach ($data as $key => $value) { |
| 61 | + $callObject->$key = $value; |
| 62 | + } |
| 63 | + |
| 64 | + // Validate incoming OCPP message using built-in JSON schema validator |
| 65 | + $validationResult = JsonSchemaValidator::validate($callObject, 'v1.6', false); |
| 66 | + |
| 67 | + // If validation fails, validator already built a proper CallError |
| 68 | + if ($validationResult instanceof CallError) { |
| 69 | + Log::warning("OCPP message validation failed for action: {$action}", [ |
| 70 | + 'messageId' => $messageId, |
| 71 | + 'errorCode' => $validationResult->errorCode, |
| 72 | + 'errorDescription' => $validationResult->errorDescription, |
| 73 | + ]); |
| 74 | + $response = $validationResult->toArray(); |
| 75 | + } |
| 76 | + |
| 77 | + // Build job class name: App\Jobs\Ocpp\v16\{Action} |
| 78 | + $jobClass = "App\\Jobs\\Ocpp\\v16\\{$action}"; |
| 79 | + |
| 80 | + if (!class_exists($jobClass)) { |
| 81 | + Log::warning("No job found for action: {$action}, sending NotImplemented"); |
| 82 | + $error = new NotImplementedError($messageId); |
| 83 | + $response = $error->toArray(); |
| 84 | + } |
| 85 | + |
| 86 | + // Instantiate and execute the specific OCPP job |
| 87 | + $job = app($jobClass); |
| 88 | + $response = $job->handle($messageId, $data); |
| 89 | + |
| 90 | + if ($response) { |
| 91 | + dump('OcppMessageJob response:', $response); |
| 92 | + Log::info('OCPP Response ready to send', ['response' => $response]); |
| 93 | + |
| 94 | + $this->respond($response); |
| 95 | + } |
| 96 | + |
| 97 | + $this->delete(); |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + public function getName() |
| 102 | + { |
| 103 | + return $this->actionName; |
| 104 | + } |
| 105 | + |
| 106 | + public function respond($response) |
| 107 | + { |
| 108 | + $broker = $this->getRabbitMQ(); |
| 109 | + $message = $this->getRabbitMQMessage(); |
| 110 | + |
| 111 | + if ($message->has('reply_to')) { |
| 112 | + $broker->getChannel()->basic_publish( |
| 113 | + new \PhpAmqpLib\Message\AMQPMessage( |
| 114 | + json_encode($response), |
| 115 | + ['correlation_id' => $message->get('correlation_id') ?? ''] |
| 116 | + ), |
| 117 | + 'amq.topic', |
| 118 | + $message->get('reply_to') |
| 119 | + ); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments