migrate to new 2.0 API

This commit is contained in:
Nicolas Leclercq 2016-07-29 15:22:55 +02:00
parent 1e275a22ef
commit 8562438f63

View file

@ -17,7 +17,7 @@ const (
// DefaultAuthURL is netatmo auth url // DefaultAuthURL is netatmo auth url
authURL = baseURL + "oauth2/token" authURL = baseURL + "oauth2/token"
// DefaultDeviceURL is netatmo device url // DefaultDeviceURL is netatmo device url
deviceURL = baseURL + "api/devicelist" deviceURL = baseURL + "/api/getstationsdata"
) )
// Config is used to specify credential to Netatmo API // Config is used to specify credential to Netatmo API
@ -44,14 +44,12 @@ type Client struct {
httpResponse *http.Response httpResponse *http.Response
} }
// DeviceCollection hold all devices from netatmo account (stations and modules) // DeviceCollection hold all devices from netatmo account
// Error : returned error (nil if OK) // Error : returned error (nil if OK)
// Stations : List of stations // Stations : List of stations
// Modules : List of additionnal modules
type DeviceCollection struct { type DeviceCollection struct {
Body struct { Body struct {
Stations []*Device `json:"devices"` Devices []*Device `json:"devices"`
Modules []*Device
} }
} }
@ -67,17 +65,15 @@ type DeviceCollection struct {
// "NAModule2" : for the wind gauge module // "NAModule2" : for the wind gauge module
// DashboardData : Data collection from device sensors // DashboardData : Data collection from device sensors
// DataType : List of available datas // DataType : List of available datas
// MainDevice : Id of main station (only for module) // LinkedModules : Associated modules (only for station)
// AssociatedModules : Associated modules (only for station)
type Device struct { type Device struct {
ID string `json:"_id"` ID string `json:"_id"`
StationName string `json:"station_name"` StationName string `json:"station_name"`
ModuleName string `json:"module_name"` ModuleName string `json:"module_name"`
Type string Type string
DashboardData DashboardData `json:"dashboard_data"` DashboardData DashboardData `json:"dashboard_data"`
DataType []string `json:"data_type"` DataType []string `json:"data_type"`
MainDevice string `json:"main_device,omitempty"` LinkedModules []*Device `json:"modules"`
AssociatedModules []*Device `json:"-"`
} }
// DashboardData is used to store sensor values // DashboardData is used to store sensor values
@ -200,8 +196,8 @@ func processHTTPResponse(resp *http.Response, err error, holder interface{}) err
return nil return nil
} }
// GetDeviceCollection returns the list of stations owned by the user, and their modules // GetStations returns the list of stations owned by the user, and their modules
func (c *Client) GetDeviceCollection() (*DeviceCollection, error) { func (c *Client) Read() (*DeviceCollection, error) {
//resp, err := c.doHTTPPostForm(deviceURL, url.Values{"app_type": {"app_station"}}) //resp, err := c.doHTTPPostForm(deviceURL, url.Values{"app_type": {"app_station"}})
resp, err := c.doHTTPGet(deviceURL, url.Values{"app_type": {"app_station"}}) resp, err := c.doHTTPGet(deviceURL, url.Values{"app_type": {"app_station"}})
dc := &DeviceCollection{} dc := &DeviceCollection{}
@ -210,39 +206,34 @@ func (c *Client) GetDeviceCollection() (*DeviceCollection, error) {
return nil, err return nil, err
} }
// associated each module to its station
for i, station := range dc.Body.Stations {
for _, module := range dc.Body.Modules {
if module.MainDevice == station.ID {
dc.Body.Stations[i].AssociatedModules = append(dc.Body.Stations[i].AssociatedModules, module)
}
}
}
return dc, nil return dc, nil
} }
// Stations returns the list of stations // Devices returns the list of devices
func (dc *DeviceCollection) Stations() []*Device { func (dc *DeviceCollection) Devices() []*Device {
return dc.Body.Stations return dc.Body.Devices
} }
// Modules returns the list of modules associated to this station // Stations is an alias of Devices
// also return station itself in the list func (dc *DeviceCollection) Stations() []*Device {
func (s *Device) Modules() []*Device { return dc.Devices()
modules := s.AssociatedModules }
// Modules returns associated device module
func (d *Device) Modules() []*Device {
modules := d.LinkedModules
modules = append(modules, d)
modules = append(modules, s)
return modules return modules
} }
// Data returns timestamp and the list of sensor value for this module // Data returns timestamp and the list of sensor value for this module
func (s *Device) Data() (int, map[string]interface{}) { func (d *Device) Data() (int, map[string]interface{}) {
m := make(map[string]interface{}) m := make(map[string]interface{})
for _, datatype := range s.DataType { for _, datatype := range d.DataType {
m[datatype] = reflect.Indirect(reflect.ValueOf(s.DashboardData)).FieldByName(datatype).Interface() m[datatype] = reflect.Indirect(reflect.ValueOf(d.DashboardData)).FieldByName(datatype).Interface()
} }
return int(s.DashboardData.LastMesure), m return int(d.DashboardData.LastMesure), m
} }