-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
140 lines (132 loc) · 3.83 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
// SPDX-License-Identifier: BSD-2-Clause
//
// Copyright 2018 Larry Rosenman, LERCTR Consulting, [email protected]
//
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/lrosenman/ambient/pkg/ambient"
"os"
"time"
)
/*
This example queries all devices (weather stations) that are registered for the account the application and api keys are associated with and shows them in the console.
To generate an application and api key for your own account, do so at https://ambientweather.net/account in the API Keys section.
API Docs:
https://ambientweather.docs.apiary.io/#reference/0/devices/list-user's-devices
https://ambientweather.docs.apiary.io/#reference/0/device-data/query-device-data
https://ambientweather.docs.apiary.io/#introduction/rate-limiting
Sample Usage:
go run main.go
*/
// This is a DEMO KEY, replace it with your own.
//
//goland:noinspection SpellCheckingInspection
const applicationKey = "21a439e927a84a25bb79ffe894fdd372b3e9d2e8bcef4167943b52cbe4530d9f"
// This is a DEMO KEY, replace it with your own.
//
//goland:noinspection SpellCheckingInspection
const apiKey = "78f9704baaab411a87edeed59052cbb687a4aa7764a44accbaf6447492b0ca8c"
func main() {
key := ambient.NewKey(applicationKey, apiKey)
dr, err := ambient.Device(key)
if err != nil {
panic(err)
}
switch dr.HTTPResponseCode {
case 200:
case 429, 502, 503:
{
fmt.Printf("Error code %d, retrying.\n", dr.HTTPResponseCode)
time.Sleep(1 * time.Second)
dr, err = ambient.Device(key)
if err != nil {
panic(err)
}
switch dr.HTTPResponseCode {
case 200:
default:
{
panic(dr)
}
}
}
default:
{
_, err := fmt.Fprintf(os.Stderr, "HTTPResponseCode=%d\n", dr.HTTPResponseCode)
if err != nil {
return
}
panic(dr)
}
}
ar := make([]ambient.APIDeviceMacResponse, len(dr.DeviceRecord))
for z := range dr.DeviceRecord {
// API RateLimit
time.Sleep(1 * time.Second)
ar2, err := ambient.DeviceMac(key, dr.DeviceRecord[z].Macaddress, time.Now(), 1)
if err != nil {
panic(err)
}
ar[z] = ar2
switch ar[z].HTTPResponseCode {
case 200:
case 429, 502, 503:
{
fmt.Printf("Error code %d, retrying.\n", ar[z].HTTPResponseCode)
time.Sleep(1 * time.Second)
ar2, err = ambient.DeviceMac(key, dr.DeviceRecord[z].Macaddress, time.Now(), 1)
if err != nil {
panic(err)
}
ar[z] = ar2
switch ar[z].HTTPResponseCode {
case 200:
default:
{
_, err := fmt.Fprintf(os.Stderr, "HTTPResponseCode=%d\n", ar[z].HTTPResponseCode)
if err != nil {
return
}
panic(ar)
}
}
}
default:
{
_, err := fmt.Fprintf(os.Stderr, "HTTPResponseCode=%d\n", ar[z].HTTPResponseCode)
if err != nil {
return
}
panic(ar)
}
}
}
var drPrettyJSON bytes.Buffer
err = json.Indent(&drPrettyJSON, dr.JSONResponse, "", "\t")
if err != nil {
return
}
drDeviceRecordJSON, _ := json.MarshalIndent(dr.DeviceRecord, "", "\t")
fmt.Printf("DeviceResponse:\nHTTPResponseCode: %d, ResponseTime: %v\n", dr.HTTPResponseCode, dr.ResponseTime)
fmt.Printf("Device Record:\n%+v\n", string(drDeviceRecordJSON))
fmt.Printf("JSONResponse:\n%s\n\n", string(drPrettyJSON.Bytes()))
for idx := range ar {
var arPrettyJSON bytes.Buffer
err := json.Indent(&arPrettyJSON, ar[idx].JSONResponse, "", "\t")
if err != nil {
return
}
arRecordJSON, _ := json.MarshalIndent(ar[idx].Record, "", "\t")
arRecordFieldsJSON, _ := json.MarshalIndent(ar[idx].RecordFields, "", "\t")
fmt.Printf(
"DeviceMacResponse[%s(%s)]:\nHTTPResponseCode: %d, ResponseTime: %v\n", dr.DeviceRecord[idx].Info.Name,
dr.DeviceRecord[idx].Macaddress, ar[idx].HTTPResponseCode, ar[idx].ResponseTime,
)
fmt.Printf("Record:\n%+v\n", string(arRecordJSON))
fmt.Printf("RecordFields:\n%+v\n", string(arRecordFieldsJSON))
fmt.Printf("JSONResponse:\n%s\n\n", string(arPrettyJSON.Bytes()))
}
}