fix(pkg/daemon): use measuredValue interface instead of different structs
This commit is contained in:
parent
30603e348c
commit
08c2cbbf57
@ -6,10 +6,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/cmd/daemon"
|
"github.com/go-flucky/flucky/cmd/daemon"
|
||||||
"github.com/go-flucky/flucky/cmd/humidity"
|
|
||||||
"github.com/go-flucky/flucky/cmd/rgbled"
|
"github.com/go-flucky/flucky/cmd/rgbled"
|
||||||
"github.com/go-flucky/flucky/cmd/sensor"
|
"github.com/go-flucky/flucky/cmd/sensor"
|
||||||
"github.com/go-flucky/flucky/cmd/temperature"
|
|
||||||
"github.com/go-flucky/flucky/pkg/types"
|
"github.com/go-flucky/flucky/pkg/types"
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/pkg/config"
|
"github.com/go-flucky/flucky/pkg/config"
|
||||||
@ -58,9 +56,9 @@ func Execute(version string) {
|
|||||||
rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/flucky/config.json", "Config file")
|
rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/flucky/config.json", "Config file")
|
||||||
|
|
||||||
daemon.InitCmd(rootCmd, configFile)
|
daemon.InitCmd(rootCmd, configFile)
|
||||||
humidity.InitCmd(rootCmd, configFile)
|
//humidity.InitCmd(rootCmd, configFile)
|
||||||
rgbled.InitCmd(rootCmd, configFile)
|
rgbled.InitCmd(rootCmd, configFile)
|
||||||
sensor.InitCmd(rootCmd, configFile)
|
sensor.InitCmd(rootCmd, configFile)
|
||||||
temperature.InitCmd(rootCmd, configFile)
|
//temperature.InitCmd(rootCmd, configFile)
|
||||||
rootCmd.Execute()
|
rootCmd.Execute()
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/go-flucky/flucky/pkg/config"
|
"github.com/go-flucky/flucky/pkg/config"
|
||||||
"github.com/go-flucky/flucky/pkg/daemon"
|
"github.com/go-flucky/flucky/pkg/daemon"
|
||||||
"github.com/go-flucky/flucky/pkg/logger"
|
"github.com/go-flucky/flucky/pkg/logger"
|
||||||
"github.com/go-flucky/flucky/pkg/sensor"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,12 +33,7 @@ var daemonCmd = &cobra.Command{
|
|||||||
|
|
||||||
logger := logger.NewDefaultLogger(logger.LogLevelDebug)
|
logger := logger.NewDefaultLogger(logger.LogLevelDebug)
|
||||||
|
|
||||||
measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(temperatureUnit)
|
daemon.Start(cnf, duration, compression, logger)
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Can not parse temperature unit: %v", temperatureUnit)
|
|
||||||
}
|
|
||||||
|
|
||||||
daemon.Start(cnf, duration, compression, measurementUnit, round, logger)
|
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -225,7 +225,7 @@ func (c *Configuration) EnableSensor(name string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetHumiditySensors returns a list of humidity sensors
|
// GetHumiditySensors returns a list of humidity sensors
|
||||||
func (c *Configuration) GetHumiditySensors(option Option) []sensor.HumiditySensor {
|
func (c *Configuration) GetHumiditySensors(option Option) []sensor.Sensor {
|
||||||
humiditySensors := c.getHumiditySensors()
|
humiditySensors := c.getHumiditySensors()
|
||||||
|
|
||||||
switch option {
|
switch option {
|
||||||
@ -235,22 +235,22 @@ func (c *Configuration) GetHumiditySensors(option Option) []sensor.HumiditySenso
|
|||||||
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
|
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c.convertHumiditySensors(humiditySensors)
|
return c.convertSensors(humiditySensors)
|
||||||
case DISABLED:
|
case DISABLED:
|
||||||
for i, humiditySensor := range humiditySensors {
|
for i, humiditySensor := range humiditySensors {
|
||||||
if humiditySensor.SensorEnabled {
|
if humiditySensor.SensorEnabled {
|
||||||
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
|
humiditySensors = append(humiditySensors[:i], humiditySensors[i+1:]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c.convertHumiditySensors(humiditySensors)
|
return c.convertSensors(humiditySensors)
|
||||||
default:
|
default:
|
||||||
return c.convertHumiditySensors(humiditySensors)
|
return c.convertSensors(humiditySensors)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetHumiditySensorsByName returns a list of temperature sensors by name,
|
// GetHumiditySensorsByName returns a list of temperature sensors by name,
|
||||||
// uuid or wire-id
|
// uuid or wire-id
|
||||||
func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.HumiditySensor {
|
func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.Sensor {
|
||||||
configHumiditySensors := make(map[string]*types.Sensor, 0)
|
configHumiditySensors := make(map[string]*types.Sensor, 0)
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
@ -271,7 +271,7 @@ func (c *Configuration) GetHumiditySensorsByName(names []string) []sensor.Humidi
|
|||||||
humiditySensors = append(humiditySensors, cs)
|
humiditySensors = append(humiditySensors, cs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.convertHumiditySensors(humiditySensors)
|
return c.convertSensors(humiditySensors)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Configuration) GetRGBLEDs(option Option) []rgbled.RGBLED {
|
func (c *Configuration) GetRGBLEDs(option Option) []rgbled.RGBLED {
|
||||||
@ -320,7 +320,7 @@ func (c *Configuration) GetRGBLEDsByName(names []string) []rgbled.RGBLED {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetTemperatureSensors returns a list of temperature sensors
|
// GetTemperatureSensors returns a list of temperature sensors
|
||||||
func (c *Configuration) GetTemperatureSensors(option Option) []sensor.TemperatureSensor {
|
func (c *Configuration) GetTemperatureSensors(option Option) []sensor.Sensor {
|
||||||
temperatureSensors := c.getTemperatureSensors()
|
temperatureSensors := c.getTemperatureSensors()
|
||||||
|
|
||||||
switch option {
|
switch option {
|
||||||
@ -330,22 +330,22 @@ func (c *Configuration) GetTemperatureSensors(option Option) []sensor.Temperatur
|
|||||||
temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...)
|
temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c.convertTemperatureSensors(temperatureSensors)
|
return c.convertSensors(temperatureSensors)
|
||||||
case DISABLED:
|
case DISABLED:
|
||||||
for i, temperatureSensor := range temperatureSensors {
|
for i, temperatureSensor := range temperatureSensors {
|
||||||
if temperatureSensor.SensorEnabled {
|
if temperatureSensor.SensorEnabled {
|
||||||
temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...)
|
temperatureSensors = append(temperatureSensors[:i], temperatureSensors[i+1:]...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c.convertTemperatureSensors(temperatureSensors)
|
return c.convertSensors(temperatureSensors)
|
||||||
default:
|
default:
|
||||||
return c.convertTemperatureSensors(temperatureSensors)
|
return c.convertSensors(temperatureSensors)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTemperatureSensorsByName returns a list of temperature sensors by name,
|
// GetTemperatureSensorsByName returns a list of temperature sensors by name,
|
||||||
// uuid or wire-id
|
// uuid or wire-id
|
||||||
func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.TemperatureSensor {
|
func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.Sensor {
|
||||||
configTemperatureSensors := make(map[string]*types.Sensor, 0)
|
configTemperatureSensors := make(map[string]*types.Sensor, 0)
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
@ -366,7 +366,7 @@ func (c *Configuration) GetTemperatureSensorsByName(names []string) []sensor.Tem
|
|||||||
temperatureSensors = append(temperatureSensors, cs)
|
temperatureSensors = append(temperatureSensors, cs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.convertTemperatureSensors(temperatureSensors)
|
return c.convertSensors(temperatureSensors)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveRGBLED deletes a LED by its name or its unique UUID
|
// RemoveRGBLED deletes a LED by its name or its unique UUID
|
||||||
@ -431,22 +431,26 @@ func (c *Configuration) RenameSensor(oldName, newName string) error {
|
|||||||
return fmt.Errorf("Could not find remote %v to replace into with %v", oldName, newName)
|
return fmt.Errorf("Could not find remote %v to replace into with %v", oldName, newName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Configuration) convertHumiditySensors(sensors []*types.Sensor) []sensor.HumiditySensor {
|
func (c *Configuration) convertSensors(sensors []*types.Sensor) []sensor.Sensor {
|
||||||
humiditySensors := make([]sensor.HumiditySensor, 0)
|
cachedSensors := make([]sensor.Sensor, 0)
|
||||||
|
|
||||||
for _, s := range sensors {
|
for _, s := range sensors {
|
||||||
switch s.SensorModel {
|
switch s.SensorModel {
|
||||||
case types.DHT11:
|
case types.DHT11:
|
||||||
humiditySensors = append(humiditySensors, &sensor.DHT11{
|
cachedSensors = append(cachedSensors, &sensor.DHT11{
|
||||||
Sensor: s,
|
Sensor: s,
|
||||||
})
|
})
|
||||||
case types.DHT22:
|
case types.DHT22:
|
||||||
humiditySensors = append(humiditySensors, &sensor.DHT22{
|
cachedSensors = append(cachedSensors, &sensor.DHT22{
|
||||||
|
Sensor: s,
|
||||||
|
})
|
||||||
|
case types.DS18B20:
|
||||||
|
cachedSensors = append(cachedSensors, &sensor.DS18B20{
|
||||||
Sensor: s,
|
Sensor: s,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return humiditySensors
|
return cachedSensors
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Configuration) convertRGBLEDs(rgbLEDs []*types.RGBLED) []rgbled.RGBLED {
|
func (c *Configuration) convertRGBLEDs(rgbLEDs []*types.RGBLED) []rgbled.RGBLED {
|
||||||
@ -461,28 +465,6 @@ func (c *Configuration) convertRGBLEDs(rgbLEDs []*types.RGBLED) []rgbled.RGBLED
|
|||||||
return leds
|
return leds
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Configuration) convertTemperatureSensors(sensors []*types.Sensor) []sensor.TemperatureSensor {
|
|
||||||
temperatureSensors := make([]sensor.TemperatureSensor, 0)
|
|
||||||
|
|
||||||
for _, s := range sensors {
|
|
||||||
switch s.SensorModel {
|
|
||||||
case types.DHT11:
|
|
||||||
temperatureSensors = append(temperatureSensors, &sensor.DHT11{
|
|
||||||
Sensor: s,
|
|
||||||
})
|
|
||||||
case types.DHT22:
|
|
||||||
temperatureSensors = append(temperatureSensors, &sensor.DHT22{
|
|
||||||
Sensor: s,
|
|
||||||
})
|
|
||||||
case types.DS18B20:
|
|
||||||
temperatureSensors = append(temperatureSensors, &sensor.DS18B20{
|
|
||||||
Sensor: s,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return temperatureSensors
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Configuration) getHumiditySensors() []*types.Sensor {
|
func (c *Configuration) getHumiditySensors() []*types.Sensor {
|
||||||
humiditySensors := make([]*types.Sensor, 0)
|
humiditySensors := make([]*types.Sensor, 0)
|
||||||
for _, s := range c.Sensors {
|
for _, s := range c.Sensors {
|
||||||
|
@ -16,15 +16,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Start the daemon
|
// Start the daemon
|
||||||
func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, degree types.TemperatureUnit, round float64, logger logger.Logger) {
|
func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, logger logger.Logger) {
|
||||||
|
|
||||||
// Info
|
// Info
|
||||||
logger.Info("Use clean-cache-interval: %v", cleanCacheInterval.String())
|
logger.Info("Use clean-cache-interval: %v", cleanCacheInterval.String())
|
||||||
logger.Info("Use compression: %v", compression)
|
logger.Info("Use compression: %v", compression)
|
||||||
logger.Info("Round values: %v", round)
|
|
||||||
logger.Info("Temperature unit: %v", degree)
|
|
||||||
|
|
||||||
temperatureLogfile := logfile.New(cnf.Device.TemperatureLogfile)
|
|
||||||
|
|
||||||
ticker := time.Tick(cleanCacheInterval)
|
ticker := time.Tick(cleanCacheInterval)
|
||||||
|
|
||||||
@ -32,16 +28,16 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
|
|||||||
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
|
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)
|
||||||
|
|
||||||
errorChannel := make(chan error, 0)
|
errorChannel := make(chan error, 0)
|
||||||
//humidityChannel := make(chan *types.Humidity, 0)
|
measuredValueChannel := make(chan types.MeasuredValue, 0)
|
||||||
temperatureChannel := make(chan *types.Temperature, 0)
|
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
childContext, cancel := context.WithCancel(ctx)
|
childContext, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
// go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel)
|
logfile := logfile.New(cnf)
|
||||||
go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), degree, round, temperatureChannel, errorChannel)
|
|
||||||
|
|
||||||
temperatureCache := make([]*types.Temperature, 0)
|
measuredValuesCache := make([]types.MeasuredValue, 0)
|
||||||
|
|
||||||
|
go sensor.ReadContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), measuredValueChannel, errorChannel)
|
||||||
|
|
||||||
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
rgbLEDs := cnf.GetRGBLEDs(config.ENABLED)
|
||||||
|
|
||||||
@ -70,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.AppendTemperatures(temperatureLogfile, compression, temperatureCache)
|
err = logfile.Append(compression, measuredValuesCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
err = rgbled.Error(rgbLEDs)
|
err = rgbled.Error(rgbLEDs)
|
||||||
@ -81,10 +77,10 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
|
|||||||
cancel()
|
cancel()
|
||||||
logger.Fatal("Can not save temperatures: %v", err)
|
logger.Fatal("Can not save temperatures: %v", err)
|
||||||
}
|
}
|
||||||
temperatureCache = make([]*types.Temperature, 0)
|
measuredValuesCache = make([]types.MeasuredValue, 0)
|
||||||
|
|
||||||
case temperature, _ := <-temperatureChannel:
|
case measuredValue, _ := <-measuredValueChannel:
|
||||||
temperatureCache = append(temperatureCache, temperature)
|
measuredValuesCache = append(measuredValuesCache, measuredValue)
|
||||||
|
|
||||||
case killSignal := <-interrupt:
|
case killSignal := <-interrupt:
|
||||||
logger.Warn("Daemon was interruped by system signal %v\n", killSignal)
|
logger.Warn("Daemon was interruped by system signal %v\n", killSignal)
|
||||||
@ -96,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 temperature data from the cache")
|
logger.Warn("Save remaining data from the cache: %v", len(measuredValuesCache))
|
||||||
err = logfile.AppendTemperatures(temperatureLogfile, compression, temperatureCache)
|
err = logfile.Append(compression, measuredValuesCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal("%v", err)
|
logger.Fatal("%v", err)
|
||||||
}
|
}
|
||||||
|
20
pkg/internal/collect/measuredValues.go
Normal file
20
pkg/internal/collect/measuredValues.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package collect
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-flucky/flucky/pkg/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func MeasuredValues(measuredValueChannel <-chan types.MeasuredValue) []types.MeasuredValue {
|
||||||
|
cachedMeasuredValues := make([]types.MeasuredValue, 0)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case measuredValue, more := <-measuredValueChannel:
|
||||||
|
if more {
|
||||||
|
cachedMeasuredValues = append(cachedMeasuredValues, measuredValue)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return cachedMeasuredValues
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,20 +0,0 @@
|
|||||||
package collect
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/go-flucky/flucky/pkg/types"
|
|
||||||
)
|
|
||||||
|
|
||||||
func Temperatures(temperatureChannel <-chan *types.Temperature) []*types.Temperature {
|
|
||||||
temperatureList := make([]*types.Temperature, 0)
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case temperature, more := <-temperatureChannel:
|
|
||||||
if more {
|
|
||||||
temperatureList = append(temperatureList, temperature)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return temperatureList
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,187 +1,175 @@
|
|||||||
package logfile
|
package logfile
|
||||||
|
|
||||||
import (
|
// type csvLogfile struct {
|
||||||
"encoding/csv"
|
// logfile string
|
||||||
"encoding/json"
|
// }
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/pkg/sensor"
|
// func (cl *csvLogfile) GetLogfile() string {
|
||||||
"github.com/go-flucky/flucky/pkg/types"
|
// return cl.logfile
|
||||||
)
|
// }
|
||||||
|
|
||||||
type csvLogfile struct {
|
// func (cl *csvLogfile) ReadHumidities() ([]*types.Humidity, error) {
|
||||||
logfile string
|
// if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
||||||
}
|
// return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
|
||||||
|
// }
|
||||||
|
|
||||||
func (cl *csvLogfile) GetLogfile() string {
|
// humidities := make([]*types.Humidity, 0)
|
||||||
return cl.logfile
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cl *csvLogfile) ReadHumidities() ([]*types.Humidity, error) {
|
// f, err := os.Open(cl.logfile)
|
||||||
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
// if err != nil {
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, cl.logfile)
|
// return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
|
||||||
}
|
// }
|
||||||
|
// defer f.Close()
|
||||||
|
|
||||||
humidities := make([]*types.Humidity, 0)
|
// jsonDecoder := json.NewDecoder(f)
|
||||||
|
// err = jsonDecoder.Decode(&humidities)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
||||||
|
// }
|
||||||
|
|
||||||
f, err := os.Open(cl.logfile)
|
// return humidities, nil
|
||||||
if err != nil {
|
// }
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, cl.logfile)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
jsonDecoder := json.NewDecoder(f)
|
// func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
|
||||||
err = jsonDecoder.Decode(&humidities)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return humidities, nil
|
// if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
||||||
}
|
// return nil, fmt.Errorf("%v %v: %v", errorLogfileNotFound, cl.logfile, err)
|
||||||
|
// }
|
||||||
|
|
||||||
func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
|
// temperatures := make([]*types.Temperature, 0)
|
||||||
|
|
||||||
if _, err := os.Stat(cl.logfile); os.IsNotExist(err) {
|
// f, err := os.Open(cl.logfile)
|
||||||
return nil, fmt.Errorf("%v %v: %v", errorLogfileNotFound, cl.logfile, err)
|
// if err != nil {
|
||||||
}
|
// return nil, fmt.Errorf("%v %v: %v", errorLogfileOpen, cl.logfile, err)
|
||||||
|
// }
|
||||||
|
// defer f.Close()
|
||||||
|
|
||||||
temperatures := make([]*types.Temperature, 0)
|
// r := csv.NewReader(f)
|
||||||
|
// records, err := r.ReadAll()
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, cl.logfile, err)
|
||||||
|
// }
|
||||||
|
|
||||||
f, err := os.Open(cl.logfile)
|
// for _, record := range records {
|
||||||
if err != nil {
|
// times := make([]time.Time, 0)
|
||||||
return nil, fmt.Errorf("%v %v: %v", errorLogfileOpen, cl.logfile, err)
|
// for _, j := range []int{3, 4} {
|
||||||
}
|
// time, err := time.Parse(timeFormat, record[j])
|
||||||
defer f.Close()
|
// if err != nil {
|
||||||
|
// return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[j], err)
|
||||||
|
// }
|
||||||
|
// times = append(times, time)
|
||||||
|
// }
|
||||||
|
|
||||||
r := csv.NewReader(f)
|
// temperatureValue, err := strconv.ParseFloat(record[1], 64)
|
||||||
records, err := r.ReadAll()
|
// if err != nil {
|
||||||
if err != nil {
|
// return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[1], err)
|
||||||
return nil, fmt.Errorf("%v %v: %v", errorLogfileDecode, cl.logfile, err)
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
for _, record := range records {
|
// measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(record[2])
|
||||||
times := make([]time.Time, 0)
|
// if err != nil {
|
||||||
for _, j := range []int{3, 4} {
|
// return nil, fmt.Errorf("%v %v: %v", errorParseMeasurementUnit, record[2], err)
|
||||||
time, err := time.Parse(timeFormat, record[j])
|
// }
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[j], err)
|
|
||||||
}
|
|
||||||
times = append(times, time)
|
|
||||||
}
|
|
||||||
|
|
||||||
temperatureValue, err := strconv.ParseFloat(record[1], 64)
|
// temperature := &types.Temperature{
|
||||||
if err != nil {
|
// TemperatureID: record[0], // 0
|
||||||
return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[1], err)
|
// TemperatureValue: temperatureValue, // 1
|
||||||
}
|
// TemperatureUnit: measurementUnit, // 2
|
||||||
|
// TemperatureFromDate: times[0], // 3
|
||||||
|
// TemperatureTillDate: times[1], // 4
|
||||||
|
// SensorID: record[5], // 5
|
||||||
|
// }
|
||||||
|
|
||||||
measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(record[2])
|
// // Creation date
|
||||||
if err != nil {
|
// temperatureCreationDate, err := time.Parse(timeFormat, record[6])
|
||||||
return nil, fmt.Errorf("%v %v: %v", errorParseMeasurementUnit, record[2], err)
|
// if err != nil {
|
||||||
}
|
// return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[5], err)
|
||||||
|
// }
|
||||||
|
// temperature.CreationDate = &temperatureCreationDate
|
||||||
|
|
||||||
temperature := &types.Temperature{
|
// if record[7] != "" {
|
||||||
TemperatureID: record[0], // 0
|
// temperatureUpdateDate, err := time.Parse(timeFormat, record[7])
|
||||||
TemperatureValue: temperatureValue, // 1
|
// if err != nil {
|
||||||
TemperatureUnit: measurementUnit, // 2
|
// return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[7], err)
|
||||||
TemperatureFromDate: times[0], // 3
|
// }
|
||||||
TemperatureTillDate: times[1], // 4
|
|
||||||
SensorID: record[5], // 5
|
|
||||||
}
|
|
||||||
|
|
||||||
// Creation date
|
// temperature.UpdateDate = &temperatureUpdateDate
|
||||||
temperatureCreationDate, err := time.Parse(timeFormat, record[6])
|
// }
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[5], err)
|
|
||||||
}
|
|
||||||
temperature.CreationDate = &temperatureCreationDate
|
|
||||||
|
|
||||||
if record[7] != "" {
|
// temperatures = append(temperatures, temperature)
|
||||||
temperatureUpdateDate, err := time.Parse(timeFormat, record[7])
|
// }
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[7], err)
|
|
||||||
}
|
|
||||||
|
|
||||||
temperature.UpdateDate = &temperatureUpdateDate
|
// return temperatures, nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
temperatures = append(temperatures, temperature)
|
// func (cl *csvLogfile) WriteHumidities(humidities []*types.Humidity) error {
|
||||||
}
|
|
||||||
|
|
||||||
return temperatures, nil
|
// f, err := os.Create(cl.logfile)
|
||||||
}
|
// if err != nil {
|
||||||
|
// return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
|
||||||
|
// }
|
||||||
|
// defer f.Close()
|
||||||
|
|
||||||
func (cl *csvLogfile) WriteHumidities(humidities []*types.Humidity) error {
|
// w := csv.NewWriter(f)
|
||||||
|
|
||||||
f, err := os.Create(cl.logfile)
|
// for _, humidity := range humidities {
|
||||||
if err != nil {
|
// w.Write([]string{
|
||||||
return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
|
// fmt.Sprintf("%v", humidity.HumidityID),
|
||||||
}
|
// fmt.Sprintf("%v", humidity.HumidityValue),
|
||||||
defer f.Close()
|
// fmt.Sprintf("%v", humidity.HumidityFromDate.Format(timeFormat)),
|
||||||
|
// fmt.Sprintf("%v", humidity.HumidityTillDate.Format(timeFormat)),
|
||||||
|
// fmt.Sprintf("%v", humidity.SensorID),
|
||||||
|
// fmt.Sprintf("%v", humidity.CreationDate.Format(timeFormat)),
|
||||||
|
// fmt.Sprintf("%v", humidity.UpdateDate.Format(timeFormat)),
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
|
||||||
w := csv.NewWriter(f)
|
// w.Flush()
|
||||||
|
|
||||||
for _, humidity := range humidities {
|
// return nil
|
||||||
w.Write([]string{
|
// }
|
||||||
fmt.Sprintf("%v", humidity.HumidityID),
|
|
||||||
fmt.Sprintf("%v", humidity.HumidityValue),
|
|
||||||
fmt.Sprintf("%v", humidity.HumidityFromDate.Format(timeFormat)),
|
|
||||||
fmt.Sprintf("%v", humidity.HumidityTillDate.Format(timeFormat)),
|
|
||||||
fmt.Sprintf("%v", humidity.SensorID),
|
|
||||||
fmt.Sprintf("%v", humidity.CreationDate.Format(timeFormat)),
|
|
||||||
fmt.Sprintf("%v", humidity.UpdateDate.Format(timeFormat)),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Flush()
|
// func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
|
||||||
|
// f, err := os.Create(cl.logfile)
|
||||||
|
// if err != nil {
|
||||||
|
// return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
|
||||||
|
// }
|
||||||
|
// defer f.Close()
|
||||||
|
|
||||||
return nil
|
// writeCreationDate(temperatures)
|
||||||
}
|
|
||||||
|
|
||||||
func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
|
// w := csv.NewWriter(f)
|
||||||
f, err := os.Create(cl.logfile)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("%v: %v", errorLogfileCreate, cl.logfile)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
writeCreationDate(temperatures)
|
// for _, temperature := range temperatures {
|
||||||
|
// record := make([]string, 0)
|
||||||
|
|
||||||
w := csv.NewWriter(f)
|
// if temperature.UpdateDate != nil {
|
||||||
|
// record = []string{
|
||||||
|
// fmt.Sprintf("%v", temperature.TemperatureID),
|
||||||
|
// fmt.Sprintf("%v", temperature.TemperatureValue),
|
||||||
|
// fmt.Sprintf("%v", temperature.TemperatureUnit),
|
||||||
|
// fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)),
|
||||||
|
// fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
|
||||||
|
// fmt.Sprintf("%v", temperature.SensorID),
|
||||||
|
// fmt.Sprintf("%v", temperature.CreationDate.Format(timeFormat)),
|
||||||
|
// fmt.Sprintf("%v", temperature.UpdateDate.Format(timeFormat)),
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// record = []string{
|
||||||
|
// fmt.Sprintf("%v", temperature.TemperatureID),
|
||||||
|
// fmt.Sprintf("%v", temperature.TemperatureValue),
|
||||||
|
// fmt.Sprintf("%v", temperature.TemperatureUnit),
|
||||||
|
// fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)),
|
||||||
|
// fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
|
||||||
|
// fmt.Sprintf("%v", temperature.SensorID),
|
||||||
|
// fmt.Sprintf("%v", temperature.CreationDate.Format(timeFormat)),
|
||||||
|
// fmt.Sprintf(""),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
for _, temperature := range temperatures {
|
// w.Write(record)
|
||||||
record := make([]string, 0)
|
// }
|
||||||
|
|
||||||
if temperature.UpdateDate != nil {
|
// w.Flush()
|
||||||
record = []string{
|
|
||||||
fmt.Sprintf("%v", temperature.TemperatureID),
|
|
||||||
fmt.Sprintf("%v", temperature.TemperatureValue),
|
|
||||||
fmt.Sprintf("%v", temperature.TemperatureUnit),
|
|
||||||
fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)),
|
|
||||||
fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
|
|
||||||
fmt.Sprintf("%v", temperature.SensorID),
|
|
||||||
fmt.Sprintf("%v", temperature.CreationDate.Format(timeFormat)),
|
|
||||||
fmt.Sprintf("%v", temperature.UpdateDate.Format(timeFormat)),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
record = []string{
|
|
||||||
fmt.Sprintf("%v", temperature.TemperatureID),
|
|
||||||
fmt.Sprintf("%v", temperature.TemperatureValue),
|
|
||||||
fmt.Sprintf("%v", temperature.TemperatureUnit),
|
|
||||||
fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)),
|
|
||||||
fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
|
|
||||||
fmt.Sprintf("%v", temperature.SensorID),
|
|
||||||
fmt.Sprintf("%v", temperature.CreationDate.Format(timeFormat)),
|
|
||||||
fmt.Sprintf(""),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Write(record)
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
w.Flush()
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@ -22,3 +22,5 @@ var errorNoValidSensorID = errors.New("No sensor id detected or available")
|
|||||||
var errorNoValidTemperatureID = errors.New("No valid temperature id detected or available")
|
var errorNoValidTemperatureID = errors.New("No valid temperature id detected or available")
|
||||||
var errorNoValidTime = errors.New("No time detected or available")
|
var errorNoValidTime = errors.New("No time detected or available")
|
||||||
var errorNoValidTimePeriods = errors.New("No valid time periods")
|
var errorNoValidTimePeriods = errors.New("No valid time periods")
|
||||||
|
|
||||||
|
var errorTypeSwitch = errors.New("Can not detect type via type switch")
|
||||||
|
@ -5,9 +5,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Logfile interface {
|
type Logfile interface {
|
||||||
GetLogfile() string
|
Append(compression bool, measuredValues []types.MeasuredValue) error
|
||||||
ReadHumidities() ([]*types.Humidity, error)
|
Read() ([]types.MeasuredValue, error)
|
||||||
ReadTemperatures() ([]*types.Temperature, error)
|
// ReadCustom(r io.Reader) ([]types.MeasuredValue, error)
|
||||||
WriteHumidities(humidities []*types.Humidity) error
|
Write(measuredValues []types.MeasuredValue) error
|
||||||
WriteTemperatures(temperatures []*types.Temperature) error
|
// WriteCustom(w io.Writer, measuredValues []types.MeasuredValue) error
|
||||||
}
|
}
|
||||||
|
@ -3,68 +3,162 @@ package logfile
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/go-flucky/flucky/pkg/internal/collect"
|
||||||
|
"github.com/go-flucky/flucky/pkg/internal/prittyprint"
|
||||||
|
|
||||||
|
"github.com/go-flucky/flucky/pkg/config"
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/pkg/types"
|
"github.com/go-flucky/flucky/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type jsonLogfile struct {
|
type jsonLogfile struct {
|
||||||
logfile string
|
cnf *config.Configuration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jl *jsonLogfile) GetLogfile() string {
|
func (jl *jsonLogfile) Append(compression bool, measuredValues []types.MeasuredValue) error {
|
||||||
return jl.logfile
|
|
||||||
|
allMeasuredValues, err := jl.Read()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println(len(allMeasuredValues))
|
||||||
|
|
||||||
|
allMeasuredValues = append(allMeasuredValues, measuredValues...)
|
||||||
|
|
||||||
|
log.Println(len(allMeasuredValues))
|
||||||
|
|
||||||
|
err = jl.Write(allMeasuredValues)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jl *jsonLogfile) ReadHumidities() ([]*types.Humidity, error) {
|
func (jl *jsonLogfile) Read() ([]types.MeasuredValue, error) {
|
||||||
if _, err := os.Stat(jl.logfile); os.IsNotExist(err) {
|
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, jl.logfile)
|
measuredValues := make([]types.MeasuredValue, 0)
|
||||||
|
|
||||||
|
humidityValues, err := jl.readHumidities()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
measuredValues = append(measuredValues, humidityValues...)
|
||||||
|
|
||||||
|
temperatureValues, err := jl.readTemperatures()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
measuredValues = append(measuredValues, temperatureValues...)
|
||||||
|
|
||||||
|
return measuredValues, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jl *jsonLogfile) readHumidities() ([]types.MeasuredValue, error) {
|
||||||
|
|
||||||
|
if _, err := os.Stat(jl.cnf.Device.HumidityLogfile); os.IsNotExist(err) {
|
||||||
|
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, jl.cnf.Device.HumidityLogfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
humidities := make([]*types.Humidity, 0)
|
humidities := make([]*types.Humidity, 0)
|
||||||
|
|
||||||
f, err := os.Open(jl.logfile)
|
f, err := os.Open(jl.cnf.Device.HumidityLogfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, jl.logfile)
|
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, jl.cnf.Device.HumidityLogfile)
|
||||||
|
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
jsonDecoder := json.NewDecoder(f)
|
if err = json.NewDecoder(f).Decode(&humidities); err != nil {
|
||||||
err = jsonDecoder.Decode(&humidities)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return humidities, nil
|
measuredValues := make([]types.MeasuredValue, 0)
|
||||||
|
|
||||||
|
for _, humidity := range humidities {
|
||||||
|
measuredValues = append(measuredValues, humidity)
|
||||||
|
}
|
||||||
|
|
||||||
|
return measuredValues, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jl *jsonLogfile) ReadTemperatures() ([]*types.Temperature, error) {
|
func (jl *jsonLogfile) readTemperatures() ([]types.MeasuredValue, error) {
|
||||||
if _, err := os.Stat(jl.logfile); os.IsNotExist(err) {
|
if _, err := os.Stat(jl.cnf.Device.TemperatureLogfile); os.IsNotExist(err) {
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, jl.logfile)
|
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, jl.cnf.Device.TemperatureLogfile)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
temperatures := make([]*types.Temperature, 0)
|
temperatures := make([]*types.Temperature, 0)
|
||||||
|
|
||||||
f, err := os.Open(jl.logfile)
|
f, err := os.Open(jl.cnf.Device.TemperatureLogfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, jl.logfile)
|
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, jl.cnf.Device.TemperatureLogfile)
|
||||||
|
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
jsonDecoder := json.NewDecoder(f)
|
if err := json.NewDecoder(f).Decode(&temperatures); err != nil {
|
||||||
err = jsonDecoder.Decode(&temperatures)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return temperatures, nil
|
measuredValues := make([]types.MeasuredValue, 0)
|
||||||
|
|
||||||
|
for _, temperature := range temperatures {
|
||||||
|
measuredValues = append(measuredValues, temperature)
|
||||||
|
}
|
||||||
|
|
||||||
|
return measuredValues, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jl *jsonLogfile) WriteHumidities(humidities []*types.Humidity) error {
|
func (jl *jsonLogfile) Write(measuredValues []types.MeasuredValue) error {
|
||||||
|
humidities := make([]*types.Humidity, 0)
|
||||||
|
temperatures := make([]*types.Temperature, 0)
|
||||||
|
|
||||||
f, err := os.Create(jl.logfile)
|
for _, measuredValue := range measuredValues {
|
||||||
|
switch v := measuredValue.(type) {
|
||||||
|
case *types.Humidity:
|
||||||
|
humidities = append(humidities, v)
|
||||||
|
case *types.Temperature:
|
||||||
|
temperatures = append(temperatures, v)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("%v", errorTypeSwitch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
errorChannel := make(chan error, 0)
|
||||||
|
|
||||||
|
wg := new(sync.WaitGroup)
|
||||||
|
wg.Add(2)
|
||||||
|
|
||||||
|
go jl.writeHumidities(humidities, errorChannel, wg)
|
||||||
|
go jl.writeTemperatures(temperatures, errorChannel, wg)
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
errors := collect.Errors(errorChannel)
|
||||||
|
if len(errors) > 0 {
|
||||||
|
return prittyprint.FormatErrors(errors)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (jl *jsonLogfile) writeHumidities(humidities []*types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
|
f, err := os.Create(jl.cnf.Device.HumidityLogfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%v: %v", errorLogfileCreate, jl.logfile)
|
errorChannel <- fmt.Errorf("%v: %v", errorLogfileCreate, jl.cnf.Device.HumidityLogfile)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
@ -72,25 +166,28 @@ func (jl *jsonLogfile) WriteHumidities(humidities []*types.Humidity) error {
|
|||||||
jsonEncoder.SetIndent("", " ")
|
jsonEncoder.SetIndent("", " ")
|
||||||
err = jsonEncoder.Encode(humidities)
|
err = jsonEncoder.Encode(humidities)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%v: %v", errorLogfileEncode, err)
|
errorChannel <- fmt.Errorf("%v: %v", errorLogfileEncode, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jl *jsonLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
|
func (jl *jsonLogfile) writeTemperatures(temperatures []*types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||||
f, err := os.Create(jl.logfile)
|
defer wg.Done()
|
||||||
|
|
||||||
|
f, err := os.Create(jl.cnf.Device.TemperatureLogfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%v: %v", errorLogfileCreate, jl.logfile)
|
errorChannel <- fmt.Errorf("%v: %v", errorLogfileCreate, jl.cnf.Device.TemperatureLogfile)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
writeCreationDate(temperatures)
|
//writeCreationDate(temperatures)
|
||||||
|
|
||||||
jsonEncoder := json.NewEncoder(f)
|
jsonEncoder := json.NewEncoder(f)
|
||||||
jsonEncoder.SetIndent("", " ")
|
jsonEncoder.SetIndent("", " ")
|
||||||
err = jsonEncoder.Encode(temperatures)
|
err = jsonEncoder.Encode(temperatures)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%v: %v", errorLogfileEncode, err)
|
errorChannel <- fmt.Errorf("%v: %v", errorLogfileEncode, err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -1,45 +1,39 @@
|
|||||||
package logfile
|
package logfile
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"github.com/go-flucky/flucky/pkg/config"
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"sort"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/pkg/types"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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 = "2006-01-02 15:04:05.999999999 -0700"
|
// var timeFormat = "2006-01-02 15:04:05.999999999 -0700"
|
||||||
|
|
||||||
// AppendTemperatures with temperature values from a logfile. As additional
|
// // AppendTemperatures with temperature values from a logfile. As additional
|
||||||
// option it's possible to compress the temperature data.
|
// // option it's possible to compress the temperature data.
|
||||||
func AppendTemperatures(logfile Logfile, compression bool, temperatures []*types.Temperature) error {
|
// func AppendTemperatures(logfile Logfile, compression bool, temperatures []*types.Temperature) error {
|
||||||
|
|
||||||
allTemperatures := make([]*types.Temperature, 0)
|
// allTemperatures := make([]*types.Temperature, 0)
|
||||||
|
|
||||||
if _, err := os.Stat(logfile.GetLogfile()); err == nil {
|
// if _, err := os.Stat(logfile.GetLogfile()); err == nil {
|
||||||
temperaturesFromLogfile, err := logfile.ReadTemperatures()
|
// temperaturesFromLogfile, err := logfile.ReadTemperatures()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
|
|
||||||
allTemperatures = append(allTemperatures, temperaturesFromLogfile...)
|
// allTemperatures = append(allTemperatures, temperaturesFromLogfile...)
|
||||||
}
|
// }
|
||||||
|
|
||||||
allTemperatures = append(allTemperatures, temperatures...)
|
// allTemperatures = append(allTemperatures, temperatures...)
|
||||||
|
|
||||||
if compression {
|
// if compression {
|
||||||
allTemperatures = CompressTemperature(allTemperatures)
|
// allTemperatures = CompressTemperature(allTemperatures)
|
||||||
}
|
// }
|
||||||
|
|
||||||
err := logfile.WriteTemperatures(allTemperatures)
|
// err := logfile.WriteTemperatures(allTemperatures)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return err
|
// return err
|
||||||
}
|
// }
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
// CompressTemperature compresses the temperatures from an array. It is checked
|
// CompressTemperature compresses the temperatures from an array. It is checked
|
||||||
// whether the measured temperature of a value corresponds to that of the
|
// whether the measured temperature of a value corresponds to that of the
|
||||||
@ -47,116 +41,118 @@ func AppendTemperatures(logfile Logfile, compression bool, temperatures []*types
|
|||||||
// validity date of the predecessor value is set to that of the current value.
|
// validity date of the predecessor value is set to that of the current value.
|
||||||
// No information is lost as a result. The validity period of the measured value
|
// No information is lost as a result. The validity period of the measured value
|
||||||
// is thereby exclusively increased.
|
// is thereby exclusively increased.
|
||||||
func CompressTemperature(temperatures []*types.Temperature) []*types.Temperature {
|
// func Compression(measuredValues []types) []*types.Temperature {
|
||||||
compressedTemperatures := make([]*types.Temperature, 0)
|
// compressedTemperatures := make([]*types.Temperature, 0)
|
||||||
lastTemperatureBySensors := make(map[string]*types.Temperature, 0)
|
// lastTemperatureBySensors := make(map[string]*types.Temperature, 0)
|
||||||
|
|
||||||
// Sort all measured temperatures beforehand by the starting validity date to
|
// // Sort all measured temperatures beforehand by the starting validity date to
|
||||||
// avoid errors when compressing the temperatures.
|
// // avoid errors when compressing the temperatures.
|
||||||
SortTemperatures(temperatures)
|
// SortTemperatures(temperatures)
|
||||||
|
|
||||||
for _, temperature := range temperatures {
|
// for _, temperature := range temperatures {
|
||||||
if lastTemperatureBySensor, ok := lastTemperatureBySensors[temperature.SensorID]; ok {
|
// if lastTemperatureBySensor, ok := lastTemperatureBySensors[temperature.SensorID]; ok {
|
||||||
if lastTemperatureBySensor.TemperatureValue == temperature.TemperatureValue {
|
// if lastTemperatureBySensor.TemperatureValue == temperature.TemperatureValue {
|
||||||
|
|
||||||
lastTemperatureBySensors[temperature.SensorID].TemperatureTillDate = temperature.TemperatureTillDate
|
// lastTemperatureBySensors[temperature.SensorID].TemperatureTillDate = temperature.TemperatureTillDate
|
||||||
|
|
||||||
now := time.Now()
|
// now := time.Now()
|
||||||
lastTemperatureBySensors[temperature.SensorID].UpdateDate = &now
|
// lastTemperatureBySensors[temperature.SensorID].UpdateDate = &now
|
||||||
} else {
|
// } else {
|
||||||
compressedTemperatures = append(compressedTemperatures, lastTemperatureBySensors[temperature.SensorID])
|
// compressedTemperatures = append(compressedTemperatures, lastTemperatureBySensors[temperature.SensorID])
|
||||||
lastTemperatureBySensors[temperature.SensorID] = temperature
|
// lastTemperatureBySensors[temperature.SensorID] = temperature
|
||||||
}
|
// }
|
||||||
} else {
|
// } else {
|
||||||
lastTemperatureBySensors[temperature.SensorID] = temperature
|
// lastTemperatureBySensors[temperature.SensorID] = temperature
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Copy all remaining entries from the map into the array
|
// // Copy all remaining entries from the map into the array
|
||||||
for _, lastTemperatureBySensor := range lastTemperatureBySensors {
|
// for _, lastTemperatureBySensor := range lastTemperatureBySensors {
|
||||||
compressedTemperatures = append(compressedTemperatures, lastTemperatureBySensor)
|
// compressedTemperatures = append(compressedTemperatures, lastTemperatureBySensor)
|
||||||
}
|
// }
|
||||||
|
|
||||||
return compressedTemperatures
|
// return compressedTemperatures
|
||||||
}
|
// }
|
||||||
|
|
||||||
// New returns a log file with basic functions for reading and writing data. The
|
// New returns a log file with basic functions for reading and writing data. The
|
||||||
// file extension of the logfile is taken into account to format the logfile
|
// file extension of the logfile is taken into account to format the logfile
|
||||||
// into the correct format.
|
// into the correct format.
|
||||||
func New(logfile string) Logfile {
|
func New(cnf *config.Configuration) Logfile {
|
||||||
|
|
||||||
ext := filepath.Ext(logfile)
|
// switch ext {
|
||||||
|
// case ".csv":
|
||||||
|
// return &csvLogfile{
|
||||||
|
// logfile: logfile,
|
||||||
|
// }
|
||||||
|
// case ".json":
|
||||||
|
// return &jsonLogfile{
|
||||||
|
// cnf: cnf,
|
||||||
|
// }
|
||||||
|
// case ".xml":
|
||||||
|
// return &xmlLogfile{
|
||||||
|
// logfile: logfile,
|
||||||
|
// }
|
||||||
|
// default:
|
||||||
|
// return &jsonLogfile{
|
||||||
|
// logfile: logfile,
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
switch ext {
|
|
||||||
case ".csv":
|
|
||||||
return &csvLogfile{
|
|
||||||
logfile: logfile,
|
|
||||||
}
|
|
||||||
case ".json":
|
|
||||||
return &jsonLogfile{
|
return &jsonLogfile{
|
||||||
logfile: logfile,
|
cnf: cnf,
|
||||||
}
|
|
||||||
case ".xml":
|
|
||||||
return &xmlLogfile{
|
|
||||||
logfile: logfile,
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return &jsonLogfile{
|
|
||||||
logfile: logfile,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SplittTemperatures into multiple arrays. The Size can be defined by
|
// // SplittTemperatures into multiple arrays. The Size can be defined by
|
||||||
// temperatureSplitBy parameter.
|
// // temperatureSplitBy parameter.
|
||||||
func SplittTemperatures(temperatures []*types.Temperature, templeratureSplitBy int) [][]*types.Temperature {
|
// func SplittTemperatures(temperatures []*types.Temperature, templeratureSplitBy int) [][]*types.Temperature {
|
||||||
splittedTemperatures := make([][]*types.Temperature, 0)
|
// splittedTemperatures := make([][]*types.Temperature, 0)
|
||||||
newTemperatures := make([]*types.Temperature, 0)
|
// newTemperatures := make([]*types.Temperature, 0)
|
||||||
for _, temperature := range temperatures {
|
// for _, temperature := range temperatures {
|
||||||
if len(newTemperatures) == templeratureSplitBy {
|
// if len(newTemperatures) == templeratureSplitBy {
|
||||||
splittedTemperatures = append(splittedTemperatures, newTemperatures)
|
// splittedTemperatures = append(splittedTemperatures, newTemperatures)
|
||||||
newTemperatures = make([]*types.Temperature, 0)
|
// newTemperatures = make([]*types.Temperature, 0)
|
||||||
}
|
// }
|
||||||
newTemperatures = append(newTemperatures, temperature)
|
// newTemperatures = append(newTemperatures, temperature)
|
||||||
}
|
// }
|
||||||
splittedTemperatures = append(splittedTemperatures, newTemperatures)
|
// splittedTemperatures = append(splittedTemperatures, newTemperatures)
|
||||||
return splittedTemperatures
|
// return splittedTemperatures
|
||||||
}
|
// }
|
||||||
|
|
||||||
// SortTemperatures by TemperatureFromDate
|
// // SortTemperatures by TemperatureFromDate
|
||||||
func SortTemperatures(temperatures []*types.Temperature) {
|
// func SortTemperatures(temperatures []*types.Temperature) {
|
||||||
sort.SliceStable(temperatures, func(i int, j int) bool {
|
// sort.SliceStable(temperatures, func(i int, j int) bool {
|
||||||
return temperatures[i].TemperatureFromDate.Before(temperatures[j].TemperatureFromDate)
|
// return temperatures[i].TemperatureFromDate.Before(temperatures[j].TemperatureFromDate)
|
||||||
})
|
// })
|
||||||
}
|
// }
|
||||||
|
|
||||||
// ValidateTemperatures Checks if the temperature data is valid.
|
// // ValidateTemperatures Checks if the temperature data is valid.
|
||||||
// - Check the temperature id (uuid)
|
// // - Check the temperature id (uuid)
|
||||||
// - Checks whether the time specifications are historically in a sequence.
|
// // - Checks whether the time specifications are historically in a sequence.
|
||||||
// - Check the sensor id (uuid)
|
// // - Check the sensor id (uuid)
|
||||||
func ValidateTemperatures(temperatures []*types.Temperature) error {
|
// func ValidateTemperatures(temperatures []*types.Temperature) error {
|
||||||
for _, temperature := range temperatures {
|
// for _, temperature := range temperatures {
|
||||||
if !validUUID.MatchString(temperature.TemperatureID) {
|
// if !validUUID.MatchString(temperature.TemperatureID) {
|
||||||
return errorNoValidTemperatureID
|
// return errorNoValidTemperatureID
|
||||||
} else if temperature.TemperatureValue == 0 {
|
// } else if temperature.TemperatureValue == 0 {
|
||||||
return errorNoValidMesuredValue
|
// return errorNoValidMesuredValue
|
||||||
} else if temperature.TemperatureFromDate.After(temperature.TemperatureTillDate) {
|
// } else if temperature.TemperatureFromDate.After(temperature.TemperatureTillDate) {
|
||||||
return errorNoValidTimePeriods
|
// return errorNoValidTimePeriods
|
||||||
} else if !validUUID.MatchString(temperature.SensorID) {
|
// } else if !validUUID.MatchString(temperature.SensorID) {
|
||||||
return errorNoValidSensorID
|
// return errorNoValidSensorID
|
||||||
} else if temperature.CreationDate.After(*temperature.UpdateDate) && temperature.UpdateDate != nil {
|
// } else if temperature.CreationDate.After(*temperature.UpdateDate) && temperature.UpdateDate != nil {
|
||||||
return errorNoValidTimePeriods
|
// return errorNoValidTimePeriods
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
func writeCreationDate(temperatures []*types.Temperature) {
|
// func writeCreationDate(temperatures []*types.Temperature) {
|
||||||
now := time.Now()
|
// now := time.Now()
|
||||||
for _, temperature := range temperatures {
|
// for _, temperature := range temperatures {
|
||||||
if temperature.CreationDate == nil {
|
// if temperature.CreationDate == nil {
|
||||||
temperature.CreationDate = &now
|
// temperature.CreationDate = &now
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
@ -1,121 +1,112 @@
|
|||||||
package logfile
|
package logfile
|
||||||
|
|
||||||
import (
|
// type xmlLogfile struct {
|
||||||
"encoding/xml"
|
// logfile string
|
||||||
"fmt"
|
// }
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/pkg/types"
|
// func (xl *xmlLogfile) GetLogfile() string {
|
||||||
)
|
// return xl.logfile
|
||||||
|
// }
|
||||||
|
|
||||||
type xmlLogfile struct {
|
// func (xl *xmlLogfile) ReadHumidities() ([]*types.Humidity, error) {
|
||||||
logfile string
|
// if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
|
||||||
}
|
// return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
|
||||||
|
// }
|
||||||
|
|
||||||
func (xl *xmlLogfile) GetLogfile() string {
|
// humidities := make([]*types.Humidity, 0)
|
||||||
return xl.logfile
|
|
||||||
}
|
|
||||||
|
|
||||||
func (xl *xmlLogfile) ReadHumidities() ([]*types.Humidity, error) {
|
// f, err := os.Open(xl.logfile)
|
||||||
if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
|
// if err != nil {
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
|
// return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
||||||
}
|
// }
|
||||||
|
// defer f.Close()
|
||||||
|
|
||||||
humidities := make([]*types.Humidity, 0)
|
// xmlDecoder := xml.NewDecoder(f)
|
||||||
|
// err = xmlDecoder.Decode(&humidities)
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
||||||
|
// }
|
||||||
|
|
||||||
f, err := os.Open(xl.logfile)
|
// return humidities, nil
|
||||||
if err != nil {
|
// }
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
xmlDecoder := xml.NewDecoder(f)
|
// func (xl *xmlLogfile) ReadTemperatures() ([]*types.Temperature, error) {
|
||||||
err = xmlDecoder.Decode(&humidities)
|
// if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
|
||||||
if err != nil {
|
// return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
return humidities, nil
|
// f, err := os.Open(xl.logfile)
|
||||||
}
|
// if err != nil {
|
||||||
|
// return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
||||||
|
// }
|
||||||
|
// defer f.Close()
|
||||||
|
|
||||||
func (xl *xmlLogfile) ReadTemperatures() ([]*types.Temperature, error) {
|
// bytes, err := ioutil.ReadAll(f)
|
||||||
if _, err := os.Stat(xl.logfile); os.IsNotExist(err) {
|
// if err != nil {
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
|
// return nil, fmt.Errorf("%v: %v", errorLogfileRead, err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
f, err := os.Open(xl.logfile)
|
// tmpTemperatures := new(Temperatures)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
bytes, err := ioutil.ReadAll(f)
|
// err = xml.Unmarshal(bytes, tmpTemperatures)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileRead, err)
|
// return nil, fmt.Errorf("%v: %v", errorLogfileUnmarshal, err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
tmpTemperatures := new(Temperatures)
|
// temperatures := make([]*types.Temperature, 0)
|
||||||
|
|
||||||
err = xml.Unmarshal(bytes, tmpTemperatures)
|
// for _, tmpTemperature := range tmpTemperatures.Temperatures {
|
||||||
if err != nil {
|
// temperatures = append(temperatures, tmpTemperature.Temperature)
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileUnmarshal, err)
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
temperatures := make([]*types.Temperature, 0)
|
// return temperatures, nil
|
||||||
|
// }
|
||||||
|
|
||||||
for _, tmpTemperature := range tmpTemperatures.Temperatures {
|
// func (xl *xmlLogfile) WriteHumidities(humidities []*types.Humidity) error {
|
||||||
temperatures = append(temperatures, tmpTemperature.Temperature)
|
|
||||||
}
|
|
||||||
|
|
||||||
return temperatures, nil
|
// f, err := os.Create(xl.logfile)
|
||||||
}
|
// if err != nil {
|
||||||
|
// return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile)
|
||||||
|
// }
|
||||||
|
// defer f.Close()
|
||||||
|
|
||||||
func (xl *xmlLogfile) WriteHumidities(humidities []*types.Humidity) error {
|
// xmlEncoder := xml.NewEncoder(f)
|
||||||
|
// xmlEncoder.Indent("", " ")
|
||||||
|
// err = xmlEncoder.Encode(humidities)
|
||||||
|
// if err != nil {
|
||||||
|
// return fmt.Errorf("%v: %v", errorLogfileEncode, err)
|
||||||
|
// }
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
|
||||||
f, err := os.Create(xl.logfile)
|
// func (xl *xmlLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
|
||||||
if err != nil {
|
// f, err := os.Create(xl.logfile)
|
||||||
return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile)
|
// if err != nil {
|
||||||
}
|
// return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile)
|
||||||
defer f.Close()
|
// }
|
||||||
|
// defer f.Close()
|
||||||
|
|
||||||
xmlEncoder := xml.NewEncoder(f)
|
// writeCreationDate(temperatures)
|
||||||
xmlEncoder.Indent("", " ")
|
|
||||||
err = xmlEncoder.Encode(humidities)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("%v: %v", errorLogfileEncode, err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (xl *xmlLogfile) WriteTemperatures(temperatures []*types.Temperature) error {
|
// tmpTemperatures := new(Temperatures)
|
||||||
f, err := os.Create(xl.logfile)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("%v: %v", errorLogfileCreate, xl.logfile)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
writeCreationDate(temperatures)
|
// for _, temperature := range temperatures {
|
||||||
|
// tmpTemperature := &Temperature{
|
||||||
|
// Temperature: temperature,
|
||||||
|
// }
|
||||||
|
|
||||||
tmpTemperatures := new(Temperatures)
|
// tmpTemperatures.Temperatures = append(tmpTemperatures.Temperatures, tmpTemperature)
|
||||||
|
// }
|
||||||
|
|
||||||
for _, temperature := range temperatures {
|
// bytes, err := xml.MarshalIndent(tmpTemperatures, "", " ")
|
||||||
tmpTemperature := &Temperature{
|
// if err != nil {
|
||||||
Temperature: temperature,
|
// return fmt.Errorf("%v: %v", errorLogfileMarshal, err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
tmpTemperatures.Temperatures = append(tmpTemperatures.Temperatures, tmpTemperature)
|
// _, err = f.Write(bytes)
|
||||||
}
|
// if err != nil {
|
||||||
|
// return fmt.Errorf("%v: %v", errorLogfileWrite, err)
|
||||||
|
// }
|
||||||
|
|
||||||
bytes, err := xml.MarshalIndent(tmpTemperatures, "", " ")
|
// return nil
|
||||||
if err != nil {
|
// }
|
||||||
return fmt.Errorf("%v: %v", errorLogfileMarshal, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = f.Write(bytes)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("%v: %v", errorLogfileWrite, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@ -3,7 +3,6 @@ package sensor
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -22,8 +21,9 @@ func (s *DHT11) GetSensorModel() types.SensorModel {
|
|||||||
return s.Sensor.SensorModel
|
return s.Sensor.SensorModel
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadHumidity measure the humidity
|
// Read measured values
|
||||||
func (s *DHT11) ReadHumidity(round float64) (*types.Humidity, error) {
|
func (s *DHT11) Read() ([]types.MeasuredValue, error) {
|
||||||
|
|
||||||
err := dht.HostInit()
|
err := dht.HostInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("HostInit error: %v", err)
|
return nil, fmt.Errorf("HostInit error: %v", err)
|
||||||
@ -39,117 +39,59 @@ func (s *DHT11) ReadHumidity(round float64) (*types.Humidity, error) {
|
|||||||
return nil, fmt.Errorf("NewDHT error: %v", err)
|
return nil, fmt.Errorf("NewDHT error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
humidityValue, _, err := dht.Read()
|
humidityValue, temperatureValue, err := dht.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Read error: %v", err)
|
return nil, fmt.Errorf("Read error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if round != 0 {
|
measuredValues := []types.MeasuredValue{
|
||||||
humidityValue = math.Round(humidityValue/round) * round
|
&types.Humidity{
|
||||||
}
|
|
||||||
|
|
||||||
humidity := &types.Humidity{
|
|
||||||
HumidityID: uuid.NewV4().String(),
|
HumidityID: uuid.NewV4().String(),
|
||||||
HumidityValue: humidityValue,
|
HumidityValue: humidityValue,
|
||||||
HumidityFromDate: time.Now(),
|
HumidityFromDate: time.Now(),
|
||||||
HumidityTillDate: time.Now(),
|
HumidityTillDate: time.Now(),
|
||||||
SensorID: s.SensorID,
|
SensorID: s.SensorID,
|
||||||
}
|
},
|
||||||
|
&types.Temperature{
|
||||||
return humidity, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadHumidityWriteIntoChannel and write values into a channel
|
|
||||||
func (s *DHT11) ReadHumidityWriteIntoChannel(round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
|
|
||||||
if wg != nil {
|
|
||||||
defer wg.Done()
|
|
||||||
}
|
|
||||||
|
|
||||||
humidity, err := s.ReadHumidity(round)
|
|
||||||
if err != nil {
|
|
||||||
errorChannel <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
humidityChannel <- humidity
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadHumidityContinously into a channel until context closed
|
|
||||||
func (s *DHT11) ReadHumidityContinously(ctx context.Context, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
s.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadTemperature measure the temperature
|
|
||||||
func (s *DHT11) ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, error) {
|
|
||||||
err := dht.HostInit()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("HostInit error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
gpio, err := types.GPIOToString(*s.GPIONumber)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
dht, err := dht.NewDHT(gpio, dht.Celsius, "")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("NewDHT error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, temperatureValue, err := dht.Read()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Read error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert temperature degree
|
|
||||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
|
|
||||||
|
|
||||||
if round != 0 {
|
|
||||||
temperatureValue = math.Round(temperatureValue/round) * round
|
|
||||||
}
|
|
||||||
|
|
||||||
temperature := &types.Temperature{
|
|
||||||
TemperatureID: uuid.NewV4().String(),
|
TemperatureID: uuid.NewV4().String(),
|
||||||
TemperatureValue: temperatureValue,
|
TemperatureValue: temperatureValue,
|
||||||
TemperatureUnit: degree,
|
|
||||||
TemperatureFromDate: time.Now(),
|
TemperatureFromDate: time.Now(),
|
||||||
TemperatureTillDate: time.Now(),
|
TemperatureTillDate: time.Now(),
|
||||||
SensorID: s.SensorID,
|
SensorID: s.SensorID,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return temperature, nil
|
return measuredValues, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadTemperatureWriteIntoChannel and write values into a channel
|
// ReadChannel reads the measured values from the sensor and writes them to a
|
||||||
func (s *DHT11) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
// channel.
|
||||||
|
func (s *DHT11) ReadChannel(measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||||
if wg != nil {
|
if wg != nil {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
temperature, err := s.ReadTemperature(degree, round)
|
measuredValues, err := s.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChannel <- err
|
errorChannel <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
temperatureChannel <- temperature
|
|
||||||
|
for _, measuredValue := range measuredValues {
|
||||||
|
measuredValueChannel <- measuredValue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadTemperatureContinously into a channel until context closed
|
// ReadContinously reads the measured values continously from the sensor and
|
||||||
func (s *DHT11) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
|
// writes them to a channel.
|
||||||
|
func (s *DHT11) ReadContinously(ctx context.Context, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
|
s.ReadChannel(measuredValueChannel, errorChannel, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package sensor
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -22,8 +21,9 @@ func (s *DHT22) GetSensorModel() types.SensorModel {
|
|||||||
return s.Sensor.SensorModel
|
return s.Sensor.SensorModel
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadHumidity measure the humidity
|
// Read measured values
|
||||||
func (s *DHT22) ReadHumidity(round float64) (*types.Humidity, error) {
|
func (s *DHT22) Read() ([]types.MeasuredValue, error) {
|
||||||
|
|
||||||
err := dht.HostInit()
|
err := dht.HostInit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("HostInit error: %v", err)
|
return nil, fmt.Errorf("HostInit error: %v", err)
|
||||||
@ -39,118 +39,59 @@ func (s *DHT22) ReadHumidity(round float64) (*types.Humidity, error) {
|
|||||||
return nil, fmt.Errorf("NewDHT error: %v", err)
|
return nil, fmt.Errorf("NewDHT error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
humidityValue, _, err := dht.Read()
|
humidityValue, temperatureValue, err := dht.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Read error: %v", err)
|
return nil, fmt.Errorf("Read error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if round != 0 {
|
measuredValues := []types.MeasuredValue{
|
||||||
humidityValue = math.Round(humidityValue/round) * round
|
&types.Humidity{
|
||||||
}
|
|
||||||
|
|
||||||
humidity := &types.Humidity{
|
|
||||||
HumidityID: uuid.NewV4().String(),
|
HumidityID: uuid.NewV4().String(),
|
||||||
HumidityValue: humidityValue,
|
HumidityValue: humidityValue,
|
||||||
HumidityFromDate: time.Now(),
|
HumidityFromDate: time.Now(),
|
||||||
HumidityTillDate: time.Now(),
|
HumidityTillDate: time.Now(),
|
||||||
SensorID: s.SensorID,
|
SensorID: s.SensorID,
|
||||||
}
|
},
|
||||||
|
&types.Temperature{
|
||||||
return humidity, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadHumidityWriteIntoChannel and write values into a channel
|
|
||||||
func (s *DHT22) ReadHumidityWriteIntoChannel(round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
|
|
||||||
if wg != nil {
|
|
||||||
defer wg.Done()
|
|
||||||
}
|
|
||||||
|
|
||||||
humidity, err := s.ReadHumidity(round)
|
|
||||||
if err != nil {
|
|
||||||
errorChannel <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
humidityChannel <- humidity
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadHumidityContinously into a channel until context closed
|
|
||||||
func (s *DHT22) ReadHumidityContinously(ctx context.Context, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
s.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadTemperature measure the temperature
|
|
||||||
func (s *DHT22) ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, error) {
|
|
||||||
err := dht.HostInit()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("HostInit error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
gpio, err := types.GPIOToString(*s.GPIONumber)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
dht, err := dht.NewDHT(gpio, dht.Celsius, "")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("NewDHT error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, temperatureValue, err := dht.Read()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Read error: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert temperature degree
|
|
||||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
|
|
||||||
|
|
||||||
// round
|
|
||||||
if round != 0 {
|
|
||||||
temperatureValue = math.Round(temperatureValue/round) * round
|
|
||||||
}
|
|
||||||
|
|
||||||
temperature := &types.Temperature{
|
|
||||||
TemperatureID: uuid.NewV4().String(),
|
TemperatureID: uuid.NewV4().String(),
|
||||||
TemperatureValue: temperatureValue,
|
TemperatureValue: temperatureValue,
|
||||||
TemperatureUnit: degree,
|
|
||||||
TemperatureFromDate: time.Now(),
|
TemperatureFromDate: time.Now(),
|
||||||
TemperatureTillDate: time.Now(),
|
TemperatureTillDate: time.Now(),
|
||||||
SensorID: s.SensorID,
|
SensorID: s.SensorID,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return temperature, nil
|
return measuredValues, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadTemperatureWriteIntoChannel and write values into a channel
|
// ReadChannel reads the measured values from the sensor and writes them to a
|
||||||
func (s *DHT22) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
// channel.
|
||||||
|
func (s *DHT22) ReadChannel(measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||||
if wg != nil {
|
if wg != nil {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
temperature, err := s.ReadTemperature(degree, round)
|
measuredValues, err := s.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChannel <- err
|
errorChannel <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
temperatureChannel <- temperature
|
|
||||||
|
for _, measuredValue := range measuredValues {
|
||||||
|
measuredValueChannel <- measuredValue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadTemperatureContinously into a channel until context closed
|
// ReadContinously reads the measured values continously from the sensor and
|
||||||
func (s *DHT22) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
|
// writes them to a channel.
|
||||||
|
func (s *DHT22) ReadContinously(ctx context.Context, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
|
s.ReadChannel(measuredValueChannel, errorChannel, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -25,13 +24,8 @@ func (s *DS18B20) GetSensorModel() types.SensorModel {
|
|||||||
return s.Sensor.SensorModel
|
return s.Sensor.SensorModel
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetSensor return the sensor struct
|
// Read measured values
|
||||||
func (s *DS18B20) GetSensor() *types.Sensor {
|
func (s *DS18B20) Read() ([]types.MeasuredValue, error) {
|
||||||
return s.Sensor
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadTemperature measure the temperature
|
|
||||||
func (s *DS18B20) ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, error) {
|
|
||||||
|
|
||||||
data, err := ioutil.ReadFile(filepath.Join("/sys/bus/w1/devices", *s.WireID, "/w1_slave"))
|
data, err := ioutil.ReadFile(filepath.Join("/sys/bus/w1/devices", *s.WireID, "/w1_slave"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -52,50 +46,49 @@ func (s *DS18B20) ReadTemperature(degree types.TemperatureUnit, round float64) (
|
|||||||
|
|
||||||
temperatureValue := c / 1000
|
temperatureValue := c / 1000
|
||||||
|
|
||||||
// Convert temperature degree
|
measuredValues := []types.MeasuredValue{
|
||||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
|
&types.Temperature{
|
||||||
|
|
||||||
// round
|
|
||||||
if round != 0 {
|
|
||||||
temperatureValue = math.Round(temperatureValue/round) * round
|
|
||||||
}
|
|
||||||
|
|
||||||
temperature := &types.Temperature{
|
|
||||||
TemperatureID: uuid.NewV4().String(),
|
TemperatureID: uuid.NewV4().String(),
|
||||||
TemperatureValue: temperatureValue,
|
TemperatureValue: temperatureValue,
|
||||||
TemperatureUnit: degree,
|
|
||||||
TemperatureFromDate: time.Now(),
|
TemperatureFromDate: time.Now(),
|
||||||
TemperatureTillDate: time.Now(),
|
TemperatureTillDate: time.Now(),
|
||||||
SensorID: s.SensorID,
|
SensorID: s.SensorID,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return temperature, nil
|
return measuredValues, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadTemperatureWriteIntoChannel and write values into a channel
|
// ReadChannel reads the measured values from the sensor and writes them to a
|
||||||
func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
// channel.
|
||||||
|
func (s *DS18B20) ReadChannel(measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||||
if wg != nil {
|
if wg != nil {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
temperature, err := s.ReadTemperature(degree, round)
|
measuredValues, err := s.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChannel <- err
|
errorChannel <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
temperatureChannel <- temperature
|
|
||||||
|
for _, measuredValue := range measuredValues {
|
||||||
|
measuredValueChannel <- measuredValue
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadTemperatureContinously into a channel until context closed
|
// ReadContinously reads the measured values continously from the sensor and
|
||||||
func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
|
// writes them to a channel.
|
||||||
|
func (s *DS18B20) ReadContinously(ctx context.Context, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
|
s.ReadChannel(measuredValueChannel, errorChannel, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,18 +7,9 @@ import (
|
|||||||
"github.com/go-flucky/flucky/pkg/types"
|
"github.com/go-flucky/flucky/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HumiditySensor is a interface to describe required functions to measure humidities
|
type Sensor interface {
|
||||||
type HumiditySensor interface {
|
|
||||||
GetSensorModel() types.SensorModel
|
GetSensorModel() types.SensorModel
|
||||||
ReadHumidity(round float64) (*types.Humidity, error)
|
Read() ([]types.MeasuredValue, error)
|
||||||
ReadHumidityWriteIntoChannel(round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup)
|
ReadChannel(measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup)
|
||||||
ReadHumidityContinously(ctx context.Context, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error)
|
ReadContinously(ctx context.Context, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error)
|
||||||
}
|
|
||||||
|
|
||||||
// TemperatureSensor is a interface to describe required functions to measure temperatures
|
|
||||||
type TemperatureSensor interface {
|
|
||||||
GetSensorModel() types.SensorModel
|
|
||||||
ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, error)
|
|
||||||
ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup)
|
|
||||||
ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error)
|
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,16 @@ import (
|
|||||||
"github.com/go-flucky/flucky/pkg/types"
|
"github.com/go-flucky/flucky/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ReadHumidities returns a list of measured humidities by humidity sensors
|
// Read measured values from sensors
|
||||||
func ReadHumidities(humiditySensors []HumiditySensor, round float64) ([]*types.Humidity, error) {
|
func Read(ctx context.Context, sensors []Sensor) ([]types.MeasuredValue, error) {
|
||||||
humidityChannel := make(chan *types.Humidity, len(humiditySensors))
|
measuredValueChannel := make(chan types.MeasuredValue, len(sensors))
|
||||||
errorChannel := make(chan error, len(humiditySensors))
|
errorChannel := make(chan error, len(sensors))
|
||||||
|
|
||||||
wg := new(sync.WaitGroup)
|
wg := new(sync.WaitGroup)
|
||||||
wg.Add(len(humiditySensors))
|
wg.Add(len(sensors))
|
||||||
|
|
||||||
for _, humiditySensor := range humiditySensors {
|
for _, sensor := range sensors {
|
||||||
go humiditySensor.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, wg)
|
go sensor.ReadContinously(ctx, measuredValueChannel, errorChannel)
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
@ -29,132 +29,29 @@ func ReadHumidities(humiditySensors []HumiditySensor, round float64) ([]*types.H
|
|||||||
return nil, prittyprint.FormatErrors(errors)
|
return nil, prittyprint.FormatErrors(errors)
|
||||||
}
|
}
|
||||||
|
|
||||||
humidities := collect.Humidities(humidityChannel)
|
measuredValues := collect.MeasuredValues(measuredValueChannel)
|
||||||
|
|
||||||
return humidities, nil
|
return measuredValues, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadHumiditiesWriteIntoChannel reads the humidity values of humidity sensors and writes them into a channel
|
// ReadChannel reads the measured values from sensors and writes them to a
|
||||||
func ReadHumiditiesWriteIntoChannel(ctx context.Context, humiditySensors []HumiditySensor, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error, wg *sync.WaitGroup) {
|
// channel.
|
||||||
for _, humiditySensor := range humiditySensors {
|
func ReadChannel(ctx context.Context, sensors []Sensor, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error, wg *sync.WaitGroup) {
|
||||||
humiditySensor.ReadHumidityWriteIntoChannel(round, humidityChannel, errorChannel, wg)
|
for _, sensor := range sensors {
|
||||||
|
sensor.ReadChannel(measuredValueChannel, errorChannel, wg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadHumiditiesContinuously reads the humidity values of humidity sensors continuously and writes them into a channel
|
// ReadContinuously reads the measured values continously from sensors and writes
|
||||||
func ReadHumiditiesContinuously(ctx context.Context, humiditySensors []HumiditySensor, round float64, humidityChannel chan<- *types.Humidity, errorChannel chan<- error) {
|
// them to a channel.
|
||||||
|
func ReadContinuously(ctx context.Context, sensors []Sensor, measuredValueChannel chan<- types.MeasuredValue, errorChannel chan<- error) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
|
errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
ReadHumiditiesWriteIntoChannel(ctx, humiditySensors, round, humidityChannel, errorChannel, nil)
|
ReadChannel(ctx, sensors, measuredValueChannel, errorChannel, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadTemperatures returns a list of measured temperatures by temperature sensors
|
|
||||||
func ReadTemperatures(temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64) ([]*types.Temperature, error) {
|
|
||||||
temperatureChannel := make(chan *types.Temperature, len(temperatureSensors))
|
|
||||||
errorChannel := make(chan error, len(temperatureSensors))
|
|
||||||
|
|
||||||
wg := new(sync.WaitGroup)
|
|
||||||
wg.Add(len(temperatureSensors))
|
|
||||||
|
|
||||||
for _, temperatureSensor := range temperatureSensors {
|
|
||||||
go temperatureSensor.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, wg)
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
|
|
||||||
errors := collect.Errors(errorChannel)
|
|
||||||
if len(errors) > 0 {
|
|
||||||
return nil, prittyprint.FormatErrors(errors)
|
|
||||||
}
|
|
||||||
|
|
||||||
temperatures := collect.Temperatures(temperatureChannel)
|
|
||||||
|
|
||||||
return temperatures, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadTemperaturesWriteIntoChannel reads the temperature values of temperature sensors and writes them into a channel
|
|
||||||
func ReadTemperaturesWriteIntoChannel(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
|
||||||
for _, temperatureSensor := range temperatureSensors {
|
|
||||||
temperatureSensor.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, wg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadTemperaturesContinuously reads the temperature values of temperature sensors continuously and writes them into a chann
|
|
||||||
func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
errorChannel <- fmt.Errorf("Context closed: %v", ctx.Err())
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
ReadTemperaturesWriteIntoChannel(ctx, temperatureSensors, degree, round, temperatureChannel, errorChannel, nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertTemperatureMeasurementUnit(value float64, fromDegree types.TemperatureUnit, toDegree types.TemperatureUnit) float64 {
|
|
||||||
|
|
||||||
switch fromDegree {
|
|
||||||
// Celsius
|
|
||||||
case types.TemperatureUnitCelsius:
|
|
||||||
switch toDegree {
|
|
||||||
// Celsius -> Celsius
|
|
||||||
case types.TemperatureUnitCelsius:
|
|
||||||
return value
|
|
||||||
// Celsius -> Fahrenheit
|
|
||||||
case types.TemperatureUnitFahrenheit:
|
|
||||||
return (value * 9 / 5) + 32
|
|
||||||
// Celsius -> Kelvin
|
|
||||||
case types.TemperatureUnitKelvin:
|
|
||||||
return value + 273.15
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fahrenheit
|
|
||||||
case types.TemperatureUnitFahrenheit:
|
|
||||||
switch toDegree {
|
|
||||||
// Fahrenheit -> Celsius
|
|
||||||
case types.TemperatureUnitCelsius:
|
|
||||||
return (value - 32) * 5 / 9
|
|
||||||
// Fahrenheit -> Fahrenheit
|
|
||||||
case types.TemperatureUnitFahrenheit:
|
|
||||||
return value
|
|
||||||
// Fahrenheit -> Kelvin
|
|
||||||
case types.TemperatureUnitKelvin:
|
|
||||||
return (value-32)*5/9 + 273.15
|
|
||||||
}
|
|
||||||
|
|
||||||
case types.TemperatureUnitKelvin:
|
|
||||||
switch toDegree {
|
|
||||||
// Kelvin -> Celsius
|
|
||||||
case types.TemperatureUnitCelsius:
|
|
||||||
return value - 273.15
|
|
||||||
// Kelvin -> Fahrenheit
|
|
||||||
case types.TemperatureUnitFahrenheit:
|
|
||||||
return (value-273.15)*9/5 + 32
|
|
||||||
// Kevin -> Kelvin
|
|
||||||
case types.TemperatureUnitKelvin:
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
|
|
||||||
func SelectTemperatureMeasurementUnit(unit string) (types.TemperatureUnit, error) {
|
|
||||||
switch unit {
|
|
||||||
case "celsius":
|
|
||||||
return types.TemperatureUnitCelsius, nil
|
|
||||||
case "fahrenheit":
|
|
||||||
return types.TemperatureUnitFahrenheit, nil
|
|
||||||
case "kelvin":
|
|
||||||
return types.TemperatureUnitKelvin, nil
|
|
||||||
default:
|
|
||||||
return "", fmt.Errorf("Can not determine temperature measurement unit")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -12,3 +12,15 @@ type Humidity struct {
|
|||||||
CreationDate *time.Time `json:"creation_date" xml:"creation_date"`
|
CreationDate *time.Time `json:"creation_date" xml:"creation_date"`
|
||||||
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
|
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Humidity) GetID() string {
|
||||||
|
return h.HumidityID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Humidity) GetSensorID() string {
|
||||||
|
return h.SensorID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Humidity) GetValue() float64 {
|
||||||
|
return h.HumidityValue
|
||||||
|
}
|
||||||
|
7
pkg/types/measuredValue.go
Normal file
7
pkg/types/measuredValue.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
type MeasuredValue interface {
|
||||||
|
GetID() string
|
||||||
|
GetSensorID() string
|
||||||
|
GetValue() float64
|
||||||
|
}
|
@ -1,14 +1,11 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import "time"
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Temperature ...
|
// Temperature ...
|
||||||
type Temperature struct {
|
type Temperature struct {
|
||||||
TemperatureID string `json:"temperature_id" xml:"temperature_id"`
|
TemperatureID string `json:"temperature_id" xml:"temperature_id"`
|
||||||
TemperatureValue float64 `json:"temperature_value,string" xml:"temperature_value,string"`
|
TemperatureValue float64 `json:"temperature_value,string" xml:"temperature_value,string"`
|
||||||
TemperatureUnit TemperatureUnit `json:"temperature_unit" xml:"temperature_unit"`
|
|
||||||
TemperatureFromDate time.Time `json:"temperature_from_date" xml:"temperature_from_date"`
|
TemperatureFromDate time.Time `json:"temperature_from_date" xml:"temperature_from_date"`
|
||||||
TemperatureTillDate time.Time `json:"temperature_till_date" xml:"temperature_till_date"`
|
TemperatureTillDate time.Time `json:"temperature_till_date" xml:"temperature_till_date"`
|
||||||
SensorID string `json:"sensor_id" xml:"sensor_id"`
|
SensorID string `json:"sensor_id" xml:"sensor_id"`
|
||||||
@ -16,16 +13,14 @@ type Temperature struct {
|
|||||||
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
|
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TemperatureUnit of measurement for temperature
|
func (t *Temperature) GetID() string {
|
||||||
type TemperatureUnit string
|
return t.TemperatureID
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
func (t *Temperature) GetSensorID() string {
|
||||||
// TemperatureUnitCelsius indicates the temperature in Celsius
|
return t.SensorID
|
||||||
TemperatureUnitCelsius TemperatureUnit = "celsius"
|
}
|
||||||
|
|
||||||
// TemperatureUnitFahrenheit indicates the temperature in Fahrenheit
|
func (t *Temperature) GetValue() float64 {
|
||||||
TemperatureUnitFahrenheit = "fahrenheit"
|
return t.TemperatureValue
|
||||||
|
}
|
||||||
// TemperatureUnitKelvin indicates the temperature in Kelvin
|
|
||||||
TemperatureUnitKelvin = "kelvin"
|
|
||||||
)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user