UNPKG

matterbridge-roborock-vacuum-plugin

Version:
155 lines 7.13 kB
import { debugStringify } from 'matterbridge/logger'; import { CleanModeSetting } from '../../../behaviors/roborock.vacuum/core/CleanModeSetting.js'; import { MopRoute, MopWaterFlow, VacuumSuctionPower } from '../../../behaviors/roborock.vacuum/enums/index.js'; import { MapInfo } from '../../../core/application/models/index.js'; import { DeviceStatus, RequestMessage } from '../../models/index.js'; export class V10MessageDispatcher { logger; client; dispatcherName = 'V10MessageDispatcher'; constructor(logger, client) { this.logger = logger; this.client = client; } async getNetworkInfo(duid) { const request = new RequestMessage({ method: 'get_network_info' }); return await this.client.get(duid, request); } async getSerialNumber(duid) { const request = new RequestMessage({ method: 'get_serial_number' }); const response = await this.client.get(duid, request); return response && response.length > 0 ? response[0].serial_number : duid; } async getDeviceStatus(duid) { const request = new RequestMessage({ method: 'get_prop', params: ['get_status'] }); const response = await this.client.get(duid, request); if (response) { this.logger.debug('Device status: ', debugStringify(response)); return new DeviceStatus(duid, response[0]); } return undefined; } /* --------------- Core Data Retrieval --------------- */ async getHomeMap(duid) { const request = new RequestMessage({ method: 'get_map_v1', secure: true }); const response = await this.client.get(duid, request); return response ?? {}; } async getMapInfo(duid) { const request = new RequestMessage({ method: 'get_multi_maps_list' }); const response = (await this.client.get(duid, request)) ?? []; return new MapInfo(response.length > 0 ? response[0] : { max_multi_map: 0, max_bak_map: 0, multi_map_count: 0, map_info: [] }); } async getRoomMap(duid, activeMap) { const request = new RequestMessage({ method: 'get_room_mapping' }); const response = (await this.client.get(duid, request)) ?? []; return response; } /* ---------------- Cleaning Commands ---------------- */ async goHome(duid) { const request = new RequestMessage({ method: 'app_charge' }); await this.client.send(duid, request); } async startCleaning(duid) { const request = new RequestMessage({ method: 'app_start' }); await this.client.send(duid, request); } async startRoomCleaning(duid, roomIds, repeat) { const request = new RequestMessage({ method: 'app_segment_clean', params: [{ segments: roomIds, repeat: repeat }], }); await this.client.send(duid, request); } async pauseCleaning(duid) { const request = new RequestMessage({ method: 'app_pause' }); await this.client.send(duid, request); } async resumeCleaning(duid) { const request = new RequestMessage({ method: 'app_resume' }); await this.client.send(duid, request); } async resumeRoomCleaning(duid) { const request = new RequestMessage({ method: 'resume_segment_clean' }); await this.client.send(duid, request); } async stopCleaning(duid) { const request = new RequestMessage({ method: 'app_stop' }); await this.client.send(duid, request); } async findMyRobot(duid) { const request = new RequestMessage({ method: 'find_me' }); await this.client.get(duid, request); } async sendCustomMessage(duid, def) { const request = new RequestMessage(def); return this.client.send(duid, request); } getCustomMessage(duid, def) { return this.client.get(duid, def); } async getCleanModeData(duid) { const currentMopMode = await this.getCustomMessage(duid, new RequestMessage({ method: 'get_mop_mode' })); const suctionPowerRaw = await this.getCustomMessage(duid, new RequestMessage({ method: 'get_custom_mode' })); const waterFlowRaw = await this.getCustomMessage(duid, new RequestMessage({ method: 'get_water_box_custom_mode' })); let suctionPower; let waterFlow; let mopRoute; let distance_off = 0; if (Array.isArray(suctionPowerRaw)) { suctionPower = suctionPowerRaw[0]; } else { suctionPower = suctionPowerRaw; } if (Array.isArray(currentMopMode)) { mopRoute = currentMopMode[0]; } else { mopRoute = currentMopMode; } if (typeof waterFlowRaw === 'object' && waterFlowRaw !== null && 'water_box_mode' in waterFlowRaw) { waterFlow = waterFlowRaw.water_box_mode; if ('distance_off' in waterFlowRaw) { distance_off = waterFlowRaw.distance_off ?? 0; } } else { waterFlow = waterFlowRaw; } return new CleanModeSetting(suctionPower, waterFlow, distance_off, mopRoute, undefined); } async changeCleanMode(duid, setting) { const { suctionPower, waterFlow, distance_off, mopRoute } = setting; this.logger.notice(`Change clean mode for ${duid} to suctionPower: ${suctionPower}, waterFlow: ${waterFlow}, mopRoute: ${mopRoute}, distance_off: ${distance_off}`); const currentMopMode = await this.getCustomMessage(duid, new RequestMessage({ method: 'get_custom_mode' })); const smartMopMode = VacuumSuctionPower.Smart; const smartMopRoute = MopRoute.Smart; const customMopMode = VacuumSuctionPower.Custom; const customMopRoute = MopRoute.Custom; if (currentMopMode == smartMopMode && mopRoute == smartMopRoute) return; if (currentMopMode == customMopMode && mopRoute == customMopRoute) return; // if change mode from smart plan, firstly change to custom if (currentMopMode == smartMopMode) { await this.client.send(duid, new RequestMessage({ method: 'set_mop_mode', params: [customMopRoute] })); } if (suctionPower && suctionPower != 0) { await this.client.send(duid, new RequestMessage({ method: 'set_custom_mode', params: [suctionPower] })); } if (waterFlow && waterFlow == MopWaterFlow.CustomizeWithDistanceOff && distance_off && distance_off != 0) { await this.client.send(duid, new RequestMessage({ method: 'set_water_box_custom_mode', params: { 'water_box_mode': waterFlow, 'distance_off': distance_off }, })); } else if (waterFlow && waterFlow != 0) { await this.client.send(duid, new RequestMessage({ method: 'set_water_box_custom_mode', params: [waterFlow] })); } if (mopRoute && mopRoute != 0) { await this.client.send(duid, new RequestMessage({ method: 'set_mop_mode', params: [mopRoute] })); } } } //# sourceMappingURL=V10MessageDispatcher.js.map