Homebridge 插件编写

本文介绍了如何编写Homebridge插件,包括API需求分析、注册Accessory设备、实例化Service和Characteristic、启动homebridge,以及提供了开关和温度传感器插件的示例。通过插件,可以将非HomeKit设备接入HomeKit,实现Siri物联网控制。
摘要由CSDN通过智能技术生成

前言

Homebridge is a lightweight NodeJS server that emulates the iOS HomeKit API; 之前在linux上部署过homebridge,最近玩路由在路由器上部署了homebridge,具体怎么部署就不说了,还是来重温一下homebridge插件如何编写,搭建一个Siri物联网吧,小白不会nodejs有点忧桑!

Plugins can publish Accessories and/or Platforms. Accessories are individual devices, like a smart switch or a garage door. Platforms act like a single device but can expose a set of devices, like a house full of smart lightbulbs. 即插件会发布 Accessories 和 Platforms,Accessories是一个独立的设备,而Platforms是这些设备所连接的同一个平台/同一组设备

如何编写插件

上图可知,homebridge的作用就是连接Homekit协议和Device协议,起一个桥的作用。插件包含两个文件pakage.json和index.js,package.json是管理依赖的,index.js是写插件核心逻辑的。

先来看一下HomeKit协议的layout布局:

  • Home:整栋房子的Accessory设备
  • Room:一间屋子的Accessory设备
  • Platform:一组Accessory设备
  • Accessory:独立的Accessory设备
  • Bridge:特殊的Accessory设备,允许与不能直接与HomeKit通信的Accessory设备通信
  • Service:Service对应Accessory设备的功能,如车库门有开门和关门服务
  • Characteristic:每个服务有一系列的Characteristic特性,每个特性有3个权限,read、write和notify,这些Characteristic特性可以在 here 里找到

再来看一下homebridge插件的文件结构

--> mySwitch
	--> config
		--> config.json #插件配置文件
	--> plugin
		--> index.js #核心逻辑,需要自己编写
		-->package.json #插件包管理

config.json模板如下:

{
   
    "bridge": {
   
        "name": "Homebridge",
        "username": "94:A1:A2:BE:0B:30",
        "port": 59376,
        "pin": "033-73-874"
    },
    "description": "This is an example configuration file with one fake accessory and one fake platform. You can use this as a template for creating your own configuration file containing devices you actually own.",
    "accessories": [
        {
   
            "accessory": "MyDormSwitch",//index.js会调用
            "name": "灯"
        }
    ],
    "platforms": []
}

package.json模板如下:

{
   
  "name": "homebridge-myswitch",//必须以homebridge-开头,index.js会调用
  "version": "1.0.0",
  "description": "this is a switch plugin",
  "main": "index.js",
  "scripts": {
   
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "homebridge-plugin"
  ],
  "author": "Bayer",
  "license": "ISC",
  "engines": {
   
      "node": ">=0.12.0",
      "homebridge": ">=0.2.0"
  }
}
//以上字段必须都要有

第一步:API需求分析

以一个开关插件为例,假设它需要的API有:

  • GET请求能够返回一个布尔值表示当前开关的状态(通过读取switch的On characteristic )
  • POST请求包含一个布尔值表示开关的目标状态(通过设置switch的On characteristic)

第二步:注册Accessory设备

将mySwitch插件注入到homebridge,mySwitch是一个javascript对象

  module.exports = function (homebridge) {
   
  Service = homebridge.hap.Service;
  Characteristic = homebridge.hap.Characteristic;
  /* registerAccessory' three parameters is 
				      plugin-name(must begin with homebridge-* add must be defined in package.json), 
					  accessory-name(must be defined in config.js),
					  constructor-name(must be a function)*/
  homebridge.registerAccessory("homebridge-myswitch", "MyDormSwitch", mySwitch);
};

第三步:实例化Service服务

支持的service有(可以从HomeKitTypes.js文件中找到)

HomeKitTypes.js:2650: * Service "Accessory Information"
HomeKitTypes.js:2674: * Service "Air Purifier" #空气净化器
HomeKitTypes.js:2697: * Service "Air Quality Sensor" #空气质量传感器
HomeKitTypes.js:2727: * Service "Battery Service"
HomeKitTypes.js:2747: * Service "Camera RTP Stream Management"
HomeKitTypes.js:2770: * Service "Carbon Dioxide Sensor"
HomeKitTypes.js:2794: * Service "Carbon Monoxide Sensor"
HomeKitTypes.js:2818: * Service "Contact Sensor" #接触传感器
HomeKitTypes.js:2840: * Service "Door"
HomeKitTypes.js:2862: * Service "Doorbell"
HomeKitTypes.js:2882: * Service "Fan" #风扇
HomeKitTypes.js:2902: * Service "Fan v2"
HomeKitTypes.js:2926: * Service "Filter Maintenance"
HomeKitTypes.js:2946: * Service "Faucet"
HomeKitTypes.js:2965: * Service "Garage Door Opener"
HomeKitTypes.js:2987: * Service "Heater Cooler"
HomeKitTypes.js:3014: * Service "Humidifier Dehumidifier"
HomeKitTypes.js:3041: * Service "Humidity Sensor" #湿度传感器
HomeKitTypes.js:3063: * Service "Irrigation System"
HomeKitTypes.js:3085: * Service "Leak Sensor"
HomeKitTypes.js:3107: * Service "Light Sensor" #光照传感器
HomeKitTypes.js:3129: * Service "Lightbulb" #灯泡
HomeKitTypes.js:3151: * Service "Lock Management"
HomeKitTypes.js:3177: * Service "Lock Mechanism"
HomeKitTypes.js:3196: * Service "Microphone"
HomeKitTypes.js:3215: * Service "Motion Sensor"
HomeKitTypes.js:3237: * Service "Occupancy Sensor" #人体传感器
HomeKitTypes.js:3259: * Service "Outlet"
HomeKitTypes.js:3278: * Service "Security System" #安全系统
HomeKitTypes.js:3300: * Service "Service Label"
HomeKitTypes.js:3318: * Service "Slat"
HomeKitTypes.js:3340: * Service "Smoke Sensor" #烟雾传感器
HomeKitTypes.js:3362: * Service "Speaker"
HomeKitTypes.js:3381: * Service "Stateless Programmable Switch"
HomeKitTypes.js:3400: * Service "Switch" #开关
HomeKitTypes.js:3418: * Service "Temperature Sensor" #温度传感器
HomeKitTypes.js:3440: * Service "Thermostat" #恒温器
HomeKitTypes.js:3466: * Service "Valve"
HomeKitTypes.js:3491: * Service "Window" #窗户
HomeKitTypes.js:3513: * Service "Window Covering" #窗帘

注册switch时涉及到的两个service:

  • AccessoryInformation service:每个Accessory都需要广播与设备本身相关的信息,无论其类型如何;它包含了ManufacturerModel SerialNumber等特性

    /**
     * Service "Accessory Information"
     */
    
    Service.AccessoryInformation = function(displayName, subtype) {
         
      Service.call(this, displayName, '0000003E-0000-1000-8000-0026BB765291', subtype);
    
      // Required Characteristics
      this.addCharacteristic(Characteristic.Identify)
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值