-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.nut
More file actions
34 lines (31 loc) · 1.11 KB
/
agent.nut
File metadata and controls
34 lines (31 loc) · 1.11 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
// AGENT CODE ********************************************
nextBusAPIURL <- "http://webservices.nextbus.com/service/publicXMLFeed?command=predictions&a=sf-muni&r=%s&s=%s"
busLine <- "38"
busStop <- "6425"
function getMinutesUntilArrival() {
local formattedURL = format(nextBusAPIURL, busLine, busStop);
local nextBusResponse = http.get(formattedURL).sendsync();
local listOfMinutesAway = array();
local prevIndex = 0;
local index = 0;
local minutesAway = null;
while (true) {
index = nextBusResponse.body.find("minutes=", prevIndex)
if (index) {
minutesAway = nextBusResponse.body.slice(index + 9, index + 11)
if (minutesAway[1] == "\"") {
minutesAway = minutesAway.slice(0, 1)
}
minutesAway = minutesAway.tointeger()
} else {
break
}
server.log(minutesAway)
listOfMinutesAway.push(minutesAway)
prevIndex = index + 11
}
// to device: light selected LEDs
device.send("yo", listOfMinutesAway);
imp.wakeup(20, getMinutesUntilArrival);
}
getMinutesUntilArrival();