PKGBUILD/cmd/sensor/temperature/add.go

50 lines
1.1 KiB
Go

package temperature
import (
"fmt"
"log"
temperatureSensor "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor/temperature"
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
"github.com/spf13/cobra"
)
var sensorType string
var addTemperatureSensorCmd = &cobra.Command{
Use: "add",
Short: "Add Temperature Sensor",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
t, err := matchSensorTyp(sensorType)
if err != nil {
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() {
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)
}