homebridge-kodi
Version:
Kodi plugin for Homebridge
166 lines • 8.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AudioLibraryCleanSwitchAccessory = exports.AudioLibraryScanSwitchAccessory = void 0;
const internal_1 = require("../../internal");
const kodi = require("../kodi");
// ===================================
// = AudioLibraryScanSwitchAccessory =
// ===================================
class AudioLibraryScanSwitchAccessory extends internal_1.KodiAccessory {
constructor(platform, accessory, log, config, name, version) {
super();
this.platform = platform;
this.accessory = accessory;
this.log = log;
this.config = config;
this.name = name;
this.version = version;
this.log.info('Adding AudioLibraryScanSwitchAccessory');
this.accessory.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Name, name)
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'github.com DeutscheMark')
.setCharacteristic(this.platform.Characteristic.Model, 'Homebridge-Kodi AudioLibraryScanSwitch')
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.platform.api.hap.uuid.generate(name))
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, version);
this.switchService =
this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch);
this.switchService.setCharacteristic(this.platform.Characteristic.Name, name);
this.switchService.getCharacteristic(this.platform.Characteristic.On)
.onGet(async () => {
return kodi.getActionResult(this.config, 'XBMC.GetInfoBooleans', { 'booleans': ['Library.IsScanningMusic'] })
.then(([, result]) => {
if (result) {
const isScanning = result['Library.IsScanningMusic'];
this.log.debug('Getting ' + this.name + ': ' + isScanning);
return isScanning;
}
else {
return false;
}
})
.catch(error => {
this.log.error('Getting ' + this.name + ' - Error: ' + error.message);
return false;
});
})
.onSet(async (on) => {
if (on) {
kodi.getActionResult(this.config, 'AudioLibrary.Scan', { 'showdialogs': true })
.then(ok => {
if (ok) {
this.log.debug('Setting ' + this.name + ': ' + on);
}
else {
this.log.debug('Setting ' + this.name + ': ' + on + ' (not ok)');
}
})
.catch(error => {
this.log.error('Setting ' + this.name + ': ' + on + ' - Error: ' + error.message);
});
}
else {
this.log.debug('Setting ' + this.name + ': ' + on + ' - A scan can\'t be stopped.');
this.resetToCurrentStatus(on); // Setting to off is not supported by Kodi JSON-RPC
}
});
}
resetToCurrentStatus(on) {
kodi.getActionResult(this.config, 'XBMC.GetInfoBooleans', { 'booleans': ['Library.IsScanningMusic'] })
.then(([, result]) => {
if (result) {
const isScanning = result['Library.IsScanningMusic'];
this.log.debug('Setting ' + this.name + ': ' + isScanning);
this.updateValue(isScanning);
}
else {
this.log.debug('Setting ' + this.name + ': ' + on + ' (no result)');
this.updateValue(false);
}
})
.catch(error => {
this.log.error('Setting ' + this.name + ': ' + on + ' - Error: ' + error.message);
this.updateValue(false);
});
}
updateValue(on) {
setTimeout(() => {
this.switchService.getCharacteristic(this.platform.api.hap.Characteristic.On).updateValue(on);
}, 100);
}
}
exports.AudioLibraryScanSwitchAccessory = AudioLibraryScanSwitchAccessory;
// ====================================
// = AudioLibraryCleanSwitchAccessory =
// ====================================
class AudioLibraryCleanSwitchAccessory extends internal_1.KodiAccessory {
constructor(platform, accessory, log, config, name, version) {
super();
this.platform = platform;
this.accessory = accessory;
this.log = log;
this.config = config;
this.name = name;
this.version = version;
this.log.info('Adding AudioLibraryCleanSwitchAccessory');
this.accessory.getService(this.platform.Service.AccessoryInformation)
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'github.com DeutscheMark')
.setCharacteristic(this.platform.Characteristic.Model, 'Homebridge-Kodi AudioLibraryCleanSwitch')
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.platform.api.hap.uuid.generate(name))
.setCharacteristic(this.platform.Characteristic.FirmwareRevision, version);
this.switchService =
this.accessory.getService(this.platform.Service.Switch) || this.accessory.addService(this.platform.Service.Switch);
this.switchService.setCharacteristic(this.platform.Characteristic.Name, name);
this.switchService.getCharacteristic(this.platform.Characteristic.On)
.onGet(async () => {
return kodi.storageGetItem(this.platform.api.user.persistPath(), this.name)
.then(on => {
this.log.debug('Getting ' + this.name + ': ' + on);
return on === 'true' ? true : false;
})
.catch(error => {
this.log.error('Getting ' + this.name + ' - Error: ' + error.message);
return false;
});
})
.onSet(async (on) => {
if (on) {
// TODO: This takes as long as the clean and produces Timeout-Errors because of that. Should be more async!
kodi.getActionResult(this.config, 'AudioLibrary.Clean', { 'showdialogs': true })
.then(([ok, result]) => {
if (ok && result) {
this.persistStatus(true);
}
else {
this.log.debug('Setting ' + this.name + ': ' + on + ' - Clean did not start!');
this.updateValueAndPersist(false);
}
})
.catch(error => {
this.log.error('Setting ' + this.name + ': ' + on + ' - Error: ' + error.message);
this.updateValueAndPersist(false);
});
}
else {
this.log.debug('Setting ' + this.name + ': ' + on + ' - A clean can\'t be stopped.');
this.persistStatus(false); // Setting to off is not supported by Kodi JSON-RPC
}
});
}
updateValueAndPersist(on) {
setTimeout(() => {
this.switchService.getCharacteristic(this.platform.api.hap.Characteristic.On).updateValue(on);
}, 100);
this.persistStatus(on);
}
persistStatus(status) {
kodi.storageSetItem(this.platform.api.user.persistPath(), this.name, status ? 'true' : 'false')
.then(() => {
this.log.debug('Setting ' + this.name + ': ' + status + ' - Status is persisted!');
})
.catch(error => {
this.log.error('Setting ' + this.name + ': ' + status + ' - Error: ' + error.message);
});
}
}
exports.AudioLibraryCleanSwitchAccessory = AudioLibraryCleanSwitchAccessory;
//# sourceMappingURL=kodiAudioLibraryAccessory.js.map