forked from PSeitz/yamaha-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchainedCommands.js
120 lines (103 loc) · 3.74 KB
/
chainedCommands.js
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
var Promise = require("bluebird");
var xml2js = Promise.promisifyAll(require("xml2js"));
const LIST_SIZE = 8;
function topOfCurrentListCheck(currentLine) {
return (currentLine-1)%LIST_SIZE === 0;
}
function splitPathToArray(path) {
const pathArray = path.split('/');
let menuArray = [];
for (const menuName of pathArray) {
if (menuName) menuArray.push(menuName);
}
return menuArray;
}
function Yamaha()
{
}
// Navigates and selects the #number of the webradio favorites
Yamaha.prototype.switchToFavoriteNumber = function(favoritelistname, number){
var self = this;
return self.powerOn().then(function(){
self.setMainInputTo("NET RADIO").then( function(){
self.selectWebRadioListItem(1).then(function(){
self.whenMenuReady("NET_RADIO").then(function(){
return self.selectWebRadioListItem(number);
});
});
});
});
};
// unfinished - not working
Yamaha.prototype.switchToWebRadioWithName = function(name){
var self = this;
self.setMainInputTo("NET RADIO").then(function(){
self.getWebRadioList().then(xml2js.parseStringAsync).then(function(result){
console.log(result);
}, function (err) {
console.log("err "+err);
});
});
};
Yamaha.prototype.getIndexOfMenuItem = async function(item, listName) {
await this.jumpListItem(listName, 1);
list = await this.getWebRadioList()
while (true) {
const Current_List = list.YAMAHA_AV[listName][0].List_Info[0].Current_List[0];
let index = 1;
for (const key in Current_List) {
const itemName = Current_List[key][0]['Txt'][0];
if (itemName === item) {
return index;
}
index++
}
const Cursor_Position = list.YAMAHA_AV[listName][0].List_Info[0].Cursor_Position[0];
const currentLine = Number(Cursor_Position.Current_Line[0]);
if (!topOfCurrentListCheck) {
throw new Error('top of viewable list not under cursor');
}
const maxLine = Number(Cursor_Position.Max_Line[0]);
const nextCurrentLine = currentLine + LIST_SIZE;
if (nextCurrentLine > maxLine) {
break;
}
await this.jumpListItem(listName, nextCurrentLine);
list = await this.getWebRadioList();
}
return -1;
}
Yamaha.prototype.gotoFolder = async function(path, listName) {
const menuOrder = splitPathToArray(path);
await this.setMainInputTo(listName.replace('_', ' '));
let list = await this.getList(listName);
let menuName = list.getMenuName();
while (menuOrder.indexOf(menuName) < 0) {
if (list.getMenuLayer() <= 1) {
throw new Error('cannot gotoFolder, reached top of tree without finding known folder');
}
await this.remoteCursor('Return');
list = await this.getWebRadioList();
await this.jumpListItem(listName, 1);
menuName = list.getMenuName();
}
maxIters = 100;
iters = 0;
while(menuName !== menuOrder[menuOrder.length - 1]) {
iters++
if (iters > maxIters) {
throw new Error(`gotoFolder decend has run over 100 iterations`)
}
const currentLayer = menuOrder.indexOf(menuName);
const nextMenuItemName = menuOrder[currentLayer + 1];
list = await this.getWebRadioList();
const index = await this.getIndexOfMenuItem(nextMenuItemName, listName);
if (index < 0) {
throw new Error(`cannot find menu item "${nextMenuItemName}" in "${menuName}"`)
}
await this.selectListItem(listName, index);
list = await this.getWebRadioList();
menuName = list.getMenuName();
}
}
module.exports = Yamaha;