UNPKG

node-red-contrib-xstorage

Version:

A Node-RED storageModule for load flows from xStorage and save flows at xStorage.

278 lines (261 loc) 8.36 kB
'use strict' const mchat = require('motechat') const os = require('os') const { name, version } = require('../package.json') const { EventEmitter } = require('events') //let config = require('./config.js') let mcState = false let mcEvent = new EventEmitter() mcEvent.setMaxListeners(0) let settings = {} //debug(`${name} version:${version}`) let conf = { "AppName": (process.env.MCHAT_APPNAME || os.hostname()) + "-xs", "AppKey": process.env.APP_KEY || "1u6WauSf", "DCenter": process.env.MCHAT_DC || "dc", "IOC": process.env.MCHAT_IOC || "", "MotebusGW": process.env.MCHAT_MBGWIP || "127.0.0.1", "MotebusPort": process.env.MCHAT_PORT || "6262", "WatchLevel": process.env.MCHAT_WATCHLEVEL || "0" } let eiInfo = { "EiOwner": process.env.NCHAT_EIOWNER || "", "EiName": (process.env.MCHAT_EINAME || os.hostname()) + "-xs", "EiType": process.env.MCHAT_EITYPE || ".xs", "EiTag": process.env.MCHAT_EITAG || "", "EiLoc": process.env.MCHAT_EILOC || "" } let reginfo = { "SToken":"", "EiToken":"", "WIP":"", "LIP":"", "EdgeInfo":eiInfo, "Option":{"SaveDSIM":true} } let isReg = false; let WIP; let EiUDID; let LIP; let DDN; let checkUDID = process.env.MCHAT_UDID || "0"; class MCClient { static getMessage({ Topic = '', DDN = '', Data } = {}) { return { SToken: reginfo.SToken, Topic, DDN, Data, SendTimeout: process.env.SENDTIMEOUT || 6, WaitReply: process.env.WAITREPLY || 12 } } static getRPC({ Topic = '', DDN = '', Func, Data } = {}) { return { SToken: reginfo.SToken, Topic, DDN, Func, Data, SendTimeout: process.env.SENDTIMEOUT || 6, WaitReply: process.env.WAITREPLY || 12 } } static async setEi({ name, tag, location}){ function isDiffEi({ name, tag, location }) { const { EiName, EiTag, EiLoc } = reginfo.EdgeInfo; if (name !== EiName) return true if (tag !== EiTag) return true if (location !== EiLoc) return true return false } if (!isDiffEi({ name, tag, location })) { return new Promise(resolve => resolve({})) } reginfo.EdgeInfo = {"EiName":name,"EiType":reginfo.EdgeInfo.EiType,"EiTag":tag,"EiLoc":location} return new Promise(resolve => { mchat.Set(reginfo, result => resolve(result)) }) } static async setup() { //debug("DC env: ", process.env.DC) await mchat.Open(conf, async (openret) => { //open motechat EiUDID = openret.Info.udid; //* WIP = openret.Info.wanIP; //* LIP = openret.Info.localIP; //* //console.log('motechat open result=', openret) if (checkUDID === '1'){ reginfo.EdgeInfo.EiName = EiUDID + '-' + eiInfo.EiName; } reginfo.WIP = WIP; reginfo.LIP = LIP; let regret = await mchat.Reg(reginfo); DDN = regret.result.DDN; reginfo.SToken = regret.result.SToken; reginfo.EiToken = regret.result.EiToken; //console.log("motechat reg result= ", regret); let setret = await mchat.Set(reginfo); mchat.OnEvent('message', (ch, inctl, data, cb) => { mcEvent.emit('message', { ch, inctl, data, mcBack: cb }) // cb({ ErrCode: 0, ErrMsg: 'OK' }) }, '') mchat.OnEvent('state', state => mcEvent.emit('state', { state }), '') }) } } const getResult = (reply) => { let result; if(typeof reply == 'object') result = reply; else result = reply[0]; if(Array.isArray(result)) result = result[0]; return result; } const checkNormal = (reply) => { if (!Array.isArray(reply)) { const { ErrCode } = reply return ErrCode == 0 } const [result] = reply if (result.State) { const { ErrCode } = result.State return ErrCode == 0 } if (result.IN.State) { const { ErrCode } = result.IN.State return ErrCode == 0 } } function isRegist(){ return reginfo.SToken; } module.exports = { async setup() { await MCClient.setup(); }, /** * @summary Send the message to the DDN, and return the promise of the reply * @function send * @public * * @param {string} topic the topic * @param {string} DDN the DDN * @param {*} payload the data that send to the DDN * * @returns {Promise<object>} return the send reply */ send(topic, DDN, payload) { return new Promise((resolve, reject) => { const message = MCClient.getMessage({ Topic: topic, DDN, Data: payload }) mchat.Send(message, reply => { const isNormal = checkNormal(reply) if (!isNormal) reject(reply) resolve(reply) }) }) }, callXShare(topic, DDN, func, payload = {}) { return new Promise((resolve, reject) => { let xrpc = MCClient.getRPC({ Topic: topic, DDN, Func: func, Data: payload }) mchat.Call(xrpc, reply => { const isNormal = checkNormal(reply) if (!isNormal) reject(reply) resolve(reply) }) }) }, /** * @summary Call the function of the DDN, and then return the reply * @function call * @public * * @param {stirng} topic the topic * @param {string} DDN the DDN * @param {string} func the function name * @param {*} payload the data that send to the called topic * * @returns {Promise<object>} return the call reply */ call(topic, DDN, func, payload = {}) { return new Promise((resolve, reject) => { let xrpc = MCClient.getRPC({ Topic: topic, DDN, Func: func, Data: payload }) mchat.Call(xrpc, reply => { const isNormal = checkNormal(reply) if (!isNormal) reject(reply) resolve(reply) }) }) }, /** * @summary set the triggerID signal to the event emitter * @function setEvent * @public * * @param {string} event the event signal * @param {function} callback the event callback fucntion */ setEvent(event, callback) { if (event !== "message" && event !== "status") return //debug("setting event") mcEvent.on(event, callback) //debug(mcEvent) }, set(payload = {}) { return new Promise((resolve, reject) => { mchat.mbSetConfig(payload).then(reply => { const isNormal = checkNormal(reply) if (!isNormal) reject(reply) resolve(reply) }).catch(err => { reject(err) }) }) }, /** * @summary Call the function mbGetConfig * @function get * @public * * @param {*} payload the data that send to the called topic * * @returns {Promise<object>} return the call reply */ get(payload = {}) { return new Promise((resolve, reject) => { mchat.mbGetConfig(payload).then(reply => { const isNormal = checkNormal(reply) if (!isNormal) reject(reply) resolve(reply) }).catch(err => { reject(err) }) }) }, /** * @summary remove the event listener from the event emitter * @function removeEvent * @public * * @param {string} event the event signal that removed from the event emitter * @param {function} callback the event callback that removed */ removeEvent(event, callback) { mcEvent.removeListener(event, callback) }, getDDN() { return DDN }, getResult(reply){ return getResult(reply) }, isRegist() { return isRegist(); }, setEiName: MCClient.setEi, reged(){ return reginfo.SToken }, }