refactor: Change the setting update validation, so we don't print unnecessary warnings
MODIFY
lib/basedriver/device-settings.js
lib/basedriver/device-settings.js
@@ -73,22 +73,24 @@ class DeviceSettings {
// calls updateSettings from implementing driver every time a setting is changed.
async update (newSettings) {
- if (!_.isObject(newSettings)) {
- throw new Error('Settings update should be called with valid JSON');
+ if (!_.isPlainObject(newSettings)) {
+ throw new Error(`Settings update should be called with valid JSON. Got ` +
+ `${JSON.stringify(newSettings)} instead`);
+ }
+ for (const prop of _.keys(newSettings)) {
+ if (!_.isUndefined(this._settings[prop])) {
+ if (this._settings[prop] === newSettings[prop]) {
+ log.debug(`The value of '${prop}' setting did not change. Skipping the update for it`);
+ continue;
}
- for (let prop of _.keys(newSettings)) {
- if (_.isUndefined(this._settings[prop])) {
- log.warn(`Didn't know about setting '${prop}'. Are you sure you ` +
- `spelled it correctly? Proceeding anyway. Valid settings: ${_.keys(this._settings)}`);
}
- if (this._settings[prop] !== newSettings[prop]) {
// update setting only when there is updateSettings defined.
- if (this.onSettingsUpdate) {
+ if (_.isFunction(this.onSettingsUpdate)) {
await this.onSettingsUpdate(prop, newSettings[prop], this._settings[prop]);
this._settings[prop] = newSettings[prop];
} else {
- log.errorAndThrow('Unable to update settings; onSettingsUpdate method not found');
- }
+ log.errorAndThrow(`Unable to update settings; ` +
+ `onSettingsUpdate method not found on '${this.constructor.name}'`);
}
}
}

refactor: Change the setting update validation, so we don't print unnecessary warnings (#372)