forked from JackV2020/thermostatPlusBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThermostatPlusBasicApp.qml
More file actions
927 lines (706 loc) · 37.1 KB
/
Copy pathThermostatPlusBasicApp.qml
File metadata and controls
927 lines (706 loc) · 37.1 KB
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
921
922
923
924
925
926
927
import QtQuick 2.1
import qb.components 1.0
import qb.base 1.0;
import FileIO 1.0
import BxtClient 1.0
App { id : app
property url thermostatPlusBasicTileUrl : "ThermostatPlusBasicTile.qml"
property ThermostatPlusBasicTile thermostatPlusBasicTile
property url thermostatPlusBasicControlUrl : "ThermostatPlusBasicControl.qml"
property ThermostatPlusBasicControl thermostatPlusBasicControl
property url thermostatPlusBasicSettingsUrl : "ThermostatPlusBasicSettings.qml"
property ThermostatPlusBasicSettings thermostatPlusBasicSettings
// fill some language tables
property variant languages : ["Nederlands", "English", "Deutsch" ]
property variant statesLng : [ ["Comfort", "Thuis", "Slapen","Weg"] , ["Comfort", "Home", "Sleep","Away"] , ["Komfort", "Zuhause", "Schlafen","Unterwegs"] ]
property variant temporaryLng : [ "Tijdelijk" , "Temporary " , "Momentan" ]
property variant programOnOffLng: [ ["Programma Aan", "Programma Uit"] , ["Program On", "Program Off"] , ["Programm An", "Programm Aus"] ]
// guiMode is a trick to avoid the 'TERUG' on the settings screen.
// The settings screens is reached from the Control screen via the Tile.
property string guiMode
// lastSettingsMode remembers which last settings screen was active
property int lastSettingsMode
property string mode : ""
property string toonIP : ""
property int currentLng : -1
property variant xmlhttpGetStatus
property variant xmlhttpSetStatus
property variant toonStatusData
property bool xmlhttpGetStatusAsyncMode
property int currentTempInt
property string currentTemp : ""
property int currentSetpointInt : 0
property string currentSetpoint : ""
property int programState : 0
property int toonprogramState : 0 // retrieved from Toon and used to turn it off when needed
property int activeState : -1
property string activeStateText : ""
property int nextState : -1
property string nextStateStr : ""
property string nextSetpoint : ""
property string nextTime : ""
property bool burnerInfo : false
property string fireBigTile : "file:///qmf/qml/apps/thermostatPlusBasic/drawables/fireBigTile.png"
property string fireSmallTile : "file:///qmf/qml/apps/thermostatPlusBasic/drawables/fireSmallTile.png"
property string fireBigControl : "file:///qmf/qml/apps/thermostatPlusBasic/drawables/fireBigControl.png"
property string fireSmallControl: "file:///qmf/qml/apps/thermostatPlusBasic/drawables/fireSmallControl.png"
property string upImg : "file:///qmf/qml/apps/thermostatPlusBasic/drawables/Up.png"
property string downImg : "file:///qmf/qml/apps/thermostatPlusBasic/drawables/Down.png"
property string copyImg : "file:///qmf/qml/apps/thermostatPlusBasic/drawables/copy.png"
property string pasteImg : "file:///qmf/qml/apps/thermostatPlusBasic/drawables/paste.png"
property string settingsFile : "file:///mnt/data/tsc/thermostatPlusBasicSettings.json"
property string variablesFile : "file:///qmf/www/thermostatPlusBasicVariables.html"
property variant oldVariables : {}
// -------------------------------------- Program and temperature variables
property string programDate
property variant nextDateTime
property variant nextDateTimeInt : 0
property int programStateOverruleCounter : 0
property int programDays
property string programTempComfort
property string programTempHome
property string programTempSleep
property string programTempAway
// https://www.color-hex.com/
property string programColorComfort : "#fcb515" // bruin
property string programColorHome : "#e4ff78" // geel
property string programColorSleep : "#dd6fc8" // paars
property string programColorAway : "#6897bb" // blauw
property variant programColor : [ programColorComfort , programColorHome , programColorSleep , programColorAway ]
property variant scheduleTime : []
property variant scheduleProgram : []
property int scheduleIndex
property int nextscheduleIndex
property int scheduleSetpointInt
// -------------------------------------- Location of user settings file
FileIO {
id : userSettingsFile
source : settingsFile
}
// -------------------------- Structure user settings from settings file
property variant userSettingsJSON : {}
// -------------------------------------------- Register tile and screen
function init() {
const args = {
thumbCategory : "general",
thumbLabel : "Thermostat+B",
thumbIcon : "qrc:/tsc/flameSmall.png",
thumbIconVAlignment : "center",
thumbWeight : 30
}
registry.registerWidget("tile" , thermostatPlusBasicTileUrl , this, "thermostatPlusBasicTile", args);
registry.registerWidget("screen", thermostatPlusBasicControlUrl , this, "thermostatPlusBasicControl");
registry.registerWidget("screen", thermostatPlusBasicSettingsUrl , this, "thermostatPlusBasicSettings");
// The next two lines make you jump to that screen when you click the message on Toon
// notifications.registerType("thermostatPlusBasic", notifications.prio_HIGHEST, Qt.resolvedUrl("qrc:/tsc/notification-update.svg"), thermostatPlusBasicControlUrl , {"categoryUrl": thermostatPlusBasicControlUrl }, "thermostatPlusBasic mededelingen");
// notifications.registerSubtype("thermostatPlusBasic", "mededeling", thermostatPlusBasicControlUrl , {"categoryUrl": thermostatPlusBasicControlUrl});
}
// ---------------------------------------------------------------------
function sendNotification(text) {
notifications.send("thermostatPlusBasic", "mededeling", false, text, "category=mededeling");
}
// ------------------------------------- Actions right after APP startup
Component.onCompleted: {
// read user settings
try {
userSettingsJSON = JSON.parse(userSettingsFile.read());
log(JSON.stringify(userSettingsJSON))
// When I look with an editor in the file it is handy to see the number of programDate 8=)
// It does not need to be there because programDate == scheduleTime.length and also programDate == scheduleProgram
mode = userSettingsJSON['mode'];
toonIP = userSettingsJSON['toonIP'];
currentLng = languages.indexOf(userSettingsJSON['language'])
lastSettingsMode = userSettingsJSON['lastSettingsMode']
programState = userSettingsJSON['programState'];
programDate = userSettingsJSON['programDate'];
programDays = userSettingsJSON['programDays'];
programTempComfort = userSettingsJSON['programTempComfort'];
programTempHome = userSettingsJSON['programTempHome'];
programTempSleep = userSettingsJSON['programTempSleep'];
programTempAway = userSettingsJSON['programTempAway'];
scheduleTime = userSettingsJSON['scheduleTime'];
scheduleProgram = userSettingsJSON['scheduleProgram'];
programDays = scheduleTime.length / 6 ;
} catch(e) {
log('Startup : '+e)
initUserSettings()
saveSettings()
}
setscheduleIndex()
correctprogramDate()
guiMode = 'Control'
// Master mode needs something to start with in case the 'Slave' is not reached during first getStatus call
if (mode == 'Master') { currentSetpointInt = 1500 }
getStatus("All")
wssServerSetup("startup")
}
// ---------------------------------------------------------------------
function initUserSettings() {
// first time of app start there is no data. This creates initial data.
mode = 'Standard'
toonIP = 'IP other Toon'
currentLng = 1
lastSettingsMode = 5
var today = new Date();
var dd = right( '0' + String(today.getDate()) , 2 )
var mm = right( '0' + String(today.getMonth() + 1) , 2 ) // January is 0!
var yyyy = today.getFullYear();
programState = 0;
programDate = yyyy + '-' + mm + '-' + dd;
programDays = 7;
programTempComfort = 20;
programTempHome = 18;
programTempSleep = 12;
programTempAway = 10;
scheduleTime = []
scheduleProgram = []
for (var i = 1; i <= programDays; i++) {
scheduleTime.push('00:00')
scheduleProgram.push(2)
scheduleTime.push('07:00')
scheduleProgram.push(1)
scheduleTime.push('08:00')
scheduleProgram.push(3)
scheduleTime.push('18:00')
scheduleProgram.push(1)
scheduleTime.push('20:00')
scheduleProgram.push(0)
scheduleTime.push('23:00')
scheduleProgram.push(2)
}
}
// ---------------------------------------------------------------------
function saveSettings(){
programDays = scheduleTime.length / 6 ;
if ( programDays > 0 ) {
var tmpUserSettingsJSON = {
"mode" : mode,
"language" : languages[currentLng],
"toonIP" : toonIP,
"lastSettingsMode" : lastSettingsMode,
"programState" : programState,
"programDate" : programDate,
"programDays" : programDays,
"programTempComfort" : programTempComfort,
"programTempHome" : programTempHome,
"programTempSleep" : programTempSleep,
"programTempAway" : programTempAway,
"scheduleTime" : scheduleTime,
"scheduleProgram" : scheduleProgram
}
var settings = new XMLHttpRequest();
settings.open("PUT", settingsFile);
settings.send(JSON.stringify(tmpUserSettingsJSON));
}
}
// ---------------------------------------------------------------------
function saveVariables(){
// write data to http://toon-ip/thermostatPlusBasicVariables.html
// you may use this in other platforms like Domoticz
var tmpVariablesJSON = {
"mode" : mode,
"language" : languages[currentLng],
"toonIP" : toonIP,
"lastSettingsMode" : lastSettingsMode,
"programState" : programState,
"programDate" : programDate,
"programDays" : programDays,
"programTempComfort" : programTempComfort,
"programTempHome" : programTempHome,
"programTempSleep" : programTempSleep,
"programTempAway" : programTempAway,
"scheduleTime" : scheduleTime,
"scheduleProgram" : scheduleProgram,
"programStateOverruleCounter" : programStateOverruleCounter,
"nextTime" : nextTime,
"nextDateTime" : nextDateTime.toString(),
"currentTemp" : currentTemp.replace(',','.'),
"currentTempInt" : currentTempInt,
"currentSetpointInt" : currentSetpointInt,
"scheduleSetpointInt" : scheduleSetpointInt,
"scheduleIndex" : scheduleIndex,
"activeState" : activeState,
"activeStateText" : activeStateText,
"currentSetpoint" : currentSetpoint.replace(',','.'),
"nextscheduleIndex" : nextscheduleIndex,
"nextState" : nextState,
"nextStateStr" : nextStateStr,
"nextSetpoint" : nextSetpoint.replace(',','.'),
"heatingOn" : burnerInfo
}
if ( JSON.stringify(oldVariables) != JSON.stringify(tmpVariablesJSON) ) {
oldVariables = {
"mode" : mode,
"language" : languages[currentLng],
"toonIP" : toonIP,
"lastSettingsMode" : lastSettingsMode,
"programState" : programState,
"programDate" : programDate,
"programDays" : programDays,
"programTempComfort" : programTempComfort,
"programTempHome" : programTempHome,
"programTempSleep" : programTempSleep,
"programTempAway" : programTempAway,
"scheduleTime" : scheduleTime,
"scheduleProgram" : scheduleProgram,
"programStateOverruleCounter" : programStateOverruleCounter,
"nextTime" : nextTime,
"nextDateTime" : nextDateTime.toString(),
"currentTemp" : currentTemp.replace(',','.'),
"currentTempInt" : currentTempInt,
"currentSetpointInt" : currentSetpointInt,
"scheduleSetpointInt" : scheduleSetpointInt,
"scheduleIndex" : scheduleIndex,
"activeState" : activeState,
"activeStateText" : activeStateText,
"currentSetpoint" : currentSetpoint.replace(',','.'),
"nextscheduleIndex" : nextscheduleIndex,
"nextState" : nextState,
"nextStateStr" : nextStateStr,
"nextSetpoint" : nextSetpoint.replace(',','.'),
"heatingOn" : burnerInfo
}
var now = new Date();
tmpVariablesJSON = {
"LastUpdate" : now.getFullYear() + '-' +
('00'+(now.getMonth() + 1) ).slice(-2) + '-' +
('00'+ now.getDate() ).slice(-2) + ' ' +
('00'+ now.getHours() ).slice(-2) + ":" +
('00'+ now.getMinutes() ).slice(-2) + ":" +
('00'+ now.getSeconds() ).slice(-2) + "." +
('000'+now.getMilliseconds() ).slice(-3),
"mode" : mode,
"language" : languages[currentLng],
"toonIP" : toonIP,
"lastSettingsMode" : lastSettingsMode,
"programState" : programState,
"programDate" : programDate,
"programDays" : programDays,
"programTempComfort" : programTempComfort,
"programTempHome" : programTempHome,
"programTempSleep" : programTempSleep,
"programTempAway" : programTempAway,
"scheduleTime" : scheduleTime,
"scheduleProgram" : scheduleProgram,
"programStateOverruleCounter" : programStateOverruleCounter,
"nextTime" : nextTime,
"nextDateTime" : nextDateTime.toString(),
"currentTemp" : currentTemp.replace(',','.'),
"currentTempInt" : currentTempInt,
"currentSetpointInt" : currentSetpointInt,
"scheduleSetpointInt" : scheduleSetpointInt,
"scheduleIndex" : scheduleIndex,
"activeState" : activeState,
"activeStateText" : activeStateText,
"currentSetpoint" : currentSetpoint.replace(',','.'),
"nextscheduleIndex" : nextscheduleIndex,
"nextState" : nextState,
"nextStateStr" : nextStateStr,
"nextSetpoint" : nextSetpoint.replace(',','.'),
"heatingOn" : burnerInfo
}
var settings = new XMLHttpRequest();
settings.open("PUT", variablesFile);
settings.send(JSON.stringify(tmpVariablesJSON));
}
}
// ---------------------------------------------------------------------
function log(tolog) {
var now = new Date();
var dateTime = now.getFullYear() + '-' +
('00'+(now.getMonth() + 1) ).slice(-2) + '-' +
('00'+ now.getDate() ).slice(-2) + ' ' +
('00'+ now.getHours() ).slice(-2) + ":" +
('00'+ now.getMinutes() ).slice(-2) + ":" +
('00'+ now.getSeconds() ).slice(-2) + "." +
('000'+now.getMilliseconds() ).slice(-3);
console.log(dateTime+' thermostatPlusBasic : ' + tolog.toString())
}
// ---------------------------------------------------------------------
function logall() {
log('----------------------------------------------------------')
log('logall mode : '+mode)
log('----')
log('logall programState : '+programState)
log('logall OverruleCounter : '+programStateOverruleCounter)
log('----')
log('logall programDate : '+programDate)
log('logall programDays : '+programDays)
log('logall nextTime : '+nextTime)
log('logall nextDateTime : '+nextDateTime)
log('logall currentTemp : '+currentTemp)
log('logall currentSetpointInt : '+currentSetpointInt)
log('logall scheduleSetpointInt : '+scheduleSetpointInt)
log('----')
log('logall scheduleIndex : '+scheduleIndex)
log('logall activeState : '+activeState)
log('logall activeStateText : '+activeStateText)
log('logall currentSetpoint : '+currentSetpoint)
log('----')
log('logall nextscheduleIndex : '+nextscheduleIndex)
log('logall nextState : '+nextState)
log('logall nextStateStr : '+nextStateStr)
log('logall nextSetpoint : '+nextSetpoint)
log('----')
log('logall scheduleTime : '+scheduleTime.length / 6)
log('logall scheduleProgram : '+scheduleProgram.length/ 6)
log('logall burnerInfo : '+burnerInfo)
}
// ---------------------------------------------------------------------
function correctprogramDate() {
// correct programDate to valid date for this schedule
// ( add/substract as many times the the amount of programDays as posible )
var dt1 = new Date(programDate + " 00:00:00");
// substract now - programDate and devide by 1000ms*60s*60m*24h to get number of days to add/substract
var Difference_In_Days = Math.floor( ( Date.now() - dt1 ) / 86400000 )
var daysToAdd = Math.floor(Difference_In_Days / programDays) * programDays
if ( daysToAdd != 0 ) {
log('programDate '+programDate+' daysToAdd : '+daysToAdd)
dt1.setDate(dt1.getDate() + daysToAdd )
programDate = dt1.getFullYear() + '-' + right( '0' + String(dt1.getMonth() + 1) , 2 ) + '-' + right( '0' + String(dt1.getDate()) , 2 ) ;
log('New programdate : '+programDate)
saveSettings()
}
}
// ---------------------------------------------------------------------
function setscheduleIndex() {
// scheduleIndex is the index in scheduleProgram and scheduleTime of current moment
// substract now - programDate and devide by 1000ms*60s*60m*24h to get number of days to add/substract
var Difference_In_Days = Math.floor( ( Date.now() - Date.parse(programDate +" 00:00:00") ) / 86400000 )
var index = Difference_In_Days * 6
if (index >= scheduleProgram.length) {
// we leave the current program table and need to shift the complete table to the new period
correctprogramDate()
index = 0
}
// move to right period in de schedule day
var now = new Date();
var scheduleHHMM = Number( scheduleTime[index].replace(':','') )
var nowHHMM = Number( ( "0" + now.getHours()).slice(-2) + ( "0" + now.getMinutes()).slice(-2) )
var periodCounter = 0
while ( ( scheduleHHMM <= nowHHMM ) && (periodCounter < 6 ) ) {
periodCounter = periodCounter + 1
index = index + 1
try {
scheduleHHMM = Number( scheduleTime[index].replace(':','') )
} catch(e) { // last schedule day, last period
scheduleHHMM = 2400
}
}
scheduleIndex = index - 1
}
// ---------------------------------------------------------------------
function runProgram() {
// Disable Own / Remote internal Toon program if required
switch(mode) {
case 'Standard': if (toonprogramState != 0) { setStatus("LocalProgramOnOff","off") } ; break
case 'Mirror' : programState = 0 ; break
case 'Master' : setStatus("RemoteProgramOnOff","off") ; break
case 'Local' : if (toonprogramState != 0) { setStatus("LocalProgramOnOff","off") }
setStatus("RemoteProgramOnOff","off") ; break
}
// app program actions
if (programState == 2 ) {
// my setpoint was changed by an other Toon more than 2 times in a row or by a push button. ( Master / Local / Mirror mode or interactive setting change )
// and program was running so I may want to try to reactivate my own program again because the schedule goes to a new period
if ( Date.now() >= nextDateTimeInt ) {
log('Try to reactivate own program by forcing programState = 1')
programState = 1 ; programStateOverruleCounter = 0
saveSettings()
}
}
setscheduleIndex()
getStatus("Timer")
if (programState == 1 ) {
if ( programStateOverruleCounter > 1 ) {
// my setpoint was changed by an other Toon during more than 2 checks in a row. ( Master / Local / Mirror mode )
programState = 2
} else {
if ( currentSetpointInt != scheduleSetpointInt) {
programStateOverruleCounter = programStateOverruleCounter + 1
// Standard sets this Toon and no further action follows
// Mirror does not get here because program is diabled above 8=), when enabled above it will set other Toon based on program state.
// Master sets other Toon based on program state, which sets new value for currentSetpointInt which is used below
// Local sets this Toon and burner info of this Toon is used below to control RemoteSetpoint
setStatus("Program", activeState )
getStatus("Timer")
} else {
programStateOverruleCounter = 0
}
}
}
// regular Timer actions not needed for Standard and Mirror because these follow buttons and Standard may follow program
// regular Timer is used to force state for Master and Local modes only.
if (mode == 'Master' ) {
setStatus("RemoteSetpoint",currentSetpointInt)
}
if (mode == 'Local' ) {
if (burnerInfo) { setStatus("RemoteSetpoint",3000) }
else { setStatus("RemoteSetpoint",600) }
}
}
// ---------------------------------------------------------------------
function getStatus(calledBy) {
// calledBy : Timer / Control / All
if (typeof xmlhttpGetStatus != "undefined") { xmlhttpGetStatus.abort() }
var hostaddress = 'localhost'
if ( (mode == 'Mirror') || (mode == 'Master') ) { hostaddress = toonIP }
if (calledBy == "All") {xmlhttpGetStatusAsyncMode = false}
xmlhttpGetStatus = new XMLHttpRequest();
xmlhttpGetStatus.open('GET', 'http://'+hostaddress+'/happ_thermstat?action=getThermostatInfo', xmlhttpGetStatusAsyncMode);
xmlhttpGetStatus.onreadystatechange = function() {
if (xmlhttpGetStatus.readyState == XMLHttpRequest.DONE) {
if (xmlhttpGetStatus.status === 200) {
var returnString = xmlhttpGetStatus.response
toonStatusData = JSON.parse(returnString);
toonprogramState = toonStatusData['programState']
// in Master mode we only want the remote setpoint when called by All ( exit settings and 5 program buttons)
// Find currentSetpointInt , needed to see if we need to switch to new schedule setting
if ( ( mode != 'Master') || (calledBy == 'All' ) ) {
currentSetpointInt = toonStatusData['currentSetpoint']
}
// Find currentSetpoint , text for display
currentSetpoint = String(Math.round(currentSetpointInt / 10 ))
currentSetpoint = currentSetpoint.slice(0,-1) + "," + currentSetpoint.slice(-1)
// Find burnerInfo, currentTempInt, currentTemp all for display
currentTempInt = toonStatusData['currentTemp']
currentTemp = String(Math.round(currentTempInt / 10 ))
currentTemp = currentTemp.slice(0,-1) + "," + currentTemp.slice(-1)
burnerInfo = toonStatusData['burnerInfo'] == 1
// Find / Calculate activeState for display
// activeState = 1 * toonStatusData['activeState'] <== komt uit programma van Toon zelf en dat staat uit 8=)
// vlak na schakelmoment en voor schakelen klopt dit niet want dan moet het de vorige state zijn....... is dit erg want we gaan toch meteen schakelen
if ( programState == 0 ) {
activeState = -1
} else {
activeState = scheduleProgram[scheduleIndex]
}
// Calculate wanted scheduleSetpointInt and activeStateText for program switching
if ( programState == 0 ) {
scheduleSetpointInt = 0
activeStateText = ""
} else {
switch(scheduleProgram[scheduleIndex]) {
case 0: scheduleSetpointInt = programTempComfort * 100 ; activeStateText = statesLng[currentLng][0] ; break
case 1: scheduleSetpointInt = programTempHome * 100 ; activeStateText = statesLng[currentLng][1] ; break
case 2: scheduleSetpointInt = programTempSleep * 100 ; activeStateText = statesLng[currentLng][2] ; break
case 3: scheduleSetpointInt = programTempAway * 100 ; activeStateText = statesLng[currentLng][3] ; break
}
}
// Calculate nextscheduleIndex which may be some days later when some days have all periods with same temperature
nextscheduleIndex = ( scheduleIndex + 1 ) % ( programDays * 6 )
var safetyCounter = 0
while ( ( safetyCounter < scheduleProgram.length ) &&
( scheduleProgram[scheduleIndex] == scheduleProgram[nextscheduleIndex] ) ) {
nextscheduleIndex = ( nextscheduleIndex + 1 ) % ( programDays * 6 )
safetyCounter = safetyCounter + 1
}
// Calculate nextState
if (programState == 0)
{ nextState = -1 }
else { nextState = scheduleProgram[ nextscheduleIndex] }
// Calculate nextSetpoint, nextStateStr and nextDateTime
nextStateStr = "...."
nextSetpoint = "...."
nextTime = "...."
nextDateTime = "...."
nextDateTimeInt = 0
if (nextState > -1) {
nextStateStr = statesLng[currentLng][nextState]
switch(nextState) {
case 0: nextSetpoint = programTempComfort ; break
case 1: nextSetpoint = programTempHome ; break
case 2: nextSetpoint = programTempSleep ; break
case 3: nextSetpoint = programTempAway ; break
}
nextTime = scheduleTime[ nextscheduleIndex ]
nextDateTime = new Date(programDate + " " + nextTime)
var nextscheduleIndexDay = Math.floor ( nextscheduleIndex / 6 )
if ( nextscheduleIndex > scheduleIndex ) {
nextDateTime = addDays(nextDateTime , nextscheduleIndexDay)
} else {
nextDateTime = addDays(nextDateTime , nextscheduleIndexDay + programDays)
}
nextDateTimeInt = Date.parse(nextDateTime)
}
xmlhttpGetStatusAsyncMode = false
} else {
xmlhttpGetStatusAsyncMode = true
currentTemp = "...."
// note that the value currentSetpoint = "......" blocks the setStatus function so no settings are sent anymore
currentSetpoint = "......"
// do not disable the program, this used to bee the Toon program. Now it is in this app
// programState = 0
activeState = -1
activeStateText = ""
nextState = -1
burnerInfo = false
log("getStatus got error " + hostaddress + ' ' +xmlhttpGetStatus.status)
}
}
}
xmlhttpGetStatus.send();
}
// ---------------------------------------------------------------------
function setStatus(what,value) {
// ProgramOnOff : on | of
// RemoteProgramOnOff : on | of
// Program : Comfort | Home | Sleep | Away
// Setpoint ( degrees Celcius ) : nn.n
// RemoteSetpoint ( degrees Celcius ) : nn.n
if (currentSetpoint == "......") {
log('setStatus can not '+what+' '+value)
} else {
if (typeof xmlhttpSetStatus != "undefined") { xmlhttpSetStatus.abort() }
var hostaddress = "localhost"
if ( ( mode == 'Mirror' ) || ( mode == 'Master' ) ) { hostaddress = toonIP }
var action = "http://"+hostaddress+"/happ_thermstat?action="
switch(what) {
case "LocalProgramOnOff":
action = "http://localhost/happ_thermstat?action="
if (value == "on") {
action = action + "changeSchemeState&state=1"
} else {
action = action + "changeSchemeState&state=0"
}
break;
case "RemoteProgramOnOff":
action = "http://"+toonIP+"/happ_thermstat?action="
if (value == "on") {
action = action + "changeSchemeState&state=1"
} else {
action = action + "changeSchemeState&state=0"
}
break;
case "Program":
switch(value) {
case 0: currentSetpointInt = Math.round(programTempComfort * 100) ; break;
case 1: currentSetpointInt = Math.round(programTempHome * 100) ; break;
case 2: currentSetpointInt = Math.round(programTempSleep * 100) ; break;
case 3: currentSetpointInt = Math.round(programTempAway * 100) ; break;
}
action = action + "setSetpoint&Setpoint="+currentSetpointInt
break;
case "ProgramButton":
switch(value) {
case "Comfort": currentSetpointInt = Math.round(programTempComfort * 100) ; break;
case "Home": currentSetpointInt = Math.round(programTempHome * 100) ; break;
case "Sleep": currentSetpointInt = Math.round(programTempSleep * 100) ; break;
case "Away": currentSetpointInt = Math.round(programTempAway * 100) ; break;
}
if (programState == 1) {programState = 2}
action = action + "setSetpoint&Setpoint="+currentSetpointInt
break;
case "Setpoint":
switch(value) {
case "up" : if (currentSetpointInt < 3000 ) currentSetpointInt = currentSetpointInt + 10 ; break
case "down" : if (currentSetpointInt > 600 ) currentSetpointInt = currentSetpointInt - 10 ; break
default : if (value < 600) { value = 600 }; if (value > 3000) { value = 3000 } ; currentSetpointInt = value; break
}
if (programState == 1) {programState = 2}
action = action + "setSetpoint&Setpoint="+currentSetpointInt
break;
case "RemoteSetpoint":
action = "http://"+toonIP+"/happ_thermstat?action="
if (value < 600) { value = 600 }; if (value > 3000) { value = 3000 } ; var switchTo = value ;
action = action + "setSetpoint&Setpoint="+value
break;
}
xmlhttpSetStatus = new XMLHttpRequest();
xmlhttpSetStatus.open("GET", action, false);
xmlhttpSetStatus.onreadystatechange = function() {
if (xmlhttpSetStatus.readyState == XMLHttpRequest.DONE) {
if (xmlhttpSetStatus.status === 200) {
var returnString = xmlhttpSetStatus.response
} else {
log("setStatus got error " + hostaddress + ' ' +xmlhttpSetStatus.status)
}
}
}
xmlhttpSetStatus.send();
}
}
// ------------------------------------------- some handy functions
function right(str, chr)
{
return str.slice(str.length-chr,str.length);
}
function left(str, chr)
{
return str.slice(0, chr - str.length);
}
function max(max1, max2)
{
if (max1 > max2) { var result = max1 } else { var result = max2 }
return result;
}
function min(min1, min2)
{
if (min1 < min2) { var result = min1 } else { var result = min2 }
return result;
}
function addDays(date, days) {
const copy = new Date(Number(date))
copy.setDate(date.getDate() + days)
return copy
}
// ---------------------------------------------------------------------
// ----- Voorbereiding voor ThermostatPlus door websockets te installeren
FileIO {
id: tscCommand
source: "file:///tmp/tsc.command"
}
function sendtscCommand() {
var done = tscCommand.write("external-thermostatPlusBasic")
}
FileIO {
id: thermostatPlusVersionFile
source: "file:///qmf/qml/apps/thermostatPlus/version.txt"
}
function thermostatPlusVersion() {
var version = thermostatPlusVersionFile.read();
return version
}
Timer {
id : updateTimer
interval : 1000 * ( 12 * 60 * 60 ) // interval 12 uur. Herhaling is nodig wanneer het niet lukt om vpn te openen
repeat : true
running : false
triggeredOnStart : true
onTriggered: { wssServerSetup("update") }
}
function wssServerSetup(action){
if (action == "startup") {
try {
const someModule = Qt.createQmlObject('import QtWebSockets 1.1
WebSocketServer {
id: server
host:"0.0.0.0" // listen on the network ("127.0.0.1") would be local only
port:8765 // on port 8765
listen:false // but do not run
onClientConnected: {
webSocket.onTextMessageReceived.connect(function(message) {
log(message)
webSocket.sendTextMessage(message); // act as echo server
});
}
onErrorStringChanged: {
log(qsTr("WSS Server error: %1").arg(errorString));
}
}
',parent)
if (thermostatPlusVersion() == "") {
sendNotification('Voorbereiding OK. ThermostatPlusBasic kun je vervangen door ThermostatPlus.')
} else {
sendNotification('Voorbereiding OK. Verwijder ThermostatPlusBasic en ThermostatPlus werkt.')
}
} catch (error) {
updateTimer.start()
}
} else {
sendNotification('Voorbereiding voor ThermostatPlus wordt gestart.')
log("Start QtWebSockets installation");
sendtscCommand()
}
}
}