Running custom scripts (lambdas)

In some cases you can just want to run lambda functions, without a need to create a specific sensor or actuator. In waylay there is a script sensor which allows you to do exactly that. We are showing here the code of the script sensor just as an example how you can write your own more general sensors in the future:

const script = options.requiredProperties.script

async function main () {
    let result

    try {
        result = await eval(`(${script})()`)
    } catch(error) {
        console.error(error.message)
        send(null, { observedState: 'error' })
    }

    if (typeof result !== 'object' || result === null) {
        throw new Error('Result is not an object')
    }

    send(null, { observedState: 'done', rawData: result })
}

main().catch(send)

In this example, these two functions are written as lambdas (script_1 and script_2) and we just chain them together, where the second lambda takes as the input argument values returned by the first one and increment by 1:

()=>{
    return {foo: 1}
}
()=>{
    const v = ${nodes.script_1.rawData.foo} + 1
    return {foo: v}
}

image