Skip to content

Instantly share code, notes, and snippets.

@velotiotech

velotiotech/dao.go

Created Jun 11, 2020
Embed
What would you like to do?
package dao
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
)
type SensorDataFiltered struct {
Temperature float64 `json:"temperature"`
Humidity float64 `json:"humidity"`
City string `json:"city"`
}
var dbName = "iotdata"
var dbHost = "184.73.62.30"
var dbPort = "8086"
func (sdf *SensorDataFiltered) PostDataToInfluxDB(Data []byte) {
err := json.Unmarshal(Data, &sdf)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(sdf.Temperature, sdf.Humidity)
}
url := "http://" + dbHost + ":" + dbPort + "/write?db=" + dbName
humidity := fmt.Sprintf("%.2f", sdf.Humidity)
temperature := fmt.Sprintf("%.2f", sdf.Temperature)
city := sdf.City
requestBody := "sensordata,city=" + city + " humidity=" + humidity + ",temperature=" + temperature
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(requestBody)))
httpclient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
resp, err := httpclient.Do(req)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Status code for influxdb data port request = ", resp.StatusCode)
}
defer resp.Body.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.