请选择 进入手机版 | 继续访问电脑版

技术控

    今日:4| 主题:61818
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Node.js Roku Remote

[复制链接]
大病猫c 发表于 2016-9-30 23:07:05
248 3

Node.js Roku Remote

Node.js Roku Remote-1-技术控-interested,techniques,streaming,something,providing
  I own an Apple TV 4, Apple TV 3, Roku 4, Chromecast, and a Firefox OS TV.  From that you can probably gather that I love streaming content, particularly sports and movies.  I obviously also love coding, which is why I loved being a Partner Engineer for Mozilla's Firefox OS TV -- I was enthusiastically testing out TV apps and exploring edge APIs and responsive techniques.
  I'm always interested in finding a way to do interesting stuff with JavaScript and streaming instantly hit me.  I can't do anything with a closed ecosystem Apple TV but there are known ways of working with a Roku so I set out to do something interesting with the Roku and Node.js -- create remote control functionality.
  Node.js Roku Remote

   There's a nice utility out there called node-roku which discovers Roku devices, providing the IP address of each Roku so that you can network with it.  The node-roku utility also provides an API to retrieve device information and app listing from the Roku.  I chose to create a script which, once started, allows the user to use their computer's keyboard to navigate around a Roku, select and launch apps, and more.
   Let's start with version 0.1.0 with the source code:
  1. const readline = require('readline');

  2. const request = require('request');
  3. const Roku = require('node-roku');
  4. const xml2json = require('xml2json');

  5. // Will be populated once a device is found
  6. var address;

  7. // Map to this URL: http://******:8060/keypress/{key}
  8. const keyEndpoint = {
  9.   // Arrow Keys
  10.   left: 'Left',
  11.   right: 'Right',
  12.   up: 'Up',
  13.   down: 'Down',

  14.   // Standard Keys
  15.   space: 'Play',
  16.   backspace: 'Back',
  17.   return: 'Select',

  18.   // Sequences (shift key)
  19.   H: 'home',
  20.   R: 'Rev',
  21.   F: 'Fwd',
  22.   S: 'Search',
  23.   E: 'Enter',

  24.   // Other
  25.   r: 'InstantReplay',
  26.   b: 'InfoBackspace'
  27. };
  28. const xmlToObject = xml => {
  29.     return JSON.parse(xml2json.toJson(xml));
  30. }

  31. readline.emitKeypressEvents(process.stdin);
  32. process.stdin.setRawMode(true);

  33. console.log('Looking for the (first) Roku...');

  34. // Find the Roku
  35. // TODO:  Allow for selection of multiple Rokus; current assuming only one
  36. Roku.find((err, devices) => {
  37.   if(err) {
  38.     console.log('`roku.find` error: ', err);
  39.     process.exit();
  40.   }

  41.   if(!devices.length) {
  42.     console.log('No Roku devices found.  Bailing.');
  43.     process.exit();
  44.   }

  45.   address = devices[0];
  46.   Roku.getDevice(address, (err, deviceDetail) => {
  47.     console.log('Connected to Device: ', xmlToObject(deviceDetail).root.device.friendlyName, ' (', devices[0],')');
  48.     console.log('Press keys to navigate the Roku and select content!');
  49.   });
  50. });

  51. // Start the keypress listener
  52. process.stdin.on('keypress', (str, key) => {
  53.   var endpoint;

  54.   // Ignore everything until we're connected
  55.   if(!address) {
  56.     return;
  57.   }

  58.   // "Raw" mode so we must do our own kill switch
  59.   if(key.sequence === '\u0003') {
  60.     process.exit();
  61.   }

  62.   // Handle commands
  63.   endpoint = keyEndpoint[key.name] || keyEndpoint[key.sequence] || 'Lit_' + key.name;

  64.   // Ignore undefined keypresses (no name or sequence)
  65.   if(endpoint === 'Lit_undefined') {
  66.     return;
  67.   }

  68.   // Send command!
  69.   request.post(address + '/keypress/' + endpoint);
  70. });
复制代码
Now let's explain what's going on with the source code above:
  
       
  • xml2json  is required because device information is returned as XML   
  • Interaction with the Roku is done via POST requests with the URL format of http://******:8060/keypress/{key} ; a POST is sent on each keypress   
  • readline.emitKeypressEvents(process.stdin); and process.stdin.setRawMode(true); directs Node.js to operate outside of normal shell operation, thus we need to explicitly check for CONTROL+C  to shut down the remote and Node.js process   
  • The keypress logic is this:  we use an object, keyEndpoint , to map logical keypress events to known endpoints; if a key isn't designated, we pass it along to the Roku as a keypress (i.e. a key to a search box, for example).  
  Get roku-remote

   I've published my Roku Remote code to both GitHub and NPM -- install and usage instructions are available in both places.  Please give it a run, file issues, and I'd love contributions if you have them!
  A web interface for roku-remote would be sweet; it could have different Roku's you could direct, a listing of apps which could be clicked to launch, and so forth.  I'm happy with this first step because it fits my needs, is easy to use, and there's plenty of room to grow.  Happy streaming!
方小雪 发表于 2016-10-1 02:52:29
将薪比薪想一下,算了,不想活了。
回复 支持 反对

使用道具 举报

Jia_Shin 发表于 2016-10-7 16:23:10
不错 支持下
回复 支持 反对

使用道具 举报

nzxoi 发表于 2016-10-8 15:53:54
说不通啊,楼下的你说咋办?
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表