This repository has been archived on 2025-02-01. You can view files and clone it, but cannot push or open issues or pull requests.
esp8266-temperature/server-go-ingress/server.go

147 lines
3.2 KiB
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
influxClient "github.com/influxdata/influxdb/client/v2"
)
type sensorData struct {
ID string `json: "id"`
Name string `json: "name,omitempty"`
Temp float32 `json: "temp"`
Pressure float32 `json: "pressure"`
}
const (
database = "sensors"
username = "sensor"
password = "dQ87dpjq7V7Y93U7"
)
func main() {
http.HandleFunc("/esp8266/sensors", func(w http.ResponseWriter, r *http.Request) { handleSensor(w, r) })
http.ListenAndServe(":3000", nil)
}
func handleSensor(rw http.ResponseWriter, req *http.Request) {
ts := time.Now()
c, err := influxClient.NewHTTPClient(influxClient.HTTPConfig{
Addr: "http://10.42.30.0:8086",
Username: username,
Password: password,
})
if err != nil {
log.Println("Error connecting to the InfluxDB:", err)
rw.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(rw, "Error connecting to the InfluxDB: %s", err)
return
}
defer c.Close()
decoder := json.NewDecoder(req.Body)
var data sensorData
err = decoder.Decode(&data)
if err != nil {
log.Println("Error in the sent data:", err)
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(rw, "Error in the sent data: %s", err)
return
}
defer req.Body.Close()
log.Printf("Got data: (%s) (%s) (%f) (%f)\n", data.ID, data.Name, data.Temp, data.Pressure)
data.Name = strings.TrimSpace(data.Name)
nameOverride := false
if len(data.Name) == 0 {
data.Name = getNodeName(data.ID)
nameOverride = true
}
bp, err := influxClient.NewBatchPoints(influxClient.BatchPointsConfig{
Database: database,
Precision: "ms",
})
if err != nil {
log.Println("Error connecting to the InfluxDB:", err)
rw.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(rw, "Error connecting to the InfluxDB: %s", err)
return
}
tags := map[string]string{
"node": data.ID,
"name": data.Name,
}
fieldsTemp := map[string]interface{}{
"value": data.Temp,
}
fieldsPressure := map[string]interface{}{
"value": data.Pressure,
}
pointTemp, err := influxClient.NewPoint(
"temperature",
tags,
fieldsTemp,
ts,
)
if err != nil {
log.Println("Error connecting to the InfluxDB:", err)
rw.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(rw, "Error connecting to the InfluxDB: %s", err)
return
}
pointPressure, err := influxClient.NewPoint(
"pressure",
tags,
fieldsPressure,
ts,
)
if err != nil {
log.Fatalln("Error: ", err)
}
bp.AddPoint(pointTemp)
bp.AddPoint(pointPressure)
err = c.Write(bp)
if err != nil {
log.Println("Error connecting to the InfluxDB:", err)
rw.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(rw, "Error connecting to the InfluxDB: %s", err)
return
}
nextTimeSlot := ts.Add(15 * time.Minute)
nextTimeSlot = nextTimeSlot.Round(15 * time.Minute)
remainingAlignment := nextTimeSlot.Unix() - ts.Unix()
rw.WriteHeader(http.StatusOK)
fmt.Fprintf(rw, "Thanks !\nSleep %d\n", remainingAlignment)
if nameOverride && len(data.Name) > 0 {
fmt.Fprintf(rw, "Name: %s\n", data.Name)
}
}
func getNodeName(nodeID string) string {
knownModules := make(map[string]string)
knownModules["EC:FA:BC:07:E4:31"] = "bureau"
return knownModules[nodeID]
}