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

Unify logs #415

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class TuyaLan {

this.log.info('Starting discovery...');

TuyaDiscovery.start({ids: deviceIds})
TuyaDiscovery.start({ids: deviceIds, log: this.log})
.on('discover', config => {
if (!config || !config.id) return;
if (!devices[config.id]) return this.log.warn('Discovered a device that has not been configured yet (%s@%s).', config.id, config.ip);
Expand All @@ -116,6 +116,7 @@ class TuyaLan {

const device = new TuyaAccessory({
...devices[config.id], ...config,
log: this.log,
UUID: UUID.generate(PLUGIN_NAME + ':' + config.id),
connect: false
});
Expand All @@ -126,6 +127,7 @@ class TuyaLan {
this.log.info('Adding fake device: %s', config.name);
this.addAccessory(new TuyaAccessory({
...config,
log: this.log,
UUID: UUID.generate(PLUGIN_NAME + ':fake:' + config.id),
connect: false
}));
Expand All @@ -141,6 +143,7 @@ class TuyaLan {

const device = new TuyaAccessory({
...devices[deviceId],
log: this.log,
UUID: UUID.generate(PLUGIN_NAME + ':' + deviceId),
connect: false
});
Expand Down
4 changes: 2 additions & 2 deletions lib/BaseAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ class BaseAccessory {
this.colorFunction = this.device.context.colorFunction && {HSB: 'HSB', HEXHSB: 'HEXHSB'}[this.device.context.colorFunction.toUpperCase()];
if (!this.colorFunction && value) {
this.colorFunction = {12: 'HSB', 14: 'HEXHSB'}[value.length] || 'Unknown';
if (this.colorFunction) console.log(`[Tuya] Color format for ${this.device.context.name} (${this.device.context.version}) identified as ${this.colorFunction} (length: ${value.length}).`);
if (this.colorFunction) this.log.info(`Color format for ${this.device.context.name} (${this.device.context.version}) identified as ${this.colorFunction} (length: ${value.length}).`);
}
if (!this.colorFunction) {
this.colorFunction = 'Unknown';
console.log(`[Tuya] Color format for ${this.device.context.name} (${this.device.context.version}) is undetectable.`);
this.log.info(`Color format for ${this.device.context.name} (${this.device.context.version}) is undetectable.`);
} else if (this.colorFunction === 'HSB') {
// If not overridden by config, use the scale of 1000
if (!this.device.context.scaleBrightness) this.device.context.scaleBrightness = 1000;
Expand Down
16 changes: 3 additions & 13 deletions lib/GarageDoorAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,15 @@ class GarageDoorAccessory extends BaseAccessory {

// function to return a ID string for log messages
_logPrefix() {
return '[Tuya] ' +
(this.manufacturer ? this.manufacturer + ' ' : '') + 'GarageDoor';
return (this.manufacturer ? this.manufacturer + ' ' : '') + 'GarageDoor';
}

// function to prefix a string ID and always log to console
_alwaysLog(...args) { console.log(this._logPrefix(), ...args); }
_alwaysLog(...args) { this.log.info(this._logPrefix(), ...args); }

// function to log to console if debug is on
_debugLog(...args) {
if (this.debug) {
this._alwaysLog(...args);
}
this.log.debug(this._logPrefix(), ...args);
}

// function to return true if the garage door manufacturer is Kogan and false
Expand All @@ -82,13 +79,6 @@ class GarageDoorAccessory extends BaseAccessory {
const service = this.accessory.getService(Service.GarageDoorOpener);
this._checkServiceName(service, this.device.context.name);

// set the debug flag
if (this.device.context.debug) {
this.debug = true;
} else {
this.debug = false;
}

// Set the manufacturer string
// If the manufacturer string matches a known manufacturer, set to that string
// Otherwise set the manufacturer to the defined value
Expand Down
2 changes: 1 addition & 1 deletion lib/MultiOutletAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MultiOutletAccessory extends BaseAccessory {
this.accessory.services
.filter(service => service.UUID === Service.Outlet.UUID && !_validServices.includes(service))
.forEach(service => {
console.log('Removing', service.displayName);
this.log.info('Removing', service.displayName);
this.accessory.removeService(service);
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/RGBTWLightAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class RGBTWLightAccessory extends BaseAccessory {
}

setColorTemperature(value, callback) {
console.log(`[Tuya] setColorTemperature: ${value}`);
this.log.info(`setColorTemperature: ${value}`);
if (value === 0) return callback(null, true);

const newColor = this.convertHomeKitColorTemperatureToHomeKitColor(value);
Expand Down
26 changes: 13 additions & 13 deletions lib/SimpleBlindsAccessory.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class SimpleBlindsAccessory extends BaseAccessory {
.on('get', this.getPositionState.bind(this));

this.device.on('change', changes => {
console.log("[Tuya] Blinds saw change to " + changes[this.dpAction]);
this.log.info(" Blinds saw change to " + changes[this.dpAction]);
if (changes.hasOwnProperty(this.dpAction)) {
switch (changes[this.dpAction]) {
case this.cmdOpen: // Starting to open
Expand All @@ -126,7 +126,7 @@ class SimpleBlindsAccessory extends BaseAccessory {
const durationToOpen = Math.abs(this.assumedPosition - BLINDS_OPEN) * this.duration * 10;
this.changeTime = Date.now() - durationToOpen;

console.log("[Tuya] Blinds will be marked open in " + durationToOpen + "ms");
this.log.info(" Blinds will be marked open in " + durationToOpen + "ms");

if (this.changeTimeout) clearTimeout(this.changeTimeout);
this.changeTimeout = setTimeout(() => {
Expand All @@ -135,7 +135,7 @@ class SimpleBlindsAccessory extends BaseAccessory {
this.changeTime = false;
this.assumedPosition = BLINDS_OPEN;
this.assumedState = BLINDS_STOPPED;
console.log("[Tuya] Blinds marked open");
this.log.info(" Blinds marked open");
}, durationToOpen);
}
break;
Expand All @@ -151,7 +151,7 @@ class SimpleBlindsAccessory extends BaseAccessory {
const durationToClose = Math.abs(this.assumedPosition - BLINDS_CLOSED) * this.duration * 10;
this.changeTime = Date.now() - durationToClose;

console.log("[Tuya] Blinds will be marked closed in " + durationToClose + "ms");
this.log.info(" Blinds will be marked closed in " + durationToClose + "ms");

if (this.changeTimeout) clearTimeout(this.changeTimeout);
this.changeTimeout = setTimeout(() => {
Expand All @@ -160,15 +160,15 @@ class SimpleBlindsAccessory extends BaseAccessory {
this.changeTime = false;
this.assumedPosition = this.minPosition;
this.assumedState = BLINDS_STOPPED;
console.log("[Tuya] Blinds marked closed");
this.log.info(" Blinds marked closed");
}, durationToClose);
}
break;

case this.cmdStop: // Stopped in middle
if (this.changeTimeout) clearTimeout(this.changeTimeout);

console.log("[Tuya] Blinds last change was " + this.changeTime + "; " + (Date.now() - this.changeTime) + 'ms ago');
this.log.info(" Blinds last change was " + this.changeTime + "; " + (Date.now() - this.changeTime) + 'ms ago');

if (this.changeTime) {
/*
Expand All @@ -188,7 +188,7 @@ class SimpleBlindsAccessory extends BaseAccessory {
characteristicCurrentPosition.updateValue(adjustedPosition);
characteristicTargetPosition.updateValue(adjustedPosition);
characteristicPositionState.updateValue(Characteristic.PositionState.STOPPED);
console.log("[Tuya] Blinds marked stopped at " + adjustedPosition + "; assumed to be at " + this.assumedPosition);
this.log.info(" Blinds marked stopped at " + adjustedPosition + "; assumed to be at " + this.assumedPosition);

this.changeTime = this.targetPosition = false;
this.assumedState = BLINDS_STOPPED;
Expand Down Expand Up @@ -241,20 +241,20 @@ class SimpleBlindsAccessory extends BaseAccessory {
}

setTargetPosition(value, callback) {
console.log('[Tuya] Blinds asked to move from ' + this.assumedPosition + ' to ' + value);
this.log.info('Blinds asked to move from ' + this.assumedPosition + ' to ' + value);

if (this.changeTimeout) clearTimeout(this.changeTimeout);
this.targetPosition = value;

if (this.changeTime !== false) {
console.log("[Tuya] Blinds " + (this.assumedState === BLINDS_CLOSING ? 'closing' : 'opening') + " had started " + this.changeTime + "; " + (Date.now() - this.changeTime) + 'ms ago');
this.log.info(" Blinds " + (this.assumedState === BLINDS_CLOSING ? 'closing' : 'opening') + " had started " + this.changeTime + "; " + (Date.now() - this.changeTime) + 'ms ago');
const disposition = ((Date.now() - this.changeTime) / (10 * this.duration));
if (this.assumedState === BLINDS_CLOSING) {
this.assumedPosition = BLINDS_OPEN - disposition;
} else {
this.assumedPosition = this.minPosition + disposition;
}
console.log("[Tuya] Blinds' adjusted assumedPosition is " + this.assumedPosition);
this.log.info(" Blinds' adjusted assumedPosition is " + this.assumedPosition);
}

const duration = Math.abs(this.assumedPosition - value) * this.duration * 10;
Expand All @@ -272,10 +272,10 @@ class SimpleBlindsAccessory extends BaseAccessory {
}

if (value !== BLINDS_OPEN && value !== BLINDS_CLOSED) {
console.log("[Tuya] Blinds will stop in " + duration + "ms");
console.log("[Tuya] Blinds assumed started " + this.changeTime + "; " + (Date.now() - this.changeTime) + 'ms ago');
this.log.info(" Blinds will stop in " + duration + "ms");
this.log.info(" Blinds assumed started " + this.changeTime + "; " + (Date.now() - this.changeTime) + 'ms ago');
this.changeTimeout = setTimeout(() => {
console.log("[Tuya] Blinds asked to stop");
this.log.info(" Blinds asked to stop");
this.setState(this.dpAction, this.cmdStop);
}, duration);
}
Expand Down
Loading