#! /usr/bin/env node

const program = require('commander');

const Hs100Api = require('.');
const client = new Hs100Api.Client();

let logLevel;

let search = function (sysInfo, timeout) {
  console.log('Searching...');
  if (logLevel) client.log.setLevel(logLevel);
  client.startDiscovery({discoveryInterval: 2500, discoveryTimeout: timeout})
    .on('device-new', (device) => {
      console.log(`${device.model} ${device.type} ${device.host} ${device.deviceId}`);
      if (sysInfo) {
        console.dir(device.sysInfo, {colors: program.color === 'on', depth: 10});
      }
    });
};

let send = function (host, port, payload, timeout) {
  console.log('Sending...');
  if (logLevel) client.log.setLevel(logLevel);
  client.send({host, port, payload, timeout}).then((data) => {
    console.log('response:');
    console.dir(data, {colors: program.color === 'on', depth: 10});
  }).catch((reason) => {
    console.log('no response: %s', reason);
  });
};

let details = function (host, port, timeout) {
  console.log('Getting details...');
  if (logLevel) client.log.setLevel(logLevel);
  client.getDevice({host, port}).then((device) => {
    return device.getInfo().then((info) => {
      console.dir(info, {colors: program.color === 'on', depth: 10});
    });
  }).catch((reason) => {
    console.error(reason);
  });
};

let blink = function (host, port, times, rate, timeout) {
  console.log('Sending blink commands...');
  if (logLevel) client.log.setLevel(logLevel);
  client.getDevice({host, port}).then((device) => {
    return device.blink(times, rate).then(() => {
      console.log('Blinking complete');
    });
  }).catch((reason) => {
    console.error(reason);
  });
};

program
  .option('-D, --debug', 'turn on debug level logging', () => { logLevel = 'debug'; })
  .option('-c, --color [on]', 'output will be styled with ANSI color codes', 'on');

program
  .command('search')
  .option('-s, --sysinfo', 'output sysInfo')
  .option('-t, --timeout [timeout]', 'timeout (ms)', 5000)
  .action(function (options) {
    search(options.sysinfo, options.timeout);
  });

program
  .command('send <host> <payload>')
  .option('-t, --timeout [timeout]', 'timeout (ms)', 5000)
  .action(function (host, payload, options) {
    let [hostOnly, port] = host.split(':');
    send(hostOnly, port, payload, options.timeout);
  });

program
  .command('details <host>')
  .option('-t, --timeout [timeout]', 'timeout (ms)', 5000)
  .action(function (host, options) {
    let [hostOnly, port] = host.split(':');
    details(hostOnly, port, options.timeout);
  });

program
  .command('blink <host> [times] [rate]')
  .option('-t, --timeout [timeout]', 'timeout (ms)', 5000)
  .action(function (host, times = 5, rate = 500, options) {
    let [hostOnly, port] = host.split(':');
    blink(hostOnly, port, times, rate, options.timeout);
  });

program.parse(process.argv);

@@ -452,16 +452,46 @@
 ice.
-deviceId
+port%7D $%7Bdevice.deviceId%7D $%7Bdevice.name
 %7D%60);
@@ -1712,16 +1712,63 @@
 %7D);%0A%7D;%0A%0A
+let toInt = (s) =%3E %7B%0A  return parseInt(s);%0A%7D;%0A%0A
 program%0A
@@ -2057,32 +2057,39 @@
  'timeout (ms)',
+ toInt,
  5000)%0A  .action
@@ -2251,32 +2251,39 @@
  'timeout (ms)',
+ toInt,
  5000)%0A  .action
@@ -2503,32 +2503,39 @@
  'timeout (ms)',
+ toInt,
  5000)%0A  .action
@@ -2709,16 +2709,16 @@
 rate%5D')%0A
-
   .optio
@@ -2761,16 +2761,23 @@
 t (ms)',
+ toInt,
  5000)%0A 

Fix for timeout option