-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WsListenCommand.php
114 lines (107 loc) · 3.52 KB
/
WsListenCommand.php
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
106
107
108
109
110
111
112
113
114
<?php
/*
* Author: WOLF
* Name: WsListenCommand.php
* Modified : ven., 23 févr. 2024 14:29
* Description: ...
*
* Copyright 2024 -[MR.WOLF]-[WS]-
*/
namespace MrWolfGb\Traccar\Commands;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use MrWolfGb\Traccar\Services\TraccarService;
use Symfony\Component\Console\Attribute\AsCommand;
//use WebSocket\Client;
//use WebSocket\ConnectionException;
/**
* @internal
*/
#[AsCommand(name: 'traccar:listen')]
class WsListenCommand extends Command
{
use ConfirmableTrait;
/**
* This just implementation to read ws with package "textalk/websocket": "^1.5"
*
* The command's signature.
*
* @var string
*/
public $signature = 'traccar:listen {--ws=* : Custom websocket host}
{--session-id=* : authenticated session ID}
{--token=* : use access token}
{--log : show console logs}';
/**
* The command's description.
*
* @var string
*/
public $description = 'Listen to traccar websocket server';
/**
* The console command name aliases.
*
* @var array<int, string>
*/
protected $aliases = ['traccar:ws-listen'];
/**
* Handle the command.
*/
public function handle(TraccarService $service): int
{
// $wsUrl = config('traccar.websocket_url');
// if ($this->option('ws')) {
// $wsUrl = $this->option('ws');
// }
// $sessionCookies = $service->sessionRepository()->getCookies();
//
// if ($this->option('session-id')) {
// $wsUrl .= '?session=' . $this->option('session-id');
// } elseif ($sessionCookies->getValue()) {
// $sessionID = explode('.', $sessionCookies->getValue())[0];
// $wsUrl .= '?session=' . $sessionID;
// } elseif ($this->option('token')) {
// $wsUrl .= '?token=' . $this->option('token');
// } elseif (!empty($service->getToken())) {
// $wsUrl .= '?token=' . $service->getToken();
// } else {
// $this->error("No session or token provided");
// return self::FAILURE;
// }
// $this->info("Connecting to: " . $wsUrl);
//
// $client = new Client(
// uri: $wsUrl,
// options: [
// 'filter' => ['text'],
// 'headers' => [
// "Cookie" => $sessionCookies->getName() . "=" . $sessionCookies->getValue()
// ],
// 'timeout' => 60,
// ],
// );
// while (true) {
// try {
// $data = $client->receive();
// if (strlen($data) > 2) {
// $dataObject = json_decode($data, true);
// if (array_key_exists('positions', $dataObject)) {
// $this->info("dispatch Event positions");
// } elseif (array_key_exists('events', $dataObject)) {
// $this->info("dispatch Event event");
// } else {
// $this->info("dispatch Event unknown");
// }
// if ($this->option('log')) {
// $this->info($data);
// }
// }
// } catch (ConnectionException $e) {
// $this->error($e->getMessage());
// break;
// }
// }
// $client->close();
return self::SUCCESS;
}
}