-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
43 lines (32 loc) · 923 Bytes
/
utils.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
package main
import (
"fmt"
"cloud.google.com/go/firestore"
"golang.org/x/net/context"
firebase "firebase.google.com/go"
"google.golang.org/api/option"
)
type Dht11 struct {
Time string
Distance float32 `firebase:"temperature,omitempty"`
Message string `firebase:"humidity,omotempty"`
}
func InitClient(ctx context.Context) (*firestore.Client, error) {
sa := option.WithCredentialsFile("./dht11-data-d25a6-serviceAccountKey.json")
app, err := firebase.NewApp(ctx, nil, sa)
if err != nil {
return nil, err
}
client, err := app.Firestore(ctx)
if err != nil {
return nil, err
}
return client, nil
}
func FirebaseSend(ctx context.Context, client *firestore.Client, data Dht11, collection string) {
_, _, err := client.Collection(collection).Add(ctx, data)
if err != nil {
// log.Fatalf("Failed adding alovelace: %v", err)
fmt.Println("Failed to start firebase link, err: ", err)
}
}