PKGBUILD/cmd/sensor/add.go

72 lines
1.7 KiB
Go
Raw Normal View History

2018-11-28 17:07:20 +00:00
package sensor
import (
2019-02-22 12:08:58 +00:00
"fmt"
2019-02-24 21:46:36 +00:00
"log"
2019-02-22 12:08:58 +00:00
2019-06-15 13:58:41 +00:00
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/types"
2018-11-28 17:07:20 +00:00
"github.com/spf13/cobra"
)
var enabled bool
2019-02-24 21:46:36 +00:00
var location, wireID, wirePath string
2018-11-28 17:07:20 +00:00
var addSensorCmd = &cobra.Command{
2019-02-22 12:08:58 +00:00
Use: "add",
Short: "Add Sensor",
2019-02-24 21:46:36 +00:00
Aliases: []string{"append"},
2019-02-22 12:08:58 +00:00
Args: cobra.ExactArgs(3),
2019-03-04 09:55:11 +00:00
Example: fmt.Sprintf("flucky sensor add indoor DHT11 GPIO14\nflucky sensor add --wire-id 28-011432f0bb3d outdoor DS18B20 GPIO14"),
2019-02-24 21:46:36 +00:00
Run: func(cmd *cobra.Command, args []string) {
// read configuration
cnf, err := config.Read(configFile)
2019-02-24 21:46:36 +00:00
if err != nil {
log.Fatalln(err)
}
2019-02-28 18:48:25 +00:00
// determine sensor model
sensorModel, err := types.SelectSensorModel(args[1])
if err != nil {
log.Fatalln(err)
}
2019-03-03 18:13:37 +00:00
// determine gpio port
gpio, err := types.StringToGPIO(args[2])
if err != nil {
log.Fatalln(err)
}
2019-02-24 21:46:36 +00:00
// create new sensor struct
sensor := &types.Sensor{
SensorName: args[0],
2019-02-28 18:48:25 +00:00
SensorModel: sensorModel,
2019-02-24 21:46:36 +00:00
SensorLocation: location,
SensorEnabled: enabled,
2019-03-03 18:13:37 +00:00
GPIONumber: &gpio,
2019-02-24 21:46:36 +00:00
WireID: &wireID,
}
// // add sensor entry to list
err = cnf.AddSensor(sensor)
2019-02-24 21:46:36 +00:00
if err != nil {
log.Fatalln(err)
}
// save new configuration
err = config.Write(cnf, configFile)
2019-02-24 21:46:36 +00:00
if err != nil {
log.Fatalln(err)
}
},
2018-11-28 17:07:20 +00:00
}
func init() {
sensorCmd.AddCommand(addSensorCmd)
2019-02-24 21:46:36 +00:00
2018-11-28 17:07:20 +00:00
addSensorCmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "Enable Sensor")
2019-02-24 21:46:36 +00:00
addSensorCmd.Flags().StringVarP(&location, "location", "l", "", "Sensor location")
2018-11-28 17:07:20 +00:00
addSensorCmd.Flags().StringVarP(&wireID, "wire-id", "i", "", "Wire-ID")
addSensorCmd.Flags().StringVarP(&wirePath, "wire-path", "w", "/sys/bus/w1/devices", "Wire device path")
}