-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChargePoint.php
More file actions
105 lines (84 loc) · 3.87 KB
/
Copy pathChargePoint.php
File metadata and controls
105 lines (84 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
require __DIR__ . '/../../../vendor/autoload.php';
if (!class_exists('WebSocket\Client')) {
throw new Exception("phrity/websocket is required for examples. Run: composer require phrity/websocket");
}
use SolutionForest\OcppPhp\Ocpp\JsonSchemaValidator;
use SolutionForest\OcppPhp\Ocpp\v16\Calls;
function chargerPoingCallback($client)
{
echo "Charge Point: Connected to Central System\n\n";
$boot = new Calls\BootNotification();
$boot->chargePointVendor = 'MyVendor';
$boot->chargePointModel = 'MyModel';
$heartbeat = new Calls\Heartbeat();
$statusNotification = new Calls\StatusNotification();
$statusNotification->connectorId = 1;
$statusNotification->status = 'Available';
$statusNotification->errorCode = 'NoError';
$notImplemented = new Calls\ClearCache();
$messages = [
$boot,
$heartbeat,
$statusNotification,
$notImplemented
];
$message = $messages[rand(0, count($messages) - 1)];
JsonSchemaValidator::validate($message, 'v1.6');
$message = $message->toArray();
echo "Charge Point: Sending message: " . json_encode($message) . "\n\n";
$client->text(json_encode($message));
}
echo "# Random client\n";
// Initiate client.
while (true) {
// Server options specified or random
$options = array_merge([
'uri' => 'ws://127.0.0.1:8080',
'timeout' => rand(1, 60),
'framesize' => rand(1, 4096) * 8,
], getopt('', ['uri:', 'timeout:', 'framesize:', 'debug']));
try {
$client = new WebSocket\Client($options['uri']);
$client
->addMiddleware(new \WebSocket\Middleware\CloseHandler())
->addMiddleware(new \WebSocket\Middleware\PingResponder())
;
// If debug mode and logger is available
if (isset($options['debug']) && class_exists('WebSocket\Test\EchoLog')) {
$client->setLogger(new \WebSocket\Test\EchoLog());
echo "# Using logger\n";
}
if (isset($options['timeout'])) {
$client->setTimeout($options['timeout']);
echo "# Set timeout: {$options['timeout']}\n";
}
if (isset($options['framesize'])) {
$client->setFrameSize($options['framesize']);
echo "# Set frame size: {$options['framesize']}\n";
}
echo "# Listening on {$options['uri']}\n";
$client->onHandshake(function ($server, $connection, $request, $response) {
echo "> [{$connection->getRemoteName()}] Server connected {$response->getStatusCode()}\n";
chargerPoingCallback($server);
})->onDisconnect(function ($client, $connection) {
echo "> [{$connection->getRemoteName()}] Server disconnected\n";
})->onText(function ($client, $connection, $message) {
echo "> [{$connection->getRemoteName()}] Received [{$message->getOpcode()}] {$message->getContent()}\n";
echo "Charge Point: Received message\n\n";
})->onBinary(function ($client, $connection, $message) {
echo "> [{$connection->getRemoteName()}] Received [{$message->getOpcode()}]\n";
})->onPing(function ($client, $connection, $message) {
echo "> [{$connection->getRemoteName()}] Received [{$message->getOpcode()}]\n";
})->onPong(function ($client, $connection, $message) {
echo "> [{$connection->getRemoteName()}] Received [{$message->getOpcode()}]\n";
})->onClose(function ($client, $connection, $message) {
echo "> [{$connection->getRemoteName()}] Received [{$message->getOpcode()}] {$message->getCloseStatus()}\n";
})->onError(function ($client, $connection, $exception) {
$name = $connection ? "[{$connection->getRemoteName()}]" : "[-]";
echo "> {$name} Error: {$exception->getMessage()}\n";
})->start();
} catch (Throwable $e) {
echo "> ERROR: {$e->getMessage()}\n";
}
}