ham-dex-subscriber

1.4.12 • Public • Published

ham-dex-subscriber

Subscribe to data, notifications and api from the HAM DEX ESB.

Usage

First, install the package using npm:

npm install ham-dex-subscriber --save

In the options you have to set the parameters given by the HAM DEX provider, i.e.:

options = {
    "subscriber": process.env.HAM_DEX_SUBSCRIBER || "Demo-Subscriber",
    "id": process.env.HAM_DEX_SUBSCRIBER_ID,
    "credentials": {
        "username": process.env.HAM_DEX_USERNAME || "demo-user",
        "password": process.env.HAM_DEX_PASSWORD || "demo-password"
    },
    "esb": {
        "protocol": "https",
        "host": process.env.HAM_DEX_HOST || "ham-dex.local",
        "port": process.env.HAM_DEX_PORT || 3443
    },
    "callback": {
        "protocol": "http",
        // If you leave out "host", HAM DEX Subscriber will determine the current IP address
        // "host": process.env.HOST || "my-dns-name.local",
        "port": process.env.PORT || 3000
    }
};

With that you can instantiate an instance of the ham dex subscriber including the reference to the express app for the notification endpoint:

var subscriber = require("ham-dex-subscriber");
subscriber = subscriber(app, options);

To get the inventory you simply call the getInventoryMethod with the name of the inventory provider:

subscriber.getInventory("Provider name", function(data) {
    // Process data
});

With a third parameter it is possible to extend the inventory call. To get a time span call

var today = new Date();
var tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
subscriber.getInventory("Provider name", function(data) {
    // Process data
}, {
    prefix: 'filterprofile',
    from: today,
    to: tomorrow
});

or

var today = new Date();
var tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
subscriber.getInventory("Provider name", function(data) {
    // Process data
}, {
    field: 'timestamp',
    between: [today, tomorrow]
});

If you want to get a certain item you can ask for it with its Id

subscriber.getInventory("Provider name", function(data) {
    // Process data
}, {
    id: 123456789
});

Another way to extend the url is to set a suffix.

subscriber.getInventory("Provider name", function(data) {
    // Process data
}, {
    suffix: '/roomEntities'
});

To register an subscription to push notifications you call registerSubscription with the name of the notification provider:

var systemstatusSubscription = subscriber.registerSubscription("systemstatus", function(method, data) {
    // Process data
});

You can also pass additional options to a subscription registration:

var workSubscription = subscriber.registerSubscription("workdata", function(method, data) {
    // Process data
}, {
    from: today,
    to: tomorrow,
    beginningSlash: false
});

Another way to get push notification is via websocket.

var systemstatusSubscription = subscriber.registerWebsocket("systemstatus", function(method, data) {
    // Process data
});

To check if a subscription is still active (returns a Promise):

systemstatusSubscription.isActive().then(isActive => {
    // Do whatever ist needed
});

To end a subscription or websocket afterwards call endSubscription:

systemstatusSubscription.endSubscription();

To change the inventory of a provider there are three CRUD methods. These methods return a Promise to implement a synchronous call.

subscriber.input.put("Provider name", { "data": "Testdata" });
subscriber.input.post("Provider name", { "id": "1234", "data": "Test data" });
subscriber.input.delete("Provider name", "1234");

You can also pass additional options to an input service:

subscriber.input.post("Provider name", {
        "id": "1234",
        "data": "Test data"
    }, {
        prefix: 'filterprofile'
});

To call an api of a provider the callGateway method can be used. These method returns a Promise to implement a synchronous call.

subscriber.callGateway("Provider name", "POST", "path/entity", { "id": "1234", "data": "Testdata" }, function(data) {
    // Process data
    return true;
});

The optional parameter logger enables debug logging.

options = {
    [...],
    "logger": logger
}

License

ISC

Readme

Keywords

Package Sidebar

Install

npm i ham-dex-subscriber

Weekly Downloads

2

Version

1.4.12

License

ISC

Unpacked Size

28.7 kB

Total Files

4

Last publish

Collaborators

  • derdobi