fix: set config, set remote
This commit is contained in:
99
pkg/config/config.go
Normal file
99
pkg/config/config.go
Normal file
@ -0,0 +1,99 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/satori/go.uuid"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
||||
)
|
||||
|
||||
var configFilename = "config.json"
|
||||
|
||||
func Create(configDir string, force bool) error {
|
||||
|
||||
configPath := filepath.Join(configDir, configFilename)
|
||||
|
||||
config := &types.Config{
|
||||
DeviceID: uuid.NewV4().String(),
|
||||
}
|
||||
|
||||
// If no config file exists, create a new one on the location
|
||||
|
||||
if !force {
|
||||
if _, err := os.Stat(configPath); !os.IsNotExist(err) {
|
||||
return fmt.Errorf("%v already exists. Use -f to overwrite", configPath)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(configDir, os.ModePerm)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not create directory: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
f, err := os.Create(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not create config: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
config.JSONWriter(f)
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func Read(configDir string) (*types.Config, error) {
|
||||
|
||||
configPath := filepath.Join(configDir, configFilename)
|
||||
|
||||
var config types.Config
|
||||
|
||||
// If no config file exists, create a new one on the location
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
return nil, fmt.Errorf("Can not find config %v: %v", configPath, err)
|
||||
}
|
||||
|
||||
// open config file
|
||||
jsonFile, err := os.Open(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Can not open file %v: %v", configPath, err)
|
||||
}
|
||||
defer jsonFile.Close()
|
||||
|
||||
jsonParser := json.NewDecoder(jsonFile)
|
||||
if err := jsonParser.Decode(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func Write(config *types.Config, configDir string) error {
|
||||
|
||||
configPath := filepath.Join(configDir, configFilename)
|
||||
|
||||
// If no config file exists, create a new one on the location
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
return fmt.Errorf("Can not find config %v: %v", configPath, err)
|
||||
}
|
||||
|
||||
// open config file
|
||||
jsonFile, err := os.Create(configPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not open file %v: %v", configPath, err)
|
||||
}
|
||||
defer jsonFile.Close()
|
||||
|
||||
err = config.JSONWriter(jsonFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
167
pkg/remote/remote.go
Normal file
167
pkg/remote/remote.go
Normal file
@ -0,0 +1,167 @@
|
||||
package remote
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"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\n")
|
||||
|
||||
for _, remote := range configuration.Remotes {
|
||||
fmt.Fprintf(tw, "%v\t%v\n", remote.Name, remote.Address)
|
||||
}
|
||||
|
||||
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 {
|
||||
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// SendDevice ..
|
||||
func SendDevice(configDir string) 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 {
|
||||
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 {
|
||||
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,50 +1,10 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||||
)
|
||||
|
||||
func Print(w io.Writer) error {
|
||||
|
||||
t := types.Temperature{
|
||||
TemperatureID: "1",
|
||||
TemperatureValue: "22.5",
|
||||
TemperatureDate: "2018-10-15T23:31:28.132417Z",
|
||||
DeviceID: "510abdfb-3d0d-4248-b0e5-94ee962c2532",
|
||||
}
|
||||
|
||||
err := postTemperature(&t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func postTemperature(temperature *types.Temperature) error {
|
||||
var buffer bytes.Buffer
|
||||
temperature.EncodeToJSON(&buffer)
|
||||
|
||||
requestURL := fmt.Sprintf("%s://%s%s", "https", "flucky.cryptic.systems", "/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 {
|
||||
return fmt.Errorf("Invalid HTTP-Statuscode - expected 200, got %d", resp.StatusCode)
|
||||
}
|
||||
func Get(w io.Writer) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
48
pkg/types/types.go
Normal file
48
pkg/types/types.go
Normal file
@ -0,0 +1,48 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DeviceID string `json:"device_id"`
|
||||
Remotes []*Remote `json:"remotes"`
|
||||
}
|
||||
|
||||
func (c *Config) ToJSON() (string, error) {
|
||||
var b bytes.Buffer
|
||||
err := c.JSONWriter(&b)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
// JSONWriter needs a writer to write the struct into json string
|
||||
func (c *Config) JSONWriter(w io.Writer) error {
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.SetIndent("", " ")
|
||||
err := encoder.Encode(&c)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error in encoding struct to json: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// JSONDecoder decode a json string from a reader into a struct
|
||||
func (c *Config) JSONDecoder(r io.Reader) error {
|
||||
jsonDecoder := json.NewDecoder(r)
|
||||
if err := jsonDecoder.Decode(&c); err != nil {
|
||||
return fmt.Errorf("Can not unmarshal JSON: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Remote struct {
|
||||
Name string `json:"remote_name"`
|
||||
Address string `json:"remote_address"`
|
||||
}
|
Reference in New Issue
Block a user