Javascript SDK 
Please visit : How to start an apps (descriptor.xml, screen.xml)
Introduction
This page describes all specific methods defined in karotz scripts.
We provide a Karotz Virtual Machine [linux only] to simplify your Karotz development
KarotzVM JAVA
java -jar karotz-vm-1.0-SNAPSHOT.one-jar.jar
Missing APP_FOLDER arg
usage: karotz-vm.jar [OPTION]... APP_FOLDER
-h,--help print this help
-k,--karotz Karotz connection, set simulation off
-s,--simulation set simulation on (no Karotz needed)
Examples
Hello world – app_helloworld.zip
Launch type
You can get the origin of the application launch.
launchType.name
value of launchType.name : { “ASR”; “SCHEDULER”, “RFID” }
If “ASR” :
get the asr semantic :
grammar : twitter {$.cmd='none'}
launchType.semantic.cmd == 'none'
If “RFID” :
get the rfid tag info :
launchType.id
launchType.app
launchType.type
launchType.pict
launchType.color
launchType.data
General methods
include
include("util.js");
log
log("message to log");
setTimeout
setTimeout(100, function(){ log("hello"); });
http
var data = http.get("http://www.google.com/ig/api?weather=paris,france&hl=fr");
log("received : " + data);
url = "http://www.example.com/posttest.cgi";
dataPost = {
"fname": "toto",
"sname": "soso",
"lname": "titi"
};
header = {
"headerKey": "headerValue"
};
isForm = false;
//optionnal. default:
//header = {}; isForm = false
var data = http.post(url, dataPost , header, isForm);
log("received : " + data);
var data = http.get2("http://www.google.com/ig/api?weather=paris,france&hl=fr");
log(data.header)
log(data.content)
get2 and post2 return both content and header of http request
you can extract cookies:
var getCookie = function(header)
{
var cookies = ""
cookies["Cookie"] = []
var reg = new RegExp("^Set-Cookie: (\\w+=.+);")
var headers=ret.header.split("\n");
for (i = 0 ; i < headers.length ; i++)
{
var m = reg.exec(headers[i])
if (m != null)
{
cookies += m[1] + ";"
log(m[1]);
}
}
return cookies;
};
var cook = getCookie(ret.header)
var cookList = cook.split(";")
for (i in cookList)
{
var sub = cookList[i].split("=");
post[sub[0]]=sub[1];
}
ret = http.post2("http://10.104.2.38/cookies/",post,{"Cookie":cook});
file
var data = file.read("foo.txt");
var fileContent = data.text;
exit
exit();
ping [deprecated] use karotz.ping
If no message is sent during 15 minutes, the karotz application automatically terminated.
For example, il you play a music and do nothing for 15 minutes, the application is terminated.
To avoid that, you have to ping the karotz.
Example of ping every 5 minutes :
setTimeout(300000, function(){ log("ping"); ping(); return true; });
Karotz methods
All of the following methods must be called in interactive mode
Parameters and command line
instanceName = name of the launched instance
cmdline = command line. contains apiKey and instanceName
params = contains application parameters
params[instanceName].name
//contain value of the field in screen.xml. name is defined in screen.xml
karotz.connectAndStart
var onKarotzConnect = function(data){
//comportement de l'appli connectée (mode interractif)
}
//comportement de l'appli non connectée
data = {};
karotz.connectAndStart("localhost", 9123, onKarotzConnect, data);
karotz callback
If not notified, value of “event” is { “OK”; “TERMINATED”, “CANCELLED”, “ERROR”; }
OK : action started
TERMINATED : action is done
CANCELLED : action is cancelled before the end
ERROR : action gives an error
karotz.ping
If no message is sent during 15 minutes, the karotz application automatically terminated.
For example, il you play a music and do nothing for 15 minutes, the application is terminated.
To avoid that, you have to ping the karotz.
Example of ping every 5 minutes :
setTimeout(300000, function(){ log("ping"); karotz.ping(); return true; });
karotz.tts
start
karotz.tts.start(string text, string lang, function(event));
- text : text to say
- lang : “fr”, “en”…
- function : callback
stop
karotz.tts.stop(function(event));
- function : callback
karotz.asr
start
karotz.asr.string(string grammar, string lang, function(asrResult));
- grammar : the asr grammar (root part)
- lang : “fr-FR”, “en-GB”, “en-US”, “es-ES”, “de-DE”
- function : callback
The grammar use ABNF format.
ABNF grammar W3C standard
#ABNF 1.0 UTF-8;
language fr-FR;
mode voice;
tag-format <semantics/1.0>;
root $App;
public $App = [lesson] 1 { $.param='1'} | [lesson] 2 { $.param='2'} | [lesson] 3 { $.param='3'};
In this example : the text “lesson” is optional. The user can say “lesson 1” or “1”. The semantic interpretation will return the semantic with param=1. Semantic is useful to build useful asr return.
Your grammar argument is encapsulated in a build grammar with headers (lang, …)
#ABNF 1.0 UTF-8;
language yourLang;
mode voice;
tag-format <semantics/1.0>;
root $App;
public $App = yourGram;
The callback function argument :
asrResult.confident : 0-100 (it’s not a linear variable. more than 30 can be considered as a acceptable result.
asrResult.text : recognized text
asrResult.semantic : contain the semantic interpretation
in our example : asrResult.semantic.param = 1 or 2 or 3
karotz.ears
move absolute
karotz.ears.move(left, right, function(event));
there is 17 ears postions. 3 is top, -5 is bottom. 17 is the same postion as 0.
move relative
karotz.ears.moveRelative(left, right, function(event));
reset
karotz.ears.reset( function(event))
karotz.led
light
karotz.led.light(string color)
- color : hexadecimal color value
fade
karotz.led.fade(string color, int time, function(event))
fade the led from the current color to the new one.
- color : hexadecimal color value
- time : time in ms
- function : callback
pulse
karotz.led.pulse(string color, int pulsePeriod, int duree, funtion(event))
pulse between the current color and the paramter’s color
- color : hexadecimal color value
- pulsePeriod: pulse period in ms
- time : time in ms (-1 = always)
- function : callback
karotz.webcam
photo
karotz.webcam.photo(string url);
- url : the path to the script where the picture will be posted
Example : a php script example that show posted informations :
<?php
// name of the file : snapshot_AAAA_MM_JJ_HH_MM_SS.jpg
echo "File name '" . $_FILES['sendfile']['name'] . "' \n'";
//type
echo "File type '" . $_FILES['sendfile']['type'] . "' \n'";
// frame size
echo "File size '" . $_FILES['sendfile']['size'] . "' \n'";
// voos msg query uuid
echo "File msg uuid '" . $_POST['uuid'] . "' \n'";
echo "Copying '" . $_FILES['sendfile']['tmp_name'] . "' to '" .
$_POST['filename'] . "'";
if (!move_uploaded_file($_FILES["sendfile"]["tmp_name"],
$_POST['filename'])) echo "CANNOT MOVE {$_FILES["sendfile"]
["name"]}" . PHP_EOL;
?>
karotz.multimedia
play
karotz.multimedia.play(string path, function(event));
play a sound or a music
- path : can be an URL, a relative path or a command listed below.
- function : callback
- lock::no → unlock mpd access to sound device. call this before playing with mpd
- lock::yes → lock mpd access to sound device. this is automatically called at the end of the application
- playlist::name → play the specified playlist
- dir::name → play the specified directory
- artist::name → play the specified artist songs
- genre::name → play the specified genre songs
- title::name → play the specified song
- allsong:: → play all songs
- mode::normal → set the player to normal mode (default)
- mode::random → set the player to random mode
- mode::repeat → set the player to repeat mode
- mode::norepeat → set the player to no-repeat mode (default)
pause
karotz.multimedia.pause(function(event))
- function : callback
resume
karotz.multimedia.resume(function(event))
- function : callback
stop
karotz.multimedia.stop(function(event))
stop playing / recording
- function : callback
next
karotz.multimedia.next(function(event))
select the next song (only for usbkey)
- function : callback
previous
karotz.multimedia.previous(function(event))
select the previous song (only for usbkey)
- function : callback
artist
karotz.multimedia.artist(function(event))
retreive usb key songs of the specified artist, “:” is used as separator
- function : callback
Sample:
var artistList;
var castList = function(list){
var reg=new RegExp(":", "g");
list_split=list.split(reg);
return list_split;
}
var artistListCallback = function(list){
artistList = castList(list);
}
folder
karotz.multimedia.folder(function(event));
retreive usb key folders, “:” is used as separator
playlist
karotz.multimedia.playlist(function(event));
retreive usb key playlists, “:” is used as separator
genre
karotz.multimedia.genre(function(event));
retreive usb key gernes, “:” is used as separator
song
karotz.multimedia.song(function(event));
retreive usb key songs, “:” is used as separator
record
karotz.multimedia.record(function(event));
record a sound. format: flac
event.type = “TERMINATED” when record is done
event.data = contain data about the flac file to be posted
Sample : record 5 sec, post and play flac file
var cb=function(event)
{
if (event.type == "TERMINATED")
{
urlPost = "url";
http.post(urlPost,
{
"flacFile":event.data
}, {}, true;
setTimeout(4000,function(){ log("PLAY"); karotz.multimedia.play(urlPost); return false; });
}
}
karotz.multimedia.record(cb);
setTimeout(5000, karotz.multimedia.stop());
serial
/!\ Plug the USB before turning on the Karotz electrically
Add the access to the descriptor : serial
karotz.serial.open(port,baudrate)
open a serial port
port: /dev/ttyUSB0
baudrate: 50, 75, 110, 134, 150, 200, 300, 600,1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400 or 460800
karotz.serial.write(data)
write serial data
data: ascii data to write
karotz.serial.writeHexa(data)
write hexadecimal serial data
data: ascii data to write ex karotz.serial.writeHexa(“AF5B”);
karotz.serial.addListener(function)
add serial recieved data callback
function: fonction to fe calledback function(data)
data : data recieved
karotz listeners
button
karotz.button.addListener(function(event));
add a listener on the button
value of event : { “SIMPLE”; “DOUBLE”, “TRIPLE”, “MULTIPLE”; “LONG_START”; “LONG_STOP”; }
rfid
karotz.rfid.addListener(function(data));
add a listener on rfid
data is an object that contain information about the tag :
the tag id :
data.id
If the tag goes in or out the reader range:
data.direction : enum { IN = 1; OUT = 2; }
preprogrammed data
data.app : enum {none = 0; meteo = 1; trafic = 2;}
data.type : enum { NONE_TYPE ; FLAT ; NANOZTAG ; ZTAMPS ; }
data.pict : enum { NONE_TYPE ;}
data.color : enum { NONE_COL ; RED ; BLUE ; GREEN ; YELLOW ; PINK ; BLACK ; GREY ; ORANGE ; PURPLE ; WHITE ; DARK_RED ; DARK_BLUE ; DARK_GREEN ; DARK_YELLOW ; BROWN ; }
(color are going to change due to specific color of tag sold with Karotz )
ears
karotz.ears.addListener(function(event,step,length));
add a listener on ears
value of event : { “START_LEFT”; “START_RIGHT”; “STOP_LEFT”; “STOP_RIGHT”; “BLOCKED_LEFT”; “BLOCKED_RIGHT”; }
step : number of step the ear has made ( 0 for START_LEFT or START_RIGHT events )
length : time elasped in ms during the ear move ( 0 for START_LEFT or START_RIGHT events )
multimedia
karotz.multimedia.addListener(function(event));
add a listener on multimedia
value of event : { “PLAY”; “STOP”; “PAUSE”; “RESUME”; “MUSIC_UPDATE”; }
PLAY : the multimedia brick start playing
STOP : the multimedia brick stop playing
PAUSE : the multimedia brick is paused
RESUME : the multimedia brick is resumed
MUSIC_UPDATE : the multimedia brick has updated his music database (new flash key, or key removed)
|
|
| Abonnement au groupe KarotzDev |
| Visiter ce groupe |