-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (136 loc) · 3.24 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
pb "arduino/schema"
"bufio"
"context"
"crypto/tls"
"crypto/x509"
"errors"
"flag"
"fmt"
"github.com/tarm/serial"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/protobuf/types/known/timestamppb"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"time"
)
func readFromSerial(s *serial.Port, id int) (float32, error) {
_, err := s.Write([]byte(strconv.Itoa(id)))
if err != nil {
return 0, err
}
reader := bufio.NewReader(s)
result, err := reader.ReadBytes('\x00')
if err != nil {
return 0, err
}
res := strings.TrimSpace(string(result))
res = strings.TrimSuffix(res, "\x00")
nr, err := strconv.ParseFloat(res, 32)
if err != nil {
return 0, err
}
return float32(nr), nil
}
func Upload(dataType int, value float32, client pb.RequestClient) (*pb.Reply, error) {
return client.Add(context.Background(), &pb.Data{
DataType: pb.DataType(dataType),
Value: value,
Timestamp: timestamppb.New(time.Now()),
})
}
func LoadTLS() (credentials.TransportCredentials, error) {
serverCa, err := ioutil.ReadFile("../cert/ca-cert/ca-cert.pem")
if err != nil {
return nil, err
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(serverCa) {
return nil, errors.New("failed to add server CA certificate")
}
cert, err := tls.LoadX509KeyPair("../cert/client-cert/client-cert.pem", "../cert/client-cert/client-key.pem")
if err != nil {
return nil, err
}
config := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: certPool,
}
return credentials.NewTLS(config), nil
}
func main() {
readPtr := flag.Int("read", 0, "Read sensor value/values\n\t0 = all (default)\n\t1 = carbon monoxide\n\t2 = air quality\n\t3 = raindrops\n\t4 = soil moisture")
uploadPtr := flag.Bool("upload", false, "Upload read data to the API")
flag.Parse()
if *readPtr < 0 || *readPtr > 4 {
log.Fatal("Invalid value for -read argument, exiting")
}
c := &serial.Config{Name: "/dev/ttyACM0", Baud: 9600, ReadTimeout: time.Second * 5}
s, err := serial.OpenPort(c)
if err != nil {
log.Fatal(err)
}
defer func(s *serial.Port) {
if err := s.Close(); err != nil {
log.Println(err)
}
}(s)
tlsCred, err := LoadTLS()
if err != nil && *uploadPtr {
log.Fatal(err)
}
conn, err := grpc.Dial("127.0.0.1:9000", grpc.WithTransportCredentials(tlsCred))
if err != nil {
log.Println(err)
if *uploadPtr {
os.Exit(1)
}
}
defer func(conn *grpc.ClientConn) {
if err != nil {
return
}
if err := conn.Close(); err != nil {
log.Println(err)
}
}(conn)
client := pb.NewRequestClient(conn)
_, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if *readPtr == 0 {
for i := 1; i <= 4; i++ {
data, err := readFromSerial(s, i)
fmt.Println("Argument:", i)
if err != nil {
log.Println(err)
} else {
fmt.Println("Result:", data)
if *uploadPtr {
_, err := Upload(i, data, client)
if err != nil {
log.Println(err)
}
}
}
time.Sleep(time.Second)
}
return
}
data, err := readFromSerial(s, *readPtr)
if err != nil {
log.Println(err)
} else {
fmt.Println("Result:", data)
if *uploadPtr {
_, err := Upload(*readPtr, data, client)
if err != nil {
log.Println(err)
}
}
}
}