snowdd1 / homebridge-knx

KNX platform shim for homebridge
https://github.com/nfarina/homebridge
GNU General Public License v2.0
97 stars 56 forks source link

knxread in customAPI #105

Closed WindnSalt closed 6 years ago

WindnSalt commented 6 years ago

I was writing a custom handler to open and close my roof windows. I ran into a situation where I wanted to send a read telegram to the bus in order to read out the position of the windows in case those were moved not by HomeKit, but by KNX switches installed in the house. I only found a function to write to the bus in the customServiceAPI. Therefore I added a read function based on the knxWrite function that would send a read request to the bus. Is this compliant with the architecture or is there another way to send read telegrams that I didn't see?

/**
 * Sends a read request to the KNX bus. Answer will returned by a call of onKNXValueChange.
 * 
 * @param {string} field - The name of the characteristic. Requires a "Listen" in the characteristic or KNXObject. 
 */
knxRead (field) {
    /** @type {CharacteristicKNX} */
    var chrKNX;
    this.serviceKNX.globs.info(this.handlerName + "->customServiceAPI.knxRead(" + field + ")");

    // get the characteristic
    //iterate(this.characteristicsList);
    if (!this.characteristicsList[field]) {
        throw (new Error('HANDLER CONFIGURATION ERROR').message='Error in ' + this.handlerName + '. Field '+ field + ' does not exist');
    } else {
        chrKNX = this.characteristicsList[field];
    }
    // iterate through all group addresses to be written for that characteristic
    for (var iGA = 0; iGA < chrKNX.setGroupAddressList.length; iGA++) {
        var gaddress = chrKNX.listenGroupAddressList[iGA];
        KNXAccess.knxread(gaddress.address);
    }

}
snowdd1 commented 6 years ago

Hi @CptMW Manuel, currently there is no other way to do it. I like your implementation, except you followed my misnamed "knxread" - should be named knxReadRequest() to make obvious that it doesn't read anything by itself. Will you prepare a pull request? Thanks Raoul

WindnSalt commented 6 years ago

Hi Raoul,

will do later tonight, as soon as I’m back on my computer. It’s gonna be my first pull request, so be patient with me.

Cheers,

Manuel