-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
tv.sh
executable file
·275 lines (247 loc) · 7.7 KB
/
tv.sh
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/bash
set -o nounset
set -o errexit
ip="${_TV_IP:-}"
if [[ $ip = "" ]]; then
echo "Please set up IP for your TV."
echo ""
echo "Usage:"
echo "export _TV_IP=\"192.169.0.10\" && ./tv.sh <optional-command-to-execute-directly>";
exit 1;
fi
IP="https://$ip:1926/6"
function show_menu() {
NORMAL=`echo "\033[m"`
MENU=`echo "\033[36m"`
NUMBER=`echo "\033[33m"`
FGRED=`echo "\033[41m"`
RED_TEXT=`echo "\033[31m"`
ENTER_LINE=`echo "\033[33m"`
echo -e ""
echo -e "${RED_TEXT} Channels: ${NORMAL}"
echo -e "${MENU} ${NUMBER} 1)${MENU} Returns all installed channels ${NORMAL}"
echo -e "${MENU} ${NUMBER} 2)${MENU} Returns the current channel ${NORMAL}"
echo -e "${MENU} ${NUMBER} 3)${MENU} Changes the current channel step up ${NORMAL}"
echo -e "${MENU} ${NUMBER} 4)${MENU} Changes the current channel step down ${NORMAL}"
echo -e "${RED_TEXT} Audio: ${NORMAL}"
echo -e "${MENU} ${NUMBER} 5)${MENU} Returns the current volume ${NORMAL}"
echo -e "${MENU} ${NUMBER} 6)${MENU} Changes the current volume step up ${NORMAL}"
echo -e "${MENU} ${NUMBER} 7)${MENU} Changes the current volume step down ${NORMAL}"
echo -e "${RED_TEXT} Ambilight: ${NORMAL}"
echo -e "${MENU} ${NUMBER} 8)${MENU} Returns the number of layers and the number of pixels on each side ${NORMAL}"
echo -e "${MENU} ${NUMBER} 9)${MENU} Returns the ambilight mode ${NORMAL}"
echo -e "${MENU} ${NUMBER} 10)${MENU} Returns the ambilight colours stored in the cache ${NORMAL}"
echo -e "${RED_TEXT} System: ${NORMAL}"
echo -e "${MENU} ${NUMBER} 11)${MENU} Returns all system settings ${NORMAL}"
echo -e "${MENU} ${NUMBER} 12)${MENU} Stand by ${NORMAL}"
echo -e "${MENU} ${NUMBER} 13)${MENU} Send GET command ${NORMAL}"
echo -e "${MENU} ${NUMBER} 14)${MENU} Send POST command ${NORMAL}"
echo -e ""
echo -e "${ENTER_LINE}Please enter a menu option or ${RED_TEXT}press enter to exit. ${NORMAL}"
read opt
}
function option_picked() {
COLOR='\033[01;31m'
RESET='\033[00;00m'
MESSAGE=${@:-"${RESET}Error: No message passed"}
echo -e "${COLOR}${MESSAGE}${RESET}"
echo ""
}
function randomString() {
xxd -l16 -ps /dev/urandom | base64 | cut -c1-32
}
function signature() {
echo -n "$1" | openssl dgst -sha1 -hmac "$2" -binary | base64
}
function deviceSpecJson() {
echo "{ \"app_id\": \"gapp.id\", \"id\":\"$1\", \"device_name\" : \"heliotrope\", \"device_os\" : \"Android\", \"app_name\" : \"ApplicationName\", \"type\" : \"native\" }"
}
function pair() {
deviceId=$(randomString 16)
device=$(deviceSpecJson $deviceId)
data="{ \"device\": $device, \"scope\": [\"read\", \"write\", \"control\"] }"
response=$(curl -s -k -X POST "$IP/pair/request" --data "$data")
auth_key=$(echo $response | jq .auth_key | tr -d '"')
timestamp=$(echo $response | jq .timestamp | tr -d '"')
timeout=$(echo $response | jq .timeout | tr -d '"')
echo "Please enter 4 digit PIN code from your TV:"
read pin
secret_key="ZmVay1EQVFOaZhwQ4Kv81ypLAZNczV9sG4KkseXWn1NEk6cXmPKO/MCa9sryslvLCFMnNe4Z4CPXzToowvhHvA=="
auth_timestamp="$timestamp$pin"
case "$OSTYPE" in
linux*) auth_key_s=$(echo $secret_key | base64 -d) ;;
darwin*) auth_key_s=$(echo $secret_key | base64 -D) ;;
*) auth_key_s=$(echo $secret_key | base64 -d) ;;
esac
signature=$(signature $auth_key_s $auth_timestamp)
auth="{\"device\":{\"device_name\":\"heliotrope\",\"device_os\":\"Android\",\"app_name\":\"ApplicationName\",\"type\":\"native\",\"app_id\":\"app.id\",\"id\":\"$deviceId\"},\"auth\":{\"auth_AppId\":\"1\",\"pin\":$pin,\"auth_timestamp\":\"$auth_timestamp\",\"auth_signature\":\"$signature\"}}"
response=$(curl -k -s --digest --user $deviceId:$auth_key -X POST "$IP/pair/grant" --data "$auth")
echo "$deviceId:$auth_key" > .credentials.tv
}
function cmd() {
if [ ! -f .credentials.tv ]
then
pair
fi
auth=$(cat .credentials.tv)
command=$(curl -k -s --digest --user $auth -X GET "$IP/$1" )
echo $command
}
function cmdPost() {
if [ ! -f .credentials.tv ]
then
pair
fi
auth=$(cat .credentials.tv)
command=$(curl -k -s --digest --user $auth -X POST "$IP/input/key" -d "{\"key\":\"$1\"}" )
echo "{\"status\":\"OK\"}"
}
if [ $# -eq 1 ]
then
case $1 in
"allChannels")
cmd "channeldb/tv/channelLists/all"
;;
"currentChannel")
cmd "activities/tv"
;;
"changeChannel")
echo "@todo"
#cmdPost "activities/tv"
#config['body'] = {"channel":{"ccid": args.value },"channelList":{"id":"allcab","version":"9"}}"
;;
"channelUp")
cmdPost "ChannelStepUp"
;;
"channelDown")
cmdPost "ChannelStepDown"
;;
"volume")
cmd "audio/volume"
;;
"volumeUp")
cmdPost "VolumeUp"
;;
"volumeDown")
cmdPost "VolumeDown"
;;
"ambilightConfig")
cmd "ambilight/currentconfiguration"
;;
"ambilightTopology")
cmd "ambilight/topology"
;;
"ambilightCache")
cmd "ambilight/cached"
;;
"systemInfo")
cmd "system"
;;
"standby")
cmdPost "Standby"
;;
"getCommand")
command="${_TV_COMMAND:-}"
while [ -z "$command" ]; do read -p "Which command? " command; done
cmd "$command"
;;
"postCommand")
command="${_TV_COMMAND:-}"
while [ -z "$command" ]; do read -p "Which command? " command; done
cmdPost "$command"
;;
*)
echo "Invalid argument. Possible arguments: allChannels currentChannel channelUp channelDown volume volumeUp volumeDown ambilightConfig ambilightTopology ambilightCache systemInfo getCommand postCommand"
exit 1
;;
esac
exit 0;
fi
if [ $# -gt 1 ]
then
echo "Usage: ./tv.sh <optional-command-to-execute-directly>";
exit 1;
fi
clear
show_menu
while [ opt != '' ]
do
if [[ $opt = "" ]]; then
exit;
else
clear
case $opt in
1)
cmd "channeldb/tv/channelLists/all"
show_menu
;;
2)
cmd "activities/tv"
show_menu
;;
3)
cmdPost "ChannelStepUp"
show_menu
;;
4)
cmdPost "ChannelStepDown"
show_menu
;;
5)
cmd "audio/volume"
show_menu
;;
6)
cmdPost "VolumeUp"
show_menu
;;
7)
cmdPost "VolumeDown"
show_menu
;;
8)
cmd "ambilight/currentconfiguration"
show_menu
;;
9)
cmd "ambilight/topology"
show_menu
;;
10)
cmd "ambilight/cached"
show_menu
;;
11)
cmd "system"
show_menu
;;
12)
cmdPost "Standby"
show_menu
;;
13)
command=''
while [ -z "$command" ]; do read -p "Which command? " command; done
cmd "$command"
show_menu
;;
14)
command=''
while [ -z "$command" ]; do read -p "Which command? " command; done
cmdPost "$command"
show_menu
;;
q)
exit
;;
\n)
exit
;;
*)
clear
option_picked "Pick an option from the menu"
show_menu
;;
esac
fi
done