denniedegroot / com.ring

Ring for Homey
https://www.athom.com
Other
15 stars 15 forks source link

Improve grabber reliability #50

Closed kaohlive closed 4 years ago

kaohlive commented 4 years ago

I can grab images from my doorbell 2 just fine. Unless sometimes it doesnt work anymore. The flow card gives the error alert when run. Doing some debugging on it and while the app is attached to the debugger it always works flawlessly. I see you use a retry pattern of 3 retries. Maybe we need to increase that in some cases? Maybe 5 times.

    let retries = 3; //Make this 5 or 10 times?
kaohlive commented 4 years ago
grabImage(args, state) {
    if (this._device instanceof Error)
        return Promise.reject(this._device);

    let _this = this;
    let device_data = this.getData();

    return new Promise(function(resolve, reject) {
        Homey.app.grabImage(device_data, (error, result) => {
            if (error)
                return reject(error);

            let ringImage = new Homey.Image('jpg')
            ringImage.setBuffer(new Buffer( result, 'binary' ));

            ringImage.register().then(() => {
                new Homey.FlowCardTrigger('ring_snapshot_received').register().trigger({ring_image: ringImage}).catch(error => { this.error(error); });
                _this.setCameraImage(_this.getName(), 'Last event snapshot', ringImage, (err) =>{
                    if(err)
                        console.log('could not set camera image: '+err);
                });
                return resolve(true);
            }).catch(error => {
                return reject(error);
            });
        }).bind(_this);
    });
}
kaohlive commented 4 years ago

That code sets the image to the device also.

For future improvements we need to use a stream for the image object.

kaohlive commented 4 years ago

I have tried this: let ringImage = new Homey.Image('jpg') ringImage.setStream(async (stream) => { const buffer = new Buffer( result, 'binary' ); const readable = new require('stream').Readable({ read() {} }); readable.push(buffer); readable.push(null); return readable.pipe(stream); });

But it results in a invalid_content message. I do not understand why though.

kaohlive commented 4 years ago

I created a pull request for the cameraimage part (but not as bad as above in the example). However still cant get the stream thing working.

kaohlive commented 4 years ago

new version holds a very much improved piece of code on the image stuff