WebSDK

The WebSDK facilitates the integration of the Kitsi platform with your web page. It is used by the Script Virtual block so all methods described here can be accessed from withing Kitsunei Creator.

Methods

abortAll() → {Promise}

Aborts all the following methods: subscribe, listen, waitForBlock, delay

abortDelay()

Use it to stop an active delay.

abortSubscriptions() → {Promise}

Removes all subscriptions so far recorded. Subscriptions are also created when you register for onData events. So calling this method will stop all your onData event listeners.

abortWait()

Cancels a waitForBlock command.

APICall(method, argvs, sourceBlock) → {Promise}

Executes a hardware API function. Read more about the Hub APIs in the official reference page.
Parameters:
Name Type Description
method String the name of the method to execute
argvs Array list of arguments expected by the method.
sourceBlock Object required (only) when calling block's functions. Contains a list of properties to identify the block.
Properties
Name Type Description
address number the BCL address of the block
uid Object the block's uid

broadcast(room, data) → {Promise}

Part of the Broadcaster service. Sends data to all clients in the Kitsi net. Use this method to send data to other clients connected to the same Hub.
Parameters:
Name Type Description
room String the name of the room to broadcast to
data Any the data to transmit.
Example
// Sends a message to any client listening on the room
await Kitsi.broadcast('testRoom', 'Hello');
// Other clients should listen with:
await Kitsi.listen('testRoom', (data)=>{
	console.log("Sender said: ", JSON.stringify(data));
});

delay(the) → {Promise}

When used with 'await', pauses the execution for the given amount of time.
Parameters:
Name Type Description
the number number of milliseconds to wait for
Example
// Waits 2 seconds before moving the car backwards
(async function(){
	console.log("About to wait....");
	await Kitsi.delay(2000);	// 2 seconds
	console.log("Done waiting!!");
}());

getConnectedBlocks() → {Promise.<Array>}

Returns a list of all connected hardware blocks

listen(room, cb) → {Promise}

Broadcaster service Subscribes to a particular channel. use @broadcast() to send data to the channel
Parameters:
Name Type Description
room String the name of the room to listen data from
cb function an event handler to call when data is sent to the room.

offData(block) → {Promise}

Terminates a subscription
Parameters:
Name Type Description
block Object the same object used when creating the onData subscription

onData(block, callback)

Registers an event listener to be called every time the hardware block produces data.
Parameters:
Name Type Description
block Object the representation of the block to listen data from. NOTE: This object will be modified and a property added to record the id of the subscription.
Properties
Name Type Description
address Number the BCL address
uid Object the blocks uid
Properties
Name Type Description
high Number
med Number
low Number
callback function the function to call when data is produced. Usually, the argument passed is an Object type but may vary depending on the block
Example
// Reads data from an IMU block
const imu = await Kitsi.waitForBlock(BlockTypes.IMU);
Kitsi.onData(imu, function(data){
	console.log("X: %d, Y: %d, Z: %d", data.x, data.y, data.z);
});

readSettings(block) → {Promise.<Object>}

Reads the settings from a hardware block
Parameters:
Name Type Description
block Object the block to read settings from
Properties
Name Type Description
uid Object the block's unique identifier
Example
(async function(){
	const block = await Kitsi.waitForBlock(BlockTypes.IMU);
	const settings = await Kitsi.readSettings(block);
	console.log("Settings: %j", settings);
}());

saveSettings(block, settings) → {Promise.<Object>}

Overwrites the settings of a hardware block
Parameters:
Name Type Description
block Object the block to save settings into
Properties
Name Type Description
uid Object the block's unique identifier
settings Object the new settings to store. This MUST be the same object obtained by readSettings().
Example
// Disables the Block's LED blinking
(async function(){
	const imuBlock = await Kitsi.waitForBlock(BlockTypes.IMU);
	const settings = await Kitsi.readSettings(imuBlock);
	settings.BlinkLED = 0;				// Disable blinking
	try{
		await Kitsi.saveSettings(imuBlock, settings);
		console.log("Settings successfully saved!");
	}catch(e){
		console.log("Error saving settings: %s", JSON.stringify(e));
	}
}());

stopListening(room) → {Promise}

Cancels a broadcaster event listener (@listen())
Parameters:
Name Type Description
room String the name of the room used to listen data from.

waitForBlock(serial, optionsopt) → {Object}

Gets a list of online devices from the hub and checks if any of them has the same serial as the provided one.
Parameters:
Name Type Attributes Description
serial number The block type to wait for.
options Object <optional>
An object that contains other properties to be matched.
Properties
Name Type Description
uid Object A block UID with high, low and med properties
address number The block's BCL address.
node Object An object with the properties of the node. Use it to specify that the block you are looking for MUST be connected to a node (radio) with the given properties.
Properties
Name Type Description
uid Object The radio's UID
Examples
// Waits for any temperature block
(async function(){
	const serial = BlockTypes.Temperature;
	const temperature = await Kitsi.waitForBlock(serial);
	console.log("Temperature block [%d] Online!", temperature.address);
}());
(async function(){
	// Waits for a specific temperature block
	const serial = BlockTypes.Temperature;
	const blockUid = {uid: {high: 123, med: 456, low: 789}};
	const temperature = await Kitsi.waitForBlock(serial, blockUid);
	console.log("Temperature block [%d] Online!", temperature.address);
}());