@homebridge-plugins/homebridge-cloudflared-tunnel
Version:
The Cloudflared Tunnel plugin allows you to run a Cloudflare-Tunnel for exposing your homebridge instance for remote access.
180 lines • 8.02 kB
JavaScript
export class deviceBase {
platform;
accessory;
device;
api;
log;
config;
hap;
// Config
deviceLogging;
deviceRefreshRate;
deviceUpdateRate;
devicePushRate;
deviceFirmwareVersion;
constructor(platform, accessory, device) {
this.platform = platform;
this.accessory = accessory;
this.device = device;
this.api = this.platform.api;
this.log = this.platform.log;
this.config = this.platform.config;
this.hap = this.api.hap;
this.getDeviceLogSettings(device);
this.getDeviceRateSettings(device);
this.getDeviceConfigSettings(device);
this.getDeviceContext(accessory, device);
// Set accessory information
accessory
.getService(this.hap.Service.AccessoryInformation)
.setCharacteristic(this.hap.Characteristic.Manufacturer, 'AirNow')
.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName)
.setCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName)
.setCharacteristic(this.hap.Characteristic.Model, accessory.context.model)
.setCharacteristic(this.hap.Characteristic.SerialNumber, accessory.context.serialNumber)
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, this.deviceFirmwareVersion)
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
.updateValue(this.deviceFirmwareVersion);
}
async getDeviceLogSettings(device) {
this.deviceLogging = this.platform.debugMode ? 'debugMode' : device.logging ?? this.platform.platformLogging ?? 'standard';
const logging = this.platform.debugMode ? 'Debug Mode' : device.logging ? 'Device Config' : this.platform.platformLogging ? 'Platform Config' : 'Default';
await this.debugLog(`Using ${logging} Logging: ${this.deviceLogging}`);
}
async getDeviceRateSettings(device) {
// refreshRate
this.deviceRefreshRate = device.refreshRate ?? this.platform.platformRefreshRate ?? 3600;
const refreshRate = device.refreshRate ? 'Device Config' : this.platform.platformRefreshRate ? 'Platform Config' : 'Default';
await this.debugLog(`Using ${refreshRate} refreshRate: ${this.deviceRefreshRate}`);
// updateRate
this.deviceUpdateRate = device.updateRate ?? this.platform.platformUpdateRate ?? 5;
const updateRate = device.updateRate ? 'Device Config' : this.platform.platformUpdateRate ? 'Platform Config' : 'Default';
await this.debugLog(`Using ${updateRate} updateRate: ${this.deviceUpdateRate}`);
// pushRate
this.devicePushRate = device.pushRate ?? this.platform.platformPushRate ?? 1;
const pushRate = device.pushRate ? 'Device Config' : this.platform.platformPushRate ? 'Platform Config' : 'Default';
await this.debugLog(`Using ${pushRate} pushRate: ${this.devicePushRate}`);
}
async getDeviceConfigSettings(device) {
const deviceConfig = {};
const properties = [
'logging',
'refreshRate',
'updateRate',
'pushRate',
];
properties.forEach((prop) => {
if (device[prop] !== undefined) {
deviceConfig[prop] = device[prop];
}
});
if (Object.keys(deviceConfig).length !== 0) {
this.infoLog(`Config: ${JSON.stringify(deviceConfig)}`);
}
}
async getDeviceContext(accessory, device) {
const deviceFirmwareVersion = device.firmware ?? this.platform.version ?? '0.0.0';
const version = deviceFirmwareVersion.toString();
this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
if (version?.includes('.') === false) {
const replace = version?.replace(/^V|-.*$/g, '');
const match = replace?.match(/./g);
const validVersion = match?.join('.');
this.deviceFirmwareVersion = validVersion ?? '0.0.0';
}
else {
this.deviceFirmwareVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
}
accessory
.getService(this.hap.Service.AccessoryInformation)
.setCharacteristic(this.hap.Characteristic.HardwareRevision, this.deviceFirmwareVersion)
.setCharacteristic(this.hap.Characteristic.SoftwareRevision, this.deviceFirmwareVersion)
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, this.deviceFirmwareVersion)
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
.updateValue(this.deviceFirmwareVersion);
this.debugSuccessLog(`deviceFirmwareVersion: ${this.deviceFirmwareVersion}`);
}
/**
* Update the characteristic value and log the change.
*
* @param Service Service
* @param Characteristic Characteristic
* @param CharacteristicValue CharacteristicValue | undefined
* @param CharacteristicName string
* @return: void
*
*/
async updateCharacteristic(Service, Characteristic, CharacteristicValue, CharacteristicName) {
if (CharacteristicValue === undefined) {
this.debugLog(`${CharacteristicName}: ${CharacteristicValue}`);
}
else {
Service.updateCharacteristic(Characteristic, CharacteristicValue);
this.debugLog(`updateCharacteristic ${CharacteristicName}: ${CharacteristicValue}`);
this.debugWarnLog(`${CharacteristicName} context before: ${this.accessory.context[CharacteristicName]}`);
this.accessory.context[CharacteristicName] = CharacteristicValue;
this.debugWarnLog(`${CharacteristicName} context after: ${this.accessory.context[CharacteristicName]}`);
}
}
/**
* Logging for Device
*/
async infoLog(...log) {
if (await this.enablingDeviceLogging()) {
this.log.info(`${this.accessory.displayName}`, String(...log));
}
}
async successLog(...log) {
if (await this.enablingDeviceLogging()) {
this.log.success(`${this.accessory.displayName}`, String(...log));
}
}
async debugSuccessLog(...log) {
if (await this.enablingDeviceLogging()) {
if (await this.loggingIsDebug()) {
this.log.success(`[DEBUG] ${this.accessory.displayName}`, String(...log));
}
}
}
async warnLog(...log) {
if (await this.enablingDeviceLogging()) {
this.log.warn(`${this.accessory.displayName}`, String(...log));
}
}
async debugWarnLog(...log) {
if (await this.enablingDeviceLogging()) {
if (await this.loggingIsDebug()) {
this.log.warn(`[DEBUG] ${this.accessory.displayName}`, String(...log));
}
}
}
async errorLog(...log) {
if (await this.enablingDeviceLogging()) {
this.log.error(`${this.accessory.displayName}`, String(...log));
}
}
async debugErrorLog(...log) {
if (await this.enablingDeviceLogging()) {
if (await this.loggingIsDebug()) {
this.log.error(`[DEBUG] ${this.accessory.displayName}`, String(...log));
}
}
}
async debugLog(...log) {
if (await this.enablingDeviceLogging()) {
if (this.deviceLogging === 'debug') {
this.log.info(`[DEBUG] ${this.accessory.displayName}`, String(...log));
}
else if (this.deviceLogging === 'debugMode') {
this.log.debug(`${this.accessory.displayName}`, String(...log));
}
}
}
async loggingIsDebug() {
return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug';
}
async enablingDeviceLogging() {
return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug' || this.deviceLogging === 'standard';
}
}
//# sourceMappingURL=device.js.map