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:
Set up your Kanzi Studio project and the Simulator. See Using the services Simulator.
In the Simulator web application select a service which is running on the Kanzi Connect server.
For example, select the Media service.
In the Methods section click
to add a method.
For example, name the method playPreview.
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);
In the Methods section click Invoke to run the script on the Kanzi Connect server.
Reference for service scripting¶
call_method¶
Calls a method defined in the same service. Returns a value if one defined.
Syntax |
|
||||
Arguments |
|
||||
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 |
|
||||||
Arguments |
|
||||||
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 |
|
||
Arguments |
|
||
Examples |
cancel_method(‘playTrack’);
cancel_method(‘stop’);
|
invoke_event¶
Invokes a Kanzi Connect event defined in a service.
Syntax |
|
||||
Arguments |
|
||||
Examples |
invoke_event(‘progress’,15,150);
|
get_runtime_value¶
Reads a runtime value in a service. Returns a value on success.
Syntax |
|
||
Arguments |
|
||
Examples |
console.log(get_runtime_value('current_track.name'));
|
set_runtime_value¶
Writes a runtime value in a service.
Syntax |
|
||
Arguments |
|
||
Examples |
set_runtime_value('demo.vehicle_color',982345);
|