Giter Club home page Giter Club logo

humix-sample-module's Introduction

Humix Sample Module

This is a sample humix module that demonstrate how to build new modules that can run on humix sense.

How to use

You can use this module to build a new humix sense module for Humix.

Here are the steps you needs:

  1. config your module
  2. register your module
  3. send events, if any
  4. 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');
});

Execute Sync Command

Sync Command is different than general async command in that it could provide command result to original calling node ( via call back). Here shows an example :

  hsm.on("command1", function (data, done) { 
     
        console.log('do something with command1. data:'+data);
        done('i am done');
               
    })
    );

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

humix-sample-module's People

Contributors

jeffffrey avatar prinzh avatar

Watchers

JimmyLiao avatar James Cloos avatar  avatar  avatar  avatar Yihong Wang avatar Wei-Chieh Hsia avatar  avatar Daniel Shih avatar  avatar Ian avatar Cheng-Yu Hsieh avatar Clement Su avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.