Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link Service.AccessoryInformation to the accessory's one to keep it to date #2207

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ function toTitleCase(str) {
return str.replace(/\w\S*/g, txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
}

function linkAccessoryInformationService(sourceService, targetService, characteristic) {
log.debug(`Linking characteristic ${characteristic}...`);
try {
var value = sourceService.getCharacteristic(characteristic).value;
if (value) {
targetService.setCharacteristic(characteristic, value);
targetService.getCharacteristic(characteristic).on('get', function (callback) {
log.debug(`Characteristic ${characteristic}: GET event triggered!!`);
try {
sourceService.getCharacteristic(characteristic).getValue(callback);
} catch (err) {
log.error(`Characteristic ${characteristic}: GET event error!!`, err);
callback(err);
}
})
}
sourceService.getCharacteristic(characteristic).on('change', function (update) {
log.debug(`Characteristic ${characteristic}: CHANGE event triggered!!`, update);
if (update.newValue) {
targetService.setCharacteristic(characteristic, update.newValue);
}
});
} catch (error) {
log.error(`Linking characteristic ${characteristic}: Error`, error);
}
}

function Server(opts) {
opts = opts || {};

Expand Down Expand Up @@ -455,17 +482,11 @@ Server.prototype._createAccessory = function(accessoryInstance, displayName, acc
var existingService = accessory.getService(Service.AccessoryInformation);

// pull out any values you may have defined
var manufacturer = service.getCharacteristic(Characteristic.Manufacturer).value;
var model = service.getCharacteristic(Characteristic.Model).value;
var serialNumber = service.getCharacteristic(Characteristic.SerialNumber).value;
var firmwareRevision = service.getCharacteristic(Characteristic.FirmwareRevision).value;
var hardwareRevision = service.getCharacteristic(Characteristic.HardwareRevision).value;

if (manufacturer) existingService.setCharacteristic(Characteristic.Manufacturer, manufacturer);
if (model) existingService.setCharacteristic(Characteristic.Model, model);
if (serialNumber) existingService.setCharacteristic(Characteristic.SerialNumber, serialNumber);
if (firmwareRevision) existingService.setCharacteristic(Characteristic.FirmwareRevision, firmwareRevision);
if (hardwareRevision) existingService.setCharacteristic(Characteristic.HardwareRevision, hardwareRevision);
linkAccessoryInformationService(service, existingService, Characteristic.Manufacturer);
linkAccessoryInformationService(service, existingService, Characteristic.Model);
linkAccessoryInformationService(service, existingService, Characteristic.SerialNumber);
linkAccessoryInformationService(service, existingService, Characteristic.FirmwareRevision);
linkAccessoryInformationService(service, existingService, Characteristic.HardwareRevision);
}
else {
accessory.addService(service);
Expand Down