fix: temperature, sensor
changes: - sensor temperature add - sensor temperature list - sensor temperature rm - temperature get - temperature log - temperature push
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"text/tabwriter"
|
||||
|
||||
@ -44,10 +45,10 @@ func List(w io.Writer, configDir string) error {
|
||||
|
||||
tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0)
|
||||
|
||||
fmt.Fprint(tw, "name\taddress\n")
|
||||
fmt.Fprint(tw, "name\taddress\tregistered\n")
|
||||
|
||||
for _, remote := range configuration.Remotes {
|
||||
fmt.Fprintf(tw, "%v\t%v\n", remote.Name, remote.Address)
|
||||
fmt.Fprintf(tw, "%v\t%v\t%v\n", remote.Name, remote.Address, remote.Registered)
|
||||
}
|
||||
|
||||
tw.Flush()
|
||||
@ -123,7 +124,11 @@ func SendTemperature(temperature *stypes.Temperature, configDir string) error {
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d", resp.StatusCode)
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
@ -131,8 +136,8 @@ func SendTemperature(temperature *stypes.Temperature, configDir string) error {
|
||||
|
||||
}
|
||||
|
||||
// SendDevice ..
|
||||
func SendDevice(configDir string) error {
|
||||
// RegisterDevice ..
|
||||
func RegisterDevice(configDir string, force bool) error {
|
||||
con, err := config.Read(configDir)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -145,23 +150,39 @@ func SendDevice(configDir string) error {
|
||||
device.EncodeToJSON(&buffer)
|
||||
|
||||
for _, remote := range con.Remotes {
|
||||
requestURL := fmt.Sprintf("%s%s", remote.Address, "/devices")
|
||||
req, err := http.NewRequest("POST", requestURL, &buffer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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()
|
||||
client := http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d", resp.StatusCode)
|
||||
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
|
||||
}
|
||||
|
Reference in New Issue
Block a user