-
Notifications
You must be signed in to change notification settings - Fork 7
Freebird APIs
- new Freebird() Create a server instance of the Freebird class.
- findById() Find a netcore by name, or find a device/gadget by id.
- findByNet() Find a netcore, a device, or a gadget by network parameters.
- filter() Returns an array of netcores, devices, or gadgets you finding for.
- addTransport() Add a transportation of RPC messaging to freebird.
- start() Start the freebird server.
- stop() Stop the freebird server.
- reset() Reset the freebird server.
- permitJoin() Let the freebird allow devices to join the network.
- remove() Remove a remote device from the network.
- ban() Ban a device from the network.
- unban() Unban a device.
- ping() Ping a remote device.
- maintain() Maintain the machine network.
Create a server instance of the Freebird
class. This document will use freebird
to denote the server.
Arguments:
-
netcores
(Array): An array of netcores to register to the freebird server, where each element in the array must be an instance of theNetcore
class. Currently, there are ready-made netcores of BLE, ZigBee, CoAP, MQTT offered by this project. You can create your own netcore to work with freebird as well. -
options
(Object): An optional configuration. This is reserved for future use.
Returns:
- (Object): freebird
Examples:
var Freebird = require('freebird'),
bleCore = require('freebird-netcore-ble')(),
mqttCore = require('freebird-netcore-mqtt')(),
coapCore = require('freebird-netcore-coap')(),
zigbeeCore = require('freebird-netcore-zigbee')();
var freebird = new Freebird([ bleCore, mqttCore, coapCore, zigbeeCore ]);
Find a netcore by name, or find a device/gadget by id. If you like to find a device/gadget by address, please use .findByNet()
.
Arguments:
-
type
(String): Only accepts'netcore'
,'device'
, or'gadget'
to find a netcore, a device, or a gadget, respectively. -
id
(String | Number):id
is the netcore name when finding for a netcore.id
is a number when finding for a device or a gadget.
Returns:
- (Object): Found component, otherwise
undefined
Examples:
// find netcore by name
freebird.findById('netcore', 'foo_netcore'); // netcore instance
freebird.findById('netcore', 'no_such_netcore'); // undefined (netcore not found)
// find device by id
freebird.findById('device', 88); // device instance
freebird.findById('device', 999); // undefined (device not found)
// find gadget by id
freebird.findById('gadget', 120); // gadget instance
freebird.findById('gadget', 777); // undefined (gadget not found)
Find a netcore, a device, or a gadget by network parameters.
- To find a netcore:
findByNet('netcore', ncName)
- To find a device:
findByNet('device', ncName, permAddr)
- To find a gadget:
findByNet('gadget', ncName, permAddr, auxId)
Arguments:
-
type
(String): Only accepts'netcore'
,'device'
, or'gadget'
to find a netcore, a device, or a gadget, respectively. -
ncName
(String): Netcore name. -
permAddr
(String): Device permanent address which is required when finding for a device or a gadget. -
auxId
(String | Number): Gadget auxiliary id which is required when finding for a gadget.
Returns:
- (Object): Found component, otherwise
undefined
Examples:
// find netcore by name
freebird.findByNet('netcore', 'foo_netcore'); // netcore instance
freebird.findByNet('netcore', 'no_such_netcore'); // undefined (netcore not found)
// find device by its network info
freebird.findByNet('device', 'foo_netcore', '00:0c:29:3e:1b:d2'); // device instance
freebird.findByNet('device', 'no_such_netcore', '00:0c:29:3e:1b:d2'); // undefined (not found)
freebird.findByNet('device', 'foo_netcore', '00:00:00:00:00:00'); // undefined (not found)
// find gadget by its network info
freebird.findByNet('gadget', 'foo_netcore', '00:0c:29:3e:1b:d2', 'humidity/2'); // gadget instance
freebird.findByNet('gadget', 'no_such_netcore', '00:0c:29:3e:1b:d2', 'humidity/2'); // undefined (not found)
freebird.findByNet('gadget', 'foo_netcore', '00:00:00:00:00:00', 'humidity/2'); // undefined (not found)
freebird.findByNet('gadget', 'foo_netcore', '00:0c:29:3e:1b:d2', 'no_such_auxId'); // undefined (not found)
This method returns an array of netcores, devices, or gadgets which contains all elements of pred
returns truthy for.
Arguments:
-
type
(String): Only accepts'netcore'
,'device'
, or'gadget'
to find netcores, devices, or gadgets that meet the prediction, respectively. -
pred
(Function):function (obj) {}
, the function invoked per iteration in a netcores, devices, or gadget collection.
Returns:
- (Array): Returns the new filtered array of components
Examples:
// filter for enabled netcores
freebird.filter('netcore', function (nc) {
return nc.isEnabled();
});
// filter for enabled devices
freebird.filter('device', function (dev) {
return dev.isEnabled();
});
// filter for enabled gadgets
freebird.filter('gadget', function (gad) {
return gad.isEnabled();
});
Add a transportation of RPC messaging to freebird. This project also provides a ready-made websocket transportation freebird-rpc for remote client/server communication. The following example shows how to use it with freebird.
Arguments:
-
transp
(Object): The instance of transportation. -
callback
(Function):function (err) {}
.
Returns:
- none
Examples:
var fbRpc = require('freebird-rpc'),
http = require('http'),
httpServer,
rpcServer;
// create the websocket server
httpServer = http.createServer();
httpServer.listen(3000);
rpcServer = fbRpc.createServer(httpServer);
// register it to freebird
freebird.addTransport(rpcServer, function (err) {
if (err)
console.log(err);
});
Start the freebird server.
Arguments:
-
callback
(Function):function (err) {}
. Get called after started.
Returns:
- none
Examples:
freebird.start(function (err) {
if (!err)
console.log('freebird is up');
else
console.log(err);
});
Stop the freebird server.
Arguments:
-
callback
(Function):function (err) {}
. Get called after stopped.
Returns:
- none
Examples:
nc.stop(function (err) {
if (!err)
console.log('freebird is down');
else
console.log(err);
});
Reset the freebird server.
Arguments:
-
mode
(Number):0
for a soft reset and1
for a hard reset. -
callback
(Function):function (err) {}
. Get called after reset is applied. When freebird has restarted again, a'ready'
event will be fired.
Returns:
- none
Examples:
freebird.on('ready', function () {
console.log('freebird is ready');
});
freebird.reset(0, function (err) {
if (!err)
console.log('freebird starts to run its reset procedure');
else
console.log(err);
});
Let the freebird allow devices to join the network.
Arguments:
-
duration
(Number): Duration in seconds for allowing devices to join the network. Set it to0
can immediately close the admission. -
callback
(Function):function (err, timeLeft) {}
. Get called when freebird starts/stops to permit joining, wheretimeLeft
is a number that indicates time left for device joining in seconds, e.g., 180.
Returns:
- none
Examples:
freebird.permitJoin(180, function (err, timeLeft) {
if (!err)
console.log(timeLeft); // 180
});
freebird.permitJoin(0, function (err, timeLeft) {
if (!err)
console.log(timeLeft); // 0
});
Remove a remote device from the network.
Arguments:
-
ncName
(String): Netcore name. -
permAddr
(String): Permanent address of the device to remove. -
callback
(Function):function (err, permAddr) {}
. Get called after device removed, wherepermAddr
is permananet address of that device.
Returns:
- none
Examples:
freebird.remove('mqtt-core', '00:0c:29:ff:ed:7c', function (err, permAddr) {
if (!err)
console.log(permAddr); // 00:0c:29:ff:ed:7c
});
Ban a device from the network. Once a device is banned, it can never join the network unless you unban it.
Arguments:
-
ncName
(String): Netcore name. -
permAddr
(String): Permanent address of the device to ban. -
callback
(Function):function (err, permAddr) {}
. Get called after device banned, wherepermAddr
is permananet address of that device.
Returns:
- none
Examples:
freebird.ban('mqtt-core', '00:0c:29:ff:ed:7c', function (err, permAddr) {
if (!err)
console.log(permAddr); // 00:0c:29:ff:ed:7c
});
Unban a device.
Arguments:
-
ncName
(String): Netcore name. -
permAddr
(String): Permanent address of the device to ban. -
callback
(Function):function (err, permAddr) {}
. Get called after device unbanned, wherepermAddr
is permananet address of that device.
Returns:
- none
Examples:
freebird.unban('mqtt-core', '00:0c:29:ff:ed:7c', function (err, permAddr) {
if (!err)
console.log(permAddr); // 00:0c:29:ff:ed:7c
});
Ping a remote device.
Arguments:
-
ncName
(String): Netcore name. -
permAddr
(String): Permanent address of the device to ping. -
callback
(Function):function (err, time) {}
. Get called after ping response comes back, wheretime
is the round-trip time in milliseconds, e.g., 16.
Returns:
- none
Examples:
freebird.ping('mqtt-core', '00:0c:29:ff:ed:7c', function (err, time) {
if (!err)
console.log(time); // 42
});
Maintain the machine network. This will re-sync the remote devices and gadgets.
Arguments:
-
ncName
(String): Optional. IfncName
is given, only the corresponding sub-network controlled by the netcore will be maintained. IfncName
is not given, all sub-networks will be maintained. -
callback
(Function):function (err) {}
. Optional. Get called after maintenannce starts.
Returns:
- none
Examples:
freebird.maintain('mqtt-core', function (err) {
if (!err)
console.log('Starts to maintaining the MQTT machine network');
});
freebird.maintain(); // Maintain all ub-networks
Overview
APIs & Events
- APIs
- Events
Advanced Topics
- Remote Process Comm. (RPC)
- Plugin System
Appendix
- Device data object format
- Gadget data object format