PKGBUILD/pkg/httpcall/remote.go

115 lines
2.7 KiB
Go
Raw Normal View History

2018-11-21 19:48:10 +00:00
package httpcall
import (
"bytes"
"encoding/json"
2018-11-21 19:48:10 +00:00
"fmt"
"io/ioutil"
"log"
2018-11-21 19:48:10 +00:00
"net/http"
"time"
2018-11-21 19:48:10 +00:00
"git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
2018-11-21 19:48:10 +00:00
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
)
// SyncDevice ..
func SyncDevice(configDir string, force bool) error {
// read config
cnf, err := config.Read(configDir)
2018-11-21 19:48:10 +00:00
if err != nil {
return err
}
2019-02-17 17:23:59 +00:00
jsonBuffer := bytes.Buffer{}
// define array of devices
device := types.Device{
DeviceID: cnf.DeviceID,
DeviceLocation: &cnf.DeviceLocation,
DeviceName: &cnf.DeviceName,
DeviceLastContact: time.Now(),
CreationDate: time.Now(),
}
// encode to json
2019-02-17 17:23:59 +00:00
encoder := json.NewEncoder(&jsonBuffer)
if err != nil {
return err
}
2018-11-21 19:48:10 +00:00
2019-02-17 17:23:59 +00:00
if err = encoder.Encode(device); err != nil {
return fmt.Errorf("Can not encode device to json: %v", err)
}
log.Println(jsonBuffer.String())
2018-11-21 19:48:10 +00:00
// send array of devices to remote links
for _, remote := range cnf.Remotes {
2018-11-21 19:48:10 +00:00
if !remote.Registered || force {
requestURL := fmt.Sprintf("%v/devices/%v", remote.Address, cnf.DeviceID)
2019-02-17 17:23:59 +00:00
req, err := http.NewRequest("PUT", requestURL, &jsonBuffer)
2018-11-21 19:48:10 +00:00
if err != nil {
return err
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// if resource does not exxists, create a new one
if resp.StatusCode == 404 {
log.Println("test")
requestURL := fmt.Sprintf("%v/devices", remote.Address)
2019-02-17 17:23:59 +00:00
req, err := http.NewRequest("POST", requestURL, &jsonBuffer)
if err != nil {
return err
}
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("Can not create a new device on %v - Invalid HTTP-Statuscode, expected 200, got %d - Can not read response body: %v", remote.Name, resp.StatusCode, err)
}
return fmt.Errorf("Can not create a new device on %v - Invalid HTTP-Statuscode, expected 200, got %d: %v", remote.Name, resp.StatusCode, string(b))
}
}
2018-11-21 19:48:10 +00:00
if resp.StatusCode < 200 || resp.StatusCode > 299 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Can not update device on %v - Invalid HTTP-Statuscode, expected 200, got %d - Can not read response body: %v", remote.Name, resp.StatusCode, err)
2018-11-21 19:48:10 +00:00
}
return fmt.Errorf("Can not update device on %v - Invalid HTTP-Statuscode, expected 200, got %d: %v", remote.Name, resp.StatusCode, string(b))
2018-11-21 19:48:10 +00:00
}
2018-11-21 19:48:10 +00:00
remote.Registered = true
2018-11-21 19:48:10 +00:00
}
}
if err := config.Write(cnf, configDir); err != nil {
2018-11-21 19:48:10 +00:00
return err
}
return nil
}
// UnregisterDevice ...
func UnregisterDevice(configDir string) error {
return nil
}