Using service scripting in the Kanzi Connect Simulator

In the Kanzi Connect Simulator you can use service scripting to implement sequences and small functionality. Kanzi Connect comes with a script plugin which uses the duktape engine (https://duktape.org/). Kanzi Connect loads the script plugin on demand when you add a scripted method to a service. You can safely leave the plugin out in a production environment. If a method defines a return value, the implementation reads the value from the script context on exit.

Scripted methods run on the Kanzi Connect server main thread so they must not block for a long time. Implementing a blocking method stops the connect server main thread and blocks communication towards all the other services. For long-running tasks, you can use JavaScript scripting to call a method asynchronously and run that method periodically. You should define a cancel method whenever you create sequentially running methods.

To use service scripting in the Kanzi Connect Simulator:

  1. Set up your Kanzi Studio project and the Simulator. See Using the services Simulator.

  2. In the Simulator web application select a service which is running on the Kanzi Connect server.

    For example, select the Media service.

    ../../_images/select-media-service.png
  3. In the Methods section click image0 to add a method.

    For example, name the method playPreview.

    ../../_images/add-method.png ../../_images/name-method.png
  4. In the New Method window in the Script section enter the script you want that method to run:

    call_method('playPlaylist',1001);
    async_method('stop',5000);
    
    ../../_images/enter-script1.png
  5. In the Methods section click Invoke to run the script on the Kanzi Connect server.

    ../../_images/call-method.png

Reference for service scripting

call_method

Calls a method defined in the same service. Returns a value if one defined.

Syntax

call_method('name', method arguments);

Arguments

name

the name of the method.

method arguments

method arguments in the same order as defined in the service description. The amount of arguments and the data types of those arguments must match.

Examples

call_method(‘playTrack’,1001,1001);
call_method(‘stop’);

async_method

Queues a method call to run as an asynchronous task in the Kanzi Connect server work queue. There can be one method in a work queue at a time. Adding another method cancels the existing method. Does not return a value.

Syntax

async_method('name', timeout, method arguments);

Arguments

name

the name of the method.

timeout

integer in milliseconds.

method arguments

method arguments in the same order as defined in the service description. The amount of arguments and the data types of those arguments must match.

Examples

async_method(‘playTrack’, 2000, 1001, 1001);
async_method(‘stop’, 5000);

cancel_method

Cancels an asynchronous method call from the Kanzi Connect server work queue.

Syntax

cancel_method('name');

Arguments

name

the name of the method.

Examples

cancel_method(‘playTrack’);
cancel_method(‘stop’);

invoke_event

Invokes a Kanzi Connect event defined in a service.

Syntax

invoke_event('name', event arguments);

Arguments

name

the name of the event.

event arguments

event arguments in the same order as defined in the service description. The amount of arguments and the data types of those arguments must match.

Examples

invoke_event(‘progress’,15,150);

get_runtime_value

Reads a runtime value in a service. Returns a value on success.

Syntax

get_runtime_value('hierarchical name');

Arguments

hierarchical name

the name of the property for which you want to return the value.

Examples

console.log(get_runtime_value('current_track.name'));

set_runtime_value

Writes a runtime value in a service.

Syntax

set_runtime_value('hierarchical name');

Arguments

hierarchical name

the name of the property for which you want to set the value.

Examples

set_runtime_value('demo.vehicle_color',982345);