-
Notifications
You must be signed in to change notification settings - Fork 5
/
GoodWeConnector.php
55 lines (42 loc) · 1.49 KB
/
GoodWeConnector.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
<?php
namespace GoodWe;
class GoodWeConnector
{
const BROADCAST_MESSAGE = 'aa55c07f0102000241';
const USAGE_MESSAGE = '7f0375940049d5c2';
const INFO_MESSAGE = '7f03753100280409';
const PORT = 8899;
protected $socket;
public function __construct()
{
if (!($this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
socket_set_option($this->socket,SOL_SOCKET, SO_RCVTIMEO, ['sec' => 1, 'usec' => 0]);
}
public function sendMessage($message, $ip, $port = self::PORT)
{
$message = hex2bin($message);
if (!socket_sendto($this->socket, $message, strlen($message), 0, $ip, $port)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not send data: [$errorcode] $errormsg \n");
}
if (socket_recv($this->socket,$reply, 2048, MSG_WAITALL) === FALSE) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not receive data: [$errorcode] $errormsg \n");
}
return $reply;
}
public function sendUsageMessage($ip)
{
return $this->sendMessage(self::USAGE_MESSAGE, $ip);
}
public function getSerial($ip)
{
return $this->sendMessage(self::INFO_MESSAGE, $ip);
}
}