humix-sense
v1.1.1
Published
Foundation for Humix-Sense modules
Downloads
12
Readme
Introduction
This module is part of Humix Framework. For more information about Humix project, please refer Humix Overview
Essentially, a humix-sense
module is installed on edge devices (e.g. robot). It communicates with other modules and the local controller via NATS-based pub/sub messaging infrastructure.
Each module provide the information it collects by publishing 'event', while it also listen for 'command' to trigger actions the module supports.
This module provides an event-driven model to facilitate the creation of Humix-Sense modules. Basically, you need to proivde a config that specify 1) the name of the module 2) the commands the module accepts 3) the event the module generates 4) any child process the module depends upon ( optional )
With these information, humix-sense
module could automatically generate the namespace for each event generated and monitor the local message bus for incoming commands targeted for this module. In addition, the humix-sense
module also facilitates life-cycle management of the sensor module: it register the module during start up, monitoring the health check events(e.g. Ping/Pong) and commands (e.g. Stop) from local contorller.
The goal is to simplify the effort required for new sensor modules to communicate with the rest of Humix components, including other sensor modules and Humix Think
How to get the code
You can either clone the project of this repository
git clone https://github.com/project-humix/node-humix-sense.git
or get the module from npm repository
npm install humix-sense
How to use
You can use this module to build a new sensor module for Humix. Here are the steps you needs:
- config your module
- register your module
- send events, if any
- receive commands, if any
Provide module information
The following code shows how to config your module and register it with Humix.
// provide default config. If not provided, the module will lookup module.js in current dir to load the config
var config = {
// define the namespace of this module
"moduleName":"test",
// specify the commands to monitor
"commands" : [ "command1", "command2"],
// specify the local events to listen. These are generated by other modules on the edge.
"localEvents: ["localEvent1", "localEvent2"];
// specify the events that will be gneerated by this module.
// If not specified, all events generated with event() function will be emitted
"events" : ["event1","event2"],
/* default logger will contain 2 logger streams:
* - console stream
* - file stream, which create a file under ~/.humix/ directory
*
* configurable options including
* - filename: specify the log file name
* - fileLevel: specify the log level of logger file, default is INFO
* - consoleLevel: specify the log level of console, default is INFO
*/
"log" : {
filename : 'humix-sample-module.log',
fileLevel : 'info',
consoleLevel : 'debug'
}
// (optional)
// if the module is implemented using other language, specify the process to lunch here
"childProcess" : {
"name" : "./test/test.sh",
"params" : "7",
"respawn" : true,
"restart" : 3
},
}
var humix = require('humix-sense')(config);,
Basically you pass the config to the humix-sense module. The module will then register the current sensor with local humix-sense-controller.
The next step is to wait for the confirmation from humix-sense-controller
humix.on('connection', function(humixSensorModule){
hsm = humixSensorModule;
}
Generate events
Once you get the HumixSensorModule
object, you can then generate events with simple syntax like:
hsm.event('event1', 'Hello World');
Listen for commands
and now you can receive command by monitoring the registered commands:
hsm.on('command1', function(data){
console.log('receive data');
});
Example
To get a feeling of how to write a new humix module, you can reference a Sample Humix Module
License
This module is released under Apache 2.0 license