Skip to main content
Version: 1.0

Script

Since version 0.9.0

The Script component handles management of scripts on the device. It uses Script as RPC namespace and provides the methods:

A script on the device is a container or a slot consisting of meta data and a file - the actual executable code. The Script component offers RPC endpoints for creation, modification, configuration and execution of scripts.

A script in a slot can be running or not and can be configured to run on boot. More information about the scripting language and the opportunities it gives can be found on the Language Features page. You can also check out the short tutorial.

note

A number of limitations apply and known issues exist. Please refer to the language features page for more details.

Methods

Script.SetConfig

PropertyTypeDescription

id

number

Id of the script

config

object

Configuration that the method takes

Find more about the config properties in config section

Script.GetConfig

PropertyTypeDescription

id

number

Id of the script

Find the Script.GetConfig response properties in config section

Script.GetStatus

PropertyTypeDescription

id

number

Id of the script

Script.Create

This method creates a new script.

Request

Parameters:

PropertyTypeDescription

name

string

Name of the script. If it is missed, a default name script_id will be assigned (e.g. script_0). Optional

Response

Attributes in the result:

PropertyTypeDescription

id

number

Id of the created script

Script.PutCode

This method allows uploading code to an existing, but not currently running script. If the script is running, it must be stopped before trying to overwrite, otherwise an error is returned. Furthermore, the maximum size of a script is limited, see the section on resource limits.

note

Uploading of code might need to happen in chunks. If the size of the file to be uploaded is large, and there is a need to upload files in chunks use the append parameter to add the next part of the content. We have a Python tool in our GitHub repo that is uploading scripts in chunks of 1024 bytes.

Request

Parameters:

PropertyTypeDescription

id

number

Id of the created script

code

string

The code which will be included in the script (the length must be greater than 0). Required

append

boolean

true to append the code, false otherwise. If set to false, the existing code will be overwritten. Default value: false. Optional

Response

Attributes in the result:

PropertyTypeDescription

len

number

The total code length in bytes

Script.GetCode

This method downloads code from an existing script.

Request

Parameters:

PropertyTypeDescription

id

number

Id of the created script

offset

number

Byte offset from the beginning. Default value: 0. Optional

len

number

Bytes to read. Default value: maximum possible number of bytes till the end is reached. Optional

Response

Attributes in the result:

PropertyTypeDescription

data

string

The requested data chunk

left

number

Number of bytes remaining till the end of the code

Script.Eval

This method evaluates or executes some code inside of a specified script.

The specified script must be running.

Request

Parameters:

PropertyTypeDescription

id

number

Id of the script

code

string

Argument to evaluate (the length must be greater than 0). Required

Script with id=1
let a = 5+5

Response

Attributes in the result:

PropertyTypeDescription

result

string

The result of the evaluation

Script.Start

This method runs a script.

Up to 3 scripts can be running at any given time.

If there is no code put in the script the method will return an error.

Request

Parameters:

PropertyTypeDescription

id

number

Id of the created script

Response

Attributes in the result:

PropertyTypeDescription

was_running

boolean

true if the script was running in the previous state, false otherwise

Script.Stop

This method stops the execution of a script.

Request

Parameters:

PropertyTypeDescription

id

number

Id of the created script

Response

Attributes in the result:

PropertyTypeDescription

was_running

boolean

true if the script was running in the previous state, false otherwise

Script.List

This method lists all scripts.

Request

This method takes no parameters.

Response

Attributes in the result:

PropertyTypeDescription

scripts

array of objects

List of all created scripts

PropertyTypeDescription

id

number

Id of the script

name

string

Name of the script

enable

boolean

true if the script runs by default on boot, false otherwise

running

boolean

true if currently running, false otherwise

Script.Delete

This method removes specified script.

Request

Parameters:

PropertyTypeDescription

id

number

Id of the created script

Response

The result from this method is null.

Configuration

The configuration of the Script component shows the identifiers of the script, as well as whether it is run on boot.

Properties:

PropertyTypeDescription

id

number

Id of the script

name

string

Name of the script

enable

boolean

true if the script runs by default on boot, false otherwise

Status

The status of the Script component shows whether the script is currently running.

Properties:

PropertyTypeDescription

id

number

Id of the script

running

boolean

true if the script is currently running, false otherwise

errors

array

Optional, present only when the script execution resulted in an error. The array contains description of the type of error. Possible errors are:

ErrorCondition

crashed

The script caused a device crash

syntax_error

Incorrect javascript syntax

reference_error

Undefined variable

type_error

Accessing unexistent property or property with wrong type

out_of_memory

Out of memory

out_of_codespace

Out of code space

internal_error

Internal interpreter error

not_implemented

Functionality not implemented

file_read_error

File read error

bad_arguments

Arguments fail verification

Examples

Script.SetConfig example

http://192.168.33.1/rpc/Script.SetConfig?id=1&config={"enable":true}

Response

{
"restart_required": false
}

Script.GetConfig example

http://192.168.33.1/rpc/Script.GetConfig?id=1

Response

{
"id": 1,
"name": "scr_1",
"enable": false
}

Script.GetStatus example

http://192.168.33.1/rpc/Script.GetStatus?id=1

Response

{
"id": 1,
"running": false
}

Script.Create example

http://192.168.33.1/rpc/Script.Create?name="my_script"

Response

{
"id": 1
}

Script.PutCode example

http://192.168.33.1/rpc/Script.PutCode?id=1&code="console.log(5+6)"

Response

{
"len": 16
}

Script.GetCode example

http://192.168.33.1/rpc/Script.GetCode?id=1

Response

{
"data": "console.log(5+6)",
"left": 0
}

Script.Eval example

http://192.168.33.1/rpc/Script.Eval

Response

{
"result": "15"
}

Script.Start example

http://192.168.33.1/rpc/Script.Start?id=1

Response

{
"was_running": false
}

Script.Stop example

http://192.168.33.1/rpc/Script.Stop?id=1

Response

{
"was_running": true
}

Script.List example

http://192.168.33.1/rpc/Script.List

Response

{
"scripts": [
{
"id": 1,
"name": "my_script",
"enable": false,
"running": true
},
{
"id": 2,
"name": "my_script2",
"enable": false,
"running": false
}
]
}

Script.Delete example

http://192.168.33.1/rpc/Script.Delete?id=1

Response

null