-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.php
97 lines (93 loc) · 3.75 KB
/
demo.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
<?php
use iggyvolz\buttplug\Connection;
use iggyvolz\buttplug\DeviceInfo;
use iggyvolz\buttplug\Message\ConnectedEvent;
use iggyvolz\buttplug\Message\DeviceAdded;
use iggyvolz\buttplug\Message\DeviceRemoved;
use iggyvolz\buttplug\Message\ServerInfo;
use Iggyvolz\SimpleAttributeReflection\AttributeReflection;
use League\Event\EventDispatcher;
use League\Event\ListenerRegistry;
use League\Event\ListenerSubscriber;
use Revolt\EventLoop;
use function Amp\async;
use function Amp\delay;
require_once __DIR__ . "/vendor/autoload.php";
#[Attribute(Attribute::TARGET_METHOD)]
final readonly class EventListener
{
}
class Listeners implements ListenerSubscriber {
public function __construct(private readonly string $clientName)
{
}
private ?Connection $connection = null;
private ?ServerInfo $serverInfo = null;
/**
* @var array<int,DeviceInfo>
*/
private array $deviceInfos = [];
#[EventListener]
public function onConnected(ConnectedEvent $connectedEvent): void
{
echo "Connected!\n";
$this->connection = $connectedEvent->connection;
$this->serverInfo = $this->connection->requestServerInfo($this->clientName);
echo "Hello from " . $this->serverInfo->serverName . "!\n";
foreach($this->connection->requestDeviceList()->devices as $deviceInfo) {
echo "Existing device added: $deviceInfo->deviceName\n";
$this->deviceInfos[$deviceInfo->deviceIndex] = $deviceInfo;
$this->handleDeviceAdd($deviceInfo);
}
echo "Now scanning for devices...\n";
$this->connection->startScanning();
}
#[EventListener]
public function onDeviceAdded(DeviceAdded $event): void
{
$deviceInfo = $event->device;
echo "Device added: $deviceInfo->deviceName\n";
$this->deviceInfos[$deviceInfo->deviceIndex] = $deviceInfo;
$this->handleDeviceAdd($deviceInfo);
}
private function handleDeviceAdd(DeviceInfo $deviceInfo): void
{
echo "$deviceInfo->deviceName added!\nMessages:\n";
foreach($deviceInfo->deviceMessages as $messageType => $messages) {
echo " $messageType:\n";
foreach($messages as $message) {
echo " Feature Descriptor: " . json_encode($message->featureDescriptor) . "\n";
echo " Step Count: " . json_encode($message->stepCount) . "\n";
echo " Actuator Type: " . json_encode($message->actuatorType) . "\n";
echo " Sensor Type: " . json_encode($message->sensorType) . "\n";
echo " Sensor Range: " . json_encode($message->sensorRange) . "\n";
echo " Endpoints: " . json_encode($message->endpoints) . "\n";
}
}
}
#[EventListener]
public function onDeviceRemoved(DeviceRemoved $event): void
{
$device = $this->deviceInfos[$event->deviceIndex];
echo "Device removed: {$device->deviceName}\n";
unset($this->deviceInfos[$event->deviceIndex]);
}
public function subscribeListeners(ListenerRegistry $acceptor): void
{
foreach ((new ReflectionClass(self::class))->getMethods() as $method) {
if(AttributeReflection::getAttribute($method, EventListener::class) !== null) {
$acceptor->subscribeOnceTo($method->getParameters()[0]->getType()->getName(), function(object $e) use($method) {
async(function() use($method, $e){
$this->{$method->getName()}($e);
});
});
}
}
}
}
async(function(){
$eventHandler = new EventDispatcher();
$eventHandler->subscribeListenersFrom(new Listeners("buttplug-php"));
Connection::connect("ws://127.0.0.1:12345", $eventHandler);
});
EventLoop::run();