Skip to content

Commit

Permalink
Bonjour Fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mhabedinpour committed Jul 13, 2017
1 parent 747e4a3 commit f3eec67
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ typings/

# Samples JSON Configuration Files
samples/*.json

#HAP Specification
HAP-Specification-Non-Commercial-Version.pdf
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "has-node",
"version": "0.0.2",
"version": "0.0.3",
"description": "Homekit Accessory Server",
"main": "app.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion samples/fan+temperatureSensor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var HAS = require("../");
var config = new HAS.Config('NodeJS Fan+TemperatureSensor', '83:E6:B6:63:BC:2C', HAS.categories.fan, __dirname + '/fan+temperatureSensor.json', 8091, '200-20-200');
var config = new HAS.Config('NodeJS Fan+TemperatureSensor', '83:E6:B6:63:BC:2C', HAS.categories.fan, __dirname + '/fan+temperatureSensor.json', 8097, '200-20-200');
var server = new HAS.Server(config);
var fan = new HAS.Accessory(1);
var fanIdentify = HAS.predefined.Identify(1, undefined, function (value, callback) {
Expand Down
2 changes: 1 addition & 1 deletion samples/fan+temperatureSensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as HAS from '../';

let config = new HAS.Config('NodeJS Fan+TemperatureSensor', '83:E6:B6:63:BC:2C', HAS.categories.fan, __dirname + '/fan+temperatureSensor.json', 8091, '200-20-200');
let config = new HAS.Config('NodeJS Fan+TemperatureSensor', '83:E6:B6:63:BC:2C', HAS.categories.fan, __dirname + '/fan+temperatureSensor.json', 8097, '200-20-200');

let server = new HAS.Server(config);

Expand Down
24 changes: 13 additions & 11 deletions src/HAS.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var HAS = (function () {
var _this = this;
if (Object.keys(this.accessories).length <= 0)
throw new Error('Server must have at least one accessory.');
this.config.increaseCCN(false);
this.updateBonjour();
this.HTTPServer.timeout = 0;
this.HTTPServer.listen(0);
Expand All @@ -33,7 +34,6 @@ var HAS = (function () {
console.log("TCP Server Listening on " + _this.config.TCPPort);
});
this.isRunning = true;
this.config.increaseCCN(false);
};
HAS.prototype.stopServer = function () {
if (this.bonjourService)
Expand All @@ -45,20 +45,22 @@ var HAS = (function () {
this.isRunning = false;
};
HAS.prototype.updateBonjour = function () {
var _this = this;
setTimeout(function () {
if (_this.bonjourService)
_this.bonjourService.stop();
_this.bonjourService = _this.bonjour.publish({
name: _this.config.deviceName,
if (!this.bonjourService) {
this.bonjourService = this.bonjour.publish({
name: this.config.deviceName,
type: 'hap',
port: _this.config.TCPPort,
txt: _this.config.getTXTRecords(),
port: this.config.TCPPort,
txt: this.config.getTXTRecords(),
});
_this.bonjourService.on('up', function () {
this.bonjourService.on('up', function () {
console.log('Bonjour is up');
});
}, this.bonjourService ? 60000 : 0);
}
else {
this.bonjourService.txt = this.config.getTXTRecords();
this.bonjour._server.unregister(this.bonjourService._records());
this.bonjour._server.register(this.bonjourService._records());
}
};
HAS.prototype.addAccessory = function (accessory) {
var accessoryID = accessory.getID();
Expand Down
16 changes: 10 additions & 6 deletions src/HAS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export default class HAS {
if (Object.keys(this.accessories).length <= 0)
throw new Error('Server must have at least one accessory.');

this.config.increaseCCN(false);

this.updateBonjour();

this.HTTPServer.timeout = 0; //TCP connection should stay open as lang as it wants to
Expand All @@ -116,8 +118,6 @@ export default class HAS {
});

this.isRunning = true;

this.config.increaseCCN(false);
}

/**
Expand All @@ -138,9 +138,8 @@ export default class HAS {
* @method Publishes Bonjour service
*/
public updateBonjour() {
setTimeout(() => {
if (this.bonjourService)
this.bonjourService.stop();
if (!this.bonjourService) {
//Publish new service
this.bonjourService = this.bonjour.publish({
name: this.config.deviceName,
type: 'hap',
Expand All @@ -150,7 +149,12 @@ export default class HAS {
this.bonjourService.on('up', () => {
console.log('Bonjour is up');
});
}, this.bonjourService ? 60000 : 0);
} else {
//Update existing server TXT records
this.bonjourService.txt = this.config.getTXTRecords();
this.bonjour._server.unregister(this.bonjourService._records());
this.bonjour._server.register(this.bonjourService._records());
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/TCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ export default class TCP extends EventEmitter {
}
}
data = result;
//console.log(data.toString('ascii'));
}
let {firstLine, rest} = this.readAndDeleteFirstLineOfBuffer(data);
//console.log(data.toString('ascii'));
this.write(Buffer.concat([firstLine, delimiter, Buffer.from(`X-Real-Socket-ID: ${socket.ID}`, 'ascii'), delimiter, rest]));
});

Expand Down

0 comments on commit f3eec67

Please sign in to comment.