Image Tokens

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(( args, state ) => {

                    // get the contents of the image
                    args.droptoken.getBuffer()
                        .then( buf => {

                            // save the image
                            fs.writeFile( path.join(__dirname, 'userdata', 'kitten.jpg'), buf )
                        })
                        .catch( this.error.bind( this, 'args.droptoken.getBuffer') );

                    return Promise.resolve( 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') );