matterbridge-roborock-vacuum-plugin
Version:
Matterbridge Roborock Vacuum Plugin
105 lines • 4.23 kB
JavaScript
import { DeviceError } from '../errors/index.js';
export class MessageRoutingService {
logger;
iotApi;
messageDispatcherMap = new Map();
constructor(logger, iotApi) {
this.logger = logger;
this.iotApi = iotApi;
}
/** Set IoT API instance. */
setIotApi(iotApi) {
this.iotApi = iotApi;
}
registerMessageDispatcher(duid, messageDispatcher) {
this.messageDispatcherMap.set(duid, messageDispatcher);
}
getMessageDispatcher(duid) {
const messageDispatcher = this.messageDispatcherMap.get(duid);
if (!messageDispatcher) {
throw new DeviceError(`MessageDispatcher not initialized for device ${duid}`, duid);
}
return messageDispatcher;
}
getMapInfo(duid) {
return this.getMessageDispatcher(duid).getMapInfo(duid);
}
getRoomMap(duid, activeMap) {
return this.getMessageDispatcher(duid).getRoomMap(duid, activeMap);
}
async getSerialNumber(duid) {
const sn = await this.getMessageDispatcher(duid).getSerialNumber(duid);
return sn ?? duid;
}
/** Get current cleaning mode settings. */
async getCleanModeData(duid) {
this.logger.notice('MessageRoutingService - getCleanModeData');
const data = await this.getMessageDispatcher(duid).getCleanModeData(duid);
if (!data) {
throw new DeviceError('Failed to retrieve clean mode data', duid);
}
return data;
}
/** Get vacuum's current room from map. */
async getRoomIdFromMap(duid) {
const data = await this.getMessageDispatcher(duid).getHomeMap(duid);
return data?.vacuumRoom;
}
/** Change cleaning mode settings. */
async changeCleanMode(duid, setting) {
this.logger.notice('MessageRoutingService - changeCleanMode');
return this.getMessageDispatcher(duid).changeCleanMode(duid, setting);
}
async startClean(duid, command) {
switch (command.type) {
case 'routine': {
if (!this.iotApi) {
throw new DeviceError('IoT API must be initialized to start scene', duid);
}
this.logger.notice(`[MessageRoutingService] Start routine ${command.routineId}`);
await this.iotApi.startScene(command.routineId);
return;
}
case 'room': {
this.logger.notice(`[MessageRoutingService] Start room cleaning ${command.roomIds}`);
return this.getMessageDispatcher(duid).startRoomCleaning(duid, command.roomIds, 1);
}
case 'global': {
this.logger.notice(`[MessageRoutingService] Start global cleaning`);
return this.getMessageDispatcher(duid).startCleaning(duid);
}
}
}
async pauseClean(duid) {
this.logger.debug('MessageRoutingService - pauseClean');
await this.getMessageDispatcher(duid).pauseCleaning(duid);
}
async stopAndGoHome(duid) {
this.logger.debug('MessageRoutingService - stopAndGoHome');
await this.getMessageDispatcher(duid).goHome(duid);
}
async resumeClean(duid) {
this.logger.debug('MessageRoutingService - resumeClean');
await this.getMessageDispatcher(duid).resumeCleaning(duid);
}
async stopClean(duid) {
this.logger.debug('MessageRoutingService - stopClean');
await this.getMessageDispatcher(duid).stopCleaning(duid);
}
async playSoundToLocate(duid) {
this.logger.debug('MessageRoutingService - findMe');
await this.getMessageDispatcher(duid).findMyRobot(duid);
}
async customGet(duid, request) {
this.logger.debug('MessageRoutingService - customSend-message', request.method, request.params, request.secure);
return this.getMessageDispatcher(duid).getCustomMessage(duid, request);
}
async customSend(duid, request) {
return this.getMessageDispatcher(duid).sendCustomMessage(duid, request);
}
clearAll() {
this.messageDispatcherMap.clear();
this.logger.debug('MessageRoutingService - All data cleared');
}
}
//# sourceMappingURL=messageRoutingService.js.map