Introduction
Essentially, a humix-sense
module communicate with other modules and the local controller using NATS-based pub/sub messaging model : 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 to 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 such 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
( the brain)
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 node-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"],
// (optional)
// 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"],
// (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
},
"debug": true
}
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');
});
Life cycle management
When the humix-sense-controller wants to terminate a process, it publishes a 'stop' command. This allow each module process to terminate gracefully.
humix.on('stop', function(){
// do your cleanup tasks here
});
License
This module is released under Apache 2.0 license