前言
本教程是通過TypeScript編寫的hb-plugin,這是一個簡單的教程肃廓,但是需要具備編程基礎(chǔ)挎峦。為什么用typescript,因為ts支持異步函數(shù)搜吧,避免產(chǎn)生大量的回調(diào)市俊。
A.需求分析
需求:實現(xiàn)在homekit中去控制一盞燈的開關(guān)。
分析:基于homebridge框架進行開發(fā)滤奈,利用socket跟主機進行網(wǎng)絡(luò)通信摆昧,進而實現(xiàn)控制。
B.前期準(zhǔn)備
- vs code
- homebridge環(huán)境 Win10快速搭建Homebridge環(huán)境
- ios設(shè)備
C.進行開發(fā)
1. 注冊Accessory設(shè)備
將Light插件注入到homebridge蜒程,Lightbulb是一個對象
// 注冊Accessory設(shè)備
export default function(homebridge: any) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-Lightbulb", "Lightbulb", Lightbulb);
}
2. 實例化Service服務(wù)
注冊switch時涉及到的兩個service:
- AccessoryInformation service:每個Accessory都需要廣播與設(shè)備本身相關(guān)的信息绅你,無論其類型如何伺帘;它包含了Manufacturer(生產(chǎn)廠家)、SerialNumber(序列號)等特性
- Lightbulb service:真正的燈光service忌锯,Lightbulb設(shè)備具有布爾特性O(shè)n伪嫁,百分比特性Brightness 等;這里我們只做一個開關(guān)燈偶垮。
constructor(log, config) {
this.log = log;
this.name = config["name"];
const informationService = new Service.AccessoryInformation();
const LightbulbService = new Service.Lightbulb(this.name);
LightbulbService
.getCharacteristic(Characteristic.On)
.on("get", this.getLightOnCharacteristic.bind(this))
.on("set", this.setLightOnCharacteristic.bind(this));
this.LightbulbService = LightbulbService;
}
getServices() {
const { LightbulbService } = this;
return [LightbulbService];
}
3. 實例化Characteristic
就是將上面注冊的Service中的getCharacteristic這一部分具體實現(xiàn)张咳。在編程上的意思就是,實現(xiàn)聲明函數(shù)的方法针史。
// get請求
getLightOnCharacteristic(callback: (arg0: any) => void) {
console.log("**************Get on Function**************");
callback(null);
}
// set請求
setLightOnCharacteristic(on: boolean, callback: (arg0: any) => void) {
console.log("**************Set on Function:" + on + "**************");
if (on) {
this.socket.write(onbuf);
} else {
this.socket.write(offbuf);
}
setTimeout(function() {
console.log(inibuf);
}, 500); //500ms Rest init statue
callback(null);
}
4.建立socket
這一部分需要先導(dǎo)入typescript的net包晶伦,然后要在構(gòu)造函數(shù)里創(chuàng)建。
constructor(log, config) {
this.log = log;
this.name = config["name"];
this.socket = new net.Socket();
this.socket.connect(8989, "localhost", function() {
console.log("Connecte Server OK!");
});
this.socket.on("error", console.error); // logs socket error messages
}
5.完整代碼
//mySwitch完整index.js代碼
import net from "net";
let Service: any, Characteristic: any;
let onbuf = "turn on"; //cmd: turn on
let offbuf = "turn off"; //cmd: turn off
let inibuf = "check"; //cmd: reset
// 注冊Accessory設(shè)備
export default function(homebridge: any) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-Lightbulb", "Lightbulb", Lightbulb);
}
class Lightbulb {
log: Function;
name: string;
LightbulbService: any;
socket: net.Socket;
constructor(log, config) {
this.log = log;
this.name = config["name"];
const informationService = new Service.AccessoryInformation();
const LightbulbService = new Service.Lightbulb(this.name);
LightbulbService
.getCharacteristic(Characteristic.On)
.on("get", this.getLightOnCharacteristic.bind(this))
.on("set", this.setLightOnCharacteristic.bind(this));
this.LightbulbService = LightbulbService;
this.socket = new net.Socket();
this.socket.connect(8989, "localhost", function() {
console.log("Connecte Server OK!"); //connect ser2net
});
this.socket.on("error", console.error); // logs socket error messages
}
getServices() {
const { LightbulbService } = this;
return [LightbulbService];
}
getLightOnCharacteristic(callback: (arg0: any) => void) {
console.log("**************Get on Function**************");
callback(null);
}
setLightOnCharacteristic(on: boolean, callback: (arg0: any) => void) {
console.log("**************Set on Function:" + on + "**************");
if (on) {
this.socket.write(onbuf);
} else {
this.socket.write(offbuf);
}
setTimeout(function() {
console.log(inibuf);
}, 500); //500ms Rest init statue
callback(null);
}
}
6. 打包編譯
由于使用的是typescript啄枕,所以要利用rollup和babel將其編譯成js婚陪,這樣homebridge才能識別你的插件。
這一步挺難的频祝,因為要配置一些亂七八糟的泌参,我自己也沒搞懂,所以大家上github看我的demo配置就好了常空。
7. 其他
大致開發(fā)的流程就是這樣沽一,不是太理解的可以先運行我寫的插件homebridge-lightbulb-ts-demo。
- 去github上下載我上傳的項目漓糙,別忘記給個star
- 在你解壓后的文件夾下運行铣缠,
npm install
- 然后運行
npm run build
-
編譯完畢后,如果沒問題的話昆禽,把這個文件夾放到與homebridge平級的目錄下蝗蛙。
image.png -
配置config.json
config.json
{
"bridge": {
"name": "Homebridge",
"username": "94:A1:A2:BE:0B:30",
"port": 59376,
"pin": "031-45-666"
},
"accessories": [
{
"accessory": "MyLightbulb",
"name": "燈"
}
],
"platforms": []
}
-
在cmd窗口運行
homebridge
運行成功 -
掃二維碼添加設(shè)備
succeess 習(xí)題
ok,now you can use siri to control your light,it will very fun.
你可以將這個燈更改為調(diào)光燈交流群
homebridge交流群:107927710