fix: configuration pkg

This commit is contained in:
2019-02-17 18:23:59 +01:00
parent 60fa83244e
commit c437127531
18 changed files with 596 additions and 420 deletions

View File

@ -1,58 +0,0 @@
package types
import (
"bytes"
"encoding/json"
"fmt"
"io"
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
)
type Config struct {
DeviceID string `json:"device_id"`
DeviceName string `json:"device_name"`
DeviceLocation string `json:"device_location"`
TemperatureLogfile string `json:"temperature_logfile"`
HumidityLogfile string `json:"humiditiy_logfile"`
Remotes []*Remote `json:"remotes"`
Sensors []*stypes.Sensor `json:"sensors"`
}
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
}
// Remote ...
type Remote struct {
Name string `json:"remote_name"`
Address string `json:"remote_address"`
Registered bool `json:"remote_registered"`
Enabled bool `json:"remote_enabled"`
}