1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
| /**
* Ecoforest / EPLucon – Scriptable widget (configurable)
*
* Data model:
* - statistics endpoint geeft een "rows table": veel meetregels met created_at + veel velden.
* - Wij nemen de LAATSTE row als "actueel".
*
* Aanpasbaar:
* - Zet velden aan/uit in TOGGLES (true/false).
* - Voeg eventueel eigen velden toe in FIELDS (label + mogelijke key-namen + unit + decimals).
*
*/
// =======================
// ✅ INSTELLINGEN
// =======================
const BASE = "https://portaal.eplucon.nl/api/v2";
const API_TOKEN = "";
const MODULE_ID = 1000640;
// Wanneer je in de Scriptable app runt, wil je een preview:
const PREVIEW_FAMILY = "medium"; // "small" | "medium" | "large"
// Velden aan/uit (standaard: DG1 temp + toerental aan)
const TOGGLES = {
dg1_temp: false,
compressor_rpm: true,
outside_temp: false,
indoor_temp: true,
ww_temp: true,
ww_mode: false,
cv_pressure: false,
power_now: false,
total_import_energy: false,
total_produced_energy: false,
brine_in: false,
brine_out: false,
brine_pressure: false,
heating_in: true,
heating_out: true,
condensation_temp: false,
evaporation_temp: false,
suction_gas_temp: false,
discharge_gas_temp: false,
superheat: false,
compressor_suction_pressure: false,
compressor_discharge_pressure: false,
valve_pos_sg2: false,
valve_pos_sg3: false,
valve_pos_sg4: false,
expansion_valve_pos: false,
fan_rpm: false,
setpoint_heating_dg1: false,
setpoint_cooling_dg1: false,
setpoint_heating_sg2: false,
setpoint_cooling_sg2: false,
setpoint_heating_sg3: false,
setpoint_cooling_sg3: false,
setpoint_heating_sg4: false,
setpoint_cooling_sg4: false,
overheat: false,
inverter_temp: false,
};
// =======================
// 📌 VELDDEFINITIES
// =======================
/**
* FIELDS definieert alle velden die je kunt tonen.
*
* id: moet overeenkomen met TOGGLES key
* label: wat je in de widget ziet
* keys: mogelijke veldnamen zoals ze in jouw row-object voorkomen
* unit: suffix (°C, bar, W, rpm, etc.)
* decimals: afronding
*
* Je kunt hier makkelijk velden bijmaken/aanpassen.
*/
const FIELDS = [
{ id: "dg1_temp", label: "Actuele temp DG1", keys: ["Actuele temp. DG1"], unit: "°C", decimals: 1 },
{ id: "compressor_rpm", label: "Compressor", keys: ["Compressor toerental"], unit: "rpm", decimals: 0 },
{ id: "outside_temp", label: "Buiten", keys: ["Buitentemp."], unit: "°C", decimals: 1 },
{ id: "indoor_temp", label: "Binnen", keys: ["Binnentemperatuur"], unit: "°C", decimals: 1 },
{ id: "ww_temp", label: "WW", keys: ["WW temperatuur"], unit: "°C", decimals: 1 },
{ id: "cv_pressure", label: "CV druk", keys: ["Cv druk", "CV druk"], unit: "bar", decimals: 1 },
{ id: "power_now", label: "Actueel verbruik", keys: ["Actueel verbruik"], unit: "W", decimals: 0 },
{ id: "total_import_energy", label: "Import energie", keys: ["Totaal import energie"], unit: "kWh", decimals: 1 },
{ id: "total_produced_energy", label: "Geproduceerd", keys: ["Totaal geproduceerde energie"], unit: "kWh", decimals: 1 },
{ id: "brine_in", label: "Brine in", keys: ["Brine in temperatuur"], unit: "°C", decimals: 1 },
{ id: "brine_out", label: "Brine uit", keys: ["Brine out temperatuur"], unit: "°C", decimals: 1 },
{ id: "brine_pressure", label: "Brine druk", keys: ["Brine druk"], unit: "bar", decimals: 1 },
{ id: "heating_in", label: "Heating in", keys: ["Heating in temperatuur"], unit: "°C", decimals: 1 },
{ id: "heating_out", label: "Heating out", keys: ["Heating out temperatuur"], unit: "°C", decimals: 1 },
{ id: "condensation_temp", label: "Condensatie", keys: ["Condensatie temp."], unit: "°C", decimals: 1 },
{ id: "evaporation_temp", label: "Verdamping", keys: ["Verdamping temp."], unit: "°C", decimals: 1 },
{ id: "suction_gas_temp", label: "Zuig gas", keys: ["Zuig gas temperatuur"], unit: "°C", decimals: 1 },
{ id: "discharge_gas_temp", label: "Pers gas", keys: ["Pers gas temperatuur"], unit: "°C", decimals: 1 },
{ id: "superheat", label: "Oververhitting", keys: ["Oververhitting"], unit: "K", decimals: 1 },
{ id: "compressor_suction_pressure", label: "Zuigdruk", keys: ["Compressorzuigdruk"], unit: "bar", decimals: 1 },
{ id: "compressor_discharge_pressure", label: "Afvoerdruk", keys: ["Compressorafvoerdruk"], unit: "bar", decimals: 1 },
{ id: "valve_pos_sg2", label: "Ventiel SG2", keys: ["Positie ventiel SG2"], unit: "%", decimals: 0 },
{ id: "valve_pos_sg3", label: "Ventiel SG3", keys: ["Positie ventiel SG3"], unit: "%", decimals: 0 },
{ id: "valve_pos_sg4", label: "Ventiel SG4", keys: ["Positie ventiel SG4"], unit: "%", decimals: 0 },
{ id: "expansion_valve_pos", label: "Exp. ventiel", keys: ["Positie expansie ventiel"], unit: "%", decimals: 0 },
{ id: "fan_rpm", label: "Ventilator", keys: ["Act. vent. toerental"], unit: "rpm", decimals: 0 },
{ id: "setpoint_heating_dg1", label: "SP verw DG1", keys: ["Setpoint verwarming DG1"], unit: "°C", decimals: 1 },
{ id: "setpoint_cooling_dg1", label: "SP koel DG1", keys: ["Setpoint koeling DG1"], unit: "°C", decimals: 1 },
{ id: "setpoint_heating_sg2", label: "SP verw SG2", keys: ["Setpoint verwarming SG2"], unit: "°C", decimals: 1 },
{ id: "setpoint_cooling_sg2", label: "SP koel SG2", keys: ["Setpoint koeling SG2"], unit: "°C", decimals: 1 },
{ id: "setpoint_heating_sg3", label: "SP verw SG3", keys: ["Setpoint verwarming SG3"], unit: "°C", decimals: 1 },
{ id: "setpoint_cooling_sg3", label: "SP koel SG3", keys: ["Setpoint koeling SG3"], unit: "°C", decimals: 1 },
{ id: "setpoint_heating_sg4", label: "SP verw SG4", keys: ["Setpoint verwarming SG4"], unit: "°C", decimals: 1 },
{ id: "setpoint_cooling_sg4", label: "SP koel SG4", keys: ["Setpoint koeling SG4"], unit: "°C", decimals: 1 },
{ id: "overheat", label: "Oververhitting", keys: ["Oververhitting"], unit: "K", decimals: 1 },
{ id: "inverter_temp", label: "Inverter temp", keys: ["Inverter temperatuur"], unit: "°C", decimals: 1 },
{ id: "ww_mode", label: "WW", keys: ["WW"], unit: "", decimals: 0 },
];
// =======================
// 🔧 HELPERS
// =======================
async function showInAppOrWidget(widget, family = "medium") {
if (config.runsInWidget) {
Script.setWidget(widget);
} else {
if (family === "small") await widget.presentSmall();
else if (family === "large") await widget.presentLarge();
else await widget.presentMedium();
}
Script.complete();
}
// EPLucon lijkt invalid values te gebruiken zoals -9999.
function cleanValue(v) {
if (v === null || v === undefined) return null;
const num = Number(v);
if (!Number.isNaN(num) && (num === -9999 || num === 9999)) return null;
return v;
}
function fmtValue(v, unit, decimals) {
v = cleanValue(v);
if (v === null || v === undefined) return "—";
const num = Number(v);
if (Number.isNaN(num)) return String(v) + (unit ? ` ${unit}` : "");
return num.toFixed(decimals) + (unit ? ` ${unit}` : "");
}
async function apiGetJSON(path) {
const req = new Request(`${BASE}${path}`);
req.method = "GET";
req.headers = { "Authorization": `Bearer ${API_TOKEN}`, "Accept": "application/json" };
req.timeoutInterval = 6;
return await req.loadJSON();
}
/**
* - stats.data is niet de rows-array maar bevat zelf weer .data
* Daarom unwrapen totdat we een array vinden.
*/
function extractRows(stats) {
if (Array.isArray(stats)) return stats;
const candidates = [
stats?.data,
stats?.data?.data,
stats?.result?.data,
stats?.items,
stats?.items?.data,
];
for (const c of candidates) if (Array.isArray(c)) return c;
// follow .data chain (max 5)
let cur = stats;
for (let i = 0; i < 5; i++) {
if (Array.isArray(cur)) return cur;
if (cur && typeof cur === "object" && cur.data !== undefined) cur = cur.data;
else break;
}
return null;
}
async function fetchRowsToday(moduleId) {
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const path = `/econtrol/modules/${moduleId}/statistics`
const stats = await apiGetJSON(path);
const rows = extractRows(stats);
console.log(rows[rows.length-1])
if (!Array.isArray(rows)) {
const topKeys = stats && typeof stats === "object" ? Object.keys(stats).join(",") : String(typeof stats);
throw new Error("Kan rows-array niet vinden. Top-level keys: " + topKeys);
}
if (rows.length === 0) throw new Error("Geen rows vandaag (array leeg).");
return rows;
}
function getLatestRow(rows) {
return rows[rows.length - 1];
}
function getField(row, keys) {
for (const k of keys) {
if (row[k] !== undefined) return row[k];
}
return null;
}
// =======================
// 🖼️ WIDGET UI
// =======================
function buildWidgetFromRow(row) {
const w = new ListWidget();
w.setPadding(20, 20, 20, 20);
const title = w.addText("Ecoforest live");
title.font = Font.boldSystemFont(16);
w.addSpacer(8);
// 1) Bepaal welke FIELDS aan staan
const active = FIELDS.filter(f => TOGGLES[f.id]);
// 2) Render ze
for (const f of active) {
const raw = getField(row, f.keys);
const text = fmtValue(raw, f.unit, f.decimals);
const t = w.addText(`${f.label}: ${text}`);
t.font = Font.systemFont(18);
t.minimumScaleFactor = 0.7;
}
w.addSpacer(8);
const ts = row["created_at"] ? new Date(row["created_at"]) : new Date();
const footer = w.addText(
`Laatste meting: ${ts.toLocaleTimeString("nl-NL", { hour: "2-digit", minute: "2-digit" })}`
);
footer.font = Font.systemFont(10);
footer.textOpacity = 0.6;
return w;
}
// =======================
// ▶️ MAIN
// =======================
try {
const moduleId = MODULE_ID;
const rows = await fetchRowsToday(moduleId);
const latest = getLatestRow(rows);
const widget = buildWidgetFromRow(latest);
await showInAppOrWidget(widget, PREVIEW_FAMILY);
} catch (e) {
const w = new ListWidget();
w.setPadding(12, 12, 12, 12);
w.addText("Ecoforest live").font = Font.boldSystemFont(16);
w.addSpacer(8);
w.addText("Fout bij ophalen").font = Font.boldSystemFont(14);
w.addSpacer(4);
w.addText(String(e)).font = Font.systemFont(11);
await showInAppOrWidget(w, PREVIEW_FAMILY);
} |