UNPKG

matterbridge-roborock-vacuum-plugin

Version:
149 lines (148 loc) 6.23 kB
import { debugStringify } from 'matterbridge/logger'; import { SimpleMessageListener } from './listener/index.js'; import { RequestMessage } from './model/requestMessage.js'; import { DeviceStatus } from '../Zmodel/deviceStatus.js'; export class MessageProcessor { client; messageListener; logger; constructor(client) { this.client = client; this.messageListener = new SimpleMessageListener(); this.client.registerMessageListener(this.messageListener); } injectLogger(logger) { this.logger = logger; } registerListener(listener) { this.messageListener.registerListener(listener); } async getNetworkInfo(duid) { const request = new RequestMessage({ method: 'get_network_info' }); return await this.client.get(duid, request); } async getDeviceStatus(duid) { const request = new RequestMessage({ method: 'get_status' }); const response = await this.client.get(duid, request); if (response) { this.logger?.debug('Device status: ', debugStringify(response)); return new DeviceStatus(response); } return undefined; } async getDeviceStatusOverMQTT(duid) { const request = new RequestMessage({ method: 'get_status', secure: true }); const response = await this.client.get(duid, request); if (response) { this.logger?.debug('MQTT - Device status: ', debugStringify(response)); return new DeviceStatus(response); } return undefined; } async getRooms(duid) { const request = new RequestMessage({ method: 'get_room_mapping' }); return this.client.get(duid, request); } async gotoDock(duid) { const request = new RequestMessage({ method: 'app_charge' }); return this.client.send(duid, request); } async startClean(duid) { const request = new RequestMessage({ method: 'app_start' }); return this.client.send(duid, request); } async startRoomClean(duid, roomIds, repeat) { const request = new RequestMessage({ method: 'app_segment_clean', params: [{ segments: roomIds, repeat: repeat }], }); return this.client.send(duid, request); } async pauseClean(duid) { const request = new RequestMessage({ method: 'app_pause' }); return this.client.send(duid, request); } async resumeClean(duid) { const request = new RequestMessage({ method: 'app_resume' }); return this.client.send(duid, request); } async stopClean(duid) { const request = new RequestMessage({ method: 'app_stop' }); return this.client.send(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 findMyRobot(duid) { const request = new RequestMessage({ method: 'find_me' }); return this.client.get(duid, request); } 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 { suctionPower: suctionPower, waterFlow: waterFlow, distance_off: distance_off, mopRoute: mopRoute, }; } async changeCleanMode(duid, suctionPower, waterFlow, mopRoute, distance_off) { 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 = 110; const smartMopRoute = 306; const customMopMode = 106; const customMopRoute = 302; if (currentMopMode == smartMopMode && mopRoute == smartMopRoute) return; if (currentMopMode == customMopMode && mopRoute == customMopRoute) return; 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] })); } const CustomizeWithDistanceOff = 207; if (waterFlow && waterFlow == 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] })); } } }