feat(pkg/sensor): new support for sensor bme280
This commit is contained in:
		@@ -1,7 +1,6 @@
 | 
			
		||||
package sensor
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-flucky/flucky/pkg/config"
 | 
			
		||||
@@ -10,14 +9,20 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var enabled bool
 | 
			
		||||
var location, wireID, wirePath string
 | 
			
		||||
var gpioIn string
 | 
			
		||||
var i2cAddress uint8
 | 
			
		||||
var i2cBus int
 | 
			
		||||
var location string
 | 
			
		||||
var wireID 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"),
 | 
			
		||||
	Args:    cobra.ExactArgs(2),
 | 
			
		||||
	Example: `flucky sensor add --gpio GPIO14 indoor DHT11
 | 
			
		||||
flucky sensor add --wire-id 28-011432f0bb3d outdoor DS18B20
 | 
			
		||||
flucky sensor add --i2c-bus 1 --i2c-address 0x76 wetter-station BME280`,
 | 
			
		||||
	Run: func(cmd *cobra.Command, args []string) {
 | 
			
		||||
		// read configuration
 | 
			
		||||
		cnf, err := config.Read(configFile)
 | 
			
		||||
@@ -31,23 +36,44 @@ var addSensorCmd = &cobra.Command{
 | 
			
		||||
			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
 | 
			
		||||
		// determine gpio port if set
 | 
			
		||||
		if gpioIn != "" &&
 | 
			
		||||
			i2cAddress == 0 &&
 | 
			
		||||
			i2cBus == 0 &&
 | 
			
		||||
			wireID == "" {
 | 
			
		||||
			gpio, err := types.StringToGPIO(gpioIn)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Fatalln(err)
 | 
			
		||||
			}
 | 
			
		||||
			sensor.GPIONumber = &gpio
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// set i2c connection settings
 | 
			
		||||
		if gpioIn == "" &&
 | 
			
		||||
			i2cAddress != 0 &&
 | 
			
		||||
			i2cBus != 0 &&
 | 
			
		||||
			wireID == "" {
 | 
			
		||||
			sensor.I2CAddress = &i2cAddress
 | 
			
		||||
			sensor.I2CBus = &i2cBus
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// set wire connection settings
 | 
			
		||||
		if gpioIn == "" &&
 | 
			
		||||
			i2cAddress == 0 &&
 | 
			
		||||
			i2cBus == 0 &&
 | 
			
		||||
			wireID != "" {
 | 
			
		||||
			sensor.WireID = &wireID
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// add sensor entry to list
 | 
			
		||||
		err = cnf.AddSensor(sensor)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatalln(err)
 | 
			
		||||
@@ -64,8 +90,10 @@ var addSensorCmd = &cobra.Command{
 | 
			
		||||
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")
 | 
			
		||||
	addSensorCmd.Flags().BoolVar(&enabled, "enabled", true, "Enable Sensor")
 | 
			
		||||
	addSensorCmd.Flags().StringVar(&gpioIn, "gpio", "", "GPIO")
 | 
			
		||||
	addSensorCmd.Flags().Uint8Var(&i2cAddress, "i2c-address", 0, "I2C-Address")
 | 
			
		||||
	addSensorCmd.Flags().IntVar(&i2cBus, "i2c-bus", 0, "I2C-Bus")
 | 
			
		||||
	addSensorCmd.Flags().StringVar(&location, "location", "", "Sensor location")
 | 
			
		||||
	addSensorCmd.Flags().StringVar(&wireID, "wire-id", "", "Wire-ID")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user