-
Notifications
You must be signed in to change notification settings - Fork 39
/
webInterface.php
163 lines (152 loc) · 4.41 KB
/
webInterface.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* ----------------------------------------
* Practical - PHP LG SmartTV API
* ----------------------------------------
* https://github.com/SteveWinfield/PHP-LG-SmartTV
**/
session_start();
include 'smartTV.php';
/**
* Create instance of TV
* @param IP Address of TV
* (optional) @param Port of TV (default is 8080)
**/
$tv = new SmartTV('192.168.2.103'); // new SmartTV('192.168.2.103', 8080)
/**
* Check if session already created.
* If so.. don't request a new one.
**/
if (!isset($_SESSION['SESSION_ID'])) {
/**
* Set pairing key (if you don't know the pairing key
* execute the method ..->displayPairingKey() and it will
* be shown on your tv)
* @param Key
**/
$tv->setPairingKey(678887); // $tv->displayPairingKey();
/**
* Authenticate to the tv
* @except Login fails (wrong pairing key?)
**/
try {
$_SESSION['SESSION_ID'] = $tv->authenticate();
} catch (Exception $e) {
die('Authentication failed, I am sorry.');
}
} else {
$tv->setSession($_SESSION['SESSION_ID']);
}
if (isset($_GET['cmd'])) {
switch($_GET['cmd']) {
case 'screen':
header('Content-Type: image/jpeg');
exit ($tv->queryData(TV_INFO_SCREEN));
break;
case 'changeChannel':
if (!isset($_GET['value'])) {
exit;
}
$channelName = strtolower($_GET['value']);
foreach ($_SESSION['CHANNEL_LIST'] as $channel) {
if (strtolower($channel['chname']) == $channelName) {
$tv->processCommand(TV_CMD_CHANGE_CHANNEL, $channel);
break;
}
}
break;
case 'info':
$currentChannel = $tv->queryData(TV_INFO_CURRENT_CHANNEL);
$text = 'You\'re watching <b>'.$currentChannel->progName.'</b> on <b>'.$currentChannel->chname.'</b>';
if (!isset($_SESSION['CURRENT_CHANNEL']) || strtolower($currentChannel->chname) != strtolower($_SESSION['CURRENT_CHANNEL'])) {
$text .= '___---___Change channel: <select id="programList">';
if (!isset($_SESSION['CHANNEL_LIST'])) {
$_SESSION['CHANNEL_LIST'] = array();
foreach ($tv->queryData(TV_INFO_CHANNEL_LIST) as $channel) {
$_SESSION['CHANNEL_LIST'][] = (array)$channel;
}
}
foreach ($_SESSION['CHANNEL_LIST'] as $channel) {
$text .= '<option value="'.$channel['chname'].'" '.(strtolower($channel['chname']) == strtolower($currentChannel->chname) ? 'selected' : '').'>'.$channel['chname'].'</option>';
}
$text .= '</select>';
$_SESSION['CURRENT_CHANNEL'] = (string)$currentChannel->chname;
}
exit ($text);
break;
case 'keyPress':
if (!isset($_GET['value'])) {
exit;
}
$codes = array(
'up' => TV_CMD_UP,
'down' => TV_CMD_DOWN,
'left' => TV_CMD_LEFT,
'right' => TV_CMD_RIGHT,
'enter' => TV_CMD_OK,
'esc' => TV_CMD_EXIT,
'backspace' => TV_CMD_BACK,
'h' => TV_CMD_HOME_MENU
);
$tv->processCommand($codes[$_GET['value']]);
break;
}
exit;
} else {
unset ($_SESSION['CURRENT_CHANNEL']);
}
?>
<!doctype html>
<html lang="de">
<head>
<title>SmartTV Test</title>
</head>
<body>
<p>Here you can control your TV with your arrow keys.</p>
<img src='index.php?cmd=screen&t=12345' id='liveImage' />
<p>
<p id="channelInfo"></p>
<p id="programInfo"></p>
</p>
<p>
<strong>ENTER</strong> - OK<br />
<strong>ESC</strong> - EXIT<br />
<strong>BACKSPACE</strong> - BACK<br />
<strong>H</strong> - HOME</br>
</p>
<!-- SCRIPTS !-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(document).keydown(function (e) {
var keyCode = e.keyCode || e.which,
CHAR = {8 : 'backspace', 27 : 'esc', 13 : 'enter', 37 : 'left', 38 : 'up', 39 : 'right', 40 : 'down', 72 : 'h' };
if (CHAR[keyCode] !== "undefined") {
$.get("index.php?cmd=keyPress&value=" + CHAR[keyCode]);
e.preventDefault();
}
});
function refreshImage() {
$("#liveImage").attr("src", "index.php?cmd=screen&t="+(new Date()).getTime());
}
function refreshData() {
$.get("index.php?cmd=info", function (data) {
var lData = data.split("___---___");
$("#channelInfo").html(lData[0]);
if (lData.length > 1) {
$("#programInfo").html(lData[1]);
$("#programList").on('change', function() {
$.get("index.php?cmd=changeChannel&value=" + this.value);
});
}
});
}
refreshData();
refreshImage();
setInterval(refreshImage, 500);
setInterval(refreshData, 1000);
});
</script>
<!-- END !-->
</body>
</html>