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

     1  package main
     2  
     3  //---------------------------------------------------------------------------------------------------------------------------------------------------------
     4  // Package   : temperature.go
     5  // Port rest : 1000
     6  // Gets      :
     7  //              /eau
     8  //              /air
     9  //              /air_local
    10  // Reponse   : {"type":"temperature","id":"air_local","id_sonde":"28-0417714350ff","value":"13.75","elapse":"918.770618ms","compteur":1,"version":"1.0.0"}
    11  // BLOW vertigo
    12  // Glenn Miller
    13  //---------------------------------------------------------------------------------------------------------------------------------------------------------
    14  
    15  import (
    16  	"encoding/json"
    17  	"fmt"
    18  	"io/ioutil"
    19  	"net/http"
    20  	"os"
    21  	//"path/filepath"
    22  	"time"
    23  
    24  	"github.com/yryz/ds18b20"
    25  	"gopkg.in/yaml.v2"
    26  )
    27  
    28  // Structure du Json
    29  type apiTemperature struct {
    30  	Type     string `json:"type"`
    31  	ID       string `json:"id"`
    32  	IDSonde  string `json:"idsonde"`
    33  	Value    string `json:"value"`
    34  	FileMqtt string `json:"filemqtt"`
    35  	Elapse   string `json:"elapse"`
    36  	Compteur int    `json:"compteur"`
    37  	Version  string `json:"version"`
    38  }
    39  
    40  // Structure pour le fichier de configuration
    41  type LectureInit struct {
    42  	Sondes struct {
    43  		Air          string `yaml:"air"`
    44  		Mqttair      string `yaml:"mqttair"`
    45  		Airlocal     string `yaml:"airlocal"`
    46  		Mqttairlocal string `yaml:"mqttairlocal"`
    47  		Eau          string `yaml:"eau"`
    48  		Mqtteau      string `yaml:"mqtteau"`
    49  		Port         string `yaml:"port"`
    50  	} `yaml:"Sondes"`
    51  }
    52  
    53  // Déclare la liste
    54  var liste = map[string]*struct{ idds18, labelmqtt string }{
    55  	"air":       {"", ""},
    56  	"air_local": {"", ""},
    57  	"eau":       {"", ""},
    58  }
    59  
    60  var (
    61  	compteurEau       = 0
    62  	compteurAir       = 0
    63  	compteurAir_Local = 0
    64  	version           = "1.0.0"
    65  	lectureInit       LectureInit
    66  )
    67  
    68  // Fonction Init du programme
    69  func init() {
    70  
    71  	//filename := filepath.IsAbs("/root/DomosV2/src/Conf/config.yaml")
    72  	yamlFile, err := ioutil.ReadFile("/root/DomosV2/src/Conf/config.yaml")
    73  
    74  	if err != nil {
    75  		fmt.Println("Erreur de lecture du fichier config.yaml")
    76  		fmt.Println(err)
    77  		os.Exit(1)
    78  	}
    79  
    80  	err = yaml.Unmarshal(yamlFile, &lectureInit)
    81  
    82  	if err != nil {
    83  		fmt.Println("Erreur dans le parse du fichier config.yaml")
    84  		fmt.Println(err)
    85  		os.Exit(1)
    86  	}
    87  
    88  	liste["eau"].idds18 = lectureInit.Sondes.Eau
    89  	liste["eau"].labelmqtt = lectureInit.Sondes.Mqtteau
    90  	liste["air"].idds18 = lectureInit.Sondes.Air
    91  	liste["air"].labelmqtt = lectureInit.Sondes.Mqttair
    92  	liste["air_local"].idds18 = lectureInit.Sondes.Airlocal
    93  	liste["air_local"].labelmqtt = lectureInit.Sondes.Mqttairlocal
    94  
    95  }
    96  
    97  // Fonction main
    98  func main() {
    99  
   100  	http.HandleFunc("/eau", func(w http.ResponseWriter, r *http.Request) {
   101  		api(w, r, liste["eau"].idds18, liste["eau"].labelmqtt, "eau")
   102  	})
   103  	http.HandleFunc("/air", func(w http.ResponseWriter, r *http.Request) {
   104  		api(w, r, liste["air"].idds18, liste["air"].labelmqtt, "air")
   105  	})
   106  	http.HandleFunc("/air_local", func(w http.ResponseWriter, r *http.Request) {
   107  		api(w, r, liste["air_local"].idds18, liste["air_local"].labelmqtt, "air_local")
   108  	})
   109  
   110  	fmt.Println("Server Temperature listen port : " + lectureInit.Sondes.Port)
   111  	http.ListenAndServe(":"+lectureInit.Sondes.Port, nil)
   112  }
   113  
   114  // Fonction de réponse HTTP
   115  func api(w http.ResponseWriter, r *http.Request, id string, id_mqtt string, idstring string) {
   116  
   117  	compteur := 0
   118  
   119  	switch idstring {
   120  	case "eau":
   121  		compteurEau = compteurEau + 1
   122  		compteur = compteurEau
   123  
   124  	case "air":
   125  		compteurAir = compteurAir + 1
   126  		compteur = compteurAir
   127  
   128  	case "air_local":
   129  		compteurAir_Local = compteurAir_Local + 1
   130  		compteur = compteurAir_Local
   131  
   132  	default:
   133  		fmt.Println("Je ne comprends pas le choix !!!")
   134  	}
   135  
   136  	// Démarre chrono
   137  	start := time.Now().UTC()
   138  
   139  	// Demande la température
   140  	temperature, err := ds18b20.Temperature(id)
   141  	temperatureString := fmt.Sprintf("%.2f", temperature)
   142  
   143  	// Calcul le temps pour lecture température
   144  	elapse := time.Since(start)
   145  	elapseSeconds := fmt.Sprintf("%.3f", elapse.Seconds())
   146  
   147  	// Test si nous avons eu un problème de lecture
   148  	if err != nil {
   149  		fmt.Println("Probleme lecture sonde " + idstring + " : " + id)
   150  		fmt.Println(err)
   151  	}
   152  
   153  	// Création du JSON
   154  	donnees := apiTemperature{
   155  		Type:     "temperature",
   156  		ID:       idstring,
   157  		IDSonde:  id,
   158  		Value:    temperatureString,
   159  		FileMqtt: id_mqtt,
   160  		Elapse:   elapseSeconds,
   161  		Compteur: compteur,
   162  		Version:  version,
   163  	}
   164  
   165  	jsonData, _ := json.Marshal(donnees)
   166  
   167  	w.Header().Set("content-type", "application/json")
   168  	w.Write([]byte(jsonData))
   169  }