-
Notifications
You must be signed in to change notification settings - Fork 8
/
shelly-porssisahko.js
920 lines (767 loc) · 22.4 KB
/
shelly-porssisahko.js
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/**
* shelly-porssisahko
*
* (c) Jussi isotalo - http://jisotalo.fi
* https://github.com/jisotalo/shelly-porssisahko
*
* License: GNU Affero General Public License v3.0
*/
/**
* Number of log lines saved to history
* NOTE: Currenly commented out as we have no memory
*/
//let C_LOG = 0;
/**
* Number of historical commands kept
*/
let C_HIST = 24;
/** How many errors during getting prices until we have a break */
let C_ERRC = 3;
/** How long to wait after multiple errors (>= C_ERRC) before trying again (s) */
let C_ERRD = 120;
/** Default config to use if some/all of them are missing from KVS */
let C_DEF = {
/**
* Active mode
* 0: manual mode (on/off toggle)
* 1: price limit
* 2: cheapest hours
*/
mode: 0,
/** Settings for mode 0 */
m0: {
/** Manual relay output command */
cmd: 0
},
/** Settings for mode 1 */
m1: {
/** Price limit (€ cents/kWh) limit - if price <= relay output command is set on */
lim: 0
},
/** Settings for mode 2 */
m2: {
/** Period length (24 -> cheapest hours during 24h) */
per: 24,
/** How many cheapest hours */
cnt: 0,
/** Always on price limit */
lim: -999,
/** Should the hours be sequential / in a row */
sq: 0,
/** Maximum price limit */
m: 999
},
/** VAT added to spot price (%) */
vat: 24,
/** Day (07...22) transfer price c/kWh */
day: 0,
/** Night (22...07) transfer price c/kWh */
night: 0,
/** Backup hours as binary*/
bk: 0,
/** Relay output command if clock time is not known */
err: 0,
/** Output number to use */
out: 0,
/** Forced ON hours as binary */
fh: 0,
/** Invert output */
inv: 0
};
/** Log history */
//let logData = [];
//NOTE: Currenly commented out as we have no memory
/** Main state of app */
let _ = {
s: {
/** version number */
v: "2.6.1",
/** status as number */
st: 0,
/** active command */
cmd: 0,
/** epoch when last check was done (logic was run) */
chkTs: 0,
/** active error count */
errCnt: 0,
/** epoch of last error */
errTs: 0,
/** epoch when started (when time was ok for first time) */
upTs: 0,
/** 1 if we have somewhat ok time */
timeOK: 0,
/** 1 if config is checked */
configOK: 0,
/** If forced manually to ON, then this is the timestamp until cmd shall be on */
fCmdTs: 0,
/** current price info */
p: {
/** time when prices were read */
ts: 0,
/** current price */
now: 0,
/** lowest price of the day */
low: 0,
/** highest price of the day */
high: 0,
/** average price of the day */
avg: 0
}
},
/** prices of the day (each item is array [epoch, price]*/
p: [],
/** command history (each item is array [epoch, cmd])*/
h: [],
/** actice config */
c: C_DEF
};
/**
* True if loop is running (new one is not started + HTTP requests are not handled)
*/
let loopRunning = false;
/** Active command (internal) - here because some weird issues if was inside logic()... (memory/stack issues?) */
let cmd = false;
/**
* Returns true if hour in epoch timestamp is current hour
* @param val epoch value
* @param now current epoch time (s)
* @returns
*/
function isCurrentHour(val, now) {
let diff = now - val;
return diff >= 0 && diff < (60 * 60);
}
/**
* Returns epoch time (s) without decimals
* @param date Date object (optional) - if not provided, taking current time
* @returns
*/
function epoch(date) {
return Math.floor((date ? date.getTime() : Date.now()) / 1000.0);
}
/**
* Similar as String.padStart() which is missing from Shelly
* @param {*} str String to add characters
* @param {*} targetLength The length of the resulting string
* @param {*} padString The string to pad the current str with. (optional - default is " ")
*/
function padStart(num, targetLength, padString) {
let str = num.toString();
while (str.length < targetLength) {
str = padString ? padString + str : " " + str;
}
return str;
}
/**
* Wrapper for Date.getDate to help minifying
* @param {Date} dt
* @returns
*/
function getDate(dt) {
return dt.getDate();
}
/**
* Adds new log line row
* @param {*} str
*/
function log(data, me) {
let now = new Date();
console.log(now.toISOString().substring(11) + ": " + (me ? me + " - " : ""), data);
//NOTE: Currenly commented out as we have no memory
/*logData.push([epoch(now), me + ": " + str]);
if (logData.length >= C_LOG) {
logData.splice(0, 1);
}*/
}
/**
* Updates state
* - Checks if time is OK
* - Some things need to be kept up-to-date here
*/
function updateState() {
let now = new Date();
_.s.timeOK = now.getFullYear() > 2000 ? 1 : 0;
if (!_.s.upTs && _.s.timeOK) {
_.s.upTs = epoch(now);
}
}
/**
* Checks configuration
* If a config key is missings, adds a new one with default value
*/
function chkConfig(cb) {
//let me = "chkConfig()";
let count = 0;
if (!C_DEF) {
if (cb) {
cb(true);
}
return;
}
//Todo: Change to recursive, now hard-coded to max 2 levels
for (let prop in C_DEF) {
if (typeof _.c[prop] === "undefined") {
//log("Asetus '" + prop + "' alustettu arvoon " + JSON.stringify(C_DEF[prop]), me);
_.c[prop] = C_DEF[prop];
count++;
} else if (typeof C_DEF[prop] === "object") {
for (let innerProp in C_DEF[prop]) {
if (typeof _.c[prop][innerProp] === "undefined") {
//log("Asetus '" + prop + "." + innerProp + "' alustettu arvoon " + JSON.stringify(C_DEF[prop][innerProp]), me);
_.c[prop][innerProp] = C_DEF[prop][innerProp];
count++;
}
}
}
}
//Deleting default config after 1st check to save memory...
C_DEF = null;
if (count > 0) {
Shelly.call("KVS.Set", { key: "porssi-config", value: _.c }, function (res, err, msg, cb) {
if (err !== 0) {
//log("virhe tallennettaessa asetuksia:" + err + " - " + msg, me);
} else {
//log("asetukset tallennettu", me);
}
if (cb) {
cb(err === 0);
}
}, cb);
} else {
if (cb) {
cb(true);
}
}
}
/**
* Reads config from KVS
*/
function getConfig(isLoop) {
//let me = "getConfig()";
//log("luetaan asetukset...", me);
//Note: passing callback (cb) as userdata otherwise it can't be accessed from Shelly.call callback
Shelly.call('KVS.Get', { key: "porssi-config" }, function (res, err, msg, isLoop) {
if (!res) {
//log("asetuksia ei löytynyt - alustetaan...", me);
_.c = {};
} else {
_.c = res.value;
}
chkConfig(function (ok) {
_.s.configOK = ok ? 1 : 0;
_.s.chkTs = 0; //To run the logic again with new settings
//log("asetukset ladattu", me);
if (isLoop) {
loopRunning = false;
loop();
}
});
}, isLoop);
}
/**
* Background process loop that is called every x seconds
*/
function loop() {
if (loopRunning) {
return;
}
loopRunning = true;
updateState();
if (!_.s.configOK) {
getConfig(true);
} else if (pricesNeeded()) {
getPrices();
} else if (logicRunNeeded()) {
//Hour has changed or we now know the time (=year has changed)
logic();
} else {
//Nothing to do
loopRunning = false;
}
}
/**
* Returns true if we should get prices for today
*/
function pricesNeeded() {
//let me = "pricesNeeded()";
let now = new Date();
let res = false;
/*
Getting prices for today if
- we have a valid time
- prices have never been fetched OR prices are from different day
*/
res = _.s.timeOK && (_.s.p.ts === 0 || getDate(new Date(_.s.p.ts * 1000)) !== getDate(now));
//If fetching prices has failed too many times -> wait until trying again
if (_.s.errCnt >= C_ERRC && (epoch(now) - _.s.errTs) < C_ERRD) {
let timeLeft = (C_ERRD - (epoch(now) - _.s.errTs));
//log("liikaa virheitä, odotetaan " + timeLeft.toFixed(0) + " s", me);
res = false;
} else if (_.s.errCnt >= C_ERRC) {
//We can clear error counter (time has passed)
_.s.errCnt = 0;
}
return res;
}
/**
* Returns true if we should run the logic now
*/
function logicRunNeeded() {
let now = new Date();
let chk = new Date(_.s.chkTs * 1000);
//for debugging:
/*
return (chk.getMinutes() !== now.getMinutes()
|| chk.getFullYear() !== now.getFullYear())
|| (_.s.fCmdTs > 0 && _.s.fCmdTs - epoch(now) < 0);
*/
return (chk.getHours() !== now.getHours()
|| chk.getFullYear() !== now.getFullYear())
|| (_.s.fCmdTs > 0 && _.s.fCmdTs - epoch(now) < 0);
}
/**
* Gets prices for today and then runs the logic
*/
function getPrices() {
let now = new Date();
try {
//let me = "getPrices()";
//log("haetaan päivän hinnat (yritys " + (_.s.errCnt + 1) + "/" + C_ERRC + ")", me);
let start = now.getFullYear()
+ "-"
+ padStart(now.getMonth() + 1, 2, "0")
+ "-"
+ padStart(getDate(now), 2, "0")
+ "T00:00:00"
+ "%2b02:00";
let end = start.replace("T00:00:00", "T23:59:59");
let req = {
url: "https://dashboard.elering.ee/api/nps/price/csv?fields=fi&start=" + start + "&end=" + end,
timeout: 5,
ssl_ca: "*"
};
//Clearing variables to save memory
now = null;
start = null;
end = null;
//log("URL:" + req.url, me);
Shelly.call("HTTP.GET", req, function (res, err, msg) {
req = null;
try {
if (err === 0 && res != null && res.code === 200 && res.body_b64) {
//Clearing some fields to save memory
res.headers = null;
res.message = null;
msg = null;
//log("hintadata luettu, käydään läpi", me);
_.p = [];
_.s.p.high = -999;
_.s.p.low = 999;
//Converting base64 to text
res.body_b64 = atob(res.body_b64);
//Discarding header
res.body_b64 = res.body_b64.substring(res.body_b64.indexOf("\n") + 1);
let activePos = 0;
while (activePos >= 0) {
res.body_b64 = res.body_b64.substring(activePos);
activePos = 0;
let row = [0, 0];
activePos = res.body_b64.indexOf("\"", activePos) + 1;
if (activePos === 0) {
//" character not found -> end of data
break;
}
//epoch
row[0] = Number(res.body_b64.substring(activePos, res.body_b64.indexOf("\"", activePos)));
//skip "; after timestamp
activePos = res.body_b64.indexOf("\"", activePos) + 2;
//price
activePos = res.body_b64.indexOf(";\"", activePos) + 2;
row[1] = Number(res.body_b64.substring(activePos, res.body_b64.indexOf("\"", activePos)).replace(",", "."));
//Converting price to c/kWh and adding VAT to price
row[1] = row[1] / 10.0 * (100 + (row[1] > 0 ? _.c.vat : 0)) / 100.0;
//Add transfer fees (if any)
let hour = new Date(row[0] * 1000).getHours();
if (hour >= 7 && hour < 22) {
//day
row[1] += _.c.day;
} else {
//night
row[1] += _.c.night;
}
//Adding and calculating stuff
_.p.push(row);
_.s.p.avg += row[1];
if (row[1] > _.s.p.high) {
_.s.p.high = row[1];
}
if (row[1] < _.s.p.low) {
_.s.p.low = row[1];
}
//find next row
activePos = res.body_b64.indexOf("\n", activePos);
}
//Again to save memory..
res = null;
//Calculate average
_.s.p.avg = _.p.length > 0 ? (_.s.p.avg / _.p.length) : 0;
//Check that read price data is from today
let now = new Date();
let priceDate = new Date(_.p[0][0] * 1000);
if (getDate(priceDate) === getDate(now)) {
_.s.p.ts = epoch(now);
_.s.p.now = getPriceNow();
//log("päivän hintatiedot päivitetty", me);
} else {
//throw new Error("virhe, hinnat eri päivältä - nyt:" + now.toString() + " - data:" + priceDate.toString());
throw new Error("date err " + now.toString() + " - " + priceDate.toString());
}
} else {
//throw new Error("virhe luettaessa hintoja: " + err + "(" + msg + ") - " + JSON.stringify(res));
throw new Error("conn.err (" + msg + ") " + JSON.stringify(res));
}
} catch (err) {
_.s.errCnt += 1;
_.s.errTs = epoch();
_.s.p.ts = 0;
_.p = [];
log(err);
//NOTE: All the rest are commented for now because of memory limit
/*
if (err.message.indexOf("virhe") >= 0) {
log(err.message, me);
} else {
log("virhe: " + JSON.stringify(err), me);
}
*/
}
/*
NOTE: commented for now because of memory limit
if (_.s.errCnt >= C_ERRC) {
//log("luku epäonnistui " + EC + " kertaa, pidetään taukoa", me);
}
*/
//Run logic no matter what happened
logic();
});
} catch (err) {
log(err);
//Run logic no matter what happened
logic();
}
}
/**
* Sets relay output to cmd
* If callback given, its called with success status, like cb(true)
* @param {*} cb callback (optional)
*/
function setRelay(cb) {
//let me = "setRelay()";
let prm = "{id:" + _.c.out + ",on:" + (cmd ? "true" : "false") + "}";
Shelly.call("Switch.Set", prm, function (res, err, msg, cb) {
if (err === 0) {
//log("lähtö asetettu " + (cmd ? "PÄÄLLE" : "POIS"), me);
_.s.cmd = cmd ? 1 : 0;
while (_.h.length >= C_HIST) {
_.h.splice(0, 1);
}
_.h.push([epoch(), cmd ? 1 : 0]);
//Call callback (if any)
if (cb) {
cb(true);
}
} else {
//log("virhe asettaessa lähtöä " + (cmd ? "PÄÄLLE" : "POIS") + ": " + err + " - " + msg, me);
//Call callback (if any)
if (cb) {
cb(false);
}
}
}, cb);
}
/**
* Runs the main logic
*/
function logic() {
//let me = "logic()";
let now = new Date();
cmd = false;
try {
if (_.s.timeOK && (_.s.p.ts > 0 && getDate(new Date(_.s.p.ts * 1000)) === getDate(now))) {
//We have time and we have price data for today
_.s.p.now = getPriceNow();
//log("tämän tunnin hinta: " + _.s.p.now + " c/kWh", me);
if (_.c.mode === 0) {
//Manual mode
cmd = _.c.m0.cmd === 1;
//log("moodi on käsiohjaus, ohjaus: " + (cmd ? "PÄÄLLE" : "POIS"), me);
_.s.st = 1;
} else if (_.c.mode === 1) {
//Price limit
cmd = _.s.p.now <= (_.c.m1.lim == "avg" ? _.s.p.avg : _.c.m1.lim);
//log("moodi on hintaraja (" + _.c.m1.priceLimit + "), ohjaus: " + (cmd ? "PÄÄLLE" : "POIS"), me);
_.s.st = cmd ? 2 : 3;
} else if (_.c.mode === 2) {
//Cheapest hours
cmd = isCheapestHour();
_.s.st = cmd ? 5 : 4;
//always on price limit
if (!cmd && _.s.p.now <= (_.c.m2.lim == "avg" ? _.s.p.avg : _.c.m2.lim)) {
cmd = true;
_.s.st = 6;
}
//maximum price
if (cmd && _.s.p.now > (_.c.m2.m == "avg" ? _.s.p.avg : _.c.m2.m)) {
cmd = false;
_.s.st = 11;
}
//log("moodi on halvimmat tunnit, ohjaus: " + (cmd ? "PÄÄLLE" : "POIS"), me);
}
} else if (_.s.timeOK) {
//We have time but no data for today
_.s.st = 7;
let binNow = (1 << now.getHours());
if ((_.c.bk & binNow) == binNow) {
cmd = true;
}
//log("hintatietoja ei ole, ohjaus: " + (cmd ? "PÄÄLLE" : "POIS"), me);
} else {
//Time is not known
cmd = _.c.err === 1;
_.s.st = 8;
//log("kellonaikaa ei ole, ohjaus: " + (cmd ? "PÄÄLLE" : "POIS"), me);
}
//Forcing automatically or manually
if (_.s.timeOK) {
//Forced hours
if (_.c.fh > 0) {
let binNow = (1 << now.getHours());
if ((_.c.fh & binNow) == binNow) {
cmd = true;
_.s.st = 10;
}
}
//Manual force
if (_.s.fCmdTs > 0) {
if (_.s.fCmdTs - epoch(now) > 0) {
cmd = true;
_.s.st = 9;
} else {
_.s.fCmdTs = 0;
}
}
}
//Invert
if (_.c.inv) {
cmd = !cmd;
}
//Setting relay command and after that starting background timer again
setRelay(function (ok) {
if (ok) {
_.s.chkTs = epoch();
}
loopRunning = false;
});
} catch (err) {
//log("virhe: " + JSON.stringify(err), me);
log(err);
loopRunning = false;
}
}
/**
* Returns true if current hour is one of the cheapest
*
* NOTE: Variables are here outside function() as it caused memory issues
* (for loop i variable suddenly changed to something very odd)
* Perhaps the stack was getting too full or something because of multiple for() loops?
* This worked!
*
*/
let _perStart = 0;
let _ind = 0;
let _ind2 = 0;
function isCheapestHour() {
if (_.c.m2.cn == 0) {
return;
}
//This is (and needs to be) 1:1 in both frontend and backend code
let cheapest = [];
for (_perStart = 0; _perStart < 24; _perStart += _.c.m2.per) {
//Create array of indexes in selected period
let order = [];
for (ind = _perStart; ind < _perStart + _.c.m2.per; ind++) order.push(ind);
if (_.c.m2.sq) {
//Find cheapest in a sequence
//Loop through each possible starting index and compare average prices
let avg = 999;
let startIndex = 0;
for (_ind = 0; _ind <= order.length - _.c.m2.cnt; _ind++) {
let sum = 0;
//Calculate sum of these sequential hours
for (_ind2 = _ind; _ind2 < _ind + _.c.m2.cnt; _ind2++) {
sum += _.p[order[_ind2]][1];
};
//If average price of these sequential hours is lower -> it's better
if (sum / _.c.m2.cnt < avg) {
avg = sum / _.c.m2.cnt;
startIndex = _ind;
}
}
for (_ind = startIndex; _ind < startIndex + _.c.m2.cnt; _ind++) {
cheapest.push(order[_ind]);
}
} else {
//Sort indexes by price
for (_ind = 1; _ind < order.length; _ind++) {
let temp = order[_ind];
for (_ind2 = _ind - 1; _ind2 >= 0 && _.p[temp][1] < _.p[order[_ind2]][1]; _ind2--) {
order[_ind2 + 1] = order[_ind2];
}
order[_ind2 + 1] = temp;
}
//Select the cheapest ones
for (_ind = 0; _ind < _.c.m2.cnt; _ind++) {
cheapest.push(order[_ind]);
}
}
}
//Check if current hour is cheap enough
let epochNow = epoch();
let res = false;
for (let i = 0; i < cheapest.length; i++) {
let row = _.p[cheapest[i]];
if (isCurrentHour(row[0], epochNow)) {
//This hour is active -> current hour is one of the cheapest
res = true;
break;
}
}
_perStart = null;
_ind = null;
_ind2 = null;
return res;
}
/**
* Returns the current price. Throws error if not found
* @returns
*/
function getPriceNow() {
////let me = "getPriceNow()";
let now = epoch();
//todo start from current hour?
for (let i = 0; i < _.p.length; i++) {
if (isCurrentHour(_.p[i][0], now)) {
//This hour is active
return _.p[i][1];
}
}
//This should happen
//log("tämän tunnin hintaa ei löytynyt", me);
throw new Error("no price");
}
/**
* Parses parameters from HTTP GET request query to array of objects
* For example key=value&key2=value2
* @param {*} params
* @returns
*/
function parseParams(params) {
let res = {};
let splitted = params.split("&");
for (let i = 0; i < splitted.length; i++) {
let pair = splitted[i].split("=");
res[pair[0]] = pair[1];
}
return res;
}
/**
* Handles server HTTP requests
* @param {*} request
* @param {*} response
*/
function onServerRequest(request, response) {
try {
if (loopRunning) {
request = null;
response.code = 503;
//NOTE: Uncomment the next line for local development or remote API access (allows cors)
//response.headers = [["Access-Control-Allow-Origin", "*"]];
response.send();
return;
}
//Parsing parameters (key=value&key2=value2) to object
let params = parseParams(request.query);
request = null;
let MIME_TYPE = "application/json"; //default
response.code = 200; //default
let GZIP = true; //default
let MIME_HTML = "text/html";
let MIME_JS = "text/javascript";
let MIME_CSS = "text/css";
if (params.r === "s") {
//s = get state
updateState();
//if k given, return only the key k
/*
if (params.k) {
response.body = JSON.stringify(_[params.k]);
} else {*/
response.body = JSON.stringify(_);
GZIP = false;
//}
} else if (params.r === "r") {
//r = reload settings
_.s.configOK = false; //reload settings (prevent getting prices before new settings loaded )
getConfig(false);
_.s.p.ts = 0; //get prices
response.code = 204;
GZIP = false;
} else if (params.r === "f" && params.ts) {
//f = force
_.s.fCmdTs = Number(params.ts);
_.s.chkTs = 0;
response.code = 204;
GZIP = false;
} else if (!params.r) {
response.body = atob('#[index.html]');
MIME_TYPE = MIME_HTML;
} else if (params.r === "s.js") {
response.body = atob('#[s.js]');
MIME_TYPE = MIME_JS;
} else if (params.r === "s.css") {
response.body = atob('#[s.css]');
MIME_TYPE = MIME_CSS;
} else if (params.r === "status") {
response.body = atob('#[tab-status.html]');
MIME_TYPE = MIME_HTML;
} else if (params.r === "status.js") {
response.body = atob('#[tab-status.js]');
MIME_TYPE = MIME_JS;
} else if (params.r === "config") {
response.body = atob('#[tab-config.html]');
MIME_TYPE = MIME_HTML;
} else if (params.r === "config.js") {
response.body = atob('#[tab-config.js]');
MIME_TYPE = MIME_JS;
} else {
response.code = 404;
}
params = null;
response.headers = [["Content-Type", MIME_TYPE]];
//NOTE: Uncomment the next line for local development or remote API access (allows cors)
//response.headers.push(["Access-Control-Allow-Origin", "*"]);
if (GZIP) {
response.headers.push(["Content-Encoding", "gzip"]);
}
} catch (err) {
log(err);
response.code = 500;
}
response.send();
}
//Startup
log("shelly-porssisahko v." + _.s.v);
log("URL: http://" + (Shelly.getComponentStatus("wifi").sta_ip ?? "192.168.33.1") + "/script/" + Shelly.getCurrentScriptId());
HTTPServer.registerEndpoint('', onServerRequest);
Timer.set(10000, true, loop);
loop();