-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
81 lines (66 loc) · 1.72 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
74
75
76
77
78
79
80
81
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/simpleiot/simpleiot/data"
"github.com/simpleiot/simpleiot/sim"
)
func packetDelay() {
time.Sleep(5 * time.Second)
}
func newSendSample(portalURL string) func(string, data.Sample) error {
return func(id string, sample data.Sample) error {
sampleURL := portalURL + "/v1/devices/" + id + "/sample"
tempJSON, err := json.Marshal(sample)
if err != nil {
log.Println("Error encoding temp: ", err)
}
resp, err := http.Post(sampleURL, "application/json", bytes.NewBuffer(tempJSON))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
errstring := "Server error: " + resp.Status + " " + sampleURL
body, _ := ioutil.ReadAll(resp.Body)
errstring += " " + string(body)
return errors.New(errstring)
}
return nil
}
}
func main() {
flagPortal := flag.String("portal", "http://localhost:8080", "Portal URL")
flagDeviceID := flag.String("deviceId", "1234", "Device ID")
flag.Parse()
if *flagPortal == "" {
fmt.Println("Error: portal url must be set")
flag.PrintDefaults()
os.Exit(-1)
}
log.Printf("ID: %v, portal: %v\n", *flagDeviceID, *flagPortal)
sendSample := newSendSample(*flagPortal)
tempSim := sim.NewSim(72, 0.2, 70, 75)
voltSim := sim.NewSim(2, 0.1, 1, 5)
for {
tempSample := data.NewSample("T0", "12", tempSim.Sim())
err := sendSample(*flagDeviceID, tempSample)
if err != nil {
log.Println("Error sending sample: ", err)
}
voltSample := data.NewSample("V0", "12", voltSim.Sim())
err = sendSample(*flagDeviceID, voltSample)
if err != nil {
log.Println("Error sending sample: ", err)
}
packetDelay()
}
}