diff --git a/main.js b/main.js index 749b0be..d9c5cdc 100644 --- a/main.js +++ b/main.js @@ -9,6 +9,8 @@ const utils = require('@iobroker/adapter-core'); const axios = require('axios').default; +const baseUrl = 'https://www.jablonet.net'; + class Jablotron extends utils.Adapter { /** @@ -22,6 +24,8 @@ class Jablotron extends utils.Adapter { this.on('ready', this.onReady.bind(this)); this.on('stateChange', this.onStateChange.bind(this)); this.on('unload', this.onUnload.bind(this)); + + this.connected = false; } /** @@ -30,10 +34,13 @@ class Jablotron extends utils.Adapter { async onReady() { // Initialize your adapter here - // The adapters config (in the instance object everything under the attribute "native") is accessible via - // this.config: - this.log.info('config option1: ' + this.config.option1); - this.log.info('config option2: ' + this.config.option2); + if (!this.connected) { + try { + this.login(); + } catch (error) { + this.log.error(error); + } + } /* For every state in the system there has to be also an object of type state @@ -73,12 +80,20 @@ class Jablotron extends utils.Adapter { // same thing, but the state is deleted after 30s (getState will return null afterwards) await this.setStateAsync('testVariable', { val: true, ack: true, expire: 30 }); - // examples for the checkPassword/checkGroup functions - let result = await this.checkPasswordAsync('admin', 'iobroker'); - this.log.info('check user admin pw iobroker: ' + result); + } - result = await this.checkGroupAsync('admin', 'admin'); - this.log.info('check group user admin group admin: ' + result); + async login() { + const username = this.config.username; + const password = this.config.password; + const url = `${baseUrl}/ajax/login.php`; + const data = { + username, + password, + }; + const response = await axios.post(url, data); + const { token } = response.data; + this.log.info(`Logged in with token ${token}`); + return token; } /** @@ -99,23 +114,6 @@ class Jablotron extends utils.Adapter { } } - // If you need to react to object changes, uncomment the following block and the corresponding line in the constructor. - // You also need to subscribe to the objects with `this.subscribeObjects`, similar to `this.subscribeStates`. - // /** - // * Is called if a subscribed object changes - // * @param {string} id - // * @param {ioBroker.Object | null | undefined} obj - // */ - // onObjectChange(id, obj) { - // if (obj) { - // // The object was changed - // this.log.info(`object ${id} changed: ${JSON.stringify(obj)}`); - // } else { - // // The object was deleted - // this.log.info(`object ${id} deleted`); - // } - // } - /** * Is called if a subscribed state changes * @param {string} id @@ -131,24 +129,6 @@ class Jablotron extends utils.Adapter { } } - // If you need to accept messages in your adapter, uncomment the following block and the corresponding line in the constructor. - // /** - // * Some message was sent to this instance over message box. Used by email, pushover, text2speech, ... - // * Using this method requires "common.messagebox" property to be set to true in io-package.json - // * @param {ioBroker.Message} obj - // */ - // onMessage(obj) { - // if (typeof obj === 'object' && obj.message) { - // if (obj.command === 'send') { - // // e.g. send email or pushover or whatever - // this.log.info('send command'); - - // // Send response in callback if required - // if (obj.callback) this.sendTo(obj.from, obj.command, 'Message received', obj.callback); - // } - // } - // } - } if (require.main !== module) {