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

     1  package main
     2  
     3  //https://www.itead.cc/wiki/Nextion_Instruction_Set#Format_of_Device_Return_Data
     4  
     5  import (
     6  	"bufio"
     7  	"bytes"
     8  	"encoding/json"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"log"
    12  	"net/http"
    13  	"strconv"
    14  	"time"
    15  
    16  	"github.com/tarm/serial"
    17  )
    18  
    19  // apiTemperature : Structure du Json
    20  type apiTemperature struct {
    21  	Type     string `json:"type"`
    22  	ID       string `json:"id"`
    23  	IDSonde  string `json:"idsonde"`
    24  	Value    string `json:"value"`
    25  	FileMqtt string `json:"filemqtt"`
    26  	Elapse   string `json:"elapse"`
    27  	Compteur int    `json:"compteur"`
    28  	Version  string `json:"version"`
    29  }
    30  
    31  // apiStatus : Structure du Json
    32  type apiStatus struct {
    33  	Pompe            int    `json:"pompe"`
    34  	Electrolyse      int    `json:"electrolyse"`
    35  	Lampe            int    `json:"lampe"`
    36  	LampePortail     int    `json:"lampeportail"`
    37  	TemperatureEau   string `json:"temperatureeau"`
    38  	TemperatureAir   string `json:"temperatureair"`
    39  	TemperatureLocal string `json:"temperaturelocal"`
    40  	DateTime         string `json:"datetime"`
    41  	DureeFiltration  int64  `json:"dureefiltration"`
    42  }
    43  
    44  func main() {
    45  
    46  	// Configuration de l'interface serial
    47  	config := &serial.Config{Name: "/dev/ttyS0", Baud: 9600}
    48  
    49  	// Ouverture du port RS232
    50  	s, err := serial.OpenPort(config)
    51  	if err != nil {
    52  		// stops execution
    53  		log.Fatal(err)
    54  	}
    55  
    56  	// Fin de ligne de commande
    57  	endCom := "\xFF\xFF\xFF"
    58  
    59  	// Permet de baisser la lumière de l'afficheur
    60  	_, err = s.Write([]byte("dim=100" + endCom))
    61  
    62  	// Temps de mise en veille de l'écran
    63  	_, err = s.Write([]byte("sleep=0" + endCom))
    64  
    65  	// Temps de mise en veille de l'écran
    66  	_, err = s.Write([]byte("thsp=" + endCom))
    67  
    68  	// Permet de reveiller l'écran si pression
    69  	_, err = s.Write([]byte("thup=1" + endCom))
    70  
    71  	// Lance les goroutines
    72  	go lecture(s)
    73  	go ecriture(s)
    74  
    75  	// Permet de faire tourner le programme en boucle
    76  	for {
    77  
    78  	}
    79  
    80  }
    81  
    82  // Permet la lecture du port serial
    83  func lecture(s *serial.Port) {
    84  
    85  	// golang reader interface
    86  	r := bufio.NewReader(s)
    87  
    88  	for {
    89  		// reads until delimiter is reached
    90  		data, err := r.ReadBytes('\xFF')
    91  
    92  		// Supprime les 0XFF
    93  		data = bytes.Trim(data, "\xFF")
    94  
    95  		// Test si nous avons une erreur
    96  		if err != nil {
    97  			// stops execution
    98  			log.Fatal(err)
    99  		}
   100  
   101  		// Si un bouton est appuyé sur l'écran
   102  		switch string(data) {
   103  		case "pbt0:ON":
   104  			fmt.Println("Bouton 0 = ON")
   105  			resp, _ := http.Get("http://192.168.1.22:8080/pompe/1")
   106  			defer resp.Body.Close()
   107  
   108  		case "pbt0:OFF":
   109  			fmt.Println("Bouton 0 = OFF")
   110  			resp, _ := http.Get("http://192.168.1.22:8080/pompe/0")
   111  			defer resp.Body.Close()
   112  
   113  		case "pbt1:ON":
   114  			fmt.Println("Bouton 1 = ON")
   115  			resp, _ := http.Get("http://192.168.1.22:8080/electro/1")
   116  			defer resp.Body.Close()
   117  
   118  		case "pbt1:OFF":
   119  			fmt.Println("Bouton 1 = OFF")
   120  			resp, _ := http.Get("http://192.168.1.22:8080/electro/0")
   121  			defer resp.Body.Close()
   122  
   123  		case "pbt2:ON":
   124  			fmt.Println("Bouton 2 = ON")
   125  			resp, _ := http.Get("http://192.168.1.22:8080/lampe/1")
   126  			defer resp.Body.Close()
   127  
   128  		case "pbt2:OFF":
   129  			fmt.Println("Bouton 2 = OFF")
   130  			resp, _ := http.Get("http://192.168.1.22:8080/lampe/0")
   131  			defer resp.Body.Close()
   132  
   133  		case "pbt3:ON":
   134  			fmt.Println("Bouton 3 = ON")
   135  			resp, _ := http.Get("http://192.168.1.22:8080/lampe/1")
   136  			defer resp.Body.Close()
   137  
   138  		case "pbt3:OFF":
   139  			fmt.Println("Bouton 3 = OFF")
   140  			resp, _ := http.Get("http://192.168.1.22:8080/lampe/0")
   141  			defer resp.Body.Close()
   142  		}
   143  	}
   144  }
   145  
   146  // Permet d'envoyer des informations à l'afficheur
   147  func ecriture(s *serial.Port) {
   148  
   149  	// Fin de ligne de commande
   150  	endCom := "\xFF\xFF\xFF"
   151  
   152  	// Déclare un flag
   153  	flag := false
   154  
   155  	for {
   156  
   157  		// Température Air
   158  		rs, _ := http.Get("http://192.168.1.22:8080/status")
   159  
   160  		defer rs.Body.Close()
   161  
   162  		if rs.StatusCode == 200 {
   163  			//ms.Pompe, msg.Electrolyse, msg.Lampe, msg.LampePortail, msg.TemperatureEau, msg.TemperatureAir, msg.TemperatureLocal, msg.DateTime, msg.DureeFiltration
   164  			pompe, electrolyse, lampe, lampePortail, temperatureEau, temperatureAir, temperatureLocal, dateTime, _ := httpStatus(rs)
   165  
   166  			_, err := s.Write([]byte("Filtration.txt=\"" + dateTime + "\"" + endCom))
   167  
   168  			_, err = s.Write([]byte("TempEau.txt=\"" + temperatureEau + "\"" + endCom))
   169  			_, err = s.Write([]byte("TempAir.txt=\"" + temperatureAir + "\"" + endCom))
   170  			_, err = s.Write([]byte("TempLocal.txt=\"" + temperatureLocal + "\"" + endCom))
   171  
   172  			if err != nil {
   173  				fmt.Println("error send lampe portail")
   174  			}
   175  
   176  			if pompe == "0" {
   177  				_, err := s.Write([]byte("bt0.val=0" + endCom))
   178  
   179  				if err != nil {
   180  					fmt.Println("error send pompe")
   181  				}
   182  			} else {
   183  				_, err := s.Write([]byte("bt0.val=1" + endCom))
   184  
   185  				if err != nil {
   186  					fmt.Println("error send pompe")
   187  				}
   188  			}
   189  
   190  			if electrolyse == "0" {
   191  				_, err := s.Write([]byte("bt1.val=0" + endCom))
   192  
   193  				if err != nil {
   194  					fmt.Println("error send electrolyse")
   195  				}
   196  			} else {
   197  				_, err := s.Write([]byte("bt1.val=1" + endCom))
   198  
   199  				if err != nil {
   200  					fmt.Println("error send electrolyse")
   201  				}
   202  			}
   203  
   204  			if lampe == "0" {
   205  				_, err := s.Write([]byte("bt2.val=0" + endCom))
   206  				if err != nil {
   207  					fmt.Println("error send lampe")
   208  				}
   209  			} else {
   210  				_, err := s.Write([]byte("bt2.val=1" + endCom))
   211  				if err != nil {
   212  					fmt.Println("error send lampe")
   213  				}
   214  			}
   215  
   216  			if lampePortail == "0" {
   217  				_, err := s.Write([]byte("bt3.val=0" + endCom))
   218  
   219  				if err != nil {
   220  					fmt.Println("error send lampe portail")
   221  				}
   222  			} else {
   223  				_, err := s.Write([]byte("bt3.val=1" + endCom))
   224  
   225  				if err != nil {
   226  					fmt.Println("error send lampe portail")
   227  				}
   228  			}
   229  
   230  		}
   231  
   232  		if flag == false {
   233  			_, err := s.Write([]byte("p1.pic=4" + endCom))
   234  
   235  			if err != nil {
   236  				fmt.Println("error send wifi")
   237  			}
   238  			flag = true
   239  		} else {
   240  			_, err := s.Write([]byte("p1.pic=3" + endCom))
   241  
   242  			if err != nil {
   243  				fmt.Println("error send wifi")
   244  			}
   245  			flag = false
   246  		}
   247  
   248  		duration := time.Duration(1) * time.Second // Pause for 10 seconds
   249  		time.Sleep(duration)
   250  	}
   251  }
   252  
   253  // Permet d'interroger l'API température *************************************
   254  func httpTemperature(rs *http.Response) (string, string, string, string) {
   255  
   256  	bodyBytes, err := ioutil.ReadAll(rs.Body)
   257  	if err != nil {
   258  		fmt.Println("error send")
   259  	}
   260  
   261  	// Unmarshal
   262  	var msg apiTemperature
   263  	err = json.Unmarshal(bodyBytes, &msg)
   264  	if err != nil {
   265  		fmt.Println("error send Temperatures")
   266  	}
   267  
   268  	return msg.FileMqtt, msg.Value, msg.Elapse, strconv.Itoa(msg.Compteur)
   269  }
   270  
   271  // Permet d'interroger l'API status *************************************
   272  func httpStatus(rs *http.Response) (string, string, string, string, string, string, string, string, string) {
   273  
   274  	bodyBytes, err := ioutil.ReadAll(rs.Body)
   275  	if err != nil {
   276  		fmt.Println("error send Status")
   277  	}
   278  
   279  	// Unmarshal
   280  	var msg apiStatus
   281  	err = json.Unmarshal(bodyBytes, &msg)
   282  	if err != nil {
   283  		fmt.Println("error Unmarshal status")
   284  	}
   285  
   286  	return strconv.Itoa(msg.Pompe), strconv.Itoa(msg.Electrolyse), strconv.Itoa(msg.Lampe), strconv.Itoa(msg.LampePortail), msg.TemperatureEau, msg.TemperatureAir, msg.TemperatureLocal, msg.DateTime, strconv.FormatInt(msg.DureeFiltration, 10)
   287  }