Advertisement
Guest User

harmonyHubCli-mod

a guest
Nov 9th, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.29 KB | None | 0 0
  1. /*jslint node: true */
  2. 'use strict';
  3.  
  4. var net = require('net');
  5.  
  6. var HOST = '192.168.5.5'; //IP Address where this bridge will listen on
  7. var PORT = 2120; //Port where you this bridge will listen on
  8. var handler = function(data) {
  9.  
  10. console.log('DATA ' + ': ' + data);
  11. //someText.replace(/(\r\n|\n|\r)/gm,"");
  12. var input_array = data.toString().replace(/(\r\n|\n|\r)/gm,"").split(':');
  13. if( input_array[0] != null && input_array[1] != null ) {
  14. ev.emit('rcvd_command', "192.168.5.6", input_array[0], input_array[1]); //Specify IP address of Harmony Hub
  15. }
  16. };
  17.  
  18. // Create a server instance, and chain the listen function to it
  19. // The function passed to net.createServer() becomes the event handler for the 'connection' event
  20. // The sock object the callback function receives UNIQUE for each connection
  21. net.createServer(function(sock) {
  22.  
  23. // We have a connection - a socket object is assigned to the connection automatically
  24. console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
  25.  
  26. // Add a 'data' event handler to this instance of socket
  27. sock.on('data', handler);
  28.  
  29. // Add a 'close' event handler to this instance of socket
  30. sock.on('close', function(data) {
  31. console.log('Connection closed ');
  32. });
  33.  
  34. }).listen(PORT, HOST);
  35.  
  36. console.log('Server listening on ' + HOST +':'+ PORT);
  37. //-----------------------------------------------------------
  38.  
  39. var events = require('events'),
  40. HarmonyHubDiscover = require('harmonyhubjs-discover'),
  41. ArgumentParser = require('argparse').ArgumentParser,
  42. HarmonyUtils = require('harmony-hub-util'),
  43. first_hub = null,
  44. discover,
  45. parser;
  46.  
  47. parser = new ArgumentParser({
  48. version : '0.0.1',
  49. addHelp: true,
  50. description: 'CLI for controlling Harmony HUB'
  51. });
  52. parser.addArgument(
  53. [ '-l', '--hub'],
  54. { help: 'ip address of the hub. if not provided a discovery will be performed to find it'}
  55. );
  56.  
  57. parser.addArgument(
  58. [ '-r', '--read'],
  59. { help: 'Display supported activities/devices/commands/status that are programmed on the hub'}
  60. );
  61. parser.addArgument(
  62. [ '-a', '--activity'],
  63. { help: 'Select a activity'}
  64. );
  65. parser.addArgument(
  66. [ '-d', '--device'],
  67. { help: 'Select a device'}
  68. );
  69. parser.addArgument(
  70. [ '-c', '--command'],
  71. { help: 'Select a command to trigger. Device also needs to be specified when this is used.'}
  72. );
  73. var args = parser.parseArgs();
  74.  
  75. var read_list = ['activities', 'devices', 'commands', 'status'];
  76.  
  77. if (args.read !== null) {
  78. if (read_list.indexOf(args.read) === -1) {
  79. console.log(" For -r only supported options are " + read_list);
  80. process.exit(1);
  81. }
  82. if (read_list.indexOf(args.read) === 2 && (args.device === null && args.activity === null)) {
  83. console.log(" For -r commands, mention either a device with -d <device name> or activity with -a <activity name>.");
  84. console.log(" You can get a list of devices by running -r devices");
  85. console.log(" You can get a list of activities by running -r activities");
  86. process.exit(1);
  87. }
  88. } else {
  89. if (args.command !== null && (args.device === null && args.activity === null)) {
  90. console.log(" For executing a command with -c, mention either a device with -d <device name> or activity with -a <activity name>.");
  91. console.log(" You can get a list of activities by running -r activities");
  92. console.log(" You can get a list of devices by running -r devices");
  93. process.exit(1);
  94. }
  95. }
  96.  
  97.  
  98.  
  99. var ev = new events.EventEmitter();
  100.  
  101.  
  102. ev.on('rcvd_command', function (ip, device, cmd) {
  103. console.log("Received command: " + device + ":" + cmd);
  104. console.log("Connecting to hub at " + ip);
  105. var dt = new HarmonyUtils(ip)
  106. .then(function (hutils) {
  107. hutils.executeCommand(true, device, cmd).then(function (res) {
  108. if (res) {
  109. console.log("Command '" + cmd + "' for device '" + device + "' executed successfully.");
  110. } else {
  111. console.log("Command '" + cmd + "' for device '" + device + "' failed to executed.");
  112. }
  113. }).then(function () {
  114. hutils.end();
  115. if (args.hub === null) {
  116. //xxxxx process.exit(0);
  117. }
  118. });
  119. });
  120. });
  121. // when a hub is found.
  122. ev.on('found_a_hub', function (ip) {
  123. // for now only one hub
  124. // I dont have multiple so can't test it
  125. if (first_hub === null) {
  126. first_hub = ip;
  127. console.log("Connecting to hub at " + ip);
  128. var dt = new HarmonyUtils(ip)
  129. .then(function (hutils) {
  130. if (args.read !== null && read_list.indexOf(args.read) === 3) {
  131. hutils.readCurrentActivity().then(function (res) {
  132. console.log("Current activity : " + JSON.stringify(res));
  133. }, function (err) {
  134. console.log("\tERROR Getting current activities on the Hub : " + err);
  135. }).then(function () {
  136. hutils.end();
  137. if (args.hub === null) {
  138. process.exit(0);
  139. }
  140. });
  141. } else if (args.read !== null && read_list.indexOf(args.read) === 0) {
  142. hutils.readActivities().then(function (res) {
  143. var cnt;
  144. if (res.length === 0) {
  145. console.log("\tUnable to find any activities on the Hub");
  146. } else {
  147. console.log("List of Activities programmed on the Hub");
  148. for (cnt = 0; cnt < res.length; cnt = cnt + 1) {
  149. console.log("\t" + cnt + ". '" + res[cnt] + "'");
  150. }
  151. }
  152. }).then(function () {
  153. hutils.end();
  154. // harmoney hub discover does not cleanly exit.
  155. if (args.hub === null) {
  156. process.exit(0);
  157. }
  158. });
  159. } else if (args.read !== null && read_list.indexOf(args.read) === 1) {
  160. hutils.readDevices().then(function (res) {
  161. var cnt;
  162. if (res.length === 0) {
  163. console.log("\tUnable to find any devices on the Hub");
  164. } else {
  165. console.log("List of devices programmed on the Hub");
  166. for (cnt = 0; cnt < res.length; cnt = cnt + 1) {
  167. console.log("\t" + cnt + ". '" + res[cnt] + "'");
  168. }
  169. }
  170. }).then(function () {
  171. hutils.end();
  172. if (args.hub === null) {
  173. process.exit(0);
  174. }
  175. });
  176. } else if (args.read !== null && read_list.indexOf(args.read) === 2 && args.device !== null) {
  177. hutils.readCommands(true, args.device).then(function (res) {
  178. var cnt;
  179. if (res.length === 0) {
  180. console.log("\tUnable to find any commands on the device:" + args.device);
  181. } else {
  182. console.log("List of commands supported by device:" + args.device);
  183. for (cnt = 0; cnt < res.length; cnt = cnt + 1) {
  184. console.log("\t" + cnt + ". '" + res[cnt] + "'");
  185. }
  186. }
  187. }).then(function () {
  188. hutils.end();
  189. if (args.hub === null) {
  190. process.exit(0);
  191. }
  192. });
  193. } else if (args.read !== null && read_list.indexOf(args.read) === 2 && args.activity !== null) {
  194. hutils.readCommands(false, args.activity).then(function (res) {
  195. var cnt;
  196. if (res.length === 0) {
  197. console.log("\tUnable to find any commands for activity:" + args.activity);
  198. } else {
  199. console.log("List of commands supported by activity:" + args.activity);
  200. for (cnt = 0; cnt < res.length; cnt = cnt + 1) {
  201. console.log("\t" + cnt + ". '" + res[cnt] + "'");
  202. }
  203. }
  204. }).then(function () {
  205. hutils.end();
  206. if (args.hub === null) {
  207. process.exit(0);
  208. }
  209. });
  210. } else if (args.command !== null && args.device !== null) {
  211. hutils.executeCommand(true, args.device, args.command).then(function (res) {
  212. if (res) {
  213. console.log("Command '" + args.command + "' for device '" + args.device + "' executed successfully.");
  214. } else {
  215. console.log("Command '" + args.command + "' for device '" + args.device + "' failed to executed.");
  216. }
  217. }).then(function () {
  218. hutils.end();
  219. if (args.hub === null) {
  220. process.exit(0);
  221. }
  222. });
  223. } else if (args.command !== null && args.activity !== null) {
  224. hutils.executeCommand(false, args.activity, args.command).then(function (res) {
  225. if (res) {
  226. console.log("Command '" + args.command + "' for activity '" + args.activity + "' executed successfully.");
  227. } else {
  228. console.log("Command '" + args.command + "' for activity '" + args.activity + "' failed to executed.");
  229. }
  230. }).then(function () {
  231. hutils.end();
  232. if (args.hub === null) {
  233. process.exit(0);
  234. }
  235. });
  236. } else if (args.activity !== null) {
  237. hutils.executeActivity(args.activity).then(function (res) {
  238. if (res) {
  239. console.log("Activity '" + args.activity + "' executed successfully.");
  240. } else {
  241. console.log("Activity '" + args.activity + "' failed to executed.");
  242. }
  243. }).then(function () {
  244. hutils.end();
  245. if (args.hub === null) {
  246. process.exit(0);
  247. }
  248. });
  249. }
  250. });
  251. }
  252. });
  253.  
  254.  
  255. function discoverHub(callBackFn) {
  256. if (discover === null || discover === undefined) {
  257. discover = new HarmonyHubDiscover(61991);
  258. }
  259. discover.on('online', function (hub) {
  260. //console.log('discovered ' + hub.ip + '\n');
  261. callBackFn(hub.ip, true);
  262. });
  263. discover.on('offline', function (hub) {
  264. //console.log('lost ' + hub.ip);
  265. callBackFn(hub.ip, false);
  266. });
  267. discover.start();
  268. }
  269. function discoverHubStop() {
  270. if (discover !== null) {
  271. discover.end();
  272. }
  273. }
  274.  
  275. // Look for hubs if ip is not specified
  276. // and use the first ip that's found
  277. /*
  278. if (args.hub === null) {
  279. console.log('Starting hub Discovery');
  280. discoverHub(function (ip, add) {
  281. if (add) {
  282. console.log(" Hub Found at :" + ip);
  283. ev.emit('found_a_hub', ip);
  284. } else {
  285. ev.emit('lost_a_hub', ip);
  286. }
  287. });
  288. } else {
  289. // if IP is provided just use that to trigger the commands
  290. ev.emit('found_a_hub', args.hub);
  291. }
  292. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement