-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParticleListen.js
More file actions
169 lines (153 loc) · 3.99 KB
/
Copy pathParticleListen.js
File metadata and controls
169 lines (153 loc) · 3.99 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
// Particle stream listener for SparkRemote
spark = 'https://api.particle.io/v1/devices/'
ParticleHVAC = 'xxxxxxxxxxxxxxxxxxxxxxxx'
token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
// This reads all particle devices owned
eventUrl = spark + 'events?access_token=' + token
// Handle published events
function OnCall(msg, event, data)
{
switch(msg)
{
case 'START':
if(Http.Connected)
{
Pm.SparkRemote( 'StreamStatus', 1)
Pm.SparkRemote( 'CloudStatus', 1) // assume it's connected
break
}
if( Http.Connect( 'listen',eventUrl ) ) // Start the event stream
Pm.SparkRemote( 'StreamStatus', 1)
else
Pm.Echo('Stream failed')
Pm.SetTimer(20*1000) // recheck every 20 seconds
break
case 'HTTPDATA':
heartbeat = new Date()
if(data.length <= 2) break // keep-alive heartbeat
lines = data.split('\n')
for(i = 0; i < lines.length; i++)
procLine(lines[i])
break
case 'HTTPCLOSE':
Pm.SparkRemote( 'StreamStatus', 0)
break
case 'HTTPSTATUS':
break
default:
Pm.Echo('PL Unrecognised ' + msg)
Pm.Echo(event + ' : ' + data)
break
}
}
function OnTimer()
{
if(Http.Connected)
{
Pm.Echo('Connected')
return
}
if( Http.Connect( eventUrl ) ) // Start the event stream
Pm.SparkRemote( 'StreamStatus', 1)
else
Pm.Echo('Stream failed')
}
function procLine(data)
{
if(data.length == 0) return
// Pm.Echo('Data: '+ data)
if(data == ':ok' )
{
Pm.Echo( ' Particle stream started')
return
}
if( data.indexOf( 'event' ) >= 0 )
{
event = data.substring( data.indexOf(':') + 2)
return
}
if( !data.indexOf('}') ) // no event or bad format?
{
Pm.Echo('Particle: Unexpected data: ' + data)
return
}
data = data.substr( data.indexOf('{') ) // remove the leading "data"
Json = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
data.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + data + ')')
switch(event)
{
case 'spark/status':
if(Json.coreid == ParticleHVAC)
{
online = (Json.data == 'online') ? true:false
Pm.SparkRemote( 'CloudStatus', online)
}
else
{
Pm.Echo('Core ' + Json.coreid + ' : ' + Json.data)
}
break
case 'spark/flash/status':
Pm.Echo('Flash Status ' + Json.data) // flash status
break
case 'spark/device/app-hash':
Pm.Echo('App-hash ' + Json.data) // flash status
break
case 'spark/cc3000-patch-version': // this posts every time the device comes online
cc3000version = Json.data
break
case 'stateChg': // Mine. mode/state/fan change
if(Json.coreid == ParticleHVAC)
{
// Pm.Echo('HVAC stateChg')
}
d = isoDateToJsDate( Json.published_at )
str = Json.data
Json = !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
str.replace(/"(\\.|[^"\\])*"/g, ''))) && eval('(' + str + ')')
Pm.Echo('Mode = ' + Json.Mode + ' State = ' + Json.State + ' Fan = ' + Json.Fan)
LogHVAC( Math.floor( d.getTime() / 1000), Json.State, Json.Fan )
Pm.SparkRemote('UPDATE', Json.State, Json.Fan)
OnTimer() // read the rest of the data
break
case 'hvacData':
Pm.SparkRemote('hvacData', Json.data)
break
case 'hvacLog':
Pm.Echo('hvacLog', Json.data)
break
case 'status':
Pm.Echo('PL Status: ' + Json.data)
break
case 'pushbullet':
Pm.Echo('PL Pushbullet : ' + Json.data)
break
case 'hook-sent/pushbullet':
break
case 'hook-response/pushbullet/0':
break
default:
Pm.Echo('PL Unknown event: ' + event)
break
}
}
function LogHVAC( uxt, state, fan )
{
fso = new ActiveXObject( 'Scripting.FileSystemObject' )
tf = fso.OpenTextFile( 'hvacdata.log', 8, true)
tf.WriteLine( uxt + ',' + state + ',' + fan )
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
}