Markus Pesch
6d9368e86c
changes: - sensor temperature add - sensor temperature list - sensor temperature rm - temperature get - temperature log - temperature push
189 lines
3.8 KiB
Go
189 lines
3.8 KiB
Go
package remote
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"text/tabwriter"
|
|
|
|
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
|
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
|
)
|
|
|
|
func Add(remote *types.Remote, configDir string) error {
|
|
|
|
configuration, err := config.Read(configDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// search after duplicate remote_names
|
|
for _, r := range configuration.Remotes {
|
|
if r.Name == remote.Name {
|
|
return fmt.Errorf("Remote-Name %v already exists", remote.Name)
|
|
}
|
|
}
|
|
|
|
configuration.Remotes = append(configuration.Remotes, remote)
|
|
|
|
if err := config.Write(configuration, configDir); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func List(w io.Writer, configDir string) error {
|
|
|
|
configuration, err := config.Read(configDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
|
|
|
|
fmt.Fprint(tw, "name\taddress\tregistered\n")
|
|
|
|
for _, remote := range configuration.Remotes {
|
|
fmt.Fprintf(tw, "%v\t%v\t%v\n", remote.Name, remote.Address, remote.Registered)
|
|
}
|
|
|
|
tw.Flush()
|
|
|
|
return nil
|
|
}
|
|
|
|
func Remove(name string, configDir string) error {
|
|
|
|
con, err := config.Read(configDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var j int
|
|
for _, remote := range con.Remotes {
|
|
if remote.Name == name {
|
|
con.Remotes = append(con.Remotes[:j], con.Remotes[j+1:]...)
|
|
|
|
if j > 0 {
|
|
j = j - 1
|
|
}
|
|
continue
|
|
}
|
|
j++
|
|
}
|
|
|
|
if err := config.Write(con, configDir); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func RemoveAll(configDir string) error {
|
|
|
|
con, err := config.Read(configDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
con.Remotes = nil
|
|
|
|
if err := config.Write(con, configDir); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func SendTemperature(temperature *stypes.Temperature, configDir string) error {
|
|
|
|
con, err := config.Read(configDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var buffer bytes.Buffer
|
|
temperature.EncodeToJSON(&buffer)
|
|
|
|
for _, remote := range con.Remotes {
|
|
requestURL := fmt.Sprintf("%s%s", remote.Address, "/temperatures")
|
|
req, err := http.NewRequest("POST", requestURL, &buffer)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
client := http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d - can not read response body: %v", resp.StatusCode, err)
|
|
}
|
|
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d: %v", resp.StatusCode, string(b))
|
|
}
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// RegisterDevice ..
|
|
func RegisterDevice(configDir string, force bool) error {
|
|
con, err := config.Read(configDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
device := new(stypes.Device)
|
|
device.DeviceID = con.DeviceID
|
|
|
|
var buffer bytes.Buffer
|
|
device.EncodeToJSON(&buffer)
|
|
|
|
for _, remote := range con.Remotes {
|
|
if !remote.Registered || force {
|
|
requestURL := fmt.Sprintf("%s%s", remote.Address, "/devices")
|
|
req, err := http.NewRequest("POST", requestURL, &buffer)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
client := http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d - can not read response body: %v", resp.StatusCode, err)
|
|
}
|
|
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d: %v", resp.StatusCode, string(b))
|
|
}
|
|
remote.Registered = true
|
|
}
|
|
}
|
|
|
|
if err := config.Write(con, configDir); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func UnregisterDevice(configDir string) error {
|
|
|
|
return nil
|
|
}
|