refactor: temperature
This commit is contained in:
14
cmd/cmd.go
14
cmd/cmd.go
@ -3,9 +3,12 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky-server/pkg/types"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/cmd/remote"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/cmd/sensor"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/cmd/temperature"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
@ -28,8 +31,9 @@ var rootCmd = &cobra.Command{
|
||||
|
||||
fc := config.FluckyConfig{
|
||||
Device: &types.Device{
|
||||
DeviceID: uuid.NewV4().String(),
|
||||
DeviceName: &hostname,
|
||||
DeviceID: uuid.NewV4().String(),
|
||||
DeviceName: &hostname,
|
||||
CreationDate: time.Now(),
|
||||
},
|
||||
}
|
||||
|
||||
@ -51,8 +55,8 @@ func Execute(version string) {
|
||||
|
||||
// humidity.InitCmd(rootCmd, configDir)
|
||||
remote.InitCmd(rootCmd, cfg)
|
||||
// sensor.InitCmd(rootCmd, cfg)
|
||||
// temperature.InitCmd(rootCmd, configDir)
|
||||
sensor.InitCmd(rootCmd, cfg)
|
||||
temperature.InitCmd(rootCmd, cfg)
|
||||
|
||||
rootCmd.Execute()
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ var addRemoteCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add Remote Server",
|
||||
Args: cobra.ExactArgs(2),
|
||||
Aliases: []string{"append"},
|
||||
Example: "flucky remote add origin https://example.local",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
|
@ -2,24 +2,58 @@ package sensor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var enabled bool
|
||||
var sensorLocation, wireID, wirePath string
|
||||
var location, wireID, wirePath string
|
||||
|
||||
var addSensorCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Short: "Add Sensor",
|
||||
Aliases: []string{"append"},
|
||||
Args: cobra.ExactArgs(3),
|
||||
Example: fmt.Sprintf("flucky sensor add indoor dht11 14\nflucky sensor add --wire-id 28-011432f0bb3d outdoor ds18b20 14"),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
fc, err := config.Read(cfg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// create new sensor struct
|
||||
sensor := &types.Sensor{
|
||||
SensorName: args[0],
|
||||
SensorType: args[1],
|
||||
SensorLocation: location,
|
||||
SensorEnabled: enabled,
|
||||
GPIONumber: &args[2],
|
||||
WireID: &wireID,
|
||||
}
|
||||
|
||||
// // add sensor entry to list
|
||||
err = fc.AddSensor(sensor)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(fc, cfg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(addSensorCmd)
|
||||
|
||||
addSensorCmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "Enable Sensor")
|
||||
addSensorCmd.Flags().StringVarP(&sensorLocation, "sensor-location", "l", "", "Sensor location")
|
||||
addSensorCmd.Flags().StringVarP(&location, "location", "l", "", "Sensor location")
|
||||
addSensorCmd.Flags().StringVarP(&wireID, "wire-id", "i", "", "Wire-ID")
|
||||
addSensorCmd.Flags().StringVarP(&wirePath, "wire-path", "w", "/sys/bus/w1/devices", "Wire device path")
|
||||
}
|
||||
|
@ -1,24 +1,40 @@
|
||||
package sensor
|
||||
|
||||
// import (
|
||||
// "log"
|
||||
import (
|
||||
"log"
|
||||
|
||||
// "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
|
||||
// "github.com/spf13/cobra"
|
||||
// )
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// var disableSensorCmd = &cobra.Command{
|
||||
// Use: "disable",
|
||||
// Short: "Disable Sensor",
|
||||
// Args: cobra.ExactArgs(1),
|
||||
// Run: func(cmd *cobra.Command, args []string) {
|
||||
var disableSensorCmd = &cobra.Command{
|
||||
Use: "disable",
|
||||
Short: "Disable Sensor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Example: "flucky sensor disable outdoor",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// if err := sensor.Disable(args[0], configDir); err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// },
|
||||
// }
|
||||
// read configuration
|
||||
fc, err := config.Read(cfg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// func init() {
|
||||
// sensorCmd.AddCommand(disableSensorCmd)
|
||||
// }
|
||||
// disable sensor entry to list
|
||||
err = fc.DisableSensor(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(fc, cfg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(disableSensorCmd)
|
||||
}
|
||||
|
@ -1,24 +1,39 @@
|
||||
package sensor
|
||||
|
||||
// import (
|
||||
// "log"
|
||||
import (
|
||||
"log"
|
||||
|
||||
// "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
|
||||
// "github.com/spf13/cobra"
|
||||
// )
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// var enableSensorCmd = &cobra.Command{
|
||||
// Use: "enable",
|
||||
// Short: "Enable Sensor",
|
||||
// Args: cobra.ExactArgs(1),
|
||||
// Run: func(cmd *cobra.Command, args []string) {
|
||||
var enableSensorCmd = &cobra.Command{
|
||||
Use: "enable",
|
||||
Short: "Enable Sensor",
|
||||
Example: "flucky sensor enable outdoor",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// if err := sensor.Enable(args[0], configDir); err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// },
|
||||
// }
|
||||
// read configuration
|
||||
fc, err := config.Read(cfg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// func init() {
|
||||
// sensorCmd.AddCommand(enableSensorCmd)
|
||||
// }
|
||||
// disable sensor entry to list
|
||||
err = fc.EnableSensor(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(fc, cfg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(enableSensorCmd)
|
||||
}
|
||||
|
@ -1,29 +1,38 @@
|
||||
package sensor
|
||||
|
||||
// import (
|
||||
// "log"
|
||||
// "os"
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
// "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
|
||||
// "github.com/spf13/cobra"
|
||||
// )
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// var quiet bool
|
||||
var listSensorCmd = &cobra.Command{
|
||||
Use: "ls",
|
||||
Short: "List Sensors",
|
||||
Aliases: []string{"list"},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
fc, err := config.Read(cfg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// var listSensorCmd = &cobra.Command{
|
||||
// Use: "ls",
|
||||
// Short: "List Sensors",
|
||||
// Aliases: []string{"list"},
|
||||
// Run: func(cmd *cobra.Command, args []string) {
|
||||
// if err := sensor.Print(os.Stdout, configDir, quiet); err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// },
|
||||
// //28-01143277168e
|
||||
// }
|
||||
// print sensors on stdout
|
||||
err = fc.PrintSensors(os.Stdout)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// func init() {
|
||||
// sensorCmd.AddCommand(listSensorCmd)
|
||||
// save new configuration
|
||||
err = config.Write(fc, cfg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// listSensorCmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "List only sensor id's")
|
||||
// }
|
||||
func init() {
|
||||
sensorCmd.AddCommand(listSensorCmd)
|
||||
}
|
||||
|
@ -1,24 +1,39 @@
|
||||
package sensor
|
||||
|
||||
// import (
|
||||
// "log"
|
||||
import (
|
||||
"log"
|
||||
|
||||
// "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
|
||||
// "github.com/spf13/cobra"
|
||||
// )
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// var rmSensorCmd = &cobra.Command{
|
||||
// Use: "rm",
|
||||
// Short: "Remove Sensor",
|
||||
// Aliases: []string{"remove"},
|
||||
// Args: cobra.ExactArgs(1),
|
||||
// Run: func(cmd *cobra.Command, args []string) {
|
||||
// if err := sensor.Remove(args[0], configDir); err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// },
|
||||
// }
|
||||
var rmSensorCmd = &cobra.Command{
|
||||
Use: "rm",
|
||||
Short: "Remove Sensor",
|
||||
Example: "flucky sensor rm outdoor",
|
||||
Aliases: []string{"remove"},
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// read configuration
|
||||
fc, err := config.Read(cfg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// func init() {
|
||||
// sensorCmd.AddCommand(rmSensorCmd)
|
||||
// }
|
||||
// // add remote entry to list
|
||||
err = fc.RemoveSensor(args[0])
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// save new configuration
|
||||
err = config.Write(fc, cfg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
sensorCmd.AddCommand(rmSensorCmd)
|
||||
}
|
||||
|
@ -1,34 +0,0 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/temperature"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var follow, push bool
|
||||
|
||||
var getTemperatureCmd = &cobra.Command{
|
||||
Use: "get",
|
||||
Short: "get temperature from sensor",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if follow {
|
||||
if err := temperature.Follow(args, push, configDir, os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
if err := temperature.Get(args, push, configDir, os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
temperatureCmd.AddCommand(getTemperatureCmd)
|
||||
getTemperatureCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow output")
|
||||
getTemperatureCmd.Flags().BoolVarP(&push, "push", "p", false, "Push to remote server")
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/logs"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var writeLog bool
|
||||
|
||||
var logTemperatureCmd = &cobra.Command{
|
||||
Use: "log",
|
||||
Short: "Log print all temperatures saved from logfile on stdout",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if err := logs.ListTemperatures(configDir, os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
temperatureCmd.AddCommand(logTemperatureCmd)
|
||||
}
|
@ -1,23 +1,22 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"log"
|
||||
// import (
|
||||
// "log"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/temperature"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
// "github.com/spf13/cobra"
|
||||
// )
|
||||
|
||||
var pushTemperatureCmd = &cobra.Command{
|
||||
Use: "push",
|
||||
Short: "push temperature from sensor to remote servers",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// var pushTemperatureCmd = &cobra.Command{
|
||||
// Use: "push",
|
||||
// Short: "push temperature from sensor to remote servers",
|
||||
// Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if err := temperature.Push(configDir); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
// if err := temperature.Push(configDir); err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// },
|
||||
// }
|
||||
|
||||
func init() {
|
||||
temperatureCmd.AddCommand(pushTemperatureCmd)
|
||||
}
|
||||
// func init() {
|
||||
// temperatureCmd.AddCommand(pushTemperatureCmd)
|
||||
// }
|
||||
|
41
cmd/temperature/read.go
Normal file
41
cmd/temperature/read.go
Normal file
@ -0,0 +1,41 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/cli"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/config"
|
||||
"git.cryptic.systems/fh-trier/go-flucky/pkg/sensor"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var follow, push bool
|
||||
|
||||
var readTemperatureCmd = &cobra.Command{
|
||||
Use: "read",
|
||||
Short: "read temperature from sensor",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
// read configuration
|
||||
fc, err := config.Read(cfg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// FIXME:
|
||||
// add sensor entry to list
|
||||
temperatures, err := sensor.ReadTemperatures(fc.GetTemperatureSensors())
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
cli.PrintTemperatures(temperatures, fc, os.Stdout)
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
temperatureCmd.AddCommand(readTemperatureCmd)
|
||||
readTemperatureCmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow output")
|
||||
}
|
@ -1,19 +1,22 @@
|
||||
package temperature
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var configDir string
|
||||
var cfg string
|
||||
|
||||
var temperatureCmd = &cobra.Command{
|
||||
Use: "temperature",
|
||||
Short: "Read temperature from sensor",
|
||||
Use: "temperature",
|
||||
Short: "",
|
||||
Example: fmt.Sprintf("flucky temperature read\nflucky temperature read outdoor"),
|
||||
}
|
||||
|
||||
// Execute a
|
||||
func InitCmd(cmd *cobra.Command, cnf string) {
|
||||
configDir = cnf
|
||||
func InitCmd(cmd *cobra.Command, config string) {
|
||||
cfg = config
|
||||
|
||||
cmd.AddCommand(temperatureCmd)
|
||||
|
||||
|
Reference in New Issue
Block a user