npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ale-iot-hub-mqtt

v0.0.15

Published

Alcatel Lucent Entrerprise IOT HUB MQTT sdk

Downloads

6

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) ;
    -----
}