feat(core): PendingMapChangeHandlingCapability

This commit is contained in:
Sören Beye 2021-07-25 19:01:44 +02:00
parent e70041fd03
commit ed0455d66a
8 changed files with 181 additions and 1 deletions

View File

@ -0,0 +1,49 @@
const Capability = require("./Capability");
const NotImplementedError = require("../NotImplementedError");
/**
* Naming this is surprisingly hard.
*
* Anyways, this shall contain logic to handle the button presses which in the original app
* pop up when the robot thinks that it has discovered a new map but wants user confirmation
* to do anything with it such as overwriting the old one.
*/
/**
* @template {import("../ValetudoRobot")} T
* @extends Capability<T>
*/
class PendingMapChangeHandlingCapability extends Capability {
/**
* @abstract
* @returns {Promise<boolean>}
*/
async hasPendingChange() {
throw new NotImplementedError();
}
/**
* @abstract
* @returns {Promise<void>}
*/
async acceptChange() {
throw new NotImplementedError();
}
/**
* @abstract
* @returns {Promise<void>}
*/
async rejectChange() {
throw new NotImplementedError();
}
getType() {
return PendingMapChangeHandlingCapability.TYPE;
}
}
PendingMapChangeHandlingCapability.TYPE = "PendingMapChangeHandlingCapability";
module.exports = PendingMapChangeHandlingCapability;

View File

@ -15,6 +15,7 @@ module.exports = {
MapSegmentRenameCapability: require("./MapSegmentRenameCapability"),
MapSegmentationCapability: require("./MapSegmentationCapability"),
MapSnapshotCapability: require("./MapSnapshotCapability"),
PendingMapChangeHandlingCapability: require("./PendingMapChangeHandlingCapability"),
PersistentMapControlCapability: require("./PersistentMapControlCapability"),
PresetSelectionCapability: require("./PresetSelectionCapability"),
SensorCalibrationCapability: require("./SensorCalibrationCapability"),

View File

@ -78,7 +78,8 @@ const CAPABILITY_TYPE_TO_ROUTER_MAPPING = {
[capabilities.VoicePackManagementCapability.TYPE]: capabilityRouters.VoicePackManagementCapabilityRouter,
[capabilities.MapSegmentEditCapability.TYPE]: capabilityRouters.MapSegmentEditCapabilityRouter,
[capabilities.MapResetCapability.TYPE]: capabilityRouters.MapResetCapabilityRouter,
[capabilities.MapSegmentRenameCapability.TYPE]: capabilityRouters.MapSegmentRenameCapabilityRouter
[capabilities.MapSegmentRenameCapability.TYPE]: capabilityRouters.MapSegmentRenameCapabilityRouter,
[capabilities.PendingMapChangeHandlingCapability.TYPE]: capabilityRouters.PendingMapChangeHandlingCapabilityRouter
};
module.exports = CapabilitiesRouter;

View File

@ -0,0 +1,43 @@
const CapabilityRouter = require("./CapabilityRouter");
const Logger = require("../../Logger");
class PendingMapChangeHandlingCapabilityRouter extends CapabilityRouter {
initRoutes() {
this.router.get("/", async (req, res) => {
try {
res.json({
pending: await this.capability.hasPendingChange()
});
} catch (e) {
res.status(500).send(e.message);
}
});
this.router.put("/", async (req, res) => {
if (req.body) {
try {
switch (req.body.action) {
case "accept":
await this.capability.acceptChange();
break;
case "reject":
await this.capability.rejectChange();
break;
default:
// noinspection ExceptionCaughtLocallyJS
throw new Error("Invalid action");
}
res.sendStatus(200);
} catch (e) {
Logger.warn("Error while committing map change", e);
res.status(500).json(e.message);
}
} else {
res.status(400).send("Missing parameters in request body");
}
});
}
}
module.exports = PendingMapChangeHandlingCapabilityRouter;

View File

@ -0,0 +1,79 @@
{
"/api/v2/robot/capabilities/PendingMapChangeHandlingCapability": {
"get": {
"tags": [
"PendingMapChangeHandlingCapability"
],
"summary": "Check if there is a pending map change",
"responses": {
"200": {
"description": "Ok",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
}
}
}
}
}
}
}
},
"put": {
"tags": [
"PendingMapChangeHandlingCapability"
],
"summary": "Accept or reject a pending map change",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"action": {
"type": "string",
"enum": [
"accept",
"reject"
]
}
}
}
}
}
},
"responses": {
"200": {
"$ref": "#/components/responses/200"
},
"400": {
"$ref": "#/components/responses/400"
}
}
}
},
"/api/v2/robot/capabilities/PendingMapChangeHandlingCapability/properties": {
"get": {
"tags": [
"PendingMapChangeHandlingCapability"
],
"summary": "Get various capability-related properties",
"responses": {
"200": {
"description": "Ok",
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
}
}
}
}

View File

@ -14,6 +14,7 @@ module.exports = {
MapSegmentRenameCapabilityRouter: require("./MapSegmentRenameCapabilityRouter"),
MapSegmentationCapabilityRouter: require("./MapSegmentationCapabilityRouter"),
MapSnapshotCapabilityRouter: require("./MapSnapshotCapabilityRouter"),
PendingMapChangeHandlingCapabilityRouter: require("./PendingMapChangeHandlingCapabilityRouter"),
PersistentMapControlCapabilityRouter: require("./PersistentMapControlCapabilityRouter"),
PresetSelectionCapabilityRouter: require("./PresetSelectionCapabilityRouter"),
SensorCalibrationCapabilityRouter: require("./SensorCalibrationCapabilityRouter"),

View File

@ -95,6 +95,11 @@ This capability enables you to list all existing map snapshots as well as restor
Snapshots are made automatically by the robots firmware. They're basically backups.
Use this if your robot has lost track of where it is and somehow corrupted the map.
## PendingMapChangeHandlingCapability <a id="PendingMapChangeHandlingCapability"></a>
Some robots may occasionally discover a new map and ask for user confirmation to actually use it.
This capability enables you to either accept or reject the new map.
## PersistentMapControlCapability <a id="PersistentMapControlCapability"></a>
This capability enables you to control whether the robot persists its map across cleanups. When

View File

@ -48,6 +48,7 @@ const options = {
{name: "SpeakerVolumeControlCapability", description: "Speaker volume control capability"},
{name: "VoicePackManagementCapability", description: "Voice pack management capability"},
{name: "CombinedVirtualRestrictionsCapability", description: "Combined virtual restrictions capability"},
{name: "PendingMapChangeHandlingCapability", description: "Pending map change handling capability"}
],
components: {
responses: {