-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSparkRemote.js
488 lines (429 loc) · 12.6 KB
/
SparkRemote.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
// Sparkostat script running on PngMagic http://www.curioustech.net/pngmagic.html
//
particleUrl = 'https://api.particle.io/v1/devices/'
ParticleHVAC = 'xxxxxxxxxxxxxxxxxxxxxxxx'
token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
kwh = 3600 // killowatt hours (compressor+fan)
ppkwh = 0.126 // price per KWH (price / KWH)
ppkwh += 0.091 + 0.03 // surcharge 9.1%, school 3%, misc
ccfs = 0.70 / (60*60) // NatGas cost per hour divided into seconds
modes = new Array('Off', 'Cool', 'Heat', 'Auto')
btnX = 120
btnY = 40
btnW = 38
if(Reg.overrideTemp == 0)
Reg.overrideTemp = -1.2
var hvacJson
pubset = false
streaming = false
cloud = false
online = true
Pm.Window('SparkRemote')
Gdi.Width = 208
Gdi.Height = 320
Pm.ParticleListen()
Pm.SetTimer(30*60*1000) // infrequent changing values are checked ever 30 mins
OnTimer()
// Handle published events
function OnCall(msg, event, data, d2)
{
switch(msg)
{
case 'HTTPSTATUS':
switch(+event)
{
case 400: s = 'Bad request'; break
case 408: s = 'Request timeout'; break
case 12002: s = 'Timeout'; break
case 12152: s = 'INVALID_SERVER_RESPONSE'; break
case 12157: s = 'Enable Internet Options-> Advanced -> SSL2/3'; break
default: s = ' '
}
Pm.Echo( ' ParticleRemote error: ' + event + ' ' + s)
break
case 'HTTPDATA':
if(data.length) procLine(event, data)
break
case 'HTTPCLOSE':
break
case 'StreamStatus': // event from ParticleListen
Pm.Echo('Stream ' + event)
streaming = +event
break
case 'CloudStatus': // event from ParticleListen
Pm.Echo('Cloud ' + event)
cloud = +event
break
case 'hvacData': // event from ParticleListen (interval published data)
hvacJson = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
event.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + event + ')')
running = +hvacJson.r
fan = +hvacJson.fr
inTemp = +hvacJson.it / 10
rh = +hvacJson.rh / 10
targetTemp = +hvacJson.tt / 10
filterMins = +hvacJson.fm
outTemp = +hvacJson.ot / 10
outFL = +hvacJson.ol
outFH = +hvacJson.oh
cycleTimer = +hvacJson.ct
fanTimer = +hvacJson.ft
runTotal = +hvacJson.rt
tempDiffTotal = +hvacJson.td
stat = 0
if(running)
stat = (mode == 3) ? autoMode : mode // if(auto) stat = heat/cool
if(stat == 2 && heatMode == 0) // if (heat & heatMode == NG)
stat = 3
date = new Date()
if((date.getMinutes() & 3) == 0)
LogTemps( stat, fan, inTemp, targetTemp, rh )
if(Pm.FindWindow( 'HVAC' ))
Pm.History( 'REFRESH' )
Draw()
break
case 'UPDATE': // event from HVAC when state changes
switch(+event)
{
case 0: running = 0; break
case 1: running = 1; break
case 2: running = 1; break
}
fan = +data
Draw()
OnTimer()
if(Pm.FindWindow( 'HVAC' )) // HVAC history window is open
Pm.History( 'REFRESH' )
break
case 'BUTTON':
switch(event)
{
case 0: // Override
ovrActive = !ovrActive
SetVar('override', ovrActive ? (Reg.overrideTemp * 10) : 0)
break
case 1: // reset filter
SetVar('resetfilter', 0)
filterMins = 0
break
case 2: // fan
fanMode ^= 1; SetVar('fanmode', fanMode)
break
case 3: // mode
mode = (mode + 1) & 3; SetVar('mode', mode)
break
case 4: // mode
heatMode = (heatMode+1) % 3; SetVar('heatMode', heatMode)
break
case 5: // blank
break
case 6: // cool H up
setTemp(1, coolTempH + 0.1, 1); SetVar('cooltemph', (coolTempH * 10).toFixed())
break
case 7: // cool H dn
setTemp(1, coolTempH - 0.1, 1); SetVar('cooltemph', (coolTempH * 10).toFixed())
break
case 8: // cool L up
setTemp(1, coolTempL + 0.1, 0); SetVar('cooltempl', (coolTempL * 10).toFixed())
break
case 9: // cool L dn
setTemp(1, coolTempL - 0.1, 0); SetVar('cooltempl', (coolTempL * 10).toFixed())
break
case 10: // heat H up
setTemp(2, heatTempH + 0.2, 1); SetVar('heattemph', (heatTempH * 10).toFixed())
break
case 11: // heat H dn
setTemp(2, heatTempH - 0.2, 1); SetVar('heattemph', (heatTempH * 10).toFixed())
break
case 12: // heat L up
setTemp(2, heatTempL + 0.2, 0); SetVar('heattempl', (heatTempL * 10).toFixed())
break
case 13: // heat L dn
setTemp(2, heatTempL - 0.2, 0); SetVar('heattempl', (heatTempL * 10).toFixed())
break
case 14: // thresh up
if(cycleThresh < 6.3){ cycleThresh += 0.1; SetVar('cyclethresh', (cycleThresh * 10).toFixed()); }
break
case 15: // thresh dn
if(cycleThresh > 0.1){ cycleThresh -= 0.1; SetVar('cyclethresh', (cycleThresh * 10).toFixed()); }
break
case 16: // fanDelay up
if(fanDelay < 255){ fanDelay += 10; SetVar('fanpostdelay', fanDelay); }
break
case 17: // fanDelay dn
if(fanDelay > 0){ fanDelay -= 10; SetVar('fanpostdelay', fanDelay); }
break
case 18: // idleMin up
idleMin++; SetVar('idlemin', idleMin)
break
case 19: // idleMin dn
idleMin--; SetVar('idlemin', idleMin)
break
case 20: // cycleMin up
cycleMin++; SetVar('cyclemin', cycleMin)
break
case 21: // cycleMin dn
cycleMin--; SetVar('cyclemin', cycleMin)
break
case 22: // cycleMax up
cycleMax+=60; SetVar('cyclemax', cycleMax)
break
case 23: // cycleMax dn
cycleMax--; SetVar('cyclemax', cycleMax)
break
case 24: // override time up
overrideTime+=60; SetVar('overridetime', overrideTime)
break
case 25: // override time dn
overrideTime-=10; SetVar('overridetime', overrideTime)
break
case 26: // override temp up
Reg.overrideTemp += 0.1
break
case 27: // override temp dn
Reg.overrideTemp -= 0.1
break
}
Draw()
break
default:
Pm.Echo('SR Unrecognised ' + msg)
break
}
}
function OnTimer()
{
GetSettings()
}
function SetVar(v, val)
{
Http.Connect( 'setvar', particleUrl + ParticleHVAC + '/setvar?access_token=' + token, 'POST', 'params=' + v + ',' + val )
}
function GetSettings()
{
Http.Connect('settings', particleUrl + ParticleHVAC + '/settings?access_token=' + token )
}
function procLine(event, data)
{
Json = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
data.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + data + ')')
switch(event)
{
case 'settings':
str = Json.result
Json = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
str.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + str + ')')
mode = +Json.m
autoMode = +Json.am
heatMode = +Json.hm
fanMode = +Json.fm
ovrActive = +Json.ot
eHeatThresh = +Json.ht
coolTempL = +Json.c0 / 10
coolTempH = +Json.c1 / 10
heatTempL = +Json.h0 / 10
heatTempH = +Json.h1 / 10
idleMin = +Json.im
cycleMin = +Json.cn
cycleMax = +Json.cx
cycleThresh = +Json.ct / 10
Reg.cycleThresh = cycleThresh
fanDelay = Json.fd
overrideTime = +Json.ov
remoteTimer = Json.rm
remoteTimeout = Json.ro
if(!pubset)
{
SetVar('pubtime', 60) // set to publish values every 60 seconds
pubset = true
}
Draw()
if(!streaming)
{
Pm.ParticleListen('START') // start the event listener
}
break
case 'setvar':
break
default:
Pm.Echo('SR Unknown event: ' + event)
break
}
}
// mimic thermostat
function setTemp( mode, Temp, hl)
{
if(mode == 3) // auto
{
mode = autoMode
}
switch(mode)
{
case 1:
if(Temp < 65.0 || Temp > 88.0) // ensure sane values
break
if(hl)
{
coolTempH = Temp
coolTempL = Math.min(coolTempH, coolTempL) // don't allow h/l to invert
}
else
{
coolTempL = Temp
coolTempH = Math.max(coolTempL, coolTempH)
}
save = heatTempH - heatTempL
heatTempH = Math.min(coolTempL - 2, heatTempH) // Keep 2.0 degree differencial for Auto mode
heatTempL = heatTempH - save // shift heat low by original diff
break
case 2:
if(Temp < 63.0 || Temp > 86.0) // ensure sane values
break
if(hl)
{
heatTempH = Temp
heatTempL = Math.min(heatTempH, heatTempL)
}
else
{
heatTempL = Temp;
heatTempH = Math.max(heatTempL, heatTempH);
}
save = coolTempH - coolTempL;
coolTempL = Math.max(heatTempH - 2, coolTempL);
coolTempH = coolTempL + save;
break
}
}
function Draw()
{
Gdi.Clear(0) // transaprent
// rounded window
Gdi.Brush( Gdi.Argb( 160, 0, 0, 0) )
Gdi.FillRectangle(0, 0, Gdi.Width-1, Gdi.Height-1)
Gdi.Pen( Gdi.Argb(255, 0, 0, 255), 1 )
Gdi.Rectangle(0, 0, Gdi.Width-1, Gdi.Height-1)
// Title
Gdi.Font( 'Courier New', 15, 'BoldItalic')
Gdi.Brush( Gdi.Argb(255, 255, 230, 25) )
Gdi.Text( 'Spark-O-Stat', 5, 1 )
Gdi.Brush( online ? Gdi.Argb(255, 25, 25, 255) : Gdi.Argb(255, 255, 0, 0) )
Gdi.Text( 'X', Gdi.Width-17, 1 )
Gdi.Font( 'Arial' , 11, 'Regular')
Gdi.Brush( Gdi.Argb(255, 255, 255, 255) )
date = new Date()
Gdi.Text( date.toLocaleTimeString(), Gdi.Width-84, 2 )
Gdi.Font( 'Arial' , 13, 'Regular')
x = 5
y = 22
if(hvacJson == undefined)
return
Gdi.Text('In: ' + inTemp + '°', x, y)
Gdi.Text( '>' + targetTemp + '° ' + rh + '%', x + 54, y)
Gdi.Text('O:' + outTemp + '°', x + 150, y)
y = btnY
Gdi.Text('Fan:', x, y); Gdi.Text(fan ? "On" : "Off", x + 100, y, 'Right')
y += 20
s = 'huh'
switch(mode)
{
case 1: s = 'Cooling'; break
case 2: s = 'Heating'; break
case 3: s = 'eHeating'; break
}
bh = 18
Gdi.Text('Run:', x, y)
Gdi.Text(running ? s : "Off", x + 100, y, 'Right')
y += bh
Gdi.Text('Cool Hi:', x, y); Gdi.Text(coolTempH.toFixed(1) + '°', x + 112, y, 'Right')
y += bh
Gdi.Text('Cool Lo:', x, y); Gdi.Text(coolTempL.toFixed(1) + '°', x + 112, y, 'Right')
y += bh
Gdi.Text('Heat Hi:', x, y); Gdi.Text(heatTempH.toFixed(1) + '°', x + 112, y, 'Right')
y += bh
Gdi.Text('Heat Lo:', x, y); Gdi.Text(heatTempL.toFixed(1) + '°', x + 112, y, 'Right')
y += bh
Gdi.Text('Threshold:', x, y); Gdi.Text(cycleThresh.toFixed(1) + '°', x + 112, y, 'Right')
y += bh
Gdi.Text('Fan Delay:', x, y); Gdi.Text(fanDelay , x + 112, y, 'Time')
y += bh
Gdi.Text('Idle Min:', x, y); Gdi.Text(idleMin , x + 112, y, 'Time')
y += bh
Gdi.Text('cycle Min:', x, y); Gdi.Text(cycleMin, x + 112, y, 'Time')
y += bh
Gdi.Text('cycle Max:', x, y); Gdi.Text(cycleMax , x + 112, y, 'Time')
y += bh
Gdi.Text('ovr Time:', x, y); Gdi.Text(overrideTime , x + 112, y, 'Time')
y += bh
a = Reg.overrideTemp
Gdi.Text('Override:', x, y); Gdi.Text(a + '°' , x + 112, y, 'Right')
if(ovrActive)
Gdi.Pen(Gdi.Argb(255,255,20,20), 2 ) // Button square
else
Gdi.Pen(Gdi.Argb(255,20,20,255), 2 ) // Button square
Gdi.Rectangle(x, y, 64, 15, 2)
Pm.Button(x, y, 64, 15)
y = Gdi.Height - 36
if(mode == 1 || (mode==2 && heatMode == 0)) // cool or HP
cost = ppkwh * runTotal / (1000*60*60) * kwh
else
cost = ccfs * runTotal
Gdi.Text('Filter:', x, y); Gdi.Text(filterMins*60, x + 100, y, 'Time')
Gdi.Pen(Gdi.Argb(255,20,20,255), 2 ) // Button square
Pm.Button(x, y, 100, 15)
Gdi.Rectangle(x, y, 100, 15, 2)
Gdi.Text('Cost:', x+110, y); Gdi.Text( '$' +cost.toFixed(2) , x + 190, y, 'Right')
y += bh
Gdi.Text('Cyc:', x, y); Gdi.Text( cycleTimer, x + 100, y, 'Time')
Gdi.Text('Tot:', x+110, y); Gdi.Text(runTotal, x + 190, y, 'Time')
heatModes = Array('HP', 'NG', 'Auto')
buttons = Array(fanMode ? 'On' : 'Auto', modes[mode],
heatModes[heatMode], ' ',
'+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-' )
for (n = 0, row = 0; row < buttons.length / 2; row++)
{
for (col = 0; col < 2; col++)
{
x = btnX + (col * btnW)
y = btnY + (row * bh)
drawButton(buttons[n++], x, y, btnW, bh-2)
}
}
}
function drawButton(text, x, y, w, h)
{
Gdi.GradientBrush( 0,y, 22, 24, Gdi.Argb(200, 200, 200, 255), Gdi.Argb(200, 60, 60, 255 ), 90)
Gdi.FillRectangle( x, y, w-2, h, 3)
ShadowText( text, x+(w/2), y, Gdi.Argb(255, 255, 255, 255) )
Pm.Button(x, y, w, h)
}
function ShadowText(str, x, y, clr)
{
Gdi.Brush( Gdi.Argb(255, 0, 0, 0) )
Gdi.Text( str, x+1, y+1, 'Center')
Gdi.Brush( clr )
Gdi.Text( str, x, y, 'Center')
}
function LogTemps( stat, fan, inTemp, targetTemp, inrh )
{
if(targetTemp == 0)
return
fso = new ActiveXObject( 'Scripting.FileSystemObject' )
date = new Date()
tf = fso.OpenTextFile( 'stattemp.log', 8, true)
tf.WriteLine( Math.floor( date.getTime() / 1000 ) + ',' + stat + ',' + fan + ',' + inTemp + ',' + targetTemp +',' + inrh )
tf.Close()
fso = null
}
function isoDateToJsDate(value)
{
var a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)(?:([\+-])(\d{2})\:(\d{2}))?Z?$/.exec(value)
if (a)
{
var utcMilliseconds = Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6])
if( a[7] == '-' ) utcMilliseconds += a[8] * (1000 * 60 * 60)
else utcMilliseconds -= a[8] * (1000 * 60 * 60)
return new Date(utcMilliseconds)
}
return value
}