Closed kaohlive closed 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);
});
}
That code sets the image to the device also.
For future improvements we need to use a stream for the image object.
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.
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.
new version holds a very much improved piece of code on the image stuff
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.