fix: renamed packages

This commit is contained in:
Markus Pesch 2018-11-21 20:48:10 +01:00
parent 6d9368e86c
commit 1672663944
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
15 changed files with 206 additions and 121 deletions

View File

@ -10,7 +10,7 @@ import (
var addRemoteCmd = &cobra.Command{ var addRemoteCmd = &cobra.Command{
Use: "add", Use: "add",
Short: "add", Short: "Add Remove Server",
Args: cobra.ExactArgs(2), Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {

View File

@ -10,7 +10,7 @@ import (
var listRemoteCmd = &cobra.Command{ var listRemoteCmd = &cobra.Command{
Use: "list", Use: "list",
Short: "list", Short: "List Remove Servers",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if err := remote.List(os.Stdout, configDir); err != nil { if err := remote.List(os.Stdout, configDir); err != nil {

View File

@ -3,7 +3,7 @@ package remote
import ( import (
"log" "log"
"git.cryptic.systems/fh-trier/go-flucky/pkg/remote" "git.cryptic.systems/fh-trier/go-flucky/pkg/httpcall"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -14,7 +14,7 @@ var registerRemoteCmd = &cobra.Command{
Short: "register on remote servers", Short: "register on remote servers",
Run: func(cmd *cobra.Command, args []string) { 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) log.Fatal(err)
} }
}, },

View File

@ -8,7 +8,7 @@ var configDir string
var remoteCmd = &cobra.Command{ var remoteCmd = &cobra.Command{
Use: "remote", Use: "remote",
Short: "Manage Remote", Short: "Manage Remote Server",
} }
func InitCmd(cmd *cobra.Command, cnf string) { func InitCmd(cmd *cobra.Command, cnf string) {

View File

@ -9,9 +9,9 @@ import (
var all bool var all bool
var removeRemoteCmd = &cobra.Command{ var rmRemoteCmd = &cobra.Command{
Use: "remove", Use: "rm",
Short: "remove", Short: "Remove Remote Server",
Args: cobra.RangeArgs(0, 1), Args: cobra.RangeArgs(0, 1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -28,6 +28,6 @@ var removeRemoteCmd = &cobra.Command{
} }
func init() { func init() {
remoteCmd.AddCommand(removeRemoteCmd) remoteCmd.AddCommand(rmRemoteCmd)
removeRemoteCmd.Flags().BoolVarP(&all, "all", "a", false, "Select all remote connections") rmRemoteCmd.Flags().BoolVarP(&all, "all", "a", false, "Select all remote connections")
} }

View File

@ -1,24 +1,49 @@
package temperature package temperature
import ( import (
"fmt"
"log" "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" "github.com/spf13/cobra"
) )
var sensorType string
var addTemperatureSensorCmd = &cobra.Command{ var addTemperatureSensorCmd = &cobra.Command{
Use: "add", Use: "add",
Short: "Add Temperature Sensor", Short: "Add Temperature Sensor",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { 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) 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() { func init() {
temperatureSensorCmd.AddCommand(addTemperatureSensorCmd) 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)
} }

View File

@ -4,7 +4,7 @@ import (
"log" "log"
"os" "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" "github.com/spf13/cobra"
) )
@ -15,7 +15,7 @@ var listTemperatureSensorCmd = &cobra.Command{
Short: "List Temperature Sensors", Short: "List Temperature Sensors",
Run: func(cmd *cobra.Command, args []string) { 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) log.Fatal(err)
} }
}, },

View File

@ -3,7 +3,7 @@ package temperature
import ( import (
"log" "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" "github.com/spf13/cobra"
) )
@ -13,7 +13,7 @@ var rmTemperatureSensorCmd = &cobra.Command{
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { 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) log.Fatal(err)
} }
}, },

63
pkg/httpcall/remote.go Normal file
View File

@ -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
}

View File

@ -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
}

View File

@ -1,14 +1,10 @@
package remote package remote
import ( import (
"bytes"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http"
"text/tabwriter" "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/config"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types" "git.cryptic.systems/fh-trier/go-flucky/pkg/types"
) )
@ -98,91 +94,3 @@ func RemoveAll(configDir string) error {
return nil 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
}

View File

@ -3,12 +3,26 @@ package sensor
import ( import (
"os" "os"
"path/filepath" "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) sensorPath := filepath.Join(wirePath, sensorID)
if _, err := os.Stat(sensorPath); os.IsNotExist(err) { if _, err := os.Stat(sensorPath); os.IsNotExist(err) {
return false return false
} }
return true 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
}

View File

@ -1,4 +1,4 @@
package sensor package temperature
import ( import (
"fmt" "fmt"
@ -7,10 +7,12 @@ import (
"text/tabwriter" "text/tabwriter"
"git.cryptic.systems/fh-trier/go-flucky/pkg/config" "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" "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 // read cnf file
cnf, err := config.Read(configDir) cnf, err := config.Read(configDir)
@ -19,13 +21,19 @@ func AddTemperature(sensorID, sensorName, configDir, wirePath string) error {
} }
// check if sensor exists // 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)) 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{ temperatureSensor := &types.WireSensor{
ID: sensorID, ID: sensorID,
Name: sensorName, Name: sensorName,
Typ: types.SENSOR_DS18B20,
WirePath: wirePath, WirePath: wirePath,
} }
@ -40,7 +48,8 @@ func AddTemperature(sensorID, sensorName, configDir, wirePath string) error {
return nil 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 // read cnf file
cnf, err := config.Read(configDir) cnf, err := config.Read(configDir)
@ -53,14 +62,14 @@ func PrintTemperature(w io.Writer, configDir string, quiet bool) error {
// headline // headline
if !quiet { if !quiet {
fmt.Fprint(tw, "id\tname\tpath\n") fmt.Fprint(tw, "id\tname\ttype\tpath\n")
} }
for _, sensor := range cnf.TemperatureSensors { for _, sensor := range cnf.TemperatureSensors {
if quiet { if quiet {
fmt.Fprintf(tw, "%v\n", sensor.ID) fmt.Fprintf(tw, "%v\n", sensor.ID)
} else { } 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 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 // read cnf file
cnf, err := config.Read(configDir) cnf, err := config.Read(configDir)

View File

@ -11,7 +11,7 @@ import (
"time" "time"
stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types" 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/types"
"git.cryptic.systems/fh-trier/go-flucky/pkg/config" "git.cryptic.systems/fh-trier/go-flucky/pkg/config"
@ -96,12 +96,16 @@ func Logs(w io.Writer, configDir string) error {
// Push ... // Push ...
func Push(sensorName, configDir string) error { func Push(sensorName, configDir string) error {
var temperatures []*stypes.Temperature
temperature, err := getTemperature(sensorName, configDir) temperature, err := getTemperature(sensorName, configDir)
if err != nil { if err != nil {
return err return err
} }
if err := remote.SendTemperature(&temperature, configDir); err != nil { temperatures = append(temperatures, &temperature)
if err := httpcall.SendTemperatures(temperatures, configDir); err != nil {
return err return err
} }

View File

@ -50,7 +50,16 @@ type Remote struct {
} }
type WireSensor struct { type WireSensor struct {
ID string `json:"sensor_id"` ID string `json:"sensor_id"`
Name string `json:"sensor_name"` Name string `json:"sensor_name"`
WirePath string `json:"wire_path"` 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"
)