-
Notifications
You must be signed in to change notification settings - Fork 0
/
polling.go
66 lines (58 loc) · 1.45 KB
/
polling.go
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/3cb/ssc"
"github.com/boltdb/bolt"
)
func poll(db *bolt.DB, pool *ssc.Pool) {
ticker := time.NewTicker(time.Minute * 1)
loc, _ := time.LoadLocation("Australia/Melbourne")
spots, err := updateSpots()
if err != nil {
log.Printf("Unable to update spots data: %v", err)
}
t := strings.Split(fmt.Sprint(time.Now().In(loc)), " ")
buf := serialize(spots, fmt.Sprintf("%v %v", t[0], t[1]))
err = updateDB(db, buf)
if err != nil {
log.Printf("Unable to store spots data in database: %v", err)
}
pool.Inbound <- &ssc.Message{Type: 2, Payload: buf}
for {
<-ticker.C
spots, err := updateSpots()
if err != nil {
log.Printf("Unable to update spots data: %v", err)
}
t := strings.Split(fmt.Sprint(time.Now().In(loc)), " ")
buf := serialize(spots, fmt.Sprintf("%v %v", t[0], t[1]))
err = updateDB(db, buf)
if err != nil {
log.Printf("Unable to store spots data in database: %v", err)
}
pool.Inbound <- &ssc.Message{Type: 2, Payload: buf}
}
}
func updateSpots() ([]Spot, error) {
spots := []Spot{}
resp, err := http.Get("https://data.melbourne.vic.gov.au/resource/vh2v-4nfs.json?$limit=5000")
if err != nil {
return spots, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return spots, err
}
err = json.Unmarshal(data, &spots)
if err != nil {
return spots, err
}
return spots, nil
}