Skip to content

Commit

Permalink
fix(miio): Only poll maps via the cloud interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Oct 30, 2022
1 parent 23c5024 commit 62a666d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
9 changes: 9 additions & 0 deletions backend/lib/miio/MiioDummycloudNotConnectedError.js
@@ -0,0 +1,9 @@
class MiioDummycloudNotConnectedError extends Error {
/** @param {object} msg The request message that was not responded to. */
constructor(msg) {
super("Dummycloud not connected. Failed to send message:" + JSON.stringify(msg));
this.name = "MiioDummycloudNotConnectedError";
}
}

module.exports = MiioDummycloudNotConnectedError;
21 changes: 17 additions & 4 deletions backend/lib/robots/MiioValetudoRobot.js
Expand Up @@ -16,6 +16,7 @@ const Tools = require("../utils/Tools");
const ValetudoRobot = require("../core/ValetudoRobot");

const entities = require("../entities");
const MiioDummycloudNotConnectedError = require("../miio/MiioDummycloudNotConnectedError");
const stateAttrs = entities.state.attributes;

class MiioValetudoRobot extends ValetudoRobot {
Expand Down Expand Up @@ -356,14 +357,26 @@ class MiioValetudoRobot extends ValetudoRobot {
* @param {object} options
* @param {number=} options.retries
* @param {number=} options.timeout custom timeout in milliseconds
* @param {boolean=} options.preferLocalInterface
* @param {"local"|"cloud"=} options.interface
* @returns {Promise<object>}
*/
sendCommand(method, args = [], options = {}) {
if (this.dummyCloud.miioSocket.connected && options.preferLocalInterface !== true) {
return this.sendCloud({"method": method, "params": args}, options);
const msg = {"method": method, "params": args};

if (options.interface === "cloud") {
if (this.dummyCloud.miioSocket.connected) {
return this.sendCloud(msg, options);
} else {
throw new MiioDummycloudNotConnectedError(msg);
}
} else if (options.interface === "local") {
return this.localSocket.sendMessage(msg, options);
} else {
return this.localSocket.sendMessage({"method": method, "params": args}, options);
if (this.dummyCloud.miioSocket.connected) {
return this.sendCloud(msg, options);
} else {
return this.localSocket.sendMessage(msg, options);
}
}
}

Expand Down
Expand Up @@ -58,7 +58,7 @@ class MiioWifiConfigurationCapability extends LinuxWifiConfigurationCapability {
"config_type": "app"
},
{
preferLocalInterface: true
interface: "local"
}
);
} else {
Expand Down
9 changes: 7 additions & 2 deletions backend/lib/robots/dreame/DreameValetudoRobot.js
Expand Up @@ -10,6 +10,7 @@ const AttachmentStateAttribute = require("../../entities/state/attributes/Attach
const AttributeSubscriber = require("../../entities/AttributeSubscriber");
const CallbackAttributeSubscriber = require("../../entities/CallbackAttributeSubscriber");
const entities = require("../../entities");
const MiioDummycloudNotConnectedError = require("../../miio/MiioDummycloudNotConnectedError");
const MiioErrorResponseRobotFirmwareError = require("../../miio/MiioErrorResponseRobotFirmwareError");
const MiioValetudoRobot = require("../MiioValetudoRobot");
const PendingMapChangeValetudoEvent = require("../../valetudo_events/events/PendingMapChangeValetudoEvent");
Expand Down Expand Up @@ -74,7 +75,10 @@ class DreameValetudoRobot extends MiioValetudoRobot {
value: "{\"frame_type\":\"I\", \"force_type\": 1, \"req_type\": 1}"
}]
},
{timeout: 7000} // user ack timeout seems to appear after ~6s on the p2028 1156
{
timeout: 7000, // user ack timeout seems to appear after ~6s on the p2028 1156
interface: "cloud"
}
);
} catch (e) {
if (e instanceof MiioErrorResponseRobotFirmwareError && e.response?.message === "user ack timeout") {
Expand All @@ -84,11 +88,12 @@ class DreameValetudoRobot extends MiioValetudoRobot {
As this is expected, we just ignore that error
*/
} else if (e instanceof MiioDummycloudNotConnectedError) {
/* intentional */
} else {
Logger.warn("Error while polling map", e);
}


return;
}

Expand Down
12 changes: 11 additions & 1 deletion backend/lib/robots/roborock/RoborockValetudoRobot.js
Expand Up @@ -9,6 +9,7 @@ const entities = require("../../entities");
const LinuxTools = require("../../utils/LinuxTools");
const LinuxWifiScanCapability = require("../common/linuxCapabilities/LinuxWifiScanCapability");
const MapLayer = require("../../entities/map/MapLayer");
const MiioDummycloudNotConnectedError = require("../../miio/MiioDummycloudNotConnectedError");
const MiioValetudoRobot = require("../MiioValetudoRobot");
const PendingMapChangeValetudoEvent = require("../../valetudo_events/events/PendingMapChangeValetudoEvent");
const ValetudoMap = require("../../entities/map/ValetudoMap");
Expand Down Expand Up @@ -375,7 +376,16 @@ class RoborockValetudoRobot extends MiioValetudoRobot {
}

async executeMapPoll() {
return this.sendCloud({"method": "get_map_v1"});
let result;
try {
result = await this.sendCommand("get_map_v1");
} catch (e) {
if (!(e instanceof MiioDummycloudNotConnectedError)) {
throw e;
}
}

return result;
}

determineNextMapPollInterval(pollResponse) {
Expand Down
4 changes: 2 additions & 2 deletions backend/lib/robots/viomi/ViomiValetudoRobot.js
Expand Up @@ -177,7 +177,7 @@ class ViomiValetudoRobot extends MiioValetudoRobot {
* @param {object} options
* @param {number=} options.retries
* @param {number=} options.timeout custom timeout in milliseconds
* @param {boolean=} options.preferLocalInterface
* @param {"local"|"cloud"=} options.interface
* @returns {Promise<object>}
*/
sendCommand(method, args = [], options = {}) {
Expand Down Expand Up @@ -457,7 +457,7 @@ class ViomiValetudoRobot extends MiioValetudoRobot {
}

async executeMapPoll() {
return this.sendCommand("set_uploadmap", [2], {timeout: 2000});
return this.sendCommand("set_uploadmap", [2], {timeout: 2000, interface: "cloud"});
}

preprocessMap(data) {
Expand Down

0 comments on commit 62a666d

Please sign in to comment.