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

homebridge-http-notification-server

v2.0.0

Published

An http/https server inside Homebridge to receive notifications from external programs

Downloads

17

Readme

homebridge-http-notification-server

homebridge-http-notification-server can be used together with Homebridge http accessories. Http accessories are Homebridge plugins, which forward HomeKit requests to another program over a http request. An example for such an accessory would be my homebridge-http-switch.

The problem with such accessories is when the state of the external program changes it cannot be directly reflected in HomeKit. So one solution would be that every http accessory packs its own http server to receive state changes. But with multiple switches this becomes a mess very fast.

This is where the homebridge-http-notification-server comes in. It is basically a Homebridge plugin, which is loaded by Homebridge like any other plugin but doesn't register any accessories or platforms. Instead it starts ONE http or https server. Http accessories can register with an unique id. Any request the external program will send to the notification server will be forwarded to the accessory which specified the respective notificationID.

Installation

sudo npm install -g homebridge-http-notification-server

Configuration

The configuration file is located in the homebridge directory and needs to be called notification-server.json

Example:

{
    "hostname": "127.0.0.1",
    "port": 8080,

    "ssl": {
        "privateKey": "/path/to/private-key.prm",
        "certificate": "/path/to/certificate.cert"
    }
}
  • hostname is optional, default value is 0.0.0.0
  • port is required, default value is 8080
  • ssl is optional. When specified notification-server will create an https server with the specified privateKey and certificate. Otherwise a default unsecured http server is started.

How to implement 'homebridge-http-notification-server' into your project

Implementation in the homebridge accessory (receiver)

First of all you need to specify a handler function in your homebridge accessory. homebridge-http-notification-server locates its registration function in the global variable notificationRegistration at plugin initialization time.

In order to be sure, that homebridge-http-notification-server was already loaded by homebridge, you listen on the event didFinishLaunching of the homebridge api.

notificationRegistration(notificationId, handlerFunction[, password]) notificationRegistration function has three parameters, the first two are required.

  • notificationId: this is id needs to be unique per homebridge instance. It is later used to identify the accessory when a request is made to the notification-server
  • handlerFunction: function which is called when the notification-server received a request for the specified notificationId. It needs to have one parameter, which is the json body from the http request.
  • password: this parameter is fully optional. If specified every request to the notification-server must be authenticated with the specified password. Later more on how a request is constructed.

Example http accessory:

let api;

module.exports = function (homebridgeAPI) {
    api = homebridgeAPI;

    homebridgeAPI.registerAccessory("homebridge-http-example-accessory", "HTTP-ACCESSORY", HTTP_ACCESSORY);
};

function HTTP_ACCESSORY(log, config) {
    // Some initialization
    this.name = config.name;
    
    this.service = new Service.Switch(this.name);
    this.service.getCharacteristic(Characteristic.On)
            .on("get", this.getStatus.bind(this))
            .on("set", this.setStatus.bind(this));

    api.on('didFinishLaunching', function() {
        // check if notificationRegistration is set, if not 'notificationRegistration' is probably not installed on the system
        if (global.notificationRegistration && typeof global.notificationRegistration === "function") {
            try {
                global.notificationRegistration("accessory-identifier", this.handleNotification.bind(this), "top-secret-password");
            } catch (error) {
                // notificationID is already taken
            }
        }
    }.bind(this));
}

HTTP_ACCESSORY.prototype = {
    
    identify: function (callback) {
        this.log("Identify requested!");
        callback();
    },

    getServices: function () {
        return [this.service];
    },
    
    handleNotification: function (jsonRequest) {
        const service = jsonRequest.service; // value is optional and only relevant if your accessory exposes multiple services
        
        const characteristic = jsonRequest.characteristic;
        const value = jsonRequest.value;
        
        // #testCharacteristic returns true if the service was added the specified characteristic.
        //  you could ad additional checks to adjust for your needs
        const validCharacteristic = this.service.testCharacteristic(characteristic);
        
        if (!validCharacteristic) {
            this.log("Encountered unknown characteristic when handling notification: " + characteristic);
            return; // in this example we ignore invalid requests
        }
        
        this.service.updateCharacteristic(characteristic, value);
    },
    
    getStatus: function(callback) {
        // request
    },
    
    setStatus: function(on, callback) {
        // request
    }
    
};

Implementation in the http application (sender)

The http application sends a request to the notification-server (inside of homebridge) to update a value of a HomeKit characteristic. The http request must be a POST request. The url would be constructed as follows:

http://<hostname>:<port>/<notificationID> (https://... if ssl is turned on)

In our example the url would look like the following: http://127.0.0.1:8080/accessory-identifier

The POST body would look like the following:

{
    "characteristic": "On",
    "value": true,
    "password": "your-top-secret-password",
    
    "accessory": "example-accessory", // optional, plugin defined 
    "service": "switch-service", // optional, plugin defined
}

Common properties:

  • characteristic is required. It represents the name of the characteristic which is going to be updated. Value must be a string. Of course this only works with characteristics which have the notify permissions in the HAP specifications.
  • value is required.
  • password optional, but required if your accessory defined a password

Plugin defined properties:

  • accessory is fully optional. The type and usage is up to be defined by the plugin. This project just suggest this property to be used to identify a given accessory, if your plugin exposes multiple accessories.
  • service is fully optional. The type and usage is up to be defined by the plugin. This project just suggest this property to be used to identify a given service, if your plugin exposes multiple services.

Some compatible http accessories

Notify me if you want to see your project here.