forked from jsgoecke/tesla
-
Notifications
You must be signed in to change notification settings - Fork 18
/
products.go
52 lines (43 loc) · 1.55 KB
/
products.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
package tesla
import (
"encoding/json"
)
type Product struct {
EnergySiteId int64 `json:"energy_site_id,omitempty"`
ResourceType string `json:"resource_type"`
ID Identifier `json:"id"`
AssetSiteId string `json:"asset_site_id,omitempty"`
GatewayId string `json:"gateway_id,omitempty"`
WarpSiteNumber string `json:"warp_site_number,omitempty"`
EnergyLeft float64 `json:"energy_left,omitempty"`
TotalPackEnergy uint64 `json:"total_pack_energy,omitempty"`
PercentageCharged float64 `json:"percentage_charged,omitempty"`
BatteryType string `json:"battery_type,omitempty"`
BackupCapable bool `json:"backup_capable,omitempty"`
BatteryPower int64 `json:"battery_power,omitempty"`
c *Client
}
type Identifier string
func (i *Identifier) UnmarshalJSON(data []byte) error {
if n := len(data); n > 1 && data[0] == '"' && data[n-1] == '"' {
return json.Unmarshal(data, (*string)(i))
}
*i = Identifier(data)
return nil
}
// ProductResponse contains the product details from the Tesla API.
type ProductsResponse struct {
Response []*Product `json:"response"`
Count int `json:"count"`
}
// Products fetches the products associated to a Tesla account via the API.
func (c *Client) Products() ([]*Product, error) {
productsResponse := &ProductsResponse{}
if err := c.getJSON(c.baseURL+"/products", productsResponse); err != nil {
return nil, err
}
for _, v := range productsResponse.Response {
v.c = c
}
return productsResponse.Response, nil
}