UNPKG

domoticz-api-linker

Version:

Domoticz API Javascript Bridge Connector

69 lines (68 loc) 2.57 kB
import { b64encode } from '../libs/Base64.js'; class DomoticzApiConnector { hostname = ''; username = ''; password = ''; useSSL; port; endpoint = ''; constructor(options) { this.hostname = options.hostname; this.username = (options.username != null) ? options.username : ''; this.password = (options.password != null) ? options.password : ''; this.useSSL = (options.useSSL != null) ? options.useSSL : false; this.port = (options.port != null) ? options.port : 80; this.endpoint = this.url('json.htm'); } url(uri) { const proto = `http${this.useSSL === true ? 's' : ''}://`; const host = this.hostname; const port = (this.port > 0) ? this.port : (this.useSSL === true ? 443 : 80); const username = this.username !== undefined ? b64encode(this.username) : ''; const password = this.password !== undefined ? b64encode(this.password) : ''; const credentials = `?username=${username.toString()}&password=${password.toString()}`; return `${proto}${host}:${port.toString()}/${uri}${credentials}`; } // DOMOTICZ NATIVE CALLS async cameras() { return await this.domoticz({ type: 'cameras' }); } async command(data) { return await this.domoticz({ type: 'command', ...data }); } async hardware() { return await this.domoticz({ type: 'hardware' }); } async devices(data) { return await this.domoticz({ type: 'devices', ...data }); } async events(data) { return await this.domoticz({ type: 'events', ...data }); } async notifications(data) { return await this.domoticz({ type: 'notifications', ...data }); } async scenes() { return await this.domoticz({ type: 'scenes' }); } async setUsed(data) { return await this.domoticz({ type: 'setUsed', ...data }); } async checkCredentials() { return await this.__generic('GET', `${this.url('json.htm')}?api-call`); } // TOOLING async get(uri) { return await this.__generic('GET', this.url(`${uri}`)); } async domoticz(data) { return await this.__generic('GET', this.endpoint.toString(), data); } // Want to write an new HTTP manager (other than fetch) just create a new // DomoticzApiProvider[foobar].js and implement only this method. // @ts-expect-error async __generic(method, endpoint, data, content) { return await new Promise(() => null); } } export { DomoticzApiConnector };