github.com/alexis81/domosgo@v0.0.0-20191016125037-5aee90a434af/DomosV2/src/netatmo.go (about)

     1  package main
     2  
     3  //---------------------------------------------------------------------------------------------------------------------------------------------------------
     4  // Package   : statsRaspberry.go
     5  // Port rest : 1003
     6  // Gets      :
     7  //              /StatsNetatmo
     8  //
     9  // netatmo.intérieur.co2 : 843
    10  // netatmo.intérieur.humidity : 52
    11  // netatmo.intérieur.noise : 43
    12  // netatmo.intérieur.pressure : 995.6
    13  // netatmo.intérieur.temperature : 22.8
    14  //
    15  //---------------------------------------------------------------------------------------------------------------------------------------------------------
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"io/ioutil"
    21  	"log"
    22  	"net/http"
    23  	"os"
    24  	"strings"
    25  	"time"
    26  
    27  	netatmo "github.com/romainbureau/netatmo-api-go"
    28  	"gopkg.in/yaml.v2"
    29  )
    30  
    31  var (
    32  	lectureInit LectureInit
    33  	version     = "1.0.0"
    34  )
    35  
    36  // Structure du Json
    37  type StatsNetatmo struct {
    38  	Type                                       string `json:"type"`
    39  	Domos_poolhouse_bureau_netatmo_Co2         string `json:"Domos.poolhouse.bureau.netatmo.Co2"`
    40  	Domos_poolhouse_bureau_netatmo_Humidity    string `json:"Domos.poolhouse.bureau.netatmo.Humidity"`
    41  	Domos_poolhouse_bureau_netatmo_Noise       string `json:"Domos.poolhouse.bureau.netatmo.Noise"`
    42  	Domos_poolhouse_bureau_netatmo_Pressure    string `json:"Domos.poolhouse.bureau.netatmo.Pressure"`
    43  	Domos_poolhouse_bureau_netatmo_Temperature string `json:"Domos.poolhouse.bureau.netatmo.Temperature"`
    44  	Domos_poolhouse_bureau_netatmo_Elapsed     string `json:"Domos.poolhouse.bureau.netatmo.Elapsed"`
    45  	Version                                    string `json:"version"`
    46  }
    47  
    48  // Structure pour le fichier de configuration
    49  type LectureInit struct {
    50  	Netatmo struct {
    51  		Clientid     string `yaml:"clientid"`
    52  		Clientsecret string `yaml:"clientsecret"`
    53  		Username     string `yaml:"username"`
    54  		Password     string `yaml:"password"`
    55  		Url          string `yaml:"url"`
    56  		Port         string `yaml:"port"`
    57  	} `yaml:"Netatmo"`
    58  }
    59  
    60  //--------------------------------------------------------------------------------------------------------------------------------
    61  // Fonction : main
    62  //
    63  //--------------------------------------------------------------------------------------------------------------------------------
    64  func main() {
    65  
    66  	// Lecture du fichier config.yaml
    67  	//filename := filepath.IsAbs("/root/DomosV2/src/Conf/config.yaml")
    68  	yamlFile, err := ioutil.ReadFile("/root/DomosV2/src/Conf/config.yaml")
    69  	if err != nil {
    70  		fmt.Println("Erreur de lecture du fichier config.yaml")
    71  		fmt.Println(err)
    72  		os.Exit(1)
    73  	}
    74  
    75  	// Parse du fichier config.yaml
    76  	err = yaml.Unmarshal(yamlFile, &lectureInit)
    77  	if err != nil {
    78  		fmt.Println("Erreur dans le parse du fichier config.yaml")
    79  		fmt.Println(err)
    80  		os.Exit(1)
    81  	}
    82  
    83  	http.HandleFunc(lectureInit.Netatmo.Url, func(w http.ResponseWriter, r *http.Request) {
    84  		api(w, r, lectureInit.Netatmo.Clientid, lectureInit.Netatmo.Clientsecret, lectureInit.Netatmo.Username, lectureInit.Netatmo.Password)
    85  	})
    86  
    87  	fmt.Println("Server StatsNetatmo listen port : " + lectureInit.Netatmo.Port)
    88  	http.ListenAndServe(":"+lectureInit.Netatmo.Port, nil)
    89  }
    90  
    91  //--------------------------------------------------------------------------------------------------------------------------------
    92  // Fonction : api
    93  // Entrées  : w, r, clientid, clientsecret, username, password
    94  // Sortie   :
    95  //--------------------------------------------------------------------------------------------------------------------------------
    96  func api(w http.ResponseWriter, r *http.Request, clientid string, clientsecret string, username string, password string) {
    97  
    98  	// Démarre chrono
    99  	start := time.Now().UTC()
   100  
   101  	// Créer un client Netatmo
   102  	netatmoClient, err := netatmo.NewClient(netatmo.Config{
   103  		ClientID:     clientid,
   104  		ClientSecret: clientsecret,
   105  		Username:     username,
   106  		Password:     password,
   107  	})
   108  
   109  	if err != nil {
   110  		log.Println(err.Error())
   111  	}
   112  
   113  	// Lecture des valeurs
   114  	values, err := netatmoClient.Read()
   115  	if err != nil {
   116  		log.Println(err.Error())
   117  	}
   118  
   119  	// Extraction des valeurs
   120  	var Values = make(map[string]float32)
   121  	for _, station := range values.Stations() {
   122  		for _, module := range station.Modules() {
   123  			_, data := module.Data()
   124  			moduleName := strings.ToLower(module.ModuleName)
   125  			for key, value := range data {
   126  				metricName := fmt.Sprintf("netatmo.%s.%s", moduleName, key)
   127  				//fmt.Print(metricName)
   128  				//fmt.Print(" --> ")
   129  				Values[metricName] = value.(float32)
   130  				//fmt.Println(value.(float32))
   131  			}
   132  		}
   133  	}
   134  
   135  	// Calcul le temps
   136  	elapse := time.Since(start)
   137  	elapseSeconds := fmt.Sprintf("%.3f", elapse.Seconds())
   138  
   139  	// Création du JSON
   140  	donnees := StatsNetatmo{
   141  		Type: "StatNetatmo",
   142  		Domos_poolhouse_bureau_netatmo_Co2:         fmt.Sprintf("%v", Values["netatmo.intérieur.co2"]),
   143  		Domos_poolhouse_bureau_netatmo_Humidity:    fmt.Sprintf("%v", Values["netatmo.intérieur.humidity"]),
   144  		Domos_poolhouse_bureau_netatmo_Noise:       fmt.Sprintf("%v", Values["netatmo.intérieur.noise"]),
   145  		Domos_poolhouse_bureau_netatmo_Pressure:    fmt.Sprintf("%v", Values["netatmo.intérieur.pressure"]),
   146  		Domos_poolhouse_bureau_netatmo_Temperature: fmt.Sprintf("%v", Values["netatmo.intérieur.temperature"]),
   147  		Domos_poolhouse_bureau_netatmo_Elapsed:     elapseSeconds,
   148  		Version: version,
   149  	}
   150  
   151  	// Création du JSON
   152  	jsonData, _ := json.Marshal(donnees)
   153  
   154  	w.Header().Set("content-type", "application/json")
   155  	w.Write([]byte(jsonData))
   156  
   157  }