-
Notifications
You must be signed in to change notification settings - Fork 0
/
airly.go
75 lines (62 loc) · 2.19 KB
/
airly.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
package main
import (
"encoding/json"
"net/http"
"net/url"
)
// APIClient manages communication with Arily API
type APIClient struct {
url string
key string
}
// SensorMeasurementsResponse is a response from API with measurements
type SensorMeasurementsResponse struct {
Current MeasurementsTimeFramed `json:"current,omitempty"`
Forecast []MeasurementsTimeFramed `json:"forecast,omitempty"`
History []MeasurementsTimeFramed `json:"history,omitempty"`
}
// MeasurementsTimeFramed is a response from API with measurement time series
type MeasurementsTimeFramed struct {
FromDateTime string `json:"fromDateTime,omitempty"`
TillDateTime string `json:"tillDateTime,omitempty"`
Values []MeasuredValue `json:"values,omitempty"`
Indexes []MeasuredIndex `json:"indexes,omitempty"`
Standards []MeasuredStandard `json:"standards,omitempty"`
}
type MeasuredValue struct {
Name string `json:"name,omitempty"`
Value float64 `json:"value,omitempty"`
}
type MeasuredIndex struct {
Name string `json:"name,omitempty"`
Value float64 `json:"value,omitempty"`
Level string `json:"level,omitempty"`
Description string `json:"description,omitempty"`
Advice string `json:"advice,omitempty"`
Color string `json:"color,omitempty"`
}
type MeasuredStandard struct {
Name string `json:"name,omitempty"`
Pollutant string `json:"pollutant,omitempty"`
Limit float64 `json:"limit,omitempty"`
Percent float64 `json:"percent,omitempty"`
}
// NewAPIClient creates a new APIClient
func NewAPIClient(apiURL string, apiKey string) *APIClient {
return &APIClient{apiURL, apiKey}
}
// SensorMeasurements returns response from Airly API for installationId
func (api *APIClient) SensorMeasurements(installationId string) (SensorMeasurementsResponse, int, error) {
var response SensorMeasurementsResponse
v := url.Values{}
v.Set("apikey", api.key)
v.Set("installationId", installationId)
req := api.url + "/v2/measurements/installation?" + v.Encode()
resp, err := http.Get(req)
if err != nil {
return response, 0, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&response)
return response, resp.StatusCode, err
}