/
read-sensors.go
57 lines (42 loc) · 1.19 KB
/
read-sensors.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
// Copyright 2018, Andrew C. Young
// License: MIT
package main
import (
"io/ioutil"
"log"
"os"
"github.com/vaelen/iot"
"github.com/vaelen/iot/examples"
"gopkg.in/yaml.v2"
)
func handleError(description string, err error) {
if err != nil {
log.Fatalf("%s: %v\n", description, err)
}
}
// Config contains the configuration options for a sensor reader
type Config struct {
ID iot.ID
Certificate string
PrivateKey string
Server string
}
func main() {
configFile := "config.yaml"
if len(os.Args) > 1 {
configFile = os.Args[1]
}
configBytes, err := ioutil.ReadFile(configFile)
handleError("Couldn't read config", err)
config := &Config{}
err = yaml.Unmarshal(configBytes, config)
handleError("Couldn't parse config", err)
log.Printf("Config: %+v", config)
credentials, err := iot.LoadRSACredentials(config.Certificate, config.PrivateKey)
handleError("Couldn't load credentials", err)
queueDirectory, err := ioutil.TempDir("", "iot-queue-")
handleError("Couldn't create queue directory", err)
sr, err := examples.NewSensorReader(&config.ID, credentials, queueDirectory, log.Println, config.Server)
handleError("Couldn't start sensor reader", err)
sr.Wait()
}