ageorgios / homebridge-sonoff-tasmota-http

Control Sonoff Basic devices with Tasmota firmware through Homebridge
Other
15 stars 19 forks source link

request timeout #16

Open lukasa1993 opened 5 years ago

lukasa1993 commented 5 years ago

please add request time out so it doesn't hang other devices on bridge

request({
    uri:"http://" + that.hostname + "/cm?user=admin&password=" + that.password + "&cmnd=Power" + that.relay,
    timeout:200,
  }, 

200 ms is more then enough for local lan response and homebridge doesn't hang other devices on same bridge

lukasa1993 commented 5 years ago
var Service,
    Characteristic;
var request = require('request');

module.exports = function (homebridge) {
  Service        = homebridge.hap.Service;
  Characteristic = homebridge.hap.Characteristic;

  homebridge.registerAccessory('homebridge-sonoff-tasmota-http', 'SonoffTasmotaHTTP', SonoffTasmotaHTTPAccessory);
};

function SonoffTasmotaHTTPAccessory(log, config) {
  this.log      = log;
  this.config   = config;
  this.name     = config['name'];
  this.relay    = config['relay'] || '';
  this.hostname = config['hostname'] || 'sonoff';
  this.password = config['password'] || '';

  this.service = new Service.Outlet(this.name);

  this.service
      .getCharacteristic(Characteristic.On)
      .on('get', this.getState.bind(this))
      .on('set', this.setState.bind(this));

  this.log('Sonoff Tasmota HTTP Initialized');
}

SonoffTasmotaHTTPAccessory.prototype.getState = function (callback) {
  var that = this;
  request({
    uri:     'http://' + that.hostname + '/cm?user=admin&password=' + that.password + '&cmnd=Power' + that.relay,
    timeout: 200,
  }, function (error, response, body) {
    if (error) {
      return callback(error);
    }
    var sonoff_reply = JSON.parse(body); // {"POWER":"ON"}
    that.log('Sonoff HTTP: ' + that.hostname + ', Relay ' + that.relay + ', Get State: ' + JSON.stringify(sonoff_reply));
    switch (sonoff_reply['POWER' + that.relay]) {
      case 'ON':
        callback(null, 1);
        break;
      case 'OFF':
        callback(null, 0);
        break;
    }
  });
};

SonoffTasmotaHTTPAccessory.prototype.setState = function (toggle, callback) {
  var newstate = '%20Off';
  if (toggle) {
    newstate = '%20On';
  }
  var that = this;
  request({
    uri:     'http://' + that.hostname + '/cm?user=admin&password=' + that.password + '&cmnd=Power' + that.relay + newstate,
    timeout: 200,
  }, function (error, response, body) {
    if (error) {
      return callback(error);
    }
    var sonoff_reply = JSON.parse(body); // {"POWER":"ON"}
    that.log('Sonoff HTTP: ' + that.hostname + ', Relay ' + that.relay + ', Set State: ' + JSON.stringify(sonoff_reply));
    switch (sonoff_reply['POWER' + that.relay]) {
      case 'ON':
        callback();
        break;
      case 'OFF':
        callback();
        break;
    }
  });
};

SonoffTasmotaHTTPAccessory.prototype.getServices = function () {
  return [this.service];
};
lukasa1993 commented 5 years ago

also since you implementing outlet service can you also please set in-use characteristic

ageorgios commented 5 years ago

make a pull request and will be happy to review it. no time for development. thank you.

although i do not understand the point of a 200ms timeout since it is asynchronus request.

lukasa1993 commented 5 years ago

whats the point id fork and implement :D