@@ -29,70 +29,55 @@ exports.init = function() {
2929 } ) ;
3030} ;
3131
32- // Private
33- exports . _populateRemotes = function ( result ) {
34- console . log ( "_populateRemotes" , result ) ;
32+ // Parse the list of remotes that irsend knows about
33+ exports . _populateRemotes = function ( irsendResult ) {
3534 var remotes = { } ;
3635
37- // list comes back with newlines, so split into an array
38- var results = result [ 1 ] . split ( '\n' ) ;
39-
40- results . forEach ( function ( remote ) {
36+ irsendResult [ 1 ] . split ( '\n' ) . forEach ( function ( remote ) {
4137 var remoteName = remote . match ( / \s ( .* ) $ / ) ;
4238 if ( remoteName ) remotes [ remoteName [ 1 ] ] = [ ] ;
4339 } ) ;
4440
45- console . log ( "_populateRemotes" , remotes ) ;
4641 return remotes ;
4742} ;
4843
49- exports . _populateCommands = function ( remotes ) {
50- console . log ( "_populateCommands" , remotes ) ;
51-
44+ // Given object whose keys represent remotes, get commands for each remote
45+ // Returns promise that will resolve when all irsend invocations complete
46+ exports . _populateCommands = function ( remotesObject ) {
5247 var commandPromises = [ ] ;
53- var remotes = Object . keys ( remotes ) ;
48+ var remoteNames = Object . keys ( remotesObject ) ;
5449
55- // Get the list of all commands for each remote
56- remotes . forEach ( function ( remote ) {
50+ remoteNames . forEach ( function ( remote ) {
5751 commandPromises . push ( exports . irsend . list ( remote , '' )
58- . then ( function ( result ) {
59- var ret = { } ;
60- ret [ remote ] = exports . _parseCommands ( result ) ;
61- return ret ;
52+ . then ( function ( irsendResult ) {
53+ return { [ remote ] : exports . _parseCommands ( irsendResult ) }
6254 } )
6355 ) ;
6456 } )
6557
66- // commandPromises should look like this:
67- // [{ TV: ['Power', 'VolUp', 'VolDown'] }, { Yamaha: ['Power', 'Xbox360'] }]
68- //
69- // Final response should look like this:
70- // { TV: ['Power', 'VolUp', 'VolDown'], Yamaha: ['Power', 'Xbox360'] }
7158 return Promise . all ( commandPromises )
72- . then ( function ( commands ) {
73- var ret = { } ;
59+ . then ( exports . _joinRemoteCommands ) ;
60+ } ;
7461
75- commands . forEach ( function ( command ) {
76- ret = Object . assign ( ret , command ) ;
77- } ) ;
62+ // Merges an array of remoteCommands into a single object
63+ exports . _joinRemoteCommands = function ( remoteCommands ) {
64+ var remotes = { } ;
7865
79- console . log ( "_populateCommands" , ret ) ;
80- return ret ;
66+ remoteCommands . forEach ( function ( remote ) {
67+ remotes = Object . assign ( remotes , remote ) ;
8168 } ) ;
69+
70+ return remotes ;
8271} ;
8372
84- exports . _parseCommands = function ( result ) {
85- console . log ( " _parseCommands" , result ) ;
73+ // Parses the results of irsend for a specifif remote
74+ exports . _parseCommands = function ( irsendResult ) {
8675 var commands = [ ] ;
8776
88- // Change multiline string into array to parse
89- var results = result [ 1 ] . split ( '\n' ) ;
90-
91- results . forEach ( function ( command ) {
77+ irsendResult [ 1 ] . split ( '\n' ) . forEach ( function ( command ) {
9278 var commandName = command . match ( / \s .* \s ( .* ) $ / ) ;
9379 if ( commandName && commandName [ 1 ] ) commands . push ( commandName [ 1 ] ) ;
9480 } ) ;
9581
96- console . log ( "_parseCommands" , commands ) ;
9782 return commands ;
9883} ;
0 commit comments