Skip to content

Commit

Permalink
switch back to request insted of axios
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Wass committed Oct 15, 2023
1 parent 9d599cb commit f07311f
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 121 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ use if sensors.statusMainState = 20 or 21 (Operation with wood-logs, Scheitholzb

## Changelog

### 0.0.8
* (xsawa32) switched back to request instead of axios with axios-cookiejar-support, because of missing dependencies in axios.package.json
### 0.0.7
* (xsawa32) changed create device and objects in main.js
### 0.0.6
Expand Down
2 changes: 1 addition & 1 deletion io-package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"common": {
"name": "rika-firenet",
"version": "0.0.7",
"version": "0.0.8",
"news": {
"0.0.6": {
"en": "update README",
Expand Down
251 changes: 136 additions & 115 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ const utils = require("@iobroker/adapter-core");

// Load your modules here, e.g.:

//axios with cookie support for iobroker
//https://github.com/3846masa/axios-cookiejar-support#usage
const axios = require('axios').default;
const axiosCookieJarSupport = require('axios-cookiejar-support').default;
const tough = require('tough-cookie');
axiosCookieJarSupport(axios);
const cookieJar = new tough.CookieJar();
const request = require('request').defaults({ jar: true });

var changeInProgress = false;

Expand All @@ -37,7 +31,7 @@ class RikaFirenet extends utils.Adapter {
this.on("objectChange", this.onObjectChange.bind(this));
// this.on("message", this.onMessage.bind(this));
this.on("unload", this.onUnload.bind(this));

//init timeout for recursive call webLogin() or getstoveValues()
this.TimeoutID = null;
}
Expand Down Expand Up @@ -71,15 +65,15 @@ class RikaFirenet extends utils.Adapter {
await this.webLogin();
}

/**
* @param {any} stateNameStr
* @param {any} stateRoleStr
* @param {any} stateReadBool
* @param {any} stateWriteBool
* @param {any} stateValueMix
* @param {string} stateTypeStr
*/
setStoveStates(stateNameStr, stateTypeStr, stateRoleStr, stateReadBool, stateWriteBool, stateValueMix){
/**
* @param {any} stateNameStr
* @param {any} stateRoleStr
* @param {any} stateReadBool
* @param {any} stateWriteBool
* @param {any} stateValueMix
* @param {string} stateTypeStr
*/
setStoveStates(stateNameStr, stateTypeStr, stateRoleStr, stateReadBool, stateWriteBool, stateValueMix) {
//set object with specific datatype and value, subscribe and set value
this.setObjectNotExists(this.config.mystoveid + "." + stateNameStr, {
type: stateTypeStr,
Expand All @@ -90,107 +84,101 @@ class RikaFirenet extends utils.Adapter {
read: stateReadBool,
write: stateWriteBool,
},
native: {},
});
//subscribe states
this.subscribeStates(this.config.mystoveid + "." + stateNameStr);

//set states
this.setState(this.config.mystoveid + "." + stateNameStr, { val: stateValueMix, ack: true });
native: {},
});

//subscribe states
this.subscribeStates(this.config.mystoveid + "." + stateNameStr);

//set states
this.setState(this.config.mystoveid + "." + stateNameStr, { val: stateValueMix, ack: true });
}

async webLogin() {
//webLogin() {
clearTimeout(this.TimeoutID);

var email = this.config.myuser;
var password = this.config.mypassword;
var apiserver = 'https://www.rika-firenet.com';
var myemail = this.config.myuser;
var mypassword = this.config.mypassword;
var myapiserver = 'https://www.rika-firenet.com';

try {
const response = await axios.post(apiserver + '/web/login', { email, password }, { jar: cookieJar, withCredentials: true });
// in response steht jetzt die Antwort
// wenn man sie außerhalb der Funktion nutzen will:
// return response; // oder einen Teil davon
this.setState("info.connection", true, true);
this.log.info('webLogin OK')

//call getstoveValues, if successfully webLogin
await this.getstoveValues();
} catch (e) {
this.setState("info.connection", false, true);
this.log.error('webLogin failed: ' + e.message);

//call webLogin() again in 1 minute, till sucessfully login
clearTimeout(this.TimeoutID);
this.TimeoutID = setTimeout(() => this.webLogin(), this.config.myinterval * 60000);
}
// in response steht jetzt die Antwort
// wenn man sie außerhalb der Funktion nutzen will:
// return response; // oder einen Teil davon
request.post({ url: 'https://www.rika-firenet.com/web/login', form: { email: myemail, password: mypassword } },
(error, response, body) => {
if (body.indexOf("summary") > -1) {// login successful

this.log.info("Web-Login successful");

//get values, if login successful
this.getstoveValues();
}
else {
this.log.error("Web-Login NOT successful");
}
}
)
}

async getstoveValues() {
//getstoveValues() {
if (changeInProgress == false) {

var stoveID = this.config.mystoveid;
var apiserver = 'https://www.rika-firenet.com';

try {
const response2 = await axios.get(apiserver + '/api/client/' + stoveID + '/status', { jar: cookieJar, withCredentials: true })
// in response steht jetzt die Antwort
// wenn man sie außerhalb der Funktion nutzen will:
// return response; // oder einen Teil davon

//output json to log
//this.log.info(JSON.stringify(response2.data));

//load json into content
const content = response2.data;

//testoutput, if correct data come in
//this.log.info("lastConfirmedRevision: " + content.lastConfirmedRevision);

//set objects and values if correct data come in
if (content.lastConfirmedRevision) {
this.setStoveStates("name", "state", "", true, false, content.name);
this.setStoveStates("stoveID", "state", "", true, false, content.stoveID);
this.setStoveStates("lastSeenMinutes", "state", "", true, false, content.lastSeenMinutes);
this.setStoveStates("lastConfirmedRevision", "state", "", true, false, content.lastConfirmedRevision);
this.setStoveStates("stoveType", "state", "", true, false, content.stoveType);
this.setStoveStates("oem", "state", "", true, false, content.oem);

//create channels
this.setStoveStates("controls", "channel", "", false, false, "");
this.setStoveStates("sensors", "channel", "", false, false, "");
this.setStoveStates("stoveFeatures", "channel", "", false, false, "");

//create and/or update states in controls, sensors and stoveFeatures
for (let [key, value] of Object.entries(content.controls)) {
this.setStoveStates(`controls.${key}`, "state", "", true, true, value);
//this.setState(this.config.mystoveid + "." + `controls.${key}`, "state", "", true, true, value);
}
for (let [key, value] of Object.entries(content.sensors)) {
this.setStoveStates(`sensors.${key}`, "state", "", true, false, value);
request.get({ url: 'https://www.rika-firenet.com/api/client/' + stoveID + '/status' },
(error, response, body) => {
this.log.info(response.statusCode + " - API-Connection sucessful");
if (response.statusCode == 200 && body.indexOf(stoveID) > -1) {// request successful
this.setState("info.connection", true, true);
var json = JSON.parse(body);
const content = json;

//testoutput, if correct data come in
//this.log.info("lastConfirmedRevision: " + content.lastConfirmedRevision);

//set objects and values if correct data come in
if (content.lastConfirmedRevision) {
this.setStoveStates("name", "state", "", true, false, content.name);
this.setStoveStates("stoveID", "state", "", true, false, content.stoveID);
this.setStoveStates("lastSeenMinutes", "state", "", true, false, content.lastSeenMinutes);
this.setStoveStates("lastConfirmedRevision", "state", "", true, false, content.lastConfirmedRevision);
this.setStoveStates("stoveType", "state", "", true, false, content.stoveType);
this.setStoveStates("oem", "state", "", true, false, content.oem);

//create channels
this.setStoveStates("controls", "channel", "", false, false, "");
this.setStoveStates("sensors", "channel", "", false, false, "");
this.setStoveStates("stoveFeatures", "channel", "", false, false, "");

//create and/or update states in controls, sensors and stoveFeatures
for (let [key, value] of Object.entries(content.controls)) {
this.setStoveStates(`controls.${key}`, "state", "", true, true, value);
//this.setState(this.config.mystoveid + "." + `controls.${key}`, "state", "", true, true, value);
}
for (let [key, value] of Object.entries(content.sensors)) {
this.setStoveStates(`sensors.${key}`, "state", "", true, false, value);
}
for (let [key, value] of Object.entries(content.stoveFeatures)) {
this.setStoveStates(`stoveFeatures.${key}`, "state", "", true, false, value);
//this.setState(this.config.mystoveid + "." + `stoveFeatures.${key}`, "state", "", true, false, value);
}
} else {
this.log.error("Malformed json: " + JSON.stringify(response2.data));
}

//call getstoveValues() every 1 minute
//https://blog.scottlogic.com/2017/09/14/asynchronous-recursion.html
clearTimeout(this.TimeoutID);
this.TimeoutID = setTimeout(() => this.getstoveValues(), this.config.myinterval * 60000);

} else {//login failed
this.log.error("get data not successful");
}
for (let [key, value] of Object.entries(content.stoveFeatures)) {
this.setStoveStates(`stoveFeatures.${key}`, "state", "", true, false, value);
//this.setState(this.config.mystoveid + "." + `stoveFeatures.${key}`, "state", "", true, false, value);
}
} else {
this.log.error("Malformed json: " + JSON.stringify(response2.data));
}

//call getstoveValues() every 1 minute
//https://blog.scottlogic.com/2017/09/14/asynchronous-recursion.html
clearTimeout(this.TimeoutID);
this.TimeoutID = setTimeout(() => this.getstoveValues(), this.config.myinterval * 60000);
} catch (e) {
this.setState("info.connection", false, true);
this.log.error('getstoveValues: ' + e.message);
this.log.error('try again in (minutes): ' + this.config.myinterval);

//if connection to API fails, cycle webLogin(), till sucessfully login
clearTimeout(this.TimeoutID);
this.TimeoutID = setTimeout(() => this.webLogin(), this.config.myinterval * 60000);
}
)
} else {
this.log.info('change in progress: try to getstoveValues() next time');
}
Expand All @@ -205,19 +193,52 @@ class RikaFirenet extends utils.Adapter {

try {
//get current json
const response1 = await axios.get(apiserver + '/api/client/' + stoveID + '/status', { jar: cookieJar, withCredentials: true })
const content = response1.data;
//const response1 = await axios.get(apiserver + '/api/client/' + stoveID + '/status', { jar: cookieJar, withCredentials: true })
//const content = response1.data;

request.get({ url: 'https://www.rika-firenet.com/api/client/' + stoveID + '/status' },
(error, response, body) => {
if (response.statusCode == 200 && body.indexOf(stoveID) > -1) {// request successful
this.log.info(response.statusCode + " - API-Connection sucessful");

//kick out adaptername, id, device and other stuff from string
const cleanControlname = controlname.split('.').slice(4).join('.');
this.log.info(cleanControlname + " = " + controlvalue);

var json = JSON.parse(body);
const content = json;

//kick out adaptername, id, device and other stuff from string
const cleanControlname = controlname.split('.').slice(4).join('.');
this.log.info(cleanControlname + " = " + controlvalue);
//change value in content.controls
content.controls[cleanControlname] = controlvalue;

//change value in content.controls
content.controls[cleanControlname] = controlvalue;
//send modified json to server (todo: make shure, that only one action at the same time is fired up in Blockly)
//const response2 = await axios.post(apiserver + '/api/client/' + stoveID + '/controls', content.controls, { jar: cookieJar, withCredentials: true })

//test
this.log.info(JSON.stringify(content.controls));

request.post({ url: 'https://www.rika-firenet.com/api/client/' + stoveID + '/controls', json: content.controls },
(error, response, body) => {
//this.log.info('Body: ' + body);
/* if (body.indexOf("summary") > -1) {// login successful
this.log.info("Web-Login successful");
//get values, if login successful
this.getstoveValues();
}
else {
this.log.error("Web-Login NOT successful");
} */
}
)

} else {
this.log.error("get data not successful");
}
}
)

//send modified json to server (todo: make shure, that only one action at the same time is fired up in Blockly)
const response2 = await axios.post(apiserver + '/api/client/' + stoveID + '/controls', content.controls, { jar: cookieJar, withCredentials: true })

} catch (e) {
this.log.error('setstoveValues: ' + e.message);
}
Expand Down Expand Up @@ -269,13 +290,13 @@ class RikaFirenet extends utils.Adapter {
*/
onStateChange(id, state) {
if (state && !state.ack) {
//if (state) {
//if (state) {
// The state was changed
this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
this.setstoveValues(id, state.val);
} //else {
// The state was deleted
//this.log.info(`state ${id} deleted`);
// The state was deleted
//this.log.info(`state ${id} deleted`);
//}
}

Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iobroker.rika-firenet",
"version": "0.0.7",
"version": "0.0.8",
"description": "Control your RIKA stove",
"author": {
"name": "xsawa32",
Expand All @@ -19,10 +19,7 @@
"url": "https://github.com/xsawa32/ioBroker.rika-firenet"
},
"dependencies": {
"@iobroker/adapter-core": "^2.4.0",
"axios": "^0.21.1",
"axios-cookiejar-support": "^1.0.1",
"tough-cookie": "^4.0.0"
"@iobroker/adapter-core": "^2.4.0"
},
"devDependencies": {
"@iobroker/testing": "^2.4.0",
Expand Down

0 comments on commit f07311f

Please sign in to comment.