• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Supereg/homebridge-http-notification-server: An http/https server inside Homebri ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

Supereg/homebridge-http-notification-server

开源软件地址:

https://github.com/Supereg/homebridge-http-notification-server

开源编程语言:

JavaScript 100.0%

开源软件介绍:

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap