forked from CuriousTech/ESP-HVAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RMT.js
101 lines (93 loc) · 1.87 KB
/
RMT.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
Url = 'http://192.168.0.103:86/events'
if(!Http.Connected)
Http.Connect( 'event', Url ) // Start the event stream
var last
mute = false
Pm.SetTimer(10*1000)
heartbeat = 0
// Handle published events
function OnCall(msg, event, data)
{
switch(msg)
{
case 'HTTPDATA':
heartbeat = new Date()
if(data.length <= 2) break // keep-alive heartbeat
mute = false
lines = data.split('\n')
for(i = 0; i < lines.length; i++)
procLine(lines[i])
break
case 'HTTPSTATUS':
Pm.Echo('RMT Status ' + event)
break
case 'HTTPCLOSE':
Pm.Echo('RMT stream retry')
// Http.Connect( 'event', Url ) // Start the event stream
break
}
}
function procLine(data)
{
if(data.length < 2) return
data = data.replace(/\n|\r/g, "")
if( data.indexOf( 'event' ) >= 0 )
{
event = data.substring( data.indexOf(':') + 2)
return
}
else if( data.indexOf( 'data' ) >= 0 )
{
data = data.substring( data.indexOf(':') + 2)
}
else
{
return // headers
}
switch(event)
{
case 'state':
LogRemote(data)
break
case 'print':
Pm.Echo( 'RMT Print: ' + data)
break
case 'alert':
Pm.Echo( 'RMT Alert: ' + data)
Pm.Beep(0)
break
}
event = ''
}
function OnTimer()
{
time = (new Date()).valueOf()
if(time - heartbeat > 120*1000)
{
if(!Http.Connected)
{
if(!mute)
{
mute = true
Pm.Echo('RMT timeout')
}
// Http.Connect( 'event', Url ) // Start the event stream
}
}
}
function LogRemote(str)
{
rmtJson = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
str.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + str + ')')
line = rmtJson.tempi + ',' + rmtJson.rhi
//Pm.Echo(str)
if(line == last || +rmtJson.tempi == -1)
return
last = line
// Pm.Echo( new Date(rmtJson.t * 1000) )
fso = new ActiveXObject( 'Scripting.FileSystemObject' )
tf = fso.OpenTextFile( 'Remote.log', 8, true)
tf.WriteLine( rmtJson.t + ',' + line )
tf.Close()
fso = null
}