-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
126 lines (107 loc) · 2.81 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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/mackerelio/mackerel-client-go"
)
var (
url = "https://api.nature.global/1/devices"
token = os.Getenv("REMOTOKEN")
mkrKey = os.Getenv("MKRKEY")
client = mackerel.NewClient(mkrKey)
)
const (
serviceName = "NatureRemo"
timezone = "Asia/Tokyo"
offset = 9 * 60 * 60
)
type Device struct {
ID string `json:"id"`
Name string `json:"name"`
TemperatureOffset int32 `json:"temperature_offset"`
HumidityOffset int32 `json:"humidity_offset"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
FirmwareVersion string `json:"firmware_version"`
NewestEvents NewestEvents `json:"newest_events"`
}
type NewestEvents struct {
Temperature SensorValue `json:"te"`
Humidity SensorValue `json:"hu"`
Illuminance SensorValue `json:"il"`
}
type SensorValue struct {
Value float64 `json:"val"`
CreatedAt string `json:"created_at"`
}
func (d *Device) FetchValesFromNatureRemo() (float64, float64, float64) {
var devices []*Device
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error")
}
req.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error request")
}
err = json.NewDecoder(resp.Body).Decode(&devices)
if err != nil {
fmt.Println("Error decode")
}
return devices[0].NewestEvents.Temperature.Value,
devices[0].NewestEvents.Humidity.Value,
devices[0].NewestEvents.Illuminance.Value
}
func PostValuesToMackerel(tem float64, hum float64, ill float64, nowTime time.Time) {
fmt.Println(nowTime)
// Post Temperature
err_tem := client.PostServiceMetricValues(serviceName, []*mackerel.MetricValue{
&mackerel.MetricValue{
Name: "Temperature.temperature",
Time: nowTime.Unix(),
Value: tem,
},
})
if err_tem != nil {
fmt.Println("Error post tem")
}
// Post Humidity
err_hum := client.PostServiceMetricValues(serviceName, []*mackerel.MetricValue{
&mackerel.MetricValue{
Name: "Humidity.humidity",
Time: nowTime.Unix(),
Value: hum,
},
})
if err_hum != nil {
fmt.Println("Error post hum")
}
// Post Illuminance
err_ill := client.PostServiceMetricValues(serviceName, []*mackerel.MetricValue{
&mackerel.MetricValue{
Name: "Illuminance.illuminance",
Time: nowTime.Unix(),
Value: ill,
},
})
if err_ill != nil {
fmt.Println("Error post ill")
}
}
func main() {
lambda.Start(Handler)
}
func Handler(ctx context.Context) {
jst := time.FixedZone(timezone, offset)
nowTime := time.Now().In(jst)
d := &Device{}
tem, hum, ill := d.FetchValesFromNatureRemo()
PostValuesToMackerel(tem, hum, ill, nowTime)
}