Skip to content

Commit

Permalink
Add login functionality to Jablotron adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
DEV2DEV-DE committed Nov 23, 2023
1 parent 04ee0a0 commit 2d2dccf
Showing 1 changed file with 24 additions and 44 deletions.
68 changes: 24 additions & 44 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);

Check failure on line 41 in main.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Trailing spaces not allowed
}
}

/*
For every state in the system there has to be also an object of type state
Expand Down Expand Up @@ -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() {

Check failure on line 85 in main.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Trailing spaces not allowed
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;
}

/**
Expand All @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 2d2dccf

Please sign in to comment.