-
-
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.
feat(vendor.dreame): ObstacleImagesCapability
- Loading branch information
Showing
10 changed files
with
172 additions
and
8 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
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
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
95 changes: 95 additions & 0 deletions
95
backend/lib/robots/dreame/capabilities/DreameObstacleImagesCapability.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,95 @@ | ||
const DreameMiotHelper = require("../DreameMiotHelper"); | ||
const DreameMiotServices = require("../DreameMiotServices"); | ||
const DreameUtils = require("../DreameUtils"); | ||
const fs = require("fs"); | ||
const Logger = require("../../../Logger"); | ||
const ObstacleImagesCapability = require("../../../core/capabilities/ObstacleImagesCapability"); | ||
|
||
|
||
/** | ||
* @extends ObstacleImagesCapability<import("../DreameValetudoRobot")> | ||
*/ | ||
class DreameObstacleImagesCapability extends ObstacleImagesCapability { | ||
constructor(options) { | ||
super(options); | ||
|
||
this.siid = DreameMiotServices["GEN2"].VACUUM_2.SIID; | ||
this.piid = DreameMiotServices["GEN2"].VACUUM_2.PROPERTIES.AI_CAMERA_SETTINGS.PIID; | ||
|
||
this.helper = new DreameMiotHelper({robot: this.robot}); | ||
} | ||
|
||
/** | ||
* @returns {Promise<boolean>} | ||
*/ | ||
async isEnabled() { | ||
const res = await this.helper.readProperty(this.siid, this.piid); | ||
const deserializedRes = DreameUtils.DESERIALIZE_AI_SETTINGS(res); | ||
|
||
return deserializedRes.obstacleImages; | ||
} | ||
|
||
/** | ||
* @returns {Promise<void>} | ||
*/ | ||
async enable() { | ||
const res = await this.helper.readProperty(this.siid, this.piid); | ||
const deserializedRes = DreameUtils.DESERIALIZE_AI_SETTINGS(res); | ||
|
||
deserializedRes.obstacleImages = true; | ||
|
||
await this.helper.writeProperty( | ||
this.siid, | ||
this.piid, | ||
DreameUtils.SERIALIZE_AI_SETTINGS(deserializedRes) | ||
); | ||
} | ||
|
||
/** | ||
* @returns {Promise<void>} | ||
*/ | ||
async disable() { | ||
const res = await this.helper.readProperty(this.siid, this.piid); | ||
const deserializedRes = DreameUtils.DESERIALIZE_AI_SETTINGS(res); | ||
|
||
deserializedRes.obstacleImages = false; | ||
|
||
await this.helper.writeProperty( | ||
this.siid, | ||
this.piid, | ||
DreameUtils.SERIALIZE_AI_SETTINGS(deserializedRes) | ||
); | ||
} | ||
|
||
/* | ||
* @param {string} image | ||
* @returns {Promise<import('stream').Readable|null>} | ||
*/ | ||
async getStreamForImage(image) { | ||
if (!/^\/data\/record\/\d+\.jpg$/.test(image)) { | ||
/* | ||
Attack scenario: | ||
someone somehow uploads a specially crafted map file containing a path for an obstacle image | ||
that points somewhere that is no obstacle image | ||
*/ | ||
Logger.warn("Unexpected obstacle image path."); | ||
|
||
return null; | ||
} | ||
|
||
try { | ||
return fs.createReadStream(image, { | ||
highWaterMark: 32 * 1024, | ||
autoClose: true | ||
}); | ||
} catch (err) { | ||
if (err.code === "ENOENT") { | ||
return null; | ||
} else { | ||
throw new Error(`Unexpected error while trying to read obstacle image: ${err.message}`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = DreameObstacleImagesCapability; |
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