From 167266394494f5a6f10b95bd1604d50066116cf2 Mon Sep 17 00:00:00 2001 From: Markus Pesch Date: Wed, 21 Nov 2018 20:48:10 +0100 Subject: [PATCH] fix: renamed packages --- cmd/remote/add.go | 2 +- cmd/remote/list.go | 2 +- cmd/remote/register.go | 4 +- cmd/remote/remote.go | 2 +- cmd/remote/remove.go | 10 +-- cmd/sensor/temperature/add.go | 29 ++++++- cmd/sensor/temperature/list.go | 4 +- cmd/sensor/temperature/rm.go | 4 +- pkg/httpcall/remote.go | 63 ++++++++++++++ pkg/httpcall/temperature.go | 52 ++++++++++++ pkg/remote/remote.go | 92 --------------------- pkg/sensor/sensor.go | 16 +++- pkg/sensor/{ => temperature}/temperature.go | 24 ++++-- pkg/temperature/temperature.go | 8 +- pkg/types/types.go | 15 +++- 15 files changed, 206 insertions(+), 121 deletions(-) create mode 100644 pkg/httpcall/remote.go create mode 100644 pkg/httpcall/temperature.go rename pkg/sensor/{ => temperature}/temperature.go (64%) diff --git a/cmd/remote/add.go b/cmd/remote/add.go index ae2bd6f..4dcef33 100644 --- a/cmd/remote/add.go +++ b/cmd/remote/add.go @@ -10,7 +10,7 @@ import ( var addRemoteCmd = &cobra.Command{ Use: "add", - Short: "add", + Short: "Add Remove Server", Args: cobra.ExactArgs(2), Run: func(cmd *cobra.Command, args []string) { diff --git a/cmd/remote/list.go b/cmd/remote/list.go index f7ee3f9..bebf40f 100644 --- a/cmd/remote/list.go +++ b/cmd/remote/list.go @@ -10,7 +10,7 @@ import ( var listRemoteCmd = &cobra.Command{ Use: "list", - Short: "list", + Short: "List Remove Servers", Run: func(cmd *cobra.Command, args []string) { if err := remote.List(os.Stdout, configDir); err != nil { diff --git a/cmd/remote/register.go b/cmd/remote/register.go index ddbb5be..16a6e47 100644 --- a/cmd/remote/register.go +++ b/cmd/remote/register.go @@ -3,7 +3,7 @@ package remote import ( "log" - "git.cryptic.systems/fh-trier/go-flucky/pkg/remote" + "git.cryptic.systems/fh-trier/go-flucky/pkg/httpcall" "github.com/spf13/cobra" ) @@ -14,7 +14,7 @@ var registerRemoteCmd = &cobra.Command{ Short: "register on remote servers", Run: func(cmd *cobra.Command, args []string) { - if err := remote.RegisterDevice(configDir, force); err != nil { + if err := httpcall.RegisterDevice(configDir, force); err != nil { log.Fatal(err) } }, diff --git a/cmd/remote/remote.go b/cmd/remote/remote.go index fb9c219..311725c 100644 --- a/cmd/remote/remote.go +++ b/cmd/remote/remote.go @@ -8,7 +8,7 @@ var configDir string var remoteCmd = &cobra.Command{ Use: "remote", - Short: "Manage Remote", + Short: "Manage Remote Server", } func InitCmd(cmd *cobra.Command, cnf string) { diff --git a/cmd/remote/remove.go b/cmd/remote/remove.go index 25b013b..a3e16ad 100644 --- a/cmd/remote/remove.go +++ b/cmd/remote/remove.go @@ -9,9 +9,9 @@ import ( var all bool -var removeRemoteCmd = &cobra.Command{ - Use: "remove", - Short: "remove", +var rmRemoteCmd = &cobra.Command{ + Use: "rm", + Short: "Remove Remote Server", Args: cobra.RangeArgs(0, 1), Run: func(cmd *cobra.Command, args []string) { @@ -28,6 +28,6 @@ var removeRemoteCmd = &cobra.Command{ } func init() { - remoteCmd.AddCommand(removeRemoteCmd) - removeRemoteCmd.Flags().BoolVarP(&all, "all", "a", false, "Select all remote connections") + remoteCmd.AddCommand(rmRemoteCmd) + rmRemoteCmd.Flags().BoolVarP(&all, "all", "a", false, "Select all remote connections") } diff --git a/cmd/sensor/temperature/add.go b/cmd/sensor/temperature/add.go index 3f10979..1014dc6 100644 --- a/cmd/sensor/temperature/add.go +++ b/cmd/sensor/temperature/add.go @@ -1,24 +1,49 @@ package temperature import ( + "fmt" "log" - "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor" + temperatureSensor "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor/temperature" + "git.cryptic.systems/fh-trier/go-flucky/pkg/types" "github.com/spf13/cobra" ) +var sensorType string + var addTemperatureSensorCmd = &cobra.Command{ Use: "add", Short: "Add Temperature Sensor", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - if err := sensor.AddTemperature(args[0], sensorName, configDir, wirePath); err != nil { + t, err := matchSensorTyp(sensorType) + if err != nil { log.Fatal(err) } + + // DS18B20 + if t == types.SENSOR_DS18B20 { + if err := temperatureSensor.AddDS18B20(args[0], sensorName, configDir, wirePath); err != nil { + log.Fatal(err) + } + } }, } func init() { temperatureSensorCmd.AddCommand(addTemperatureSensorCmd) + addTemperatureSensorCmd.Flags().StringVarP(&sensorType, "type", "t", "DS18B20", "Sensor Types: DS18B20, DHT11") +} + +func matchSensorTyp(sensorType string) (types.SensorType, error) { + + switch sensorType { + case "DS18B20": + return types.SENSOR_DS18B20, nil + case "DHT11": + return types.SENSOR_DHT11, nil + } + + return "", fmt.Errorf("Can not match %v with a sensor type", sensorType) } diff --git a/cmd/sensor/temperature/list.go b/cmd/sensor/temperature/list.go index 7aa5449..cef410d 100644 --- a/cmd/sensor/temperature/list.go +++ b/cmd/sensor/temperature/list.go @@ -4,7 +4,7 @@ import ( "log" "os" - "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor" + temperatureSensor "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor/temperature" "github.com/spf13/cobra" ) @@ -15,7 +15,7 @@ var listTemperatureSensorCmd = &cobra.Command{ Short: "List Temperature Sensors", Run: func(cmd *cobra.Command, args []string) { - if err := sensor.PrintTemperature(os.Stdout, configDir, quiet); err != nil { + if err := temperatureSensor.Print(os.Stdout, configDir, quiet); err != nil { log.Fatal(err) } }, diff --git a/cmd/sensor/temperature/rm.go b/cmd/sensor/temperature/rm.go index 36d516a..73cd918 100644 --- a/cmd/sensor/temperature/rm.go +++ b/cmd/sensor/temperature/rm.go @@ -3,7 +3,7 @@ package temperature import ( "log" - "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor" + temperatureSensor "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor/temperature" "github.com/spf13/cobra" ) @@ -13,7 +13,7 @@ var rmTemperatureSensorCmd = &cobra.Command{ Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - if err := sensor.RemoveTemperature(args[0], configDir); err != nil { + if err := temperatureSensor.Remove(args[0], configDir); err != nil { log.Fatal(err) } }, diff --git a/pkg/httpcall/remote.go b/pkg/httpcall/remote.go new file mode 100644 index 0000000..341bcea --- /dev/null +++ b/pkg/httpcall/remote.go @@ -0,0 +1,63 @@ +package httpcall + +import ( + "bytes" + "fmt" + "io/ioutil" + "net/http" + + stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types" + "git.cryptic.systems/fh-trier/go-flucky/pkg/config" +) + +// 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 +} + +// UnregisterDevice ... +func UnregisterDevice(configDir string) error { + + return nil +} diff --git a/pkg/httpcall/temperature.go b/pkg/httpcall/temperature.go new file mode 100644 index 0000000..439ecca --- /dev/null +++ b/pkg/httpcall/temperature.go @@ -0,0 +1,52 @@ +package httpcall + +import ( + "bytes" + "fmt" + "io/ioutil" + "net/http" + + "git.cryptic.systems/fh-trier/go-flucky/pkg/config" + + stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types" +) + +// SendTemperature remote servers +func SendTemperatures(temperatures []*stypes.Temperature, configDir string) error { + + con, err := config.Read(configDir) + if err != nil { + return err + } + + var buffer bytes.Buffer + for _, temperature := range temperatures { + 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 + +} diff --git a/pkg/remote/remote.go b/pkg/remote/remote.go index 1b9b883..f18993c 100644 --- a/pkg/remote/remote.go +++ b/pkg/remote/remote.go @@ -1,14 +1,10 @@ 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" ) @@ -98,91 +94,3 @@ func RemoveAll(configDir string) error { 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 -} diff --git a/pkg/sensor/sensor.go b/pkg/sensor/sensor.go index b1d4e00..5fd887d 100644 --- a/pkg/sensor/sensor.go +++ b/pkg/sensor/sensor.go @@ -3,12 +3,26 @@ package sensor import ( "os" "path/filepath" + + "git.cryptic.systems/fh-trier/go-flucky/pkg/types" ) -func sensorExists(wirePath, sensorID string) bool { +// Exists returns a boolean if the sensor exists +func Exists(wirePath, sensorID string) bool { sensorPath := filepath.Join(wirePath, sensorID) if _, err := os.Stat(sensorPath); os.IsNotExist(err) { return false } return true } + +// IsRedundant returns a boolean if the sensorID is in the array +func IsRedundant(sensorID string, sensors []*types.WireSensor) bool { + for _, sensor := range sensors { + if sensor.ID == sensorID { + return true + } + } + + return false +} diff --git a/pkg/sensor/temperature.go b/pkg/sensor/temperature/temperature.go similarity index 64% rename from pkg/sensor/temperature.go rename to pkg/sensor/temperature/temperature.go index ce4fb88..a9cbc23 100644 --- a/pkg/sensor/temperature.go +++ b/pkg/sensor/temperature/temperature.go @@ -1,4 +1,4 @@ -package sensor +package temperature import ( "fmt" @@ -7,10 +7,12 @@ import ( "text/tabwriter" "git.cryptic.systems/fh-trier/go-flucky/pkg/config" + "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor" "git.cryptic.systems/fh-trier/go-flucky/pkg/types" ) -func AddTemperature(sensorID, sensorName, configDir, wirePath string) error { +// AddDS18B20 a new temperature sensor from type ds18b20 +func AddDS18B20(sensorID, sensorName, configDir, wirePath string) error { // read cnf file cnf, err := config.Read(configDir) @@ -19,13 +21,19 @@ func AddTemperature(sensorID, sensorName, configDir, wirePath string) error { } // check if sensor exists - if !sensorExists(wirePath, sensorID) { + if !sensor.Exists(wirePath, sensorID) { return fmt.Errorf("Can not find sensor: %v", filepath.Join(wirePath, sensorID)) } + // check if sensor is redundant + if sensor.IsRedundant(sensorID, cnf.TemperatureSensors) { + return fmt.Errorf("Sensor %v already exists as temperature sensor", sensorID) + } + temperatureSensor := &types.WireSensor{ ID: sensorID, Name: sensorName, + Typ: types.SENSOR_DS18B20, WirePath: wirePath, } @@ -40,7 +48,8 @@ func AddTemperature(sensorID, sensorName, configDir, wirePath string) error { return nil } -func PrintTemperature(w io.Writer, configDir string, quiet bool) error { +// Print a list with the given writer w over all temperature sensors +func Print(w io.Writer, configDir string, quiet bool) error { // read cnf file cnf, err := config.Read(configDir) @@ -53,14 +62,14 @@ func PrintTemperature(w io.Writer, configDir string, quiet bool) error { // headline if !quiet { - fmt.Fprint(tw, "id\tname\tpath\n") + fmt.Fprint(tw, "id\tname\ttype\tpath\n") } for _, sensor := range cnf.TemperatureSensors { if quiet { fmt.Fprintf(tw, "%v\n", sensor.ID) } else { - fmt.Fprintf(tw, "%v\t%v\t%v\n", sensor.ID, sensor.Name, sensor.WirePath) + fmt.Fprintf(tw, "%v\t%v\t%v\t%v\n", sensor.ID, sensor.Name, sensor.Typ, sensor.WirePath) } } @@ -69,7 +78,8 @@ func PrintTemperature(w io.Writer, configDir string, quiet bool) error { return nil } -func RemoveTemperature(sensorID, configDir string) error { +// Remove a temperature sensor over the given sensor id +func Remove(sensorID, configDir string) error { // read cnf file cnf, err := config.Read(configDir) diff --git a/pkg/temperature/temperature.go b/pkg/temperature/temperature.go index 56db14b..f4bea89 100644 --- a/pkg/temperature/temperature.go +++ b/pkg/temperature/temperature.go @@ -11,7 +11,7 @@ import ( "time" stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types" - "git.cryptic.systems/fh-trier/go-flucky/pkg/remote" + "git.cryptic.systems/fh-trier/go-flucky/pkg/httpcall" "git.cryptic.systems/fh-trier/go-flucky/pkg/types" "git.cryptic.systems/fh-trier/go-flucky/pkg/config" @@ -96,12 +96,16 @@ func Logs(w io.Writer, configDir string) error { // Push ... func Push(sensorName, configDir string) error { + var temperatures []*stypes.Temperature + temperature, err := getTemperature(sensorName, configDir) if err != nil { return err } - if err := remote.SendTemperature(&temperature, configDir); err != nil { + temperatures = append(temperatures, &temperature) + + if err := httpcall.SendTemperatures(temperatures, configDir); err != nil { return err } diff --git a/pkg/types/types.go b/pkg/types/types.go index c3f4599..6e00c26 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -50,7 +50,16 @@ type Remote struct { } type WireSensor struct { - ID string `json:"sensor_id"` - Name string `json:"sensor_name"` - WirePath string `json:"wire_path"` + ID string `json:"sensor_id"` + Name string `json:"sensor_name"` + Typ SensorType `json:"sensor_typ"` + GPIO int `json:"gpio_number"` + WirePath string `json:"wire_path"` } + +type SensorType string + +const ( + SENSOR_DS18B20 SensorType = "DS18B20" + SENSOR_DHT11 = "DHT11" +)