Skip to content

Commit

Permalink
km200 post
Browse files Browse the repository at this point in the history
  • Loading branch information
tp1de committed Oct 28, 2023
1 parent 20d0be6 commit 76f4934
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ English documentation: https://github.com/tp1de/ioBroker.ems-esp/blob/main/doc/e
Placeholder for the next version (at the beginning of the line):
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**
* Retry http post for KM200 on error

### 2.5.2 (2023-10-19)
* Update energy statistics and recordings for history and influxdb
* include warning in log that InfluxDB V2 will not be supported in future versions anymore
Expand Down
15 changes: 10 additions & 5 deletions lib/ems.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,20 +689,25 @@ async function energy(db,idr,idw,end,intervall,step,t) {
}
}

} else {
//await adapter.sendToAsync(db,"deleteAll",[{id: adapt+idw}]);
}

if (db.substring(0,8) == "influxdb" ) {
//await adapter.sendToAsync(db,"deleteAll",[{id: adapt+idw}]);
for (let i = 0; i < recs.length;i++){
try {await adapter.sendToAsync(db,"storeState", {id: adapt+idw, state: recs[i]});} catch(e) {}
}
}

if (db.substring(0,7) == "history" ) {
for (let i = 0; i < recs.length;i++){
let status;
try {status = await adapter.sendToAsync(db,"update", {id: adapt+idw, state: recs[i]}); } catch(e) {}
if (status.success == false) try {status = await adapter.sendToAsync(db,"storeState", {id: adapt+idw, state: recs[i]});} catch(e) {}
//try {status = await adapter.sendToAsync(db,"storeState", {id: adapt+idw, state: recs[i]});} catch(e) {}
}

//try {await adapter.sendToAsync(db,"storeState", {id: adapt+idw, state: recs});} catch(e) {}
}



const v = [];
for (let i = 0; i < recs.length;i++){
v.push({ts: recs[i].ts, val: recs[i].val});
Expand Down
17 changes: 13 additions & 4 deletions lib/km200.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ async function km200_put(url,value,type) {
const urls = km200_server +"/" + url.split(".").join("/");
try {
const headers = {"Accept": '"application/json',"User-Agent": "TeleHeater/2.2.3"};
const options = {"url": urls, "headers": headers, "data": data, "method": "POST" };
const options = {"url": urls, "headers": headers, "data": data, "method": "POST", timeout: 10000 };
let r = (await axios(options)).status;
return(r);

Expand Down Expand Up @@ -442,7 +442,7 @@ async function recsw(field,d,t) {
for (let i = 0; i < d.length;i++){
if (!unloaded ) {
let status;
try {status = await adapter.sendToAsync(db,"update", d[i])} catch(e) {}
try {status = await adapter.sendToAsync(db,"update", d[i]);} catch(e) {}
if (status.success == false) try {status = await adapter.sendToAsync(db,"storeState", d[i]);} catch(e) {}
}
}
Expand All @@ -454,7 +454,7 @@ async function recsw(field,d,t) {
for (let i = 0; i < d.length;i++){
if (!unloaded ) {
let status;
try {status = await adapter.sendToAsync(db,"update", d[i])} catch(e) {}
try {status = await adapter.sendToAsync(db,"update", d[i]);} catch(e) {}
if (status.success == false) try {status = await adapter.sendToAsync(db,"storeState", d[i]);} catch(e) {}
}
}
Expand Down Expand Up @@ -923,7 +923,9 @@ const state_change = async function (id,state,obj) {
value= obj.native.km200.allowedValues[value];
const resp = await km200_put(obj.native.ems_km200 , value, obj.native.km200.type);
if (resp != 200 && resp != 204) {
adapter.log.warn("KM200 http write error " + resp + ":" + obj.native.ems_km200);
await sleep(500);
const resp = await km200_put(obj.native.ems_km200 , value, obj.native.km200.type);
if (resp != 200 && resp != 204) adapter.log.warn("KM200 http write error " + resp + ":" + obj.native.ems_km200);
}
}
catch(error) {adapter.log.warn("KM200 http write error "+ error + ":" + obj.native.ems_km200);}
Expand Down Expand Up @@ -957,6 +959,13 @@ async function getid(field,db) {
});
}

async function sleep(ms) {
if (unloaded) return;
return new Promise(resolve => {
setTimeout(() => !unloaded && resolve(true), ms);
});
}


const unload = function (u) {unloaded = u;};

Expand Down

0 comments on commit 76f4934

Please sign in to comment.