Giter VIP home page Giter VIP logo

motechat's Introduction

MoteChat is an IoT message exchange platform for IoT messages, metadata, files and microservices. Built-in security checks and device maintenance mechanism.

Install

npm i motechat

Usage

Add the motechat module to the code

const mchat = require('motechat');

MoteChat API

api Description
Open Open motechat
Close Close motechat
Publish Publish function
Isolated Publish isolated function
Reg Register to device center
UnReg Un-register from device center
Call Call function of another device
Send Send message to another device
Get Get the information of my device
Set Set the device information of my device
Search Search nearby device
OnEvent Set event handler

Open(conf, reg, cb)

the method that open motechat

Kind: global function

Param Type Description
conf Object the configuration object for init.
conf.AppName String the name of motebus MMA
conf.IOC String the MMA of IOC
conf.DCenter String the MMA of device enter
conf.AppKey String the key string of app
conf.UseWeb String can be 'websocket', 'ajax', or ''
conf.MotebusGW String the IP of motebus gateway
reg Object the information of register ( option, the info of reg to DC )
reg.EiToken String device token
reg.SToken String app token
callback openCallback the result callback function

Example 1

var conf = { "AppName":"", "IOC":"", "DCenter":"", "AppKey":"", "UseWeb":"", "MotebusGW": "127.0.0.1" } 
conf.AppName = 'myfunc'; 
conf.DCenter = '[email protected]:6788'; 
conf.AppKey = 'YfgEeop5'; 
var mChat = require('motechat'); 
mChat.Open(conf, function(result){
   console.log('init result=%s', JSON.stringify(result));  
}

**Example 2: reg to DC directly **

var conf = { "AppName":"", "IOC":"", "DCenter":"", "AppKey":"", "UseWeb":"" } 
conf.AppName = 'myfunc';
conf.DCenter = '[email protected]:6788';  
conf.AppKey = 'YfgEeop5';
var reginfo = {"EiToken":"8dilCCKj","SToken":"baTi52uE"};
var mChat = require('motechat');
mChat.Open(conf, reginfo, function(result){
   console.log('init result=%s', JSON.stringify(result));  
} 

Close(cb)

Close motechat

Param Type Description
cb closeCallback

Publish(app, func, cb)

To publish function at motechat

Kind: global function

Param Type Description
app String the name of function
func function the user function entry which is published at motechat
cb publishCallback

Example

var app = 'func';
var XrpcMcService = {
    "echo": function(head, body){
        console.log("xrpc echo: head=%s", JSON.stringify(head));
        if ( typeof body == 'object')
            sbody = JSON.stringify(body);
        else
            sbody = body;
        console.log("xrpc echo: body=%s", sbody);
        return {"echo":body};
    }
}
mChat.Publish( app, XrpcMcService, function(result){
 console.log('motechat publish: result=%s', JSON.stringify(result));
});

Isolated(func, cb)

To isolated publish function at motechat

Kind: global function

Param Type Description
func function the user function entry which is isolated published at motechat
cb isolatedRequest

Example

var XrpcMcSecService = {
    "echo": function(head, body){
        console.log("xrpc echo: head=%s", JSON.stringify(head));
        if ( typeof body == 'object')	
            sbody = JSON.stringify(body);
        else
            sbody = body;
        console.log("xrpc echo: body=%s", sbody);
        return {"echo":body};
    }
}
mChat.Isolated( XrpcMcSecService, function(result){
 console.log('motechat isolated: result=%s', JSON.stringify(result));
});

Reg(data, cb)

register to device center

Kind: global function

Param Type Description
data Object the information for registration
data.EiToken String device token
data.SToken String app token
data.WIP String WAN ip ( empty means the same as dc )
cb regCallback

Example

var mydev = {"EiToken":"8dilCCKj","SToken":"baTi52uE","WIP":""};
mChat.Reg(mydev, function(result){ 
 console.log('StartSession result=%s', JSON.stringify(result));
});  
//Note: At first time of the device, EiToken and SToken is empty 

UnReg(data, cb)

un-register from device center

Kind: global function

Param Type Description
data Object the information for registration
data.SToken String app token
cb unRegCallback

Example

var mydev = {"SToken":"baTi52uE"};
mChat.UnReg(mydev, function(result){
 console.log('EndSession result=%s', JSON.stringify(result));
});

Call(xrpc, cb)

call the function of other device

Kind: global function

Param Type Description
xrpc Object xrpc control object
xrpc.SToken String app token
xrpc.Topic String the topic name of function
xrpc.Func String the function name
xrpc.Data String the data object for function
xrpc.SendTimeout Number Timeout of send message, by sec.
xrpc.WaitReply Number The wait time of reply, by sec.
cb callCallback

Example

var ddn = '';
var topic = 'ddn://GMH21Ilc';
var func = 'echo';
var data = {"Time":"2018/4/24 10:12:08"};
var t1 = 6;
var t2 = 12;
var xrpc = {"SToken":mydev.SToken,"DDN":ddn,"Topic":topic,"Func":func,"Data":data, "SendTimeout":t1, "WaitReply":t2};
mChat.Call( xrpc, function(reply){
 console.log('CallSession reply=%s', JSON.stringify(reply));
});

Send(xmsg, cb)

send to other device

Kind: global function

Param Type Description
xmsg Object xsmg control object
xmsg.SToken String app token
xmsg.From String DDN of source device
xmsg.Topic String topic of destination device
xmsg.Data String the data which want to be sent
xmsg.SendTimeout Number Timeout of send message, by sec.
xmsg.WaitReply Number The wait time of reply, by sec.
cb sendCallback

Example

var ddn = '';
var topic = 'ddn://GMH21Ilc';
var data = {"message":"Hello World"};
var ddn = GetSocketAttr('ddn', socket.id);
var stoken = GetSocketAttr('stoken', socket.id);
var t1 = 6;
var t2 = 12;
var xmsgctl = {"SToken":stoken,"From":ddn,"DDN":ddn,"Topic":topic,"Data":data, "SendTimeout":t1,"WaitReply":t2};
mChat.Send(xmsgctl, function(reply){
 console.log('sendxmsg reply=%s', JSON.stringify(reply));
});

Get(data, cb)

get my device information

Kind: global function

Param Type Description
data Object the input data object
data.SToken String app token
cb getCallback

Example

var data = {"SToken":mydev.SToken};
mChat.Get(data, function(result){
 console.log(‘GetDeviceInfo result=%s’, result);
});

Set(data, cb)

Set device information

Kind: global function

Param Type Description
data Object input data object
data.SToken String app token
data.EdgeInfo Object {"EiName":"","EiType":"","EiTag":"","EiLoc":""}
cb setCallback

Example

var info = {"EiName":"myEi","EiType":".ei","EiTag":"#my","EiLoc":""};
var data = {"SToken":mydev.SToken,"EdgeInfo":info};
mChat.Set(data, function(result){
 console.log('SetDeviceInfo result=%s', result);
});

Search(data, cb)

Search device by key

Kind: global function

Param Type Description
data Object input data object
data.SToken String app token
data.Keyword String Keyword for search
cb searchCallback

Example

var data = {"SToken":mydev.SToken, "Keyword":"#test"};
mChat.Search(data, function(result){
 console.log('Search result=%s', result);
});  

OnEvent(stype, cb) ⇒ boolean

OnEvent, on event handler

Kind: global function

Param Type Description
stype String "message" is for getxmsg, "state" is for state changed
cb function the user routine entry

Example

var InmsgRcve = function(ch, head, from, to, msgtype, data){
 console.log('InmsgRcve: channel=%s, from=%s, to=%s, msgtype=%s, data=%s', ch, JSON.stringify(from), to, msgtype, JSON.stringify(data));
}	
Var InState = function(state){
 console.log('InState=%s', state);
}
mChat.OnEvent('message',InmsgRcve);
mChat.OnEvent('state', InState);

openCallback : function

Kind: global typedef

Param Type Description
result Object {ErrCode, ErrMsg, result}

closeCallback : function

Kind: global typedef

Param Type Description
result Object {ErrCode, ErrMsg}

publishCallback : function

Kind: global typedef

Param Type Description
result Object {ErrCode,ErrMsg}

isolatedcallback : function

Kind: global typedef

Param Type Description
result Object {ErrCode,ErrMsg}

regCallback : function

Kind: global typedef

Param Type Description
result Object {ErrCode,ErrMsg,result}

unRegCallback : function

Kind: global typedef

Param Type Description
result Object {ErrCode,ErrMsg}

callCallback : function

Kind: global typedef

Param Type Description
result Object {ErrCode,ErrMsg} or callback(reply)

sendCallback : function

Kind: global typedef

Param Type Description
result Object { ErrCode, ErrMsg } or callback(reply)

getCallback : function

Kind: global typedef

Param Type Description
result Object {ErrCode, ErrMsg} or callback(reply)

setCallback : function

Kind: global typedef

Param Type Description
result Object {ErrCode,ErrMsg} or callback(reply)

searchCallback : function

Kind: global typedef

Param Type Description
result Object {ErrCode,ErrMsg} or callback(reply)

motechat's People

Contributors

motebus avatar kevinchuehk avatar kevin3251 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.