UNPKG

ws2801-client

Version:

A client for the webserver for the WS2801-Pi module.

158 lines (157 loc) 6.91 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WS2801Client = void 0; const md5_1 = __importDefault(require("md5")); const http_client_1 = require("./http-client"); const socket_client_1 = require("./socket-client"); class WS2801Client { constructor(baseUrl, apiKey) { if (!baseUrl.startsWith('https://') && !baseUrl.startsWith('http://')) { baseUrl = `http://${baseUrl}`; } if (baseUrl.endsWith('/')) { baseUrl = baseUrl.substring(0, baseUrl.length - 1); } this.httpClient = new http_client_1.HttpClient(baseUrl, apiKey); this.socketClient = new socket_client_1.SocketClient(baseUrl); } setApiKey(apiKey) { this.httpClient.setApiKey(apiKey); } dispose() { this.socketClient.dispose(); } async loginRequired() { const response = await this.httpClient.get('/login-required'); if (response.status !== 200) { const errorMessage = await response.text(); throw new Error(`Could not check if login is required: ${errorMessage}`); } const result = await response.json(); return result.loginRequired; } async register(username, password) { const apiKey = md5_1.default(`${username}__${password}`); const body = JSON.stringify({ name: username, apiKey: apiKey }); const response = await this.httpClient.post('/register', { body: body }); if (response.status !== 200) { const errorMessage = await response.text(); throw new Error(`Could not register: ${errorMessage}`); } const result = await response.json(); this.httpClient.setApiKey(apiKey); return result.apiKey; } async login(username, password) { const apiKey = md5_1.default(`${username}__${password}`); this.httpClient.setApiKey(apiKey); const response = await this.httpClient.post('/login'); if (response.status !== 200) { const errorMessage = await response.text(); throw new Error(`Could not login: ${errorMessage}`); } return apiKey; } async getLedStrip() { const response = await this.httpClient.get('/led-strip'); if (response.status !== 200) { const errorMessage = await response.text(); throw new Error(`Could not get led strip: ${errorMessage}`); } const result = await response.json(); return result.ledStrip; } async fillLedStrip(color, brightness) { const body = brightness ? JSON.stringify({ color: color, brightness: brightness }) : JSON.stringify({ color: color }); const response = await this.httpClient.post('/led-strip/fill', { body: body }); if (response.status !== 200) { const errorMessage = await response.text(); throw new Error(`Could not fill led strip: ${errorMessage}`); } const result = await response.json(); return result.ledStrip; } async clearLedStrip() { const response = await this.httpClient.post('/led-strip/clear'); if (response.status !== 200) { const errorMessage = await response.text(); throw new Error(`Could not clear led strip: ${errorMessage}`); } const result = await response.json(); return result.ledStrip; } async setLed(ledIndex, color, brightness) { const body = brightness ? JSON.stringify({ color: color, brightness: brightness }) : JSON.stringify({ color: color }); const response = await this.httpClient.post(`/led-strip/led/${ledIndex}/set`, { body: body }); if (response.status !== 200) { const errorMessage = await response.text(); throw new Error(`Could not set color of led ${ledIndex}: ${errorMessage}`); } const result = await response.json(); return result.ledStrip; } async setLedStrip(ledStrip, brightness) { const body = brightness ? JSON.stringify({ ledStrip: ledStrip, brightness: brightness }) : JSON.stringify({ ledStrip: ledStrip }); const response = await this.httpClient.post(`/led-strip/set`, { body: body }); if (response.status !== 200) { const errorMessage = await response.text(); throw new Error(`Could not set color of led strip: ${errorMessage}`); } const result = await response.json(); return result.ledStrip; } async setBrightness(brightness) { const body = JSON.stringify({ brightness: brightness }); const response = await this.httpClient.post(`/led-strip/brightness/set`, { body: body }); if (response.status !== 200) { const errorMessage = await response.text(); throw new Error(`Could not set brightness of led strip: ${errorMessage}`); } return; } async getBrightness() { const response = await this.httpClient.get(`/led-strip/brightness`); if (response.status !== 200) { const errorMessage = await response.text(); throw new Error(`Could not get brightness of led strip: ${errorMessage}`); } const result = await response.json(); return result.brightness; } onLedStripChanged(callback) { return this.socketClient.onLedStripChanged(callback); } onBrightnessChanged(callback) { return this.socketClient.onBrightnessChanged(callback); } removeListener(id) { this.socketClient.removeListener(id); } async startAnimation(animationScript) { const body = JSON.stringify({ animationScript: animationScript }); const startResponse = await this.httpClient.post('/led-strip/animation/start', { body: body }); if (startResponse.status !== 200) { const errorMessage = await startResponse.text(); throw new Error(`Could not start animation: ${errorMessage}`); } return { finishPromise: this.waitForAnimationToFinish() }; } async stopAnimation() { const response = await this.httpClient.delete('/led-strip/animation/stop'); if (response.status !== 200) { const errorMessage = await response.text(); throw new Error(`Could not stop animation: ${errorMessage}`); } } async waitForAnimationToFinish() { const finishResponse = await this.httpClient.get('/led-strip/animation/finished'); if (finishResponse.status !== 200) { const errorMessage = await finishResponse.text(); throw new Error(`Could not wait for animation to finish: ${errorMessage}`); } } } exports.WS2801Client = WS2801Client;