* Remove usage of reflect as variable type are not propagated as expected

with influxdb go client (cf netatmo-collector-go)
* Add Info function to display RF, wifi and battery levels
* Fix GustStrength
This commit is contained in:
Nicolas Leclercq 2017-10-26 16:39:20 +02:00
parent 2be3ed5ce1
commit 3401330732
2 changed files with 130 additions and 43 deletions

View file

@ -18,6 +18,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"time"
netatmo "github.com/exzz/netatmo-api-go" netatmo "github.com/exzz/netatmo-api-go"
) )
@ -41,16 +42,28 @@ func main() {
os.Exit(1) os.Exit(1)
} }
ct := time.Now().UTC().Unix()
for _, station := range dc.Stations() { for _, station := range dc.Stations() {
fmt.Printf("Station : %s\n", station.StationName) fmt.Printf("Station : %s\n", station.StationName)
for _, module := range station.Modules() { for _, module := range station.Modules() {
fmt.Printf("\tModule : %s\n", module.ModuleName) fmt.Printf("\tModule : %s\n", module.ModuleName)
ts, data := module.Data() {
for dataType, value := range data { ts, data := module.Info()
fmt.Printf("\t\t%s : %s (%d)\n", dataType, value, ts) for dataName, value := range data {
fmt.Printf("\t\t%s : %v (updated %ds ago)\n", dataName, value, ct-ts)
}
} }
{
ts, data := module.Data()
for dataName, value := range data {
fmt.Printf("\t\t%s : %v (updated %ds ago)\n", dataName, value, ct-ts)
}
}
} }
} }
} }
@ -58,31 +71,42 @@ func main() {
Output should look like this : Output should look like this :
``` ```
Station : Home Station : Home
Module : Chambre Enfant Module : Chambre Elsa
Temperature : %!s(float32=18.4) (1479127223) BatteryPercent : 47 (updated 323s ago)
CO2 : %!s(int32=567) (1479127223) RFStatus : 68 (updated 323s ago)
Humidity : %!s(int32=65) (1479127223) Temperature : 22.8 (updated 323s ago)
Module : Chambre Humidity : 53 (updated 323s ago)
Temperature : %!s(float32=18.1) (1479127230) CO2 : 446 (updated 323s ago)
CO2 : %!s(int32=494) (1479127230) Module : Chambre parents
Humidity : %!s(int32=65) (1479127230) BatteryPercent : 50 (updated 323s ago)
Module : Salon RFStatus : 71 (updated 323s ago)
Temperature : %!s(float32=18.3) (1479127217) Temperature : 19.9 (updated 323s ago)
CO2 : %!s(int32=434) (1479127217) Humidity : 61 (updated 323s ago)
Humidity : %!s(int32=63) (1479127217) CO2 : 428 (updated 323s ago)
Module : Exterieur Module : Chambre Jules
Temperature : %!s(float32=11.9) (1479127243) BatteryPercent : 46 (updated 323s ago)
Humidity : %!s(int32=81) (1479127243) RFStatus : 60 (updated 323s ago)
Module : Pluie CO2 : 396 (updated 323s ago)
Rain : %!s(float32=0) (1479127249) Temperature : 22 (updated 323s ago)
Module : Salle à manger Humidity : 54 (updated 323s ago)
Temperature : %!s(float32=17.8) (1479127255) Module : Exterieur
CO2 : %!s(int32=473) (1479127255) BatteryPercent : 37 (updated 323s ago)
Humidity : %!s(int32=68) (1479127255) RFStatus : 66 (updated 323s ago)
Noise : %!s(int32=36) (1479127255) Temperature : 23.4 (updated 323s ago)
Pressure : %!s(float32=1033.3) (1479127255) Humidity : 52 (updated 323s ago)
Module : Pluie
BatteryPercent : 72 (updated 9684499s ago)
RFStatus : 54 (updated 9684499s ago)
Rain : 0.101 (updated 9684499s ago)
Module : Living
WifiStatus : 37 (updated 278s ago)
Temperature : 24 (updated 278s ago)
Humidity : 49 (updated 278s ago)
CO2 : 733 (updated 278s ago)
Noise : 50 (updated 278s ago)
Pressure : 1028.1 (updated 278s ago)
AbsolutePressure : 1008.4 (updated 278s ago)
``` ```
## Tips ## Tips
- Only Read() method actually do an API call and refresh all data at once - Only Read() method actually do an API call and refresh all data at once

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"reflect"
"strings" "strings"
"golang.org/x/oauth2" "golang.org/x/oauth2"
@ -51,6 +50,13 @@ type DeviceCollection struct {
// ID : Mac address // ID : Mac address
// StationName : Station name (only for station) // StationName : Station name (only for station)
// ModuleName : Module name // ModuleName : Module name
// BatteryPercent : Percentage of battery remaining
// WifiStatus : Wifi status per Base station
// RFStatus : Current radio status per module
// Firmware : Version of the software
// DateSetup : Date when the Weather station was set up
// LastMessage
// LastSeen
// Type : Module type : // Type : Module type :
// "NAMain" : for the base station // "NAMain" : for the base station
// "NAModule1" : for the outdoor module // "NAModule1" : for the outdoor module
@ -61,11 +67,14 @@ type DeviceCollection struct {
// DataType : List of available datas // DataType : List of available datas
// LinkedModules : Associated modules (only for station) // LinkedModules : 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 BatteryPercent *int32 `json:"battery_percent,omitempty"`
DashboardData DashboardData `json:"dashboard_data"` WifiStatus *int32 `json:"wifi_status,omitempty"`
RFStatus *int32 `json:"rf_status,omitempty"`
Type string
DashboardData DashboardData `json:"dashboard_data"`
//DataType []string `json:"data_type"` //DataType []string `json:"data_type"`
LinkedModules []*Device `json:"modules"` LinkedModules []*Device `json:"modules"`
} }
@ -222,19 +231,73 @@ func (d *Device) Modules() []*Device {
} }
// 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 (d *Device) Data() (int, map[string]interface{}) { func (d *Device) Data() (int64, map[string]interface{}) {
// return only populate field of DashboardData // return only populate field of DashboardData
m := make(map[string]interface{}) m := make(map[string]interface{})
r := reflect.ValueOf(d.DashboardData)
for i := 0; i < r.NumField(); i++ { if d.DashboardData.Temperature != nil {
//fmt.Println(r.Type().Field(i).Name) m["Temperature"] = *d.DashboardData.Temperature
if reflect.Indirect(r.Field(i)).IsValid() { }
m[r.Type().Field(i).Name] = reflect.Indirect(r.Field(i)) if d.DashboardData.Humidity != nil {
//fmt.Println(reflect.Indirect(r.Field(i))) m["Humidity"] = *d.DashboardData.Humidity
} }
if d.DashboardData.CO2 != nil {
m["CO2"] = *d.DashboardData.CO2
}
if d.DashboardData.Noise != nil {
m["Noise"] = *d.DashboardData.Noise
}
if d.DashboardData.Pressure != nil {
m["Pressure"] = *d.DashboardData.Pressure
}
if d.DashboardData.AbsolutePressure != nil {
m["AbsolutePressure"] = *d.DashboardData.AbsolutePressure
}
if d.DashboardData.Rain != nil {
m["Rain"] = *d.DashboardData.Rain
}
if d.DashboardData.Rain1Hour != nil {
m["Rain1Hour"] = *d.DashboardData.Rain1Hour
}
if d.DashboardData.Rain1Day != nil {
m["Rain1Day"] = *d.DashboardData.Rain1Day
}
if d.DashboardData.WindAngle != nil {
m["WindAngle"] = *d.DashboardData.WindAngle
}
if d.DashboardData.WindStrength != nil {
m["WindStrength"] = *d.DashboardData.WindStrength
}
if d.DashboardData.GustAngle != nil {
m["GustAngle"] = *d.DashboardData.GustAngle
}
if d.DashboardData.GustAngle != nil {
m["GustAngle"] = *d.DashboardData.GustAngle
}
if d.DashboardData.GustStrength != nil {
m["GustStrength"] = *d.DashboardData.GustStrength
} }
return int(*d.DashboardData.LastMesure), m return *d.DashboardData.LastMesure, m
}
// Info returns timestamp and the list of info value for this module
func (d *Device) Info() (int64, map[string]interface{}) {
// return only populate field of DashboardData
m := make(map[string]interface{})
// Return data from module level
if d.BatteryPercent != nil {
m["BatteryPercent"] = *d.BatteryPercent
}
if d.WifiStatus != nil {
m["WifiStatus"] = *d.WifiStatus
}
if d.RFStatus != nil {
m["RFStatus"] = *d.RFStatus
}
return *d.DashboardData.LastMesure, m
} }