PKGBUILD/cmd/sensor/add.go

72 lines
1.7 KiB
Go

package sensor
import (
"fmt"
"log"
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/types"
"github.com/spf13/cobra"
)
var enabled bool
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 GPIO14\nflucky sensor add --wire-id 28-011432f0bb3d outdoor DS18B20 GPIO14"),
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(configFile)
if err != nil {
log.Fatalln(err)
}
// determine sensor model
sensorModel, err := types.SelectSensorModel(args[1])
if err != nil {
log.Fatalln(err)
}
// determine gpio port
gpio, err := types.StringToGPIO(args[2])
if err != nil {
log.Fatalln(err)
}
// create new sensor struct
sensor := &types.Sensor{
SensorName: args[0],
SensorModel: sensorModel,
SensorLocation: location,
SensorEnabled: enabled,
GPIONumber: &gpio,
WireID: &wireID,
}
// // add sensor entry to list
err = cnf.AddSensor(sensor)
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(cnf, configFile)
if err != nil {
log.Fatalln(err)
}
},
}
func init() {
sensorCmd.AddCommand(addSensorCmd)
addSensorCmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "Enable Sensor")
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")
}