feat(pkg/sensor): new support for sensor bme280
This commit is contained in:
parent
96eb1f4036
commit
289aaf2093
@ -33,7 +33,7 @@ var daemonCmd = &cobra.Command{
|
|||||||
|
|
||||||
logger := logger.NewDefaultLogger(logger.LogLevelDebug)
|
logger := logger.NewDefaultLogger(logger.LogLevelDebug)
|
||||||
|
|
||||||
daemon.Start(cnf, duration, compression, logger)
|
daemon.Start(cnf, duration, compression, round, logger)
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -44,5 +44,4 @@ func InitCmd(cmd *cobra.Command, cnfFile string) {
|
|||||||
daemonCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured values")
|
daemonCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured values")
|
||||||
daemonCmd.Flags().StringVar(&cleanCacheInterval, "clean-cache-interval", "5m", "Minute intervall to clean cache and write measured values into logfile")
|
daemonCmd.Flags().StringVar(&cleanCacheInterval, "clean-cache-interval", "5m", "Minute intervall to clean cache and write measured values into logfile")
|
||||||
daemonCmd.Flags().Float64Var(&round, "round", 0.25, "Round values. The value 0 deactivates the function")
|
daemonCmd.Flags().Float64Var(&round, "round", 0.25, "Round values. The value 0 deactivates the function")
|
||||||
daemonCmd.Flags().StringVar(&temperatureUnit, "temperature-unit", "celsius", "Temperature unit")
|
|
||||||
}
|
}
|
||||||
|
53
cmd/humidity/list.go
Normal file
53
cmd/humidity/list.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package humidity
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/go-flucky/flucky/pkg/cli"
|
||||||
|
"github.com/go-flucky/flucky/pkg/config"
|
||||||
|
"github.com/go-flucky/flucky/pkg/logfile"
|
||||||
|
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||||
|
"github.com/go-flucky/flucky/pkg/types/typeswitch"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var listTemperatureCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "print humidities",
|
||||||
|
Example: fmt.Sprintf("flucky humidity logs"),
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
|
// read configuration
|
||||||
|
cnf, err := config.Read(configFile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logfile := logfile.New(cnf.Device.Logfile)
|
||||||
|
|
||||||
|
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||||
|
if err := rgbled.Logfile(rgbLEDs); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
measuredValues, err := logfile.Read()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := rgbled.Off(rgbLEDs); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
measuredValues = typeswitch.HumidityValues(measuredValues)
|
||||||
|
|
||||||
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||||
|
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
humidityCmd.AddCommand(listTemperatureCmd)
|
||||||
|
}
|
@ -41,7 +41,9 @@ var readHumidityCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||||
rgbled.Run(rgbLEDs)
|
if err := rgbled.Run(rgbLEDs); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
measuredValues, err := sensor.Read(ctx, sensors)
|
measuredValues, err := sensor.Read(ctx, sensors)
|
||||||
@ -54,8 +56,8 @@ var readHumidityCmd = &cobra.Command{
|
|||||||
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||||
|
|
||||||
if logs {
|
if logs {
|
||||||
logfile := logfile.New(cnf.Device.Logfile)
|
measuredValuesLogfile := logfile.New(cnf.Device.Logfile)
|
||||||
err := logfile.Append(compression, measuredValues)
|
err := logfile.Append(measuredValuesLogfile, compression, round, measuredValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package sensor
|
package sensor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/pkg/config"
|
"github.com/go-flucky/flucky/pkg/config"
|
||||||
@ -10,14 +9,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var enabled bool
|
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{
|
var addSensorCmd = &cobra.Command{
|
||||||
Use: "add",
|
Use: "add",
|
||||||
Short: "Add Sensor",
|
Short: "Add Sensor",
|
||||||
Aliases: []string{"append"},
|
Aliases: []string{"append"},
|
||||||
Args: cobra.ExactArgs(3),
|
Args: cobra.ExactArgs(2),
|
||||||
Example: fmt.Sprintf("flucky sensor add indoor DHT11 GPIO14\nflucky sensor add --wire-id 28-011432f0bb3d outdoor DS18B20 GPIO14"),
|
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) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// read configuration
|
// read configuration
|
||||||
cnf, err := config.Read(configFile)
|
cnf, err := config.Read(configFile)
|
||||||
@ -31,23 +36,44 @@ var addSensorCmd = &cobra.Command{
|
|||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine gpio port
|
|
||||||
gpio, err := types.StringToGPIO(args[2])
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// create new sensor struct
|
// create new sensor struct
|
||||||
sensor := &types.Sensor{
|
sensor := &types.Sensor{
|
||||||
SensorName: args[0],
|
SensorName: args[0],
|
||||||
SensorModel: sensorModel,
|
SensorModel: sensorModel,
|
||||||
SensorLocation: location,
|
SensorLocation: location,
|
||||||
SensorEnabled: enabled,
|
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)
|
err = cnf.AddSensor(sensor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
@ -64,8 +90,10 @@ var addSensorCmd = &cobra.Command{
|
|||||||
func init() {
|
func init() {
|
||||||
sensorCmd.AddCommand(addSensorCmd)
|
sensorCmd.AddCommand(addSensorCmd)
|
||||||
|
|
||||||
addSensorCmd.Flags().BoolVarP(&enabled, "enabled", "e", true, "Enable Sensor")
|
addSensorCmd.Flags().BoolVar(&enabled, "enabled", true, "Enable Sensor")
|
||||||
addSensorCmd.Flags().StringVarP(&location, "location", "l", "", "Sensor location")
|
addSensorCmd.Flags().StringVar(&gpioIn, "gpio", "", "GPIO")
|
||||||
addSensorCmd.Flags().StringVarP(&wireID, "wire-id", "i", "", "Wire-ID")
|
addSensorCmd.Flags().Uint8Var(&i2cAddress, "i2c-address", 0, "I2C-Address")
|
||||||
addSensorCmd.Flags().StringVarP(&wirePath, "wire-path", "w", "/sys/bus/w1/devices", "Wire device path")
|
addSensorCmd.Flags().IntVar(&i2cBus, "i2c-bus", 0, "I2C-Bus")
|
||||||
|
addSensorCmd.Flags().StringVar(&location, "location", "", "Sensor location")
|
||||||
|
addSensorCmd.Flags().StringVar(&wireID, "wire-id", "", "Wire-ID")
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,8 @@ import (
|
|||||||
"github.com/go-flucky/flucky/pkg/cli"
|
"github.com/go-flucky/flucky/pkg/cli"
|
||||||
"github.com/go-flucky/flucky/pkg/config"
|
"github.com/go-flucky/flucky/pkg/config"
|
||||||
"github.com/go-flucky/flucky/pkg/logfile"
|
"github.com/go-flucky/flucky/pkg/logfile"
|
||||||
|
"github.com/go-flucky/flucky/pkg/rgbled"
|
||||||
|
"github.com/go-flucky/flucky/pkg/types/typeswitch"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,11 +27,22 @@ var listTemperatureCmd = &cobra.Command{
|
|||||||
|
|
||||||
logfile := logfile.New(cnf.Device.Logfile)
|
logfile := logfile.New(cnf.Device.Logfile)
|
||||||
|
|
||||||
|
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||||
|
if err := rgbled.Logfile(rgbLEDs); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
measuredValues, err := logfile.Read()
|
measuredValues, err := logfile.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := rgbled.Off(rgbLEDs); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
measuredValues = typeswitch.TemperatureValues(measuredValues)
|
||||||
|
|
||||||
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||||
|
|
||||||
},
|
},
|
||||||
|
@ -44,7 +44,9 @@ var readTemperatureCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||||
rgbled.Run(rgbLEDs)
|
if err := rgbled.Run(rgbLEDs); err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
measuredValues, err := sensor.Read(ctx, sensors)
|
measuredValues, err := sensor.Read(ctx, sensors)
|
||||||
@ -59,14 +61,13 @@ var readTemperatureCmd = &cobra.Command{
|
|||||||
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
cli.PrintMeasuredValues(measuredValues, cnf, os.Stdout)
|
||||||
|
|
||||||
if logs {
|
if logs {
|
||||||
logfile := logfile.New(cnf.Device.Logfile)
|
measuredValuesLogfile := logfile.New(cnf.Device.Logfile)
|
||||||
err := logfile.Append(compression, measuredValues)
|
err := logfile.Append(measuredValuesLogfile, compression, round, measuredValues)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rgbled.Error(rgbLEDs)
|
rgbled.Error(rgbLEDs)
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rgbled.Off(rgbLEDs)
|
rgbled.Off(rgbLEDs)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
4
go.mod
4
go.mod
@ -3,6 +3,9 @@ module github.com/go-flucky/flucky
|
|||||||
go 1.12
|
go 1.12
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/d2r2/go-bsbmp v0.0.0-20190515110334-3b4b3aea8375
|
||||||
|
github.com/d2r2/go-i2c v0.0.0-20181113114621-14f8dd4e89ce
|
||||||
|
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e
|
||||||
github.com/go-flucky/go-dht v0.1.1
|
github.com/go-flucky/go-dht v0.1.1
|
||||||
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
||||||
github.com/kr/pretty v0.1.0 // indirect
|
github.com/kr/pretty v0.1.0 // indirect
|
||||||
@ -12,4 +15,5 @@ require (
|
|||||||
github.com/stianeikeland/go-rpio v4.2.0+incompatible
|
github.com/stianeikeland/go-rpio v4.2.0+incompatible
|
||||||
github.com/stretchr/testify v1.3.0
|
github.com/stretchr/testify v1.3.0
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
|
||||||
|
periph.io/x/periph v3.4.0+incompatible
|
||||||
)
|
)
|
||||||
|
6
go.sum
6
go.sum
@ -1,3 +1,9 @@
|
|||||||
|
github.com/d2r2/go-bsbmp v0.0.0-20190515110334-3b4b3aea8375 h1:vdUOwcZdV+bBfGUUh5oPPWSzw9p+lBnNSuGgQwGpCH4=
|
||||||
|
github.com/d2r2/go-bsbmp v0.0.0-20190515110334-3b4b3aea8375/go.mod h1:3iz1WHlYJU9b4NJei+Q8G7DN3K05arcCMlOQ+qNCDjo=
|
||||||
|
github.com/d2r2/go-i2c v0.0.0-20181113114621-14f8dd4e89ce h1:Dog7PLNz1fPaXqHPOHonpERqsF57Oh4X76pM80T1GDY=
|
||||||
|
github.com/d2r2/go-i2c v0.0.0-20181113114621-14f8dd4e89ce/go.mod h1:AwxDPnsgIpy47jbGXZHA9Rv7pDkOJvQbezPuK1Y+nNk=
|
||||||
|
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e h1:ZG3JBA6rPRl0xxQ+nNSfO7tor8w+CNCTs05DNJQYbLM=
|
||||||
|
github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e/go.mod h1:oA+9PUt8F1aKJ6o4YU1T120i7sgo1T6/1LWEEBy0BSs=
|
||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/go-flucky/go-dht v0.1.1 h1:dPz9F5D7oUaTd0GUGTvT4lBH2R043h1bkmhTlpQax1Y=
|
github.com/go-flucky/go-dht v0.1.1 h1:dPz9F5D7oUaTd0GUGTvT4lBH2R043h1bkmhTlpQax1Y=
|
||||||
|
@ -15,12 +15,14 @@ import (
|
|||||||
var humiditySensorModels = map[types.SensorModel]types.SensorModel{
|
var humiditySensorModels = map[types.SensorModel]types.SensorModel{
|
||||||
types.DHT11: types.DHT11,
|
types.DHT11: types.DHT11,
|
||||||
types.DHT22: types.DHT22,
|
types.DHT22: types.DHT22,
|
||||||
|
types.BME280: types.BME280,
|
||||||
}
|
}
|
||||||
|
|
||||||
var temperatureSensorModels = map[types.SensorModel]types.SensorModel{
|
var temperatureSensorModels = map[types.SensorModel]types.SensorModel{
|
||||||
types.DHT11: types.DHT11,
|
types.DHT11: types.DHT11,
|
||||||
types.DHT22: types.DHT22,
|
types.DHT22: types.DHT22,
|
||||||
types.DS18B20: types.DS18B20,
|
types.DS18B20: types.DS18B20,
|
||||||
|
types.BME280: types.BME280,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configuration of flucky
|
// Configuration of flucky
|
||||||
@ -82,11 +84,12 @@ func (c *Configuration) AddSensor(sensor *types.Sensor) error {
|
|||||||
return fmt.Errorf("Sensor %v with UUID %v already exists", s.SensorName, s.SensorID)
|
return fmt.Errorf("Sensor %v with UUID %v already exists", s.SensorName, s.SensorID)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *sensor.WireID != "" &&
|
if sensor.WireID != nil {
|
||||||
*s.WireID == *sensor.WireID {
|
if *s.WireID == *sensor.WireID {
|
||||||
return fmt.Errorf("Sensor with 1wire-id %v already exists as %v", *s.WireID, s.SensorName)
|
return fmt.Errorf("Sensor with 1wire-id %v already exists as %v", *s.WireID, s.SensorName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// check if sensor has a valid device id
|
// check if sensor has a valid device id
|
||||||
if sensor.DeviceID != c.Device.DeviceID {
|
if sensor.DeviceID != c.Device.DeviceID {
|
||||||
@ -260,8 +263,6 @@ func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.Sensor
|
|||||||
switch name {
|
switch name {
|
||||||
case s.SensorID:
|
case s.SensorID:
|
||||||
configHumiditySensors[s.SensorID] = s
|
configHumiditySensors[s.SensorID] = s
|
||||||
case *s.WireID:
|
|
||||||
configHumiditySensors[s.SensorID] = s
|
|
||||||
case s.SensorName:
|
case s.SensorName:
|
||||||
configHumiditySensors[s.SensorID] = s
|
configHumiditySensors[s.SensorID] = s
|
||||||
}
|
}
|
||||||
@ -381,8 +382,6 @@ func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.Sen
|
|||||||
switch name {
|
switch name {
|
||||||
case s.SensorID:
|
case s.SensorID:
|
||||||
configTemperatureSensors[s.SensorID] = s
|
configTemperatureSensors[s.SensorID] = s
|
||||||
case *s.WireID:
|
|
||||||
configTemperatureSensors[s.SensorID] = s
|
|
||||||
case s.SensorName:
|
case s.SensorName:
|
||||||
configTemperatureSensors[s.SensorID] = s
|
configTemperatureSensors[s.SensorID] = s
|
||||||
}
|
}
|
||||||
@ -464,6 +463,10 @@ func (c *Configuration) convertSensors(sensors []*types.Sensor) []sensor.Sensor
|
|||||||
|
|
||||||
for _, s := range sensors {
|
for _, s := range sensors {
|
||||||
switch s.SensorModel {
|
switch s.SensorModel {
|
||||||
|
case types.BME280:
|
||||||
|
cachedSensors = append(cachedSensors, &sensor.BME280{
|
||||||
|
Sensor: s,
|
||||||
|
})
|
||||||
case types.DHT11:
|
case types.DHT11:
|
||||||
cachedSensors = append(cachedSensors, &sensor.DHT11{
|
cachedSensors = append(cachedSensors, &sensor.DHT11{
|
||||||
Sensor: s,
|
Sensor: s,
|
||||||
|
@ -16,7 +16,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Start the daemon
|
// Start the daemon
|
||||||
func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, logger logger.Logger) {
|
func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, round float64, logger logger.Logger) {
|
||||||
|
|
||||||
// Info
|
// Info
|
||||||
logger.Info("Use clean-cache-interval: %v", cleanCacheInterval.String())
|
logger.Info("Use clean-cache-interval: %v", cleanCacheInterval.String())
|
||||||
@ -33,7 +33,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
childContext, cancel := context.WithCancel(ctx)
|
childContext, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
logfile := logfile.New(cnf.Device.Logfile)
|
measuredValuesLogfile := logfile.New(cnf.Device.Logfile)
|
||||||
|
|
||||||
measuredValuesCache := make([]types.MeasuredValue, 0)
|
measuredValuesCache := make([]types.MeasuredValue, 0)
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
|
|||||||
logger.Error("Can not turn on blue info light: %v", err)
|
logger.Error("Can not turn on blue info light: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = logfile.Append(compression, measuredValuesCache)
|
err = logfile.Append(measuredValuesLogfile, compression, round, measuredValuesCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
err = rgbled.Error(rgbLEDs)
|
err = rgbled.Error(rgbLEDs)
|
||||||
@ -92,8 +92,8 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
|
|||||||
logger.Error("Can not turn on red info light: %v", err)
|
logger.Error("Can not turn on red info light: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Warn("Save remaining data from the cache: %v", len(measuredValuesCache))
|
logger.Warn("Save remaining data from the cache")
|
||||||
err = logfile.Append(compression, measuredValuesCache)
|
err = logfile.Append(measuredValuesLogfile, compression, round, measuredValuesCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("%v", err)
|
logger.Fatal("%v", err)
|
||||||
}
|
}
|
||||||
|
@ -14,28 +14,6 @@ type csvLogfile struct {
|
|||||||
logfile string
|
logfile string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cl *csvLogfile) Append(compression bool, measuredValues []types.MeasuredValue) error {
|
|
||||||
|
|
||||||
allMeasuredValues, err := cl.Read()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
allMeasuredValues = append(allMeasuredValues, measuredValues...)
|
|
||||||
|
|
||||||
if compression {
|
|
||||||
allMeasuredValues = Compression(allMeasuredValues)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cl.Write(allMeasuredValues)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cl *csvLogfile) Read() ([]types.MeasuredValue, error) {
|
func (cl *csvLogfile) Read() ([]types.MeasuredValue, error) {
|
||||||
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
|
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Logfile interface {
|
type Logfile interface {
|
||||||
Append(compression bool, measuredValues []types.MeasuredValue) error
|
|
||||||
Read() ([]types.MeasuredValue, error)
|
Read() ([]types.MeasuredValue, error)
|
||||||
Write(measuredValues []types.MeasuredValue) error
|
Write(measuredValues []types.MeasuredValue) error
|
||||||
}
|
}
|
||||||
|
@ -17,28 +17,6 @@ type jsonLogfile struct {
|
|||||||
logfile string
|
logfile string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jl *jsonLogfile) Append(compression bool, measuredValues []types.MeasuredValue) error {
|
|
||||||
|
|
||||||
allMeasuredValues, err := jl.Read()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
allMeasuredValues = append(allMeasuredValues, measuredValues...)
|
|
||||||
|
|
||||||
if compression {
|
|
||||||
allMeasuredValues = Compression(allMeasuredValues)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = jl.Write(allMeasuredValues)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (jl *jsonLogfile) Read() ([]types.MeasuredValue, error) {
|
func (jl *jsonLogfile) Read() ([]types.MeasuredValue, error) {
|
||||||
|
|
||||||
if _, err := os.Stat(jl.logfile); os.IsNotExist(err) {
|
if _, err := os.Stat(jl.logfile); os.IsNotExist(err) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package logfile
|
package logfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
@ -9,7 +10,35 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// var validUUID = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
|
// var validUUID = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
|
||||||
var timeFormat = time.RFC3339
|
var timeFormat = "2006-01-02T15:04:05.999999Z07:00"
|
||||||
|
|
||||||
|
func Append(logfile Logfile, compression bool, round float64, measuredValues []types.MeasuredValue) error {
|
||||||
|
|
||||||
|
if round != 0 {
|
||||||
|
for _, measuredValue := range measuredValues {
|
||||||
|
measuredValue.SetValue(math.Round(measuredValue.GetValue()/round) * round)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allMeasuredValues, err := logfile.Read()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
allMeasuredValues = append(allMeasuredValues, measuredValues...)
|
||||||
|
|
||||||
|
if compression {
|
||||||
|
allMeasuredValues = Compression(allMeasuredValues)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = logfile.Write(allMeasuredValues)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Compression the measured values. The system checks whether the measured values
|
// Compression the measured values. The system checks whether the measured values
|
||||||
// of the same type correspond to those of the predecessor. If this is the case,
|
// of the same type correspond to those of the predecessor. If this is the case,
|
||||||
|
112
pkg/sensor/bme280.go
Normal file
112
pkg/sensor/bme280.go
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
package sensor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/d2r2/go-bsbmp"
|
||||||
|
"github.com/d2r2/go-i2c"
|
||||||
|
"github.com/d2r2/go-logger"
|
||||||
|
"github.com/go-flucky/flucky/pkg/types"
|
||||||
|
uuid "github.com/satori/go.uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BME280 is a sensor to measure humidity and temperature.
|
||||||
|
type BME280 struct {
|
||||||
|
*types.Sensor
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSensorModel returns the sensor model
|
||||||
|
func (s *BME280) GetSensorModel() types.SensorModel {
|
||||||
|
return s.Sensor.SensorModel
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read measured values
|
||||||
|
func (s *BME280) Read() ([]types.MeasuredValue, error) {
|
||||||
|
|
||||||
|
// Create new connection to i2c-bus on 1 line with address 0x76.
|
||||||
|
// Use i2cdetect utility to find device address over the i2c-bus
|
||||||
|
i2c, err := i2c.NewI2C(*s.I2CAddress, *s.I2CBus)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer i2c.Close()
|
||||||
|
|
||||||
|
logger.ChangePackageLogLevel("i2c", logger.InfoLevel)
|
||||||
|
|
||||||
|
sensor, err := bsbmp.NewBMP(bsbmp.BME280, i2c)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.ChangePackageLogLevel("bsbmp", logger.InfoLevel)
|
||||||
|
|
||||||
|
temperatureValue, err := sensor.ReadTemperatureC(bsbmp.ACCURACY_STANDARD)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// p, err := sensor.ReadPressurePa(bsbmp.ACCURACY_STANDARD)
|
||||||
|
// if err != nil {
|
||||||
|
// log.Fatal(err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
_, humidityValue, err := sensor.ReadHumidityRH(bsbmp.ACCURACY_STANDARD)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
measuredValues := []types.MeasuredValue{
|
||||||
|
&types.Humidity{
|
||||||
|
HumidityID: uuid.NewV4().String(),
|
||||||
|
HumidityValue: float64(humidityValue),
|
||||||
|
HumidityFromDate: time.Now(),
|
||||||
|
HumidityTillDate: time.Now(),
|
||||||
|
SensorID: s.SensorID,
|
||||||
|
},
|
||||||
|
&types.Temperature{
|
||||||
|
TemperatureID: uuid.NewV4().String(),
|
||||||
|
TemperatureValue: float64(temperatureValue),
|
||||||
|
TemperatureFromDate: time.Now(),
|
||||||
|
TemperatureTillDate: time.Now(),
|
||||||
|
SensorID: s.SensorID,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return measuredValues, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadChannel reads the measured values from the sensor and writes them to a
|
||||||
|
// channel.
|
||||||
|
func (s *BME280) ReadChannel(measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||||
|
if wg != nil {
|
||||||
|
defer wg.Done()
|
||||||
|
}
|
||||||
|
|
||||||
|
measuredValues, err := s.Read()
|
||||||
|
if err != nil {
|
||||||
|
errorChannel <- err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
measuredValuesChannel <- measuredValues
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadContinously reads the measured values continously from the sensor and
|
||||||
|
// writes them to a channel.
|
||||||
|
func (s *BME280) ReadContinously(ctx context.Context, measuredValuesChannel chan<- []types.MeasuredValue, errorChannel chan<- error) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
s.ReadChannel(measuredValuesChannel, errorChannel, nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -45,6 +45,10 @@ func (h *Humidity) GetMeasuredValueType() MeasuredValueType {
|
|||||||
return MeasuredValueTypeHumidity
|
return MeasuredValueTypeHumidity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Humidity) SetValue(value float64) {
|
||||||
|
h.HumidityValue = value
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Humidity) SetTillDate(date time.Time) {
|
func (h *Humidity) SetTillDate(date time.Time) {
|
||||||
h.HumidityTillDate = date
|
h.HumidityTillDate = date
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ type MeasuredValue interface {
|
|||||||
GetCreationDate() *time.Time
|
GetCreationDate() *time.Time
|
||||||
GetUpdateDate() *time.Time
|
GetUpdateDate() *time.Time
|
||||||
|
|
||||||
|
SetValue(value float64)
|
||||||
SetTillDate(date time.Time)
|
SetTillDate(date time.Time)
|
||||||
SetCreationDate(date *time.Time)
|
SetCreationDate(date *time.Time)
|
||||||
SetUpdateDate(date *time.Time)
|
SetUpdateDate(date *time.Time)
|
||||||
|
@ -13,6 +13,8 @@ type Sensor struct {
|
|||||||
SensorName string `json:"sensor_name" xml:"sensor_name"`
|
SensorName string `json:"sensor_name" xml:"sensor_name"`
|
||||||
SensorLocation string `json:"sensor_location" xml:"sensor_location"`
|
SensorLocation string `json:"sensor_location" xml:"sensor_location"`
|
||||||
WireID *string `json:"wire_id" xml:"wire_id"`
|
WireID *string `json:"wire_id" xml:"wire_id"`
|
||||||
|
I2CBus *int `json:"i2c_bus" xml:"i2c_bus"`
|
||||||
|
I2CAddress *uint8 `json:"i2c_address" xml:"i2c_address"`
|
||||||
GPIONumber *GPIO `json:"gpio_number" xml:"gpio_number"`
|
GPIONumber *GPIO `json:"gpio_number" xml:"gpio_number"`
|
||||||
SensorModel SensorModel `json:"sensor_model" xml:"sensor_model"`
|
SensorModel SensorModel `json:"sensor_model" xml:"sensor_model"`
|
||||||
SensorEnabled bool `json:"sensor_enabled" xml:"sensor_enabled"`
|
SensorEnabled bool `json:"sensor_enabled" xml:"sensor_enabled"`
|
||||||
@ -46,6 +48,9 @@ func (s *Sensor) Name() string {
|
|||||||
return s.SensorName
|
return s.SensorName
|
||||||
} else if s.WireID != nil {
|
} else if s.WireID != nil {
|
||||||
return *s.WireID
|
return *s.WireID
|
||||||
|
} else if s.I2CAddress != nil &&
|
||||||
|
s.I2CBus != nil {
|
||||||
|
return fmt.Sprintf("%v/%v", *s.I2CBus, *s.I2CAddress)
|
||||||
}
|
}
|
||||||
return s.SensorID
|
return s.SensorID
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,8 @@ import "fmt"
|
|||||||
type SensorModel string
|
type SensorModel string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DHT11 SensorModel = "DHT11"
|
BME280 SensorModel = "BME280"
|
||||||
|
DHT11 = "DHT11"
|
||||||
DHT22 = "DHT22"
|
DHT22 = "DHT22"
|
||||||
DS18B20 = "DS18B20"
|
DS18B20 = "DS18B20"
|
||||||
)
|
)
|
||||||
@ -13,6 +14,8 @@ const (
|
|||||||
// SelectSensorModel converts a string into a constant
|
// SelectSensorModel converts a string into a constant
|
||||||
func SelectSensorModel(model string) (SensorModel, error) {
|
func SelectSensorModel(model string) (SensorModel, error) {
|
||||||
switch model {
|
switch model {
|
||||||
|
case "BME280":
|
||||||
|
return BME280, nil
|
||||||
case "DHT11":
|
case "DHT11":
|
||||||
return DHT11, nil
|
return DHT11, nil
|
||||||
case "DHT22":
|
case "DHT22":
|
||||||
|
@ -47,6 +47,10 @@ func (t *Temperature) GetMeasuredValueType() MeasuredValueType {
|
|||||||
return MeasuredValueTypeTemperature
|
return MeasuredValueTypeTemperature
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Temperature) SetValue(value float64) {
|
||||||
|
t.TemperatureValue = value
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Temperature) SetTillDate(date time.Time) {
|
func (t *Temperature) SetTillDate(date time.Time) {
|
||||||
t.TemperatureTillDate = date
|
t.TemperatureTillDate = date
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user