Skip to content

Commit

Permalink
accepted lint suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
sirjojo69 committed Jan 12, 2025
1 parent de54cec commit c3baf4a
Showing 1 changed file with 124 additions and 128 deletions.
252 changes: 124 additions & 128 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// @ts-nocheck
"use strict";

const { Adapter } = require("@iobroker/adapter-core");
/*
* Created with @iobroker/create-adapter v1.29.1
*/

// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
const axios = require('axios');
const axios = require("axios");

/**
* The adapter instance
Expand Down Expand Up @@ -58,87 +57,84 @@ function startAdapter(options) {
}));
}

function compareValues(key, order = 'asc') {
function compareValues(key, order = "asc") {
return function innerSort(a, b) {
if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
// property doesn't exist on either object
return 0;
}
const varA = (typeof a[key] === 'string')
? a[key].toUpperCase() : a[key];
const varB = (typeof b[key] === 'string')
? b[key].toUpperCase() : b[key];
let comparison = 0;
if (varA > varB) {
comparison = 1;
} else if (varA < varB) {
comparison = -1;
}
return (
(order === 'desc') ? (comparison * -1) : comparison
);
if (!Object.prototype.hasOwnProperty.call(a, key) || !Object.prototype.hasOwnProperty.call(b, key)) {
// property doesn't exist on either object
return 0;
}

const varA = (typeof a[key] === "string")
? a[key].toUpperCase() : a[key];
const varB = (typeof b[key] === "string")
? b[key].toUpperCase() : b[key];

let comparison = 0;
if (varA > varB) {
comparison = 1;
} else if (varA < varB) {
comparison = -1;
}
return (
(order === "desc") ? (comparison * -1) : comparison
);
};
}
}

async function main() {

// adapter.log.info("aWATTar API URL: " + adapter.config.aWATTarApiUrl);
// adapter.log.info("Loading Threshold Start: " + adapter.config.LoadingThresholdStart);
// adapter.log.info("Loading Threshold End: " + adapter.config.LoadingThresholdEnd);

const url = adapter.config.aWATTarApiUrl;
const mwst = parseInt(adapter.config.MWstRate);
const mwstRate = (mwst + 100) / 100;
const workRate = parseFloat(adapter.config.WorkRate);
const loadingThresholdStart = adapter.config.LoadingThresholdStart;
if (isNaN(parseInt(loadingThresholdStart))) {return adapter.log.error("loadingThresholdStart NaN")}
if (isNaN(parseInt(loadingThresholdStart))) { return adapter.log.error("loadingThresholdStart NaN"); }
const loadingThresholdEnd = adapter.config.LoadingThresholdEnd;
if (isNaN(parseInt(loadingThresholdStart))) {return adapter.log.error("loadingThresholdEnd NaN")}
if (isNaN(parseInt(loadingThresholdEnd))) { return adapter.log.error("loadingThresholdEnd NaN"); }

const heute = new Date();
const loadingThresholdStartDateTime = new Date(heute.getFullYear(),heute.getMonth(),heute.getDate(),parseInt(loadingThresholdStart),0,0)
const loadingThresholdEndDateTime = new Date(heute.getFullYear(),heute.getMonth(),heute.getDate() + 1,parseInt(loadingThresholdEnd),0,0)
const loadingThresholdStartDateTime = new Date(heute.getFullYear(), heute.getMonth(), heute.getDate(), parseInt(loadingThresholdStart), 0, 0);
const loadingThresholdEndDateTime = new Date(heute.getFullYear(), heute.getMonth(), heute.getDate() + 1, parseInt(loadingThresholdEnd), 0, 0);

let epochToday = new Date(heute.getFullYear(),heute.getMonth(),heute.getDate()).getTime();
let epochTomorrow = new Date(heute.getFullYear(),heute.getMonth(),heute.getDate()+2).getTime() - 1;
let urlEpoch = url.concat("?start=", epochToday.toString(), "&end=", epochTomorrow.toString());
const epochToday = new Date(heute.getFullYear(), heute.getMonth(), heute.getDate()).getTime();
const epochTomorrow = new Date(heute.getFullYear(), heute.getMonth(), heute.getDate() + 2).getTime() - 1;
const urlEpoch = url.concat("?start=", epochToday.toString(), "&end=", epochTomorrow.toString());

adapter.log.debug('local request started');
adapter.log.debug("local request started");

//get data from awattar api
let response;
try {
response = await axios({
method: 'get',
method: "get",
baseURL: urlEpoch,
timeout: 10000,
responseType: 'json'
});
} catch (error) {
(error) => {
if (error.response) {
// The request was made and the server responded with a status code

this.log.warn('received error ' + error.response.status + ' response from local sensor ' + sensorIdentifier + ' with content: ' + JSON.stringify(error.response.data));
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js<div></div>
this.log.error(error.message);
} else {
// Something happened in setting up the request that triggered an Error
this.log.error(error.message);
}
responseType: "json"
});
} catch (error) {
if (error.response) {
// The request was made and the server responded with a status code
adapter.log.warn("received error " + error.response.status + " response from local sensor with content: " + JSON.stringify(error.response.data));
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
adapter.log.error(error.message);
} else {
// Something happened in setting up the request that triggered an Error
adapter.log.error(error.message);
}
return;
}
return;
}

const content = response.data;

adapter.log.debug('local request done');
adapter.log.debug('received data (' + response.status + '): ' + JSON.stringify(content));
adapter.log.debug("local request done");
adapter.log.debug("received data (" + response.status + "): " + JSON.stringify(content));

//write raw data to data point
await adapter.setObjectNotExistsAsync("Rawdata", {
Expand All @@ -155,10 +151,10 @@ async function main() {
});
await adapter.setStateAsync("Rawdata", JSON.stringify(content), true);

let array = content.data;
const array = content.data;

for(let i = 0; i < array.length; i++) {
let stateBaseName = "prices." + i + ".";
for (let i = 0; i < array.length; i++) {
const stateBaseName = "prices." + i + ".";

//ensure all necessary data points exist
await adapter.setObjectNotExistsAsync(stateBaseName + "start", {
Expand Down Expand Up @@ -186,7 +182,7 @@ async function main() {
},
native: {}
});

await adapter.setObjectNotExistsAsync(stateBaseName + "startDate", {
type: "state",
common: {
Expand Down Expand Up @@ -277,46 +273,46 @@ async function main() {
});

//calculate prices / timestamps
let startTs = array[i].start_timestamp;
let start = new Date(startTs);
let startTime = start.toLocaleTimeString('de-DE');
let startDate = start.toLocaleDateString('de-DE');
let endTs = array[i].end_timestamp;
let end = new Date(endTs);
let endTime = end.toLocaleTimeString('de-DE');
let endDate = end.toLocaleDateString('de-DE');
let nettoPriceKwh = array[i].marketprice / 10; //price is in eur per MwH. Convert it in cent per KwH
let bruttoPriceKwh = nettoPriceKwh * mwstRate;
let toalPriceKwh = bruttoPriceKwh + workRate ;
const startTs = array[i].start_timestamp;
const start = new Date(startTs);
const startTime = start.toLocaleTimeString("de-DE");
const startDate = start.toLocaleDateString("de-DE");
const endTs = array[i].end_timestamp;
const end = new Date(endTs);
const endTime = end.toLocaleTimeString("de-DE");
const endDate = end.toLocaleDateString("de-DE");
const nettoPriceKwh = array[i].marketprice / 10; //price is in eur per MwH. Convert it in cent per KwH
const bruttoPriceKwh = nettoPriceKwh * mwstRate;
const totalPriceKwh = bruttoPriceKwh + workRate;

//write prices / timestamps to their data points
await Promise.all(
[adapter.setStateAsync(stateBaseName + "start", startTime, true)
,adapter.setStateAsync(stateBaseName + "startTimestamp", startTs, true)
,adapter.setStateAsync(stateBaseName + "startDate", startDate, true)
,adapter.setStateAsync(stateBaseName + "end", endTime, true)
,adapter.setStateAsync(stateBaseName + "endTimestamp", endTs, true)
,adapter.setStateAsync(stateBaseName + "endDate", endDate, true)
,adapter.setStateAsync(stateBaseName + "nettoPriceKwh", nettoPriceKwh, true)
,adapter.setStateAsync(stateBaseName + "bruttoPriceKwh", bruttoPriceKwh, true)
,adapter.setStateAsync(stateBaseName + "totalPriceKwh", toalPriceKwh, true)
])
, adapter.setStateAsync(stateBaseName + "startTimestamp", startTs, true)
, adapter.setStateAsync(stateBaseName + "startDate", startDate, true)
, adapter.setStateAsync(stateBaseName + "end", endTime, true)
, adapter.setStateAsync(stateBaseName + "endTimestamp", endTs, true)
, adapter.setStateAsync(stateBaseName + "endDate", endDate, true)
, adapter.setStateAsync(stateBaseName + "nettoPriceKwh", nettoPriceKwh, true)
, adapter.setStateAsync(stateBaseName + "bruttoPriceKwh", bruttoPriceKwh, true)
, adapter.setStateAsync(stateBaseName + "totalPriceKwh", totalPriceKwh, true)
]);
}

adapter.log.debug('all prices written to their data points');
adapter.log.debug("all prices written to their data points");

//ordered prices
let sortedArray = array.sort(compareValues("marketprice", "asc"));
let j= 0;
const sortedArray = array.sort(compareValues("marketprice", "asc"));
let j = 0;

for(let k = 0; k < sortedArray.length; k++) {
let startTs = array[k].start_timestamp;
let start = new Date(startTs);
let endTs = array[k].end_timestamp;
let end = new Date(endTs);
for (let k = 0; k < sortedArray.length; k++) {
const startTs = sortedArray[k].start_timestamp;
const start = new Date(startTs);
const endTs = sortedArray[k].end_timestamp;
const end = new Date(endTs);

if (start >= loadingThresholdStartDateTime && end < loadingThresholdEndDateTime) {
let stateBaseName = "pricesOrdered." + j + ".";
const stateBaseName = "pricesOrdered." + j + ".";

//ensure all necessary data points exist
await adapter.setObjectNotExistsAsync(stateBaseName + "start", {
Expand All @@ -332,18 +328,18 @@ async function main() {
native: {}
});

await adapter.setObjectNotExistsAsync(stateBaseName + "startTimestamp", {
type: "state",
common: {
name: "startTimestamp",
type: "number",
role: "value",
desc: "Timestamp des Beginns der Gültigkeit des Preises",
read: true,
write: false
},
native: {}
});
await adapter.setObjectNotExistsAsync(stateBaseName + "startTimestamp", {
type: "state",
common: {
name: "startTimestamp",
type: "number",
role: "value",
desc: "Timestamp des Beginns der Gültigkeit des Preises",
read: true,
write: false
},
native: {}
});

await adapter.setObjectNotExistsAsync(stateBaseName + "startDate", {
type: "state",
Expand All @@ -370,18 +366,18 @@ async function main() {
native: {}
});

await adapter.setObjectNotExistsAsync(stateBaseName + "endTimestamp", {
type: "state",
common: {
name: "endTimestamp",
type: "number",
role: "value",
desc: "Timestamp des Endes der Gültigkeit des Preises",
read: true,
write: false
},
native: {}
});
await adapter.setObjectNotExistsAsync(stateBaseName + "endTimestamp", {
type: "state",
common: {
name: "endTimestamp",
type: "number",
role: "value",
desc: "Timestamp des Endes der Gültigkeit des Preises",
read: true,
write: false
},
native: {}
});

await adapter.setObjectNotExistsAsync(stateBaseName + "endDate", {
type: "state",
Expand Down Expand Up @@ -409,33 +405,33 @@ async function main() {
});

//calculate prices / timestamps
let startTime = start.toLocaleTimeString('de-DE');
let startDate = start.toLocaleDateString('de-DE');
let endTime = end.toLocaleTimeString('de-DE');
let endDate = end.toLocaleDateString('de-DE');
let priceKwh = array[k].marketprice / 10; //price is in eur per MwH. Convert it in cent per KwH
const startTime = start.toLocaleTimeString("de-DE");
const startDate = start.toLocaleDateString("de-DE");
const endTime = end.toLocaleTimeString("de-DE");
const endDate = end.toLocaleDateString("de-DE");
const priceKwh = sortedArray[k].marketprice / 10; //price is in eur per MwH. Convert it in cent per KwH

//write prices / timestamps to their data points
await Promise.all(
[adapter.setStateAsync(stateBaseName + "start", startTime, true)
,adapter.setStateAsync(stateBaseName + "startTimestamp", startTs, true)
,adapter.setStateAsync(stateBaseName + "startDate", startDate, true)
,adapter.setStateAsync(stateBaseName + "end", endTime, true)
,adapter.setStateAsync(stateBaseName + "endTimestamp", endTs, true)
,adapter.setStateAsync(stateBaseName + "endDate", endDate, true)
,adapter.setStateAsync(stateBaseName + "priceKwh", priceKwh, true)
])
, adapter.setStateAsync(stateBaseName + "startTimestamp", startTs, true)
, adapter.setStateAsync(stateBaseName + "startDate", startDate, true)
, adapter.setStateAsync(stateBaseName + "end", endTime, true)
, adapter.setStateAsync(stateBaseName + "endTimestamp", endTs, true)
, adapter.setStateAsync(stateBaseName + "endDate", endDate, true)
, adapter.setStateAsync(stateBaseName + "priceKwh", priceKwh, true)
]);
j++;
}

}

adapter.log.debug('all ordered prices written to their data points');
adapter.log.debug("all ordered prices written to their data points");

setTimeout(function () {
adapter.stop();
}, 10000)
}, 10000);

}

// @ts-ignore parent is a valid property on module
Expand All @@ -445,4 +441,4 @@ if (module.parent) {
} else {
// otherwise start the instance directly
startAdapter();
}
}

0 comments on commit c3baf4a

Please sign in to comment.