RoeiOfri / homebridge-palgate-opener

PalGates homebridge plugin
MIT License
27 stars 8 forks source link

Refrence Error: Charecteristic not defined #4

Closed Knilo closed 3 years ago

Knilo commented 3 years ago

Hi Roei, Really excited about this plugin and trying to set it up.

I have set the config with the device ID and token, however I am getting the current issue whenever it tries to initialize. Any idea?

[Parking Gate] Triggered GET Current DoorState
ReferenceError: Characteristic is not defined
    at PalGateOpener.handleCurrentDoorStateGet (/usr/local/lib/node_modules/homebridge-palgate-opener/palgate.js:60:24)
    at CurrentDoorState.emit (events.js:315:20)
    at CurrentDoorState.EventEmitter.emit (/usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/src/lib/EventEmitter.ts:42:22)
    at CurrentDoorState.Characteristic._this.getValue (/usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/src/lib/Characteristic.ts:451:12)
    at /usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/src/lib/Accessory.ts:1219:22
    at Array.forEach (<anonymous>)
    at Bridge.Accessory._this._handleGetCharacteristics (/usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/src/lib/Accessory.ts:1147:10)
    at HAPServer.emit (events.js:315:20)
    at HAPServer.EventEmitter.emit (/usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/src/lib/EventEmitter.ts:42:22)
    at HAPServer._this._handleCharacteristics (/usr/local/lib/node_modules/homebridge/node_modules/hap-nodejs/src/lib/HAPServer.ts:900:12)
RoeiOfri commented 3 years ago

Hi, please paste here:

  1. Plugin’s configuration yaml entry.
  2. OS, Platform (HB/Hoobs) and version.
Knilo commented 3 years ago

Thanks for the quick response.

I have hidden the token (extracted using your tool):

        {
            "accessory": "PalGateOpener",
            "name": "Parking Gate",
            "accessoryType": "garageDoor",
            "deviceId": "3G100106050",
            "token": "XXX"
        }

OS: Raspbian GNU/Linux 10 (buster) Platform: Homebridge v1.0.4

Knilo commented 3 years ago

@RoeiOfri I have upgraded my homebridge to the latest version (v. 1.1.7) and that did not resolve the issue.

RoeiOfri commented 3 years ago

Thanks for the update, I’ll try to reproduce this issue on my end, I’ll keep you posted.

Knilo commented 3 years ago

Thanks, let me know if I can help.

Knilo commented 3 years ago

@RoeiOfri I managed to fix the bug, I'll make a pull request

RoeiOfri commented 3 years ago

Thanks!

Knilo commented 3 years ago

git is annoying on this computer, here is the updated palgate.js:

Just needed to add this.api.hap. whenever the Charactersitic is referenced.

module.exports = (api) => {
  api.registerAccessory('PalGateOpener', PalGateOpener);
};

class PalGateOpener {

  constructor(log, config, api) {
      this.log = log;
      this.config = config;
      this.api = api;
      this.deviceId = config['deviceId'];
      this.token = config['token'];
      this.accessoryType = config['accessoryType'] || 'switch'
      this.name = config.name;
      this.accessoryType = this.accessoryType.toLowerCase()
      this.httpAddress = `https://api1.pal-es.com/v1/bt/device/${this.deviceId}/open-gate?outputNum=1`;

      this.Service = this.api.hap.Service;
      this.Characteristic = this.api.hap.Characteristic;
      this.log.debug('PalGate Accessory Plugin Loaded');

      // Verify accessory type meets supporting types.
      if (this.accessoryType != 'switch' && this.accessoryType != 'garagedoor') {
          this.log('Accessory Type is not supported, using it as "switch" instead!')
      }

      // AccessoryInformation service
      this.informationService = new this.api.hap.Service.AccessoryInformation()
          .setCharacteristic(this.api.hap.Characteristic.Manufacturer, "Pal Systems")
          .setCharacteristic(this.api.hap.Characteristic.Model, "PalGate App");

      // Deciding which button type to create (switch/garage door)
      switch(this.accessoryType) {
          case "switch":
              this.service = new this.api.hap.Service.Switch(this.name);
              this.service.getCharacteristic(this.api.hap.Characteristic.On)
                  .on('get', this.getOnHandler.bind(this))   // bind to getOnHandler method below
                  .on('set', this.setOnHandler.bind(this));  // bind to setOnHandler method below
          break

        case "garagedoor":
            this.service = new this.api.hap.Service.GarageDoorOpener(this.name)
            this.service.getCharacteristic(this.Characteristic.CurrentDoorState)
                .on('get', this.handleCurrentDoorStateGet.bind(this));

            this.service.getCharacteristic(this.Characteristic.TargetDoorState)
                .on('get', this.handleTargetDoorStateGet.bind(this))
                .on('set', this.handleTargetDoorStateSet.bind(this));
            break
      }
  }

 getServices() {
    return [this.informationService, this.service];
  }

  handleCurrentDoorStateGet(callback) {
    this.log.info('Triggered GET Current DoorState');
    var currentValue = this.api.hap.Characteristic.CurrentDoorState.CLOSED
    callback(null, currentValue);
  }

  handleTargetDoorStateGet(callback) {
    this.log.info('Triggered GET Target DoorState');
    var targetDoorState = this.api.hap.Characteristic.CurrentDoorState.CLOSED
    callback(null, targetDoorState);
  }

   // Handle request to open/close door
  handleTargetDoorStateSet(value, callback) {
    if (value == this.api.hap.Characteristic.TargetDoorState.OPEN) {
        var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
        var xhr = new XMLHttpRequest();
        xhr.open('get', this.httpAddress);
        xhr.setRequestHeader('x-bt-user-token', this.token);
        xhr.send();
        this.log.debug('Gate opened');
        this.service.setCharacteristic(this.Characteristic.CurrentDoorState, this.api.hap.Characteristic.CurrentDoorState.OPEN)
    } else if (value == Characteristic.CurrentDoorState.CLOSED) {
        this.log.debug('Closing gate...')
        this.service.setCharacteristic(this.Characteristic.CurrentDoorState, this.api.hap.Characteristic.CurrentDoorState.CLOSED);
    }
    callback(null)
  }

  getOnHandler(callback) {
      this.log.debug('Getting switch state');
      const value = false;
      callback(null, value);
  }

  setOnHandler(value, callback) {
      var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
      var xhr = new XMLHttpRequest();
      xhr.open('get', this.httpAddress);
      xhr.setRequestHeader('x-bt-user-token', this.token);
      xhr.send();
      this.log.info('Gate opened');
      callback(null);
    }
}
RoeiOfri commented 3 years ago

Great! I’ll push a new version in a bit. Thanks for saving me debug time ;)

Knilo commented 3 years ago

My pleasure! Thanks for making an awesome plugin!

RoeiOfri commented 3 years ago

Resolved by v1.0.5 with many thanks to @Knilo ! https://github.com/RoeiOfri/homebridge-palgate-opener/releases/tag/1.0.5