This document describes how to exchange data between a Lua plugin running on the obniz OS and the obniz JavaScript SDK.
Overview
You can facilitate communication between a Lua plugin operating on the device OS and the JavaScript SDK. This allows for bidirectional data transfer and efficient handling of large data sets.
obniz.plugin.send()
Sends a binary data sequence from the obniz.js side to the Lua plugin.
Javascript Example
// Send string data
obniz.plugin.send("obniz.js send data get device?");
// Send byte array
obniz.plugin.send([0x00, 0x01, 0x02]);
Lua Plugin Side
On the Lua side, use the on_command() function to receive the data.
function on_command(command)
-- 'command' contains the data sent from JS
os.log(command)
end
obniz.plugin.onreceive
This event handler receives a binary data sequence sent from the Lua plugin to the obniz.js side.
Javascript Example
obniz.plugin.onreceive = (data) => {
console.log(data);
};
Lua Plugin Side
Use cloud.pluginSend() to send data to the JavaScript SDK.
cloud.pluginSend("123");
obniz.plugin.onFrameStart / onFrameEnd
These handlers are triggered when frame information is sent from the plugin side.
These functions are particularly useful for segmenting and managing large data transfers.
onFrameStart: Receives theframe_idand the totallengthof the data.onreceive: For large payloads, the data will be received in multiple chunks through this handler.onFrameEnd: Called once all data defined in the frame has been completely received.
Javascript Example
// Handle the start of a frame
obniz.plugin.onFrameStart = (frame_id, length) => {
console.log(`Frame start: ID=${frame_id}, Total Length=${length}`);
};
// Handle the end of a frame
obniz.plugin.onFrameEnd = () => {
console.log("Frame reception completed");
};
// Receive chunks of data
obniz.plugin.onreceive = (data) => {
console.log(`Received chunk: ${Buffer.from(data).toString()}`);
};
Lua Plugin Side
To use framing in a Lua plugin, use the following commands:
local frame_id = 1
local total_length = 9
cloud.pluginSendFrameStart(frame_id, total_length); -- Start framing
cloud.pluginSend("123");
cloud.pluginSend("456");
cloud.pluginSend("789");
cloud.pluginSendFrameEnd(); -- End framing