homebridge-sleeptracker
Version:
Homebridge plugin for SleepTracker smart beds - Control your bed's position and features through HomeKit
141 lines • 5.46 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SleepTrackerClient = void 0;
const axios_1 = __importDefault(require("axios"));
const types_1 = require("./types");
const DEFAULT_HEADERS = {
accept: '*/*',
'content-type': 'application/json',
'accept-encoding': 'gzip',
'accept-language': 'en-US,en;q=0.9',
'User-Agent': 'sleeptracker-android-tsi/1.9.47',
};
const CLIENT_INFO = {
clientID: 'sleeptracker-android-tsi',
clientVersion: '1.9.47',
};
class SleepTrackerClient {
constructor(username, password, deviceId) {
this.username = username;
this.password = password;
this.deviceId = deviceId;
this.token = null;
this.tokenExpiration = 0;
const baseUrl = 'tsi.sleeptracker.com';
const authHost = `auth.${baseUrl}`;
const appHost = `app.${baseUrl}`;
this.authApi = axios_1.default.create({
baseURL: `https://${authHost}/v1/app/user`,
headers: {
...DEFAULT_HEADERS,
Host: authHost,
},
});
this.api = axios_1.default.create({
baseURL: `https://${appHost}/actrack-client/v2/fpcsiot/processor`,
headers: {
...DEFAULT_HEADERS,
Host: appHost,
},
});
}
async authenticate() {
const currentTime = Math.floor(Date.now() / 1000) + 60;
if (this.token && currentTime < this.tokenExpiration) {
return;
}
const authHeader = Buffer.from(`${this.username}:${this.password}`).toString('base64');
const response = await this.authApi.post('/session', {
...CLIENT_INFO,
scope: 'scope',
id: 'TEST_ANDROID_getUserSession',
}, {
headers: {
Authorization: `Basic ${authHeader}`,
},
});
const { token, expirationTimeSecs } = response.data;
this.token = token;
this.tokenExpiration = expirationTimeSecs;
this.api.defaults.headers.common['Authorization'] = `Bearer ${token}`;
}
async getDeviceInfo(deviceId) {
await this.authenticate();
const response = await this.api.post('/command/hello', {
...CLIENT_INFO,
id: 'TEST_ANDROID_cloudIoTProcessorSimpleHello',
sleeptrackerProcessorID: deviceId,
});
return response.data.helloData;
}
async getDeviceSnapshot(command) {
await this.authenticate();
const commandValue = this.getCommandValue(command);
const response = await this.api.post('/adjustableBaseControls', {
...CLIENT_INFO,
id: 'TEST_ANDROID_adjustableBaseControls',
sleeptrackerProcessorID: this.deviceId,
bedControlCommand: commandValue
});
return response.data.body.snapshots[0];
}
getCommandValue(command) {
switch (command) {
case types_1.Commands.Status:
return "1";
case types_1.Commands.HeadUp:
return "100";
case types_1.Commands.HeadDown:
return "101";
case types_1.Commands.FootUp:
return "102";
case types_1.Commands.FootDown:
return "103";
case types_1.Commands.Flat:
return "2";
case types_1.Commands.ZeroG:
return "0";
case types_1.Commands.AntiSnore:
return "4";
case types_1.Commands.TV:
return "3";
case types_1.Commands.Stop:
return "107";
default:
throw new Error(`Unknown command: ${command}`);
}
}
async sendCommand(deviceId, command, side = 0, targetPosition) {
await this.authenticate();
const commandValue = this.getCommandValue(command);
const response = await this.api.post('/adjustableBaseControls', {
...CLIENT_INFO,
id: 'TEST_ANDROID_adjustableBaseControls',
sleeptrackerProcessorID: deviceId,
bedControlCommand: commandValue,
side: side,
...(targetPosition !== undefined && { targetPosition: targetPosition })
});
if (response.data.statusCode !== 0) {
throw new Error(`Command failed: ${response.data.statusMessage}`);
}
}
async getEnvironmentSensorData(deviceId) {
await this.authenticate();
const response = await this.api.post('/latestEnvironmentSensorData', {
...CLIENT_INFO,
id: 'TEST_ANDROID_environmentalData',
sleeptrackerProcessorID: deviceId,
});
const { degreesCelsius, humidityPercentage } = response.data;
return {
temperature: (degreesCelsius === null || degreesCelsius === void 0 ? void 0 : degreesCelsius.status) === 'valid' ? Math.round(degreesCelsius.value * 10) / 10 : null,
humidity: (humidityPercentage === null || humidityPercentage === void 0 ? void 0 : humidityPercentage.status) === 'valid' ? Math.round(humidityPercentage.value * 10) / 10 : null,
};
}
}
exports.SleepTrackerClient = SleepTrackerClient;
//# sourceMappingURL=client.js.map