fix: renamed packages
This commit is contained in:
63
pkg/httpcall/remote.go
Normal file
63
pkg/httpcall/remote.go
Normal 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
|
||||
}
|
52
pkg/httpcall/temperature.go
Normal file
52
pkg/httpcall/temperature.go
Normal 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
|
||||
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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"
|
||||
)
|
||||
|
Reference in New Issue
Block a user