ale-iot-hub-mqtt
v0.0.15
Published
Alcatel Lucent Entrerprise IOT HUB MQTT sdk
Downloads
20
Readme
ALE IOT HUB rpc mqtt Sdk for Node.JS
Installation
Make sure your machine has Node.JS (version 6.x or greater) and do:
npm install ale-iot-hub-mqtt
For more information see :
Usage
For api usage see folder 'samples'. Sample with sdk using mqtt protocol.
Iot Hub example
const IOTHUBClientMQTT = require('ale-iot-hub-mqtt').IOTHUBClientMQTT;
const clientMqtt = new IOTHUBClientMQTT('mqtt://172.27.134.253:1883',
{
localTopic: 'local', remoteTopic: 'iothub'
});
const schemaService = client.SchemaService;
const deviceService = client.DeviceService;
const gatewayService = client.GatewayService;
const subscriberService = client.SubscriberService;
//connected to mqtt broker
client.Events.on('connect', () => {
console.log('evt connect');
})
//disconnected to mqtt broker
client.Events.on('close', () => {
console.log('evt close');
})
//connected to iot hub
client.Events.on('open', () => {
console.log('evt open');
})
//disconnected to iot hub
client.Events.on('off', () => {
console.log('evt off');
})
//connected or disconnected to iothub
client.Events.on('iothub-connected', (connected) => {
console.log('evt iothub-connected=' + connected);
})
client.Events.on('message', (data) => {
let jdata = JSON.parse(data);
if (jdata.message == 'event') {
switch (jdata.resource) {
case "gateways":
switch (jdata.event) {
case 'stop':
console.log("STOP Gateway");
process.exit(0);
break;
case 'created':
case 'updated':
case 'deleted':
break;
}
case "devices":
switch (jdata.event) {
case 'created':
case 'updated':
case 'deleted':
break ;
case 'updated_section':
switch (jdata.section) {
case 'actions' :
// actions
break ;
case 'state' :
//state
break ;
break;
}
break;
}
}
});
client.connect().then(connected => {
test() ;
}) ;
function test() {
gatewayService.getGateway("mqtt.1").then(data => {
console.log(data);
}, (err) => {
console.log(err);
});
deviceService.getDevice("sga$100$lights$reading_left").then(data => {
console.log(data);
}, (err) => {
console.log(err);
});
deviceService.updateDeviceSection('sga$100$lights$reading_left', 'actions', {
data : { light_on: true }
}).then((response) => {
console.log("updateDeviceSection response="+ JSON.stringify(response)) ;
}) ;
};
Rpc example
const MqttRpc = require('../lib/mqtt-rpc').MqttRpc;
const mqttClient = new MqttRpc('mqtt://mqtthost:1883',
{
localTopic: 'local',
// payloadCompressSize: "100kb",
mqttSubscribeOpts : {
qos : 0,
}
});
// connected to MQTT broker
mqttClient.Events.on('connect', () => {
console.log('evt connect');
})
// disconnected from MQTT broker
mqttClient.Events.on('close', () => {
console.log('evt close');
})
// mqtt message received request and reply
mqttClient.Events.on('mqtt-message', (message) => {
console.log( `evt message topic=${message.topic}, len=${message.payload.length}` );
})
//synchronous response
mqttClient.registerMethod('sync/test', (params, header) => {
return 'sync hello ' + params.name
});
//asynchronous response
mqttClient.registerMethod('async/test', async (params, header) => {
return Promise.resolve('async hello ' + params.name);
});
//connect to mqtt broker
mqttClient.connect().then(connected => {
mqttClient.callMethod('local', 'sync/test', { name: 'john' }).then(res => {
console.log(res);
});
mqttClient.callMethod('local', 'async/test', { name: 'john' }, { qos:2}).then(res => {
console.log(res);
});
});
Development documentation
MQTT RPC ALE IOT HUB Api
/// <reference types="node" />
import { EventEmitter } from 'events';
import { IClientOptions, MqttClient, IClientPublishOptions, IClientSubscribeOptions } from 'mqtt';
export interface IRPCOptions {
localTopic?: string;
replyTopic?: string;
requestTimeout?: number;
rpcClientMode?: boolean;
rpcServerMode?: boolean;
debug?: boolean;
payloadMaxSize?: string;
payloadCompressSize?: string;
mqttOpts?: IClientOptions;
mqttPublishOpts?: IClientPublishOptions;
mqttSubscribeOpts?: IClientSubscribeOptions;
}
export class DefaultRPCOptions implements IRPCOptions {
localTopic = 'localTopic';
replyTopic = undefined;
requestTimeout = 30000;
rpcClientMode = true;
rpcServerMode = true;
debug = false;
payloadMaxSize = '950KB';
payloadCompressSize = '300KB';
}
export interface RpcHeader {
srcUuid: string;
srcTopic: string;
}
//Events
//
// Connected th mqtt broker
// Events.on('connect', () => {})
// disconnected from MQTT broker
// Events.on('close', () => {})
// Mqtt message received
// Events.on('mqtt-message', (message: IMqttMessage) => {
// console.log( `evt message topic=${message.topic}, len=${message.payload.length}` );
//}
export interface IMqttMessage {
topic : string;
payload : Buffer ;
}
export interface IMqttRpc {
Url: string;
Connected: boolean;
Events: EventEmitter;
LocalTopic?: string;
RPCOpts: IRPCOptions;
MqttClient: MqttClient | undefined;
connect(): Promise<boolean>;
disconnect(): void;
registerMethod(method: string, cb: (params: { [key: string]: any }, header: RpcHeader) => any): void;
unregisterMethod(method: string): void;
unregisterAllMethods(): void;
notifyMethod(remoteTopic: string, method: string, params?: { [key: string]: any }, mqttpublishOpts?: IClientPublishOptions): void;
callMethod(remoteTopic: string, method: string, params?: { [key: string]: any }, mqttpublishOpts?: IClientPublishOptions, requestTimeout?:number): Promise<any>;
log(cb: () => string): void;
}
export declare class MqttRpc implements IMqttRpc {
-----
constructor(url: string, rpcOpts: IRPCOptions);
-----
}
//# sourceMappingURL=mqtt-rpc.d.ts.map
export interface IIOTHUBOptions extends IRPCOptions {
remoteTopic?: string;
pingTimeout?: number;
}
export class DefaultIOTHUBOptions extends DefaultRPCOptions implements IIOTHUBOptions {
remoteTopic = 'iothub';
localTopic = 'local';
requestTimeout = 60000;
rpcClientMode = true;
rpcServerMode = true;
debug = false;
pingTimeout = 30;
}
export interface IIOTHUBClientMQTT {
Url: string;
Connected: boolean;
IotHubConnected : boolean ;
Events: EventEmitter;
LocalTopic: string | undefined;
MqttRpc: IMqttRpc
connect(): Promise<boolean>;
disconnect(): void;
log(cb: () => string): void;
callMethod(method: string, params: { [key: string]: any }, remoteTopic?: string, requestTimeout?: number): Promise<any>;
notifyMethod(method: string, params: { [key: string]: any }, remoteTopic?: string): Promise<any>;
registerMethod(method: string, cb: (params: { [key: string]: any }, rpcHeader?: RpcHeader) => any): void;
unregisterMethod(method: string): void;
SchemaService: SchemaService;
DeviceService: DeviceService;
GatewayService: GatewayService;
SubscriberService: SubscriberService;
}
export class IOTHUBClientMQTT implements IIOTHUBClientMQTT {
-----
constructor(url: string, opts: IIOTHUBOptions) ;
-----
}