-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
73 lines (57 loc) · 1.56 KB
/
main.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
67
68
69
70
71
72
73
package main
import (
"flag"
"fmt"
"net/http"
"os"
"runtime"
"time"
)
// define a flag -r to remove all devices
var removeDevices = flag.Bool("r", false, "Remove devices from IoT Hub")
// http client
var client *http.Client
func main() {
// logical processors for goroutines
runtime.GOMAXPROCS(runtime.NumCPU())
// create a channel to allow goroutines to communicate
// in this case goroutines will communicate with main() which is also a goroutine
ch := make(chan string)
// http client
client = &http.Client{
Timeout: time.Second * 10,
}
// parse flags
flag.Parse()
// read configuration into conf struct
config, err := getConf("config.json")
// store config in global Conf variable
Conf = config
if err != nil {
// could be due to file not found or some JSON parsing error
fmt.Println("Error occurred: ", err)
os.Exit(1)
}
// get device list (returns pointer) created from the configuration
devList := getDeviceList()
if *removeDevices {
for _, device := range *devList {
deleteResp, err := device.deleteDevice()
if err != nil {
fmt.Printf("Could not delete device %s. Error: %v\n", device.Name, err)
} else {
fmt.Printf("Deleted device %s with reponse code %d\n", device.Name, deleteResp.StatusCode)
}
}
os.Exit(0)
}
for _, device := range *devList {
// for each device, run the deviceSend method as a goroutine
go device.deviceSend(Conf.Interval, ch)
}
// not sure this is good practice but we keep reading from
// the channel forever to pick up messages from the goroutines
for {
fmt.Println(<-ch)
}
}