-
-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vendor.viomi): ViomiCarpetModeControlCapability (#739)
* viomi: Implement hacky carpet turbo mode capability It seems like it's not possible to retrieve the current carpet turbo setting. Therefore we remember it when we set it, but if it was never set we fail when it is requested. * Handle errors in carpet turbo router * Remove obsolete carpet turbo intensity capability * viomi: Make carpet turbo error more explanatory * viomi: Read robot config for carpet mode setting
- Loading branch information
Showing
7 changed files
with
90 additions
and
29 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
77 changes: 77 additions & 0 deletions
77
lib/robots/viomi/capabilities/ViomiCarpetModeControlCapability.js
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,77 @@ | ||
const CarpetModeControlCapability = require("../../../core/capabilities/CarpetModeControlCapability"); | ||
const fs = require("fs"); | ||
const readline = require("readline"); | ||
|
||
|
||
class ViomiCarpetModeControlCapability extends CarpetModeControlCapability { | ||
/** | ||
* @param {object} options | ||
* @param {import("../../../core/ValetudoRobot")|any} options.robot | ||
* @param {string} [options.carpetConfigFile] | ||
*/ | ||
constructor(options) { | ||
super(options); | ||
this.carpetConfigPath = options.carpetConfigFile; | ||
} | ||
|
||
/** | ||
* This function returns the last set carpet turbo mode setting.. | ||
* Viomi does not have a command to retrieve the current setting, so we fail if it was not set recently. | ||
* | ||
* @abstract | ||
* @returns {Promise<boolean>} | ||
*/ | ||
async isEnabled() { | ||
// Disclaimer: nasty | ||
// noinspection JSUnresolvedVariable | ||
const state = this.robot.carpetModeEnabled; | ||
if (state !== null && state !== undefined) { | ||
return state; | ||
} | ||
|
||
if (this.robot.config.get("embedded") === true && this.carpetConfigPath && fs.existsSync(this.carpetConfigPath)) { | ||
const fileStream = fs.createReadStream(this.carpetConfigPath); | ||
const lineReader = readline.createInterface({ | ||
input: fileStream, | ||
crlfDelay: Infinity | ||
}); | ||
|
||
for await (const line of lineReader) { | ||
if (line.startsWith("m_carpet_turbo")) { | ||
if (line.trim().endsWith("=1")) { | ||
this.robot.carpetModeEnabled = true; | ||
} else if (line.endsWith("=0")) { | ||
this.robot.carpetModeEnabled = false; | ||
} else { | ||
break; | ||
} | ||
return this.robot.carpetModeEnabled; | ||
} | ||
} | ||
} | ||
|
||
throw new Error("Carpet turbo state cannot be remembered by Valetudo. " + | ||
"If you don't remember what you set it to, set it again."); | ||
} | ||
|
||
/** | ||
* @abstract | ||
* @returns {Promise<void>} | ||
*/ | ||
async enable() { | ||
this.robot.carpetModeEnabled = true; | ||
// 0: off, 1: medium, 2: turbo | ||
await this.robot.sendCommand("set_carpetturbo", [2], {}); | ||
} | ||
|
||
/** | ||
* @abstract | ||
* @returns {Promise<void>} | ||
*/ | ||
async disable() { | ||
this.robot.carpetModeEnabled = false; | ||
await this.robot.sendCommand("set_carpetturbo", [0], {}); | ||
} | ||
} | ||
|
||
module.exports = ViomiCarpetModeControlCapability; |
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
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