Certain applications might want to share images between Flows, e.g. a webcam that made a snapshot, or a Chromecast that wants to cast an image. A image
typed token can be used here.
To start using image tokens, first register a Image instance.
/app.js
let myImage = new Homey.Image();
myImage.setPath( path.join(__dirname, 'assets', 'images', 'kitten.jpg') );
myImage.register()
.then(() => {
// create a token & register it
let myImageToken = new Homey.FlowToken('my_token', {
type: 'image',
title: 'My Image Token'
})
myImageToken
.register()
.then( () => {
myImageToken.setValue( myImage )
.then( this.log.bind( this, 'myImageToken.setValue') )
.catch( this.error.bind( this, 'myImageToken.setValue') )
})
.catch( this.error.bind( this, 'myImageToken.register') )
// listen for a Flow action
new Homey.FlowCardAction('image_action')
.register()
.registerRunListener(async ( args, state ) => {
// get the contents of the image
const imageStream = await args.droptoken.getStream();
this.log('saving image of mime type:', imageStream.contentType, 'to: ', imageStream.filename)
// save the image
const targetFile = fs.createWriteStream( path.join(__dirname, 'userdata', filename) );
imageStream.pipe(targetFile);
return true;
})
// trigger a Flow
new Homey.FlowCardTrigger('image_trigger')
.register()
.trigger({
my_image: myImage // pass the instance here
})
.then( this.log.bind( this, 'imageTrigger.trigger') )
.catch( this.error.bind( this, 'imageTrigger.trigger') );
})
.catch( this.error.bind( this, 'myImage.register') );