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');
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 |
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 motechat
Param | Type | Description |
---|---|---|
cb | closeCallback |
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));
});
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));
});
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
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 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 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 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 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 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, 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);
Kind: global typedef
Param | Type | Description |
---|---|---|
result | Object |
{ErrCode, ErrMsg, result} |
Kind: global typedef
Param | Type | Description |
---|---|---|
result | Object |
{ErrCode, ErrMsg} |
Kind: global typedef
Param | Type | Description |
---|---|---|
result | Object |
{ErrCode,ErrMsg} |
Kind: global typedef
Param | Type | Description |
---|---|---|
result | Object |
{ErrCode,ErrMsg} |
Kind: global typedef
Param | Type | Description |
---|---|---|
result | Object |
{ErrCode,ErrMsg,result} |
Kind: global typedef
Param | Type | Description |
---|---|---|
result | Object |
{ErrCode,ErrMsg} |
Kind: global typedef
Param | Type | Description |
---|---|---|
result | Object |
{ErrCode,ErrMsg} or callback(reply) |
Kind: global typedef
Param | Type | Description |
---|---|---|
result | Object |
{ ErrCode, ErrMsg } or callback(reply) |
Kind: global typedef
Param | Type | Description |
---|---|---|
result | Object |
{ErrCode, ErrMsg} or callback(reply) |
Kind: global typedef
Param | Type | Description |
---|---|---|
result | Object |
{ErrCode,ErrMsg} or callback(reply) |
Kind: global typedef
Param | Type | Description |
---|---|---|
result | Object |
{ErrCode,ErrMsg} or callback(reply) |