公司大佬叫我研究下HomeKit,準(zhǔn)備和公司的產(chǎn)品對(duì)接上萧锉,正常流程肯定不行(需要蘋(píng)果認(rèn)證才能),所有就要學(xué)習(xí)homeBridge這個(gè)神來(lái)之筆。
homeBridge是一個(gè)大神開(kāi)發(fā)了神器茴丰,使用nodejs平臺(tái),破解了蘋(píng)果的HomeKit的那個(gè)什么協(xié)議天吓,可以將自己的設(shè)備添加到蘋(píng)果app(家庭)上面贿肩,然后實(shí)現(xiàn)控制自己的設(shè)備。像小米的很早就實(shí)現(xiàn)了龄寞,俺們公司小不能比汰规。
第一步:了解并下載homeBridge
https://github.com/nfarina/homebridge
首先你需要打開(kāi)下面的鏈接,然后好好的看一遍或者幾遍
https://github.com/nfarina/homebridge
并下載homeBridge
第二步:可能遇到的問(wèn)題
問(wèn)題1:
$ homebridge
No plugins found. See the README for information on installing plugins.
沒(méi)有找到插件物邑,第一步的網(wǎng)頁(yè)有解釋?zhuān)阈枰螺d一個(gè)插件溜哮,或者像我一樣自己開(kāi)發(fā)一個(gè),當(dāng)然我開(kāi)發(fā)的只針對(duì)本公司的設(shè)備
問(wèn)題2:
$ homebridge
Couldn't find a config.json file [snip]
沒(méi)有找到config.json文件色解,你需要?jiǎng)?chuàng)建一個(gè)config.json文件茂嗓,然后將這個(gè)文件放到. homeBridge里面
這時(shí)候你可能迷糊了,. homeBridge在哪科阎?
為了詳細(xì)述吸,我要開(kāi)始圖文解析了:
打開(kāi)Finder
在上圖kk文件下,你的就是mac名稱(chēng)锣笨,同時(shí)按下 command+shift+. 顯示隱藏文件蝌矛,就可以看到. homeBridge了
問(wèn)題3:config.json怎么寫(xiě)
直接復(fù)制吧然后放到. homeBridge下
{
"bridge": {
"name": "Homebridge",
"username": "CC:22:3D:E3:CE:30",
"port": 51825,
"pin": "111-11-122"
},
"description": "This is an example configuration file with pilight plugin.",
"platforms": [
{
"platform": "SamplePlatform",
"sid": ["6409802da3ca"],
"password": ["1234567890123456"]
}]
}
復(fù)制到這兒
platform:為平臺(tái)的名稱(chēng),后面需要注冊(cè)與這兒的名稱(chēng)保證一樣票唆。
第三步:開(kāi)始創(chuàng)建自己的插件
如果你已經(jīng)完成前兩步朴读,執(zhí)行命令會(huì)出現(xiàn)錯(cuò)誤
剛才在config.json寫(xiě)的 "platform": "SamplePlatform"平臺(tái)下未注冊(cè)任何插件,這時(shí)候你已經(jīng)配置好所有前提條件走趋,剩下的就是最后一步衅金,創(chuàng)建一個(gè)插件
1:創(chuàng)建index.js
進(jìn)到homeBridge所在的目錄:/usr/local/lib/node_modules
然后創(chuàng)建一個(gè)文件夾homebridge-sampleplatform
在文件下創(chuàng)建index.js文件
//deviceSwitch里是操作設(shè)備的命令
var kkDeviceApi = require(__dirname + '/deviceSwitch');
var Accessory, Service, Characteristic, UUIDGen;
var that; //指向SamplePlatform 初始化內(nèi)部this
module.exports = function(homebridge) {
console.log("homebridge API version: " + homebridge.version);
// Accessory must be created from PlatformAccessory Constructor
Accessory = homebridge.platformAccessory;
// Service and Characteristic are from hap-nodejs
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
// For platform plugin to be considered as dynamic platform plugin,
// registerPlatform(pluginName, platformName, constructor, dynamic), dynamic must be true
homebridge.registerPlatform("homebridge-sampleplatform", "SamplePlatform", SamplePlatform, true);
};
// Platform constructor
// config may be null
// api may be null if launched from old homebridge version
function SamplePlatform(log, config, api) {
log("SamplePlatform Init");
this.log = log;
this.config = config;
this.kkAccessories = [];
that = this;
console.log('this指向1=',this);
if (api) {
// Save the API object as plugin needs to register new accessory via this object
this.api = api;
// Listen to event "didFinishLaunching", this means homebridge already finished loading cached accessories.
// Platform Plugin should only register new accessory that doesn't exist in homebridge after this event.
// Or start discover new accessories.
this.api.on('didFinishLaunching', function() {
this.log("DidFinishLaunching");
kkDeviceApi.DeviceControl.DeviceDiscovery();
}.bind(this));
}
}
/*---------------為SamplePlatform添加方法------------*/
SamplePlatform.prototype = {
//發(fā)現(xiàn)設(shè)備
onDevice: function (devInfo) {
console.log('this指向2=',that);
var that2 = this; //指向當(dāng)前,回調(diào)函數(shù)需要使用
console.log('onDevice=',devInfo);
//判斷設(shè)備類(lèi)型
var serviceObj = this.decideDevType(devInfo['device_type']);
if (serviceObj === null) {
console.log('未知設(shè)備000');
return;
}
var uuid;
var name;
var newAccessory = null;
var devExit = 0;
var kkService = null;
for (var index in that.kkAccessories) {
var obj = that.kkAccessories[index];
console.log('對(duì)象==', obj);
if (obj.context.did === devInfo['device_mac']) {
devExit = 1;
newAccessory = obj;
break;
}
}
if (devExit) {
console.log("已存在設(shè)備cached accessory: " + newAccessory.context.did);
kkService = newAccessory.getService(serviceObj);
}else {
uuid = UUIDGen.generate(devInfo['device_mac']);
name = devInfo['device_mac'].substring(devInfo['device_mac'].length-6);
console.log("found dev: " + name);
newAccessory = new Accessory(name, uuid);
newAccessory.context.did = devInfo['device_mac'];
newAccessory.context.model = devInfo['device_type'];
kkService = new serviceObj(name);
console.log("新創(chuàng)建設(shè)備newAccessory: " + newAccessory.context.did);
}
console.log('服務(wù)=',kkService);
devInfo.accessory = newAccessory;
kkService
.getCharacteristic(Characteristic.On)
.on('set', function(value, callback) {
console.log('this指向3=',that2);
that2.controlDevice(devInfo['device_mac'], "power", value, callback);
});
// .value = dev.power;
if (devExit) {
if (this.isSwitch(devInfo['device_type'])) {
this.isSwithcGetCharacteristic(kkService, devInfo);
}else if (this.isKlight(devInfo['device_type'])) {
this.isKlightGetCharacteristic(kkService, devInfo);
}
}else {
if (this.isSwitch(devInfo['device_type'])) {
this.isSwithcAddCharacteristic(kkService, devInfo);
}else if (this.isKlight(devInfo['device_type'])) {
this.isKlightAddCharacteristic(kkService, devInfo);
}
}
newAccessory.reachable = true;
if (!devExit) {
console.log('注冊(cè)這個(gè)設(shè)備', newAccessory.context.did);
newAccessory.addService(kkService, name);
that.kkAccessories.push(newAccessory);
that.api.registerPlatformAccessories("homebridge-samplePlatform", "SamplePlatform", [newAccessory]);
}
},
configureAccessory: function(accessory) {
console.log('configureAccessory', accessory.context.did);
//accessory.updateReachability(false);
accessory.reachable = true;
accessory.on('identify', function(paired, callback) {
console.log("identify ....");
});
that.kkAccessories.push(accessory);
},
//執(zhí)行命令
controlDevice: function(did, characteristic, value, callback) {
console.log('控制設(shè)備=',did);
var devInfo = kkDeviceApi.DeviceControl.SearchDeviceInfo(did);
if (devInfo === null) {
console.log("no device found for did: " + did);
return;
}
switch(characteristic.toLowerCase()) {
case 'identify':
console.log("identfy....");
// dev.setBlink();
break;
case 'power':
console.log('power....');
kkDeviceApi.DeviceControl.SetSwitch(did, value);
break;
case 'hue':
// dev.setColor(value, dev.sat);
break;
case 'brightness':
// dev.setBright(value);
break;
case 'saturation':
// dev.setColor(dev.hue, value);
break;
default:
break;
}
if (callback)
callback();
},
decideDevType: function (devType) {
if (devType === 'k2' ||
devType === 'mini_w' ||
devType === 'k2pro' ||
devType === 'k2pro' ||
devType === 'konke_other') {
return Service.Switch;
}else {
return null;
}
},
isSwitch: function (devType) {
if (devType === 'k2' ||
devType === 'mini_w' ||
devType === 'k2pro' ||
devType === 'k2pro') {
return 1;
}
return 1;
},
isKlight: function (devType) {
if (devType === 'klight') {
return 1;
}
return 0;
},
isSwithcGetCharacteristic: function (service, devInfo) {
},
isKlightGetCharacteristic: function (lightService, devInfo) {
var that2 = this; //指向當(dāng)前,回調(diào)函數(shù)需要使用
lightService
.getCharacteristic(Characteristic.Brightness)
.on('set', function(value, callback) { that2.controlDevice(devInfo['devMac'], "brightness", value, callback);})
// .value = dev.bright;
lightService
.getCharacteristic(Characteristic.Hue)
.on('set', function(value, callback) { that2.controlDevice(devInfo['devMac'], "hue", value, callback);})
// .value = dev.hue;
lightService
.getCharacteristic(Characteristic.Saturation)
.on('set', function(value, callback) { that2.controlDevice(devInfo['devMac'], "saturation", value, callback);})
// .value = dev.sat;
},
isSwithcAddCharacteristic: function (service, devInfo) {
},
isKlightAddCharacteristic: function (lightService, devInfo) {
var that2 = this; //指向當(dāng)前氮唯,回調(diào)函數(shù)需要使用
lightService
.addCharacteristic(Characteristic.Brightness)
.on('set', function(value, callback) { that2.controlDevice(devInfo['devMac'], "brightness", value, callback);})
// .value = dev.bright;
lightService
.addCharacteristic(Characteristic.Hue)
.on('set', function(value, callback) { that2.controlDevice(devInfo['devMac'], "hue", value, callback);})
// .value = dev.hue;
lightService
.addCharacteristic(Characteristic.Saturation)
.on('set', function(value, callback) { that2.controlDevice(devInfo['devMac'], "saturation", value, callback);})
// .value = dev.sat;
}
};
exports.SamplePlatform = SamplePlatform;
//里面的方法就不解釋了鉴吹,慢慢看,總能看懂的惩琉。deviceSwitch.js是控制設(shè)備的命令豆励,你懂得
2:創(chuàng)建package.json
{
"name": "homebridge-samplePlatform",
"version": "0.0.1",
"description": "Sample Platform plugin for homebridge: https://github.com/nfarina/homebridge",
"license": "ISC",
"keywords": [
"homebridge-plugin"
],
"repository": {
"type": "git",
"url": "git://github.com/example/homebridge.git"
},
"bugs": {
"url": "http://github.com/example/homebridge/issues"
},
"engines": {
"node": ">=0.12.0",
"homebridge": ">=0.2.0"
}
}
3:在app上添加平臺(tái)
這時(shí)候會(huì)出現(xiàn)一個(gè)二維碼和pin碼
手機(jī)打開(kāi)HomeKit添加配件,掃描二維碼或者pin碼瞒渠,生成一個(gè)平臺(tái)
在deviceSwitch.js文件中會(huì)掃描局域網(wǎng)設(shè)備良蒸,然后調(diào)用index.js中的onDevice()方法將設(shè)備添加到平臺(tái)下,app會(huì)自動(dòng)生成一個(gè)開(kāi)關(guān)或者其他類(lèi)型的設(shè)備
好了伍玖,先就這樣了......