-
-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,145 additions
and
522 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Quirks are Vendor-specific settings that don't (yet) fit into a generic capability | ||
due to them being something that only one vendor/model does. | ||
They are basic toggles/selects and may change at any time. | ||
Availability of quirks may also depend on the firmware version of the robot. | ||
It is not recommended using them in automations etc. They're just here for the Valetudo UI | ||
If there are multiple similar quirks of different vendors, they shall be merged into a capability so | ||
that we don't undermine the core idea of Valetudo being a generic abstraction. | ||
One could probably also consider this a staging area for new stuff | ||
*/ | ||
|
||
class Quirk { | ||
/** | ||
* | ||
* @param {object} options | ||
* @param {string} options.id | ||
* @param {Array<string>} options.options | ||
* @param {string} options.title | ||
* @param {string} options.description | ||
* @param {() => Promise<string>} options.getter | ||
* @param {(value: string) => Promise<void>} options.setter | ||
*/ | ||
constructor(options) { | ||
this.id = options.id; | ||
this.options = options.options; | ||
this.title = options.title; | ||
this.description = options.description; | ||
this.getter = options.getter; | ||
this.setter = options.setter; | ||
} | ||
|
||
/** | ||
* | ||
* @return {Promise<{options: Array<string>, description: string, id: string, title: string, value: string}>} | ||
*/ | ||
async serialize() { | ||
return { | ||
id: this.id, | ||
options: this.options, | ||
title: this.title, | ||
description: this.description, | ||
value: await this.getter() | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Quirk; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const Capability = require("./Capability"); | ||
const Logger = require("../../Logger"); | ||
|
||
/** | ||
* | ||
* @template {import("../ValetudoRobot")} T | ||
* @extends Capability<T> | ||
*/ | ||
class QuirksCapability extends Capability { | ||
/** | ||
* | ||
* @param {object} options | ||
* @param {T} options.robot | ||
* @param {Array<import("../Quirk")>} [options.quirks] | ||
* @class | ||
*/ | ||
constructor(options) { | ||
super(options); | ||
|
||
this.quirks = options.quirks; | ||
} | ||
|
||
/** | ||
* @returns {Promise<Array<{options: Array<string>, description: string, id: string, title: string, value: string}>>} | ||
*/ | ||
async getQuirks() { | ||
let quirkFetchTimeout; | ||
let serializedQuirks = []; | ||
|
||
await Promise.race([ | ||
Promise.all(this.quirks.map(q => { | ||
return new Promise((resolve, reject) => { | ||
q.serialize().then((serializedQuirk) => { | ||
serializedQuirks.push(serializedQuirk); | ||
|
||
resolve(); | ||
}).catch(err => { | ||
Logger.warn(`Error while serializing quirk with ID ${q.id}`, err); | ||
|
||
//We're swallowing this error so that quirks that do work are still available | ||
//in the UI. Still, if this message appears in the logs, it will need investigation | ||
resolve(); | ||
}); | ||
}); | ||
})), | ||
new Promise((resolve, reject) => { | ||
quirkFetchTimeout = setTimeout(() => { | ||
reject(new Error("Timeout while fetching quirks")); | ||
}, 8000); | ||
}) | ||
]); | ||
clearTimeout(quirkFetchTimeout); | ||
|
||
return serializedQuirks; | ||
} | ||
|
||
/** | ||
* | ||
* @param {string} id | ||
* @param {string} value | ||
* @returns {Promise<void>} | ||
*/ | ||
async setQuirkValue(id, value) { | ||
const quirk = this.quirks.find(q => { | ||
return q.id === id; | ||
}); | ||
|
||
if (!quirk) { | ||
throw new Error(`No quirk with ID ${id}`); | ||
} else { | ||
return quirk.setter(value); | ||
} | ||
} | ||
|
||
/** | ||
* @returns {QuirksCapabilityProperties} | ||
*/ | ||
getProperties() { | ||
return {}; | ||
} | ||
|
||
getType() { | ||
return QuirksCapability.TYPE; | ||
} | ||
} | ||
|
||
QuirksCapability.TYPE = "QuirksCapability"; | ||
|
||
module.exports = QuirksCapability; | ||
|
||
/** | ||
* @typedef {object} QuirksCapabilityProperties | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.