Skip to content

Commit 3f4b2b3

Browse files
Update getwavelogdatatoiobroker_new_api_summed_up_stations.js
Fixed an issue where determined values from stations could vary, showing only one or several, but not all stations. This was due to faulty behaviour of my code. fixed it for asynchronous answers from HTTP requests.
1 parent 4dc9695 commit 3f4b2b3

File tree

1 file changed

+71
-68
lines changed

1 file changed

+71
-68
lines changed

getwavelogdatatoiobroker_new_api_summed_up_stations.js

Lines changed: 71 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,89 @@
11
const request = require('request');
22

3-
// Wavelog API settings using the new Wordpress endpoint
4-
const WAVELOG_URL = "https://wavelogurl.com/index.php/api/get_wp_stats";
3+
// Wavelog API settings using the new WordPress endpoint
4+
const WAVELOG_URL = "https://waveloginstance.com/index.php/api/get_wp_stats";
55
const API_KEY = "APIkey1234";
6-
const STATION_PROFILE_IDS = ["01", "02"]; // Define all the station IDs here. These are summed up later. Or only enter one Station-ID.
6+
const STATION_PROFILE_IDS = ["01", "02"]; // List of station IDs to query
77

8-
// Function for retrieving and processing data from the new API
98
function runScript() {
10-
let totalQso = 0;
11-
let totalQsoYear = 0;
12-
let ssbCount = 0, fmCount = 0, rttyCount = 0, ft8ft4Count = 0;
13-
let pskCount = 0, cwCount = 0, js8Count = 0, digiCount = 0;
9+
// Initialize counters
10+
let totalQso = 0, totalQsoYear = 0;
11+
let ssbCount = 0, fmCount = 0, rttyCount = 0, ft8ft4Count = 0;
12+
let pskCount = 0, cwCount = 0, js8Count = 0, digiCount = 0;
1413

15-
const digi_modes = ['FT8', 'FT4', 'PSK', 'RTTY', 'JS8', 'JT65', 'JT9', 'OLIVIA', 'CONTESTI', 'ROS']; // Add more as needed
14+
// List of digital modes for grouped counting
15+
const digi_modes = ['FT8','FT4','PSK','RTTY','JS8','JT65','JT9','OLIVIA','CONTESTI','ROS'];
1616

17-
// Iterate over the station IDs and retrieve the data for each station
18-
STATION_PROFILE_IDS.forEach(stationId => {
19-
const options = {
20-
url: WAVELOG_URL,
21-
method: "POST",
22-
json: true,
23-
body: {
24-
key: API_KEY,
25-
station_id: stationId,
26-
},
27-
timeout: 15000
28-
};
17+
// Counter to track how many API requests are still pending
18+
let remaining = STATION_PROFILE_IDS.length;
2919

30-
request(options, (error, response, body) => {
31-
if (error) {
32-
console.error("Error in API query:", error);
33-
return;
34-
}
35-
if (!body || body.status !== "successful") {
36-
console.error("Error: API did not return a successful status.");
37-
return;
38-
}
20+
// Iterate over each station ID and send an API request
21+
STATION_PROFILE_IDS.forEach(stationId => {
22+
const options = {
23+
url: WAVELOG_URL,
24+
method: "POST",
25+
json: true,
26+
body: { key: API_KEY, station_id: stationId },
27+
timeout: 15000
28+
};
3929

40-
// Ensure that the values are treated as numbers
41-
totalQso += Number(body.statistics.totalalltime[0].count);
42-
totalQsoYear += Number(body.statistics.totalthisyear[0].count);
30+
request(options, (error, response, body) => {
31+
try {
32+
if (error) {
33+
console.error(`API error for station ${stationId}:`, error);
34+
} else if (!body || body.status !== "successful") {
35+
console.error(`Station ${stationId}: API did not return a successful status.`);
36+
} else {
37+
// Add total QSO counts
38+
totalQso += Number(body.statistics?.totalalltime?.[0]?.count || 0);
39+
totalQsoYear += Number(body.statistics?.totalthisyear?.[0]?.count || 0);
4340

44-
if (body.statistics.totalgroupedmodes) {
45-
body.statistics.totalgroupedmodes.forEach(mode => {
46-
const col_mode = mode.col_mode;
47-
const col_submode = mode.col_submode || '';
41+
// Process grouped modes
42+
(body.statistics?.totalgroupedmodes || []).forEach(mode => {
43+
const col_mode = mode.col_mode || '';
44+
const col_submode = mode.col_submode || '';
45+
const count = Number(mode.count || 0);
4846

49-
if (col_mode === 'SSB') ssbCount += Number(mode.count);
50-
if (col_mode === 'FM') fmCount += Number(mode.count);
51-
if (col_mode === 'RTTY') rttyCount += Number(mode.count);
52-
if (col_mode === 'CW') cwCount += Number(mode.count);
53-
if (col_mode === 'PSK' || col_submode.startsWith('PSK')) pskCount += Number(mode.count);
54-
if (col_mode === 'JS8') js8Count += Number(mode.count);
55-
if (col_mode === 'FT8' || col_submode === 'FT4') ft8ft4Count += Number(mode.count);
47+
if (col_mode === 'SSB') ssbCount += count;
48+
if (col_mode === 'FM') fmCount += count;
49+
if (col_mode === 'RTTY') rttyCount += count;
50+
if (col_mode === 'CW') cwCount += count;
51+
if (col_mode === 'PSK' || col_submode.startsWith('PSK')) pskCount += count;
52+
if (col_mode === 'JS8') js8Count += count;
5653

57-
// Digital modes total
58-
digi_modes.forEach(digi => {
59-
if (col_mode.startsWith(digi) || col_submode.startsWith(digi)) {
60-
digiCount += Number(mode.count);
61-
}
62-
});
63-
});
64-
}
54+
// FT8 and FT4 (FT4 can also appear as a submode)
55+
if (col_mode === 'FT8' || col_submode === 'FT4') ft8ft4Count += count;
6556

66-
// Once all stations have been processed, the results are written to ioBroker.
67-
if (stationId === STATION_PROFILE_IDS[STATION_PROFILE_IDS.length - 1]) {
68-
console.log(`Total QSOs: ${totalQso}, This Year: ${totalQsoYear}, SSB: ${ssbCount}, FM: ${fmCount}, RTTY: ${rttyCount}, FT8+FT4: ${ft8ft4Count}, PSK: ${pskCount}, CW: ${cwCount}, JS8: ${js8Count}, Digi: ${digiCount}`);
57+
// Digital modes total
58+
digi_modes.forEach(digi => {
59+
if (col_mode.startsWith(digi) || col_submode.startsWith(digi)) {
60+
digiCount += count;
61+
}
62+
});
63+
});
64+
}
65+
} finally {
66+
// Mark this request as finished (success or failure)
67+
remaining--;
6968

70-
// Werte in ioBroker-Datenpunkte schreiben
71-
setState("javascript.0.Wavelog.totalqso", totalQso, true);
72-
setState("javascript.0.Wavelog.totalqsoyear", totalQsoYear, true);
73-
setState("javascript.0.Wavelog.ssbqso", ssbCount, true);
74-
setState("javascript.0.Wavelog.fmqso", fmCount, true);
75-
setState("javascript.0.Wavelog.rttyqso", rttyCount, true);
76-
setState("javascript.0.Wavelog.ft8ft4qso", ft8ft4Count, true);
77-
setState("javascript.0.Wavelog.pskqso", pskCount, true);
78-
setState("javascript.0.Wavelog.cwqso", cwCount, true);
79-
setState("javascript.0.Wavelog.js8qso", js8Count, true);
80-
setState("javascript.0.Wavelog.digiqso", digiCount, true);
81-
}
82-
});
69+
// When all requests are finished, write results to ioBroker
70+
if (remaining === 0) {
71+
console.log(`Total QSOs: ${totalQso}, This Year: ${totalQsoYear}, SSB: ${ssbCount}, FM: ${fmCount}, RTTY: ${rttyCount}, FT8+FT4: ${ft8ft4Count}, PSK: ${pskCount}, CW: ${cwCount}, JS8: ${js8Count}, Digi: ${digiCount}`);
72+
73+
setState("javascript.0.Wavelog.totalqso", totalQso, true);
74+
setState("javascript.0.Wavelog.totalqsoyear", totalQsoYear, true);
75+
setState("javascript.0.Wavelog.ssbqso", ssbCount, true);
76+
setState("javascript.0.Wavelog.fmqso", fmCount, true);
77+
setState("javascript.0.Wavelog.rttyqso", rttyCount, true);
78+
setState("javascript.0.Wavelog.ft8ft4qso", ft8ft4Count, true);
79+
setState("javascript.0.Wavelog.pskqso", pskCount, true);
80+
setState("javascript.0.Wavelog.cwqso", cwCount, true);
81+
setState("javascript.0.Wavelog.js8qso", js8Count, true);
82+
setState("javascript.0.Wavelog.digiqso", digiCount, true);
83+
}
84+
}
8385
});
86+
});
8487
}
8588

8689
// Run the script immediately

0 commit comments

Comments
 (0)