matterbridge-roborock-vacuum-plugin
Version:
Matterbridge Roborock Vacuum Plugin
96 lines (95 loc) • 3.45 kB
JavaScript
import axios from 'axios';
import crypto from 'node:crypto';
import { debugStringify } from 'matterbridge/logger';
export class RoborockIoTApi {
logger;
api;
constructor(userdata, logger) {
this.logger = logger;
this.api = axios.create({ baseURL: userdata.rriot.r.a });
this.api.interceptors.request.use((config) => {
try {
const timestamp = Math.floor(Date.now() / 1000);
const nonce = crypto
.randomBytes(6)
.toString('base64')
.substring(0, 6)
.replace(/[+/]/g, (m) => (m === '+' ? 'X' : 'Y'));
const url = this.api ? new URL(this.api.getUri(config)).pathname : '';
const data = [userdata.rriot.u, userdata.rriot.s, nonce, timestamp, crypto.createHash('md5').update(url).digest('hex'), '', ''].join(':');
const hmac = crypto.createHmac('sha256', userdata.rriot.h).update(data).digest('base64');
config.headers['Authorization'] = `Hawk id="${userdata.rriot.u}", s="${userdata.rriot.s}", ts="${timestamp}", nonce="${nonce}", mac="${hmac}"`;
}
catch (error) {
this.logger.error(`Failed to initialize RESTAPI ${error ? debugStringify(error) : 'undefined'}`);
}
return config;
});
}
async getHome(homeId) {
const result = await this.api.get(`user/homes/${homeId}`);
const apiResponse = result.data;
if (apiResponse.result) {
return apiResponse.result;
}
else {
this.logger.error('Failed to retrieve the home data');
return undefined;
}
}
async getHomev2(homeId) {
const result = await this.api.get('v2/user/homes/' + homeId);
const apiResponse = result.data;
if (apiResponse.result) {
return apiResponse.result;
}
else {
this.logger.error('Failed to retrieve the home data');
return undefined;
}
}
async getHomev3(homeId) {
const result = await this.api.get('v3/user/homes/' + homeId);
const apiResponse = result.data;
if (apiResponse.result) {
return apiResponse.result;
}
else {
this.logger.error('Failed to retrieve the home data');
return undefined;
}
}
async getScenes(homeId) {
const result = await this.api.get('user/scene/home/' + homeId);
const apiResponse = result.data;
if (apiResponse.result) {
return apiResponse.result;
}
else {
this.logger.error('Failed to retrieve scene');
return undefined;
}
}
async startScene(sceneId) {
const result = await this.api.post(`user/scene/${sceneId}/execute`);
const apiResponse = result.data;
if (apiResponse.result) {
return apiResponse.result;
}
else {
this.logger.error('Failed to execute scene');
return undefined;
}
}
async getCustom(url) {
const result = await this.api.get(url);
const apiResponse = result.data;
if (apiResponse.result) {
return apiResponse.result;
}
else {
this.logger.error('Failed to execute scene');
return undefined;
}
}
}