Skip to content

Commit

Permalink
Refactor login method and create object structure
Browse files Browse the repository at this point in the history
  • Loading branch information
DEV2DEV-DE committed Nov 23, 2023
1 parent 6dd04e2 commit ef3677c
Showing 1 changed file with 45 additions and 31 deletions.
76 changes: 45 additions & 31 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,25 @@ class Jablotron extends utils.Adapter {
this.on('unload', this.onUnload.bind(this));

this.connected = false;
axios.defaults.withCredentials = true; // force axios to use cookies
}

/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// Create all states needed for the adapter
await this.createObjectStructure();

if (!this.connected) {
try {
this.login();
const result = await this.login(this.config.username, this.config.password);
if (result) this.connected = true;
} catch (error) {
this.log.error(error);
}
}

/*
For every state in the system there has to be also an object of type state
Here a simple template for a boolean variable named "testVariable"
Because every adapter instance uses its own unique namespace variable names can't collide with other adapters variables
*/
await this.setObjectNotExistsAsync('testVariable', {
type: 'state',
common: {
name: 'testVariable',
type: 'boolean',
role: 'indicator',
read: true,
write: true,
},
native: {},
});

// In order to get state updates, you need to subscribe to them. The following line adds a subscription for our variable we have created above.
this.subscribeStates('testVariable');
// You can also add a subscription for multiple states. The following line watches all states starting with "lights."
Expand All @@ -82,18 +68,28 @@ class Jablotron extends utils.Adapter {

}

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;
/**
* Login to jablonet.net
* @param {string} username
* @param {string} password
*/
async login(username, password) {
try {
const url = `${baseUrl}/ajax/login.php`;
const data = {
'login': encodeURIComponent(username),
'heslo': encodeURIComponent(password),
'aStatus': 200,
'loginType': 'Login'
};
const response = await axios.get(url, { params: data });
this.log.info('Logged in to jablonet.net');
this.log.debug(JSON.stringify(response));
return true;
} catch (error) {
this.log.error(error);
return false;
}
}

/**
Expand Down Expand Up @@ -129,6 +125,24 @@ class Jablotron extends utils.Adapter {
}
}

async createObjectStructure() {
await this.setObjectNotExistsAsync('status.alarm', {
type: 'state',
common: {
name: 'Alarm status',
type: 'number',
role: 'level',
read: true,
write: true,
states: '0:disarm;1:home;2:arm;3:alarm',
min: 0,
max: 3,
},
native: {},
});

}

}

if (require.main !== module) {
Expand Down

0 comments on commit ef3677c

Please sign in to comment.