-
Notifications
You must be signed in to change notification settings - Fork 1
/
MediaRoomStbApi.php
149 lines (131 loc) · 3.68 KB
/
MediaRoomStbApi.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
//This library requires sockets.
if (!extension_loaded('sockets')) {
die('The sockets extension is not loaded.');
}
/**
* ----------------------------------------
* @title Media Room STB Remote API
* @desc API to control Media Room STBs
* @author Daniel Sousa
* ----------------------------------------
* */
class MediaRoomStbApi {
/* Constants of the button codes */
const BTN_BACK = 8;
const BTN_OK = 13;
const BTN_EXIT = 27;
const BTN_PROG_UP = 33;
const BTN_PROG_DOWN = 34;
const BTN_MENU = 36;
const BTN_LEFT = 37;
const BTN_UP = 38;
const BTN_RIGHT = 39;
const BTN_DOWN = 40;
const BTN_DELETE = 46;
const BTN_NUMBER_0 = 48;
const BTN_NUMBER_1 = 49;
const BTN_NUMBER_2 = 50;
const BTN_NUMBER_3 = 51;
const BTN_NUMBER_4 = 52;
const BTN_NUMBER_5 = 53;
const BTN_NUMBER_6 = 54;
const BTN_NUMBER_7 = 55;
const BTN_NUMBER_8 = 56;
const BTN_NUMBER_9 = 57;
const BTN_SEARCH = 106;
const BTN_GUIDE = 112;
const BTN_FAVORITES = 113;
const BTN_VOD = 114;
const BTN_DVR = 115;
const BTN_TELETEXT = 116;
const BTN_PREV = 117;
const BTN_RWD = 118;
const BTN_FWD = 121;
const BTN_NEXT = 122;
const BTN_RED = 140;
const BTN_GREEN = 141;
const BTN_YELLOW = 142;
const BTN_BLUE = 143;
const BTN_SWITCH = 156;
const BTN_OPTIONS = 157;
const BTN_INFO = 159;
const BTN_MUTE = 173;
const BTN_VOL_DOWN = 174;
const BTN_VOL_UP = 175;
const BTN_STOP = 178;
const BTN_PLAYPAUSE = 179;
const BTN_REC = 225;
const BTN_POWER = 233;
/* ---------------------- */
/* Private Variables */
private $socket;
private $ipAddress;
private $port;
private $connected;
/**
* The constructor, requires the IP Address and optionally the port.
* @param string $ipAddress The IP Address of the STB0
* @param string $port Optionally the Port. Default is 8082.
*/
public function __construct($ipAddress, $port = 8082) {
$this->ipAddress = $ipAddress;
$this->port = $port;
$this->connected = FALSE;
}
/**
* The Destructor
*/
function __destruct() {
$this->stbDisconnect();
}
/**
* Tries to connect to the STB.
* @return boolean Successfully connected or Not
*/
function stbConnect() {
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($this->socket === FALSE) {
$this->connected = FALSE;
return FALSE;
}
$result = socket_connect($this->socket, $this->ipAddress, $this->port);
if ($result === FALSE) {
$this->connected = FALSE;
return FALSE;
}
$hello = socket_read($this->socket, 2048, PHP_NORMAL_READ);
if (strpos($hello, 'hello') === FALSE) {
return FALSE;
}
$this->connected = TRUE;
return TRUE;
}
/**
* Sends a key to the connected STB.
* @param string $key The key to send
* @return boolean Success or not
*/
function sendStbCommand($key) {
if (!$this->connected){
return FALSE;
}
//Send the key=<keynumber>\0\n command.
socket_write($this->socket, "key=$key\0\n");
//Receive the reply. It should be a ok
$reply = socket_read($this->socket, 2048, PHP_NORMAL_READ);
if (strpos($reply, 'ok') === FALSE) {
return FALSE;
}
return TRUE;
}
/**
* Closes the connection to the STB.
*/
function stbDisconnect() {
if ($this->connected) {
socket_close($this->socket);
$this->connected = FALSE;
}
}
}