refac(pkg/sensor): temperature measurement unit

This commit is contained in:
Markus Pesch 2019-06-23 14:17:57 +02:00
parent 3bb10a4f78
commit 5e03d987c5
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
19 changed files with 91 additions and 76 deletions

View File

@ -7,13 +7,15 @@ 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"
) )
var cleanCacheInterval string
var compression bool var compression bool
var configFile string var configFile string
var cleanCacheInterval string
var round float64 var round float64
var temperatureUnit string
var daemonCmd = &cobra.Command{ var daemonCmd = &cobra.Command{
Use: "daemon", Use: "daemon",
@ -32,7 +34,12 @@ var daemonCmd = &cobra.Command{
logger := logger.NewDefaultLogger(logger.LogLevelDebug, false) logger := logger.NewDefaultLogger(logger.LogLevelDebug, false)
daemon.Start(cnf, duration, compression, round, logger) measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(temperatureUnit)
if err != nil {
log.Fatalf("Can not parse temperature unit: %v", temperatureUnit)
}
daemon.Start(cnf, duration, compression, measurementUnit, round, logger)
}, },
} }
@ -43,4 +50,5 @@ 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")
} }

View File

@ -9,11 +9,11 @@ import (
"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/sensor" "github.com/go-flucky/flucky/pkg/sensor"
"github.com/go-flucky/flucky/pkg/types"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var logs bool var logs bool
var temperatureUnit string
var readTemperatureCmd = &cobra.Command{ var readTemperatureCmd = &cobra.Command{
Use: "read", Use: "read",
@ -35,7 +35,12 @@ var readTemperatureCmd = &cobra.Command{
temperatureSensors = cnf.GetTemperatureSensorsByName(args) temperatureSensors = cnf.GetTemperatureSensorsByName(args)
} }
temperatures, err := sensor.ReadTemperatures(temperatureSensors, types.DegreeCelsius, round) measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(temperatureUnit)
if err != nil {
log.Fatalf("Can not parse temperature unit: %v", temperatureUnit)
}
temperatures, err := sensor.ReadTemperatures(temperatureSensors, measurementUnit, round)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -58,4 +63,5 @@ func init() {
readTemperatureCmd.Flags().BoolVar(&logs, "logs", true, "Log temperature") readTemperatureCmd.Flags().BoolVar(&logs, "logs", true, "Log temperature")
readTemperatureCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures") readTemperatureCmd.Flags().BoolVar(&compression, "compression", true, "Compress measured with logged temperatures")
readTemperatureCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function") readTemperatureCmd.Flags().Float64VarP(&round, "round", "r", 0.25, "Round values. The value 0 deactivates the function")
readTemperatureCmd.Flags().StringVar(&temperatureUnit, "temperature-unit", "celsius", "Temperature unit")
} }

View File

@ -16,12 +16,13 @@ import (
) )
// Start the daemon // Start the daemon
func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, round float64, logger logger.Logger) { func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compression bool, degree types.TemperatureUnit, 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())
logger.Info("Use compression: %v", compression) logger.Info("Use compression: %v", compression)
logger.Info("Round values: %v", round) logger.Info("Round values: %v", round)
logger.Info("Temperature unit: %v", degree)
temperatureLogfile := logfile.New(cnf.Device.TemperatureLogfile) temperatureLogfile := logfile.New(cnf.Device.TemperatureLogfile)
@ -38,7 +39,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
childContext, cancel := context.WithCancel(ctx) childContext, cancel := context.WithCancel(ctx)
// go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel) // go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel)
go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), types.DegreeCelsius, round, temperatureChannel, errorChannel) go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), degree, round, temperatureChannel, errorChannel)
temperatureCache := make([]*types.Temperature, 0) temperatureCache := make([]*types.Temperature, 0)

View File

@ -85,7 +85,7 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
temperature := &types.Temperature{ temperature := &types.Temperature{
TemperatureID: record[0], // 0 TemperatureID: record[0], // 0
TemperatureValue: temperatureValue, // 1 TemperatureValue: temperatureValue, // 1
TemperatureDegree: measurementUnit, // 2 TemperatureUnit: measurementUnit, // 2
TemperatureFromDate: times[0], // 3 TemperatureFromDate: times[0], // 3
TemperatureTillDate: times[1], // 4 TemperatureTillDate: times[1], // 4
SensorID: record[5], // 5 SensorID: record[5], // 5
@ -158,7 +158,7 @@ func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error
record = []string{ record = []string{
fmt.Sprintf("%v", temperature.TemperatureID), fmt.Sprintf("%v", temperature.TemperatureID),
fmt.Sprintf("%v", temperature.TemperatureValue), fmt.Sprintf("%v", temperature.TemperatureValue),
fmt.Sprintf("%v", temperature.TemperatureDegree), fmt.Sprintf("%v", temperature.TemperatureUnit),
fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)),
fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
fmt.Sprintf("%v", temperature.SensorID), fmt.Sprintf("%v", temperature.SensorID),
@ -169,7 +169,7 @@ func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error
record = []string{ record = []string{
fmt.Sprintf("%v", temperature.TemperatureID), fmt.Sprintf("%v", temperature.TemperatureID),
fmt.Sprintf("%v", temperature.TemperatureValue), fmt.Sprintf("%v", temperature.TemperatureValue),
fmt.Sprintf("%v", temperature.TemperatureDegree), fmt.Sprintf("%v", temperature.TemperatureUnit),
fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)),
fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)), fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
fmt.Sprintf("%v", temperature.SensorID), fmt.Sprintf("%v", temperature.SensorID),

View File

@ -2,7 +2,7 @@
{ {
"temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957",
"temperature_value": "24.562", "temperature_value": "24.562",
"temperature_degree": "celsius", "temperature_unit": "celsius",
"temperature_from_date": "2019-10-01T00:00:00+02:00", "temperature_from_date": "2019-10-01T00:00:00+02:00",
"temperature_till_date": "2019-05-01T00:00:00+02:00", "temperature_till_date": "2019-05-01T00:00:00+02:00",
"sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e",

View File

@ -2,7 +2,7 @@
{ {
"temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957",
"temperature_value": "24.562", "temperature_value": "24.562",
"temperature_degree": "celsius", "temperature_unit": "celsius",
"temperature_from_date": "2019-05-01T00:00:00+02:00", "temperature_from_date": "2019-05-01T00:00:00+02:00",
"temperature_till_date": "2019-10-01T00:00:00+02:00", "temperature_till_date": "2019-10-01T00:00:00+02:00",
"sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e",

View File

@ -2,7 +2,7 @@
{ {
"temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957",
"temperature_value": "24.562", "temperature_value": "24.562",
"temperature_degree": "celsius", "temperature_unit": "celsius",
"temperature_from_date": "2019-05-01T00:00:00+02:00", "temperature_from_date": "2019-05-01T00:00:00+02:00",
"temperature_till_date": "2019-10-01T00:00:00+02:00", "temperature_till_date": "2019-10-01T00:00:00+02:00",
"sensor_id": "", "sensor_id": "",

View File

@ -2,7 +2,7 @@
{ {
"temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957",
"temperature_value": "0", "temperature_value": "0",
"temperature_degree": "celsius", "temperature_unit": "celsius",
"temperature_from_date": "2019-05-01T00:00:00+02:00", "temperature_from_date": "2019-05-01T00:00:00+02:00",
"temperature_till_date": "2019-10-01T00:00:00+02:00", "temperature_till_date": "2019-10-01T00:00:00+02:00",
"sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e",

View File

@ -2,7 +2,7 @@
{ {
"temperature_id": "", "temperature_id": "",
"temperature_value": "24.562", "temperature_value": "24.562",
"temperature_degree": "celsius", "temperature_unit": "celsius",
"temperature_from_date": "2019-05-01T00:00:00+02:00", "temperature_from_date": "2019-05-01T00:00:00+02:00",
"temperature_till_date": "2019-10-01T00:00:00+02:00", "temperature_till_date": "2019-10-01T00:00:00+02:00",
"sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e",

View File

@ -2,7 +2,7 @@
{ {
"temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957",
"temperature_value": "24.562", "temperature_value": "24.562",
"temperature_degree": "celsius", "temperature_unit": "celsius",
"temperature_from_date": "2019-06-14T21:15:28.504051541+02:00", "temperature_from_date": "2019-06-14T21:15:28.504051541+02:00",
"temperature_till_date": "2019-06-14T21:18:07.384104493+02:00", "temperature_till_date": "2019-06-14T21:18:07.384104493+02:00",
"sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e",
@ -12,7 +12,7 @@
{ {
"temperature_id": "5f119ba3-bcea-4c3b-aabb-0406ea70f7e1", "temperature_id": "5f119ba3-bcea-4c3b-aabb-0406ea70f7e1",
"temperature_value": "24.375", "temperature_value": "24.375",
"temperature_degree": "celsius", "temperature_unit": "celsius",
"temperature_from_date": "2019-06-14T21:15:28.583856443+02:00", "temperature_from_date": "2019-06-14T21:15:28.583856443+02:00",
"temperature_till_date": "2019-06-14T21:18:07.463893776+02:00", "temperature_till_date": "2019-06-14T21:18:07.463893776+02:00",
"sensor_id": "efcd755e-82d1-4789-a50b-355b8735b8d8", "sensor_id": "efcd755e-82d1-4789-a50b-355b8735b8d8",

View File

@ -2,7 +2,7 @@
{ {
"temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957", "temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957",
"temperature_value": "24.562", "temperature_value": "24.562",
"temperature_degree": "celsius", "temperature_unit": "celsius",
"temperature_from_date": "2019-06-14T21:15:28.504051541+02:00", "temperature_from_date": "2019-06-14T21:15:28.504051541+02:00",
"temperature_till_date": "2019-06-14T21:18:07.384104493+02:00", "temperature_till_date": "2019-06-14T21:18:07.384104493+02:00",
"sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e", "sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e",
@ -12,7 +12,7 @@
{ {
"temperature_id": "5f119ba3-bcea-4c3b-aabb-0406ea70f7e1", "temperature_id": "5f119ba3-bcea-4c3b-aabb-0406ea70f7e1",
"temperature_value": "24.375", "temperature_value": "24.375",
"temperature_degree": "celsius", "temperature_unit": "celsius",
"temperature_from_date": "2019-06-14T21:15:28.583856443+02:00", "temperature_from_date": "2019-06-14T21:15:28.583856443+02:00",
"temperature_till_date": "2019-06-14T21:15:28.583856443+02:00", "temperature_till_date": "2019-06-14T21:15:28.583856443+02:00",
"sensor_id": "efcd755e-82d1-4789-a50b-355b8735b8d8", "sensor_id": "efcd755e-82d1-4789-a50b-355b8735b8d8",

View File

@ -2,7 +2,7 @@
<temperature> <temperature>
<temperature_id>a469503b-fc16-4e72-8d29-7eeee08ba957</temperature_id> <temperature_id>a469503b-fc16-4e72-8d29-7eeee08ba957</temperature_id>
<temperature_value>24.562</temperature_value> <temperature_value>24.562</temperature_value>
<temperature_degree>celsius</temperature_degree> <temperature_unit>celsius</temperature_unit>
<temperature_from_date>2019-06-14T21:15:28.504051541+02:00</temperature_from_date> <temperature_from_date>2019-06-14T21:15:28.504051541+02:00</temperature_from_date>
<temperature_till_date>2019-06-14T21:18:07.384104493+02:00</temperature_till_date> <temperature_till_date>2019-06-14T21:18:07.384104493+02:00</temperature_till_date>
<sensor_id>84eac248-6927-4db6-b6f9-7891ce2d301e</sensor_id> <sensor_id>84eac248-6927-4db6-b6f9-7891ce2d301e</sensor_id>
@ -12,7 +12,7 @@
<temperature> <temperature>
<temperature_id>5f119ba3-bcea-4c3b-aabb-0406ea70f7e1</temperature_id> <temperature_id>5f119ba3-bcea-4c3b-aabb-0406ea70f7e1</temperature_id>
<temperature_value>24.375</temperature_value> <temperature_value>24.375</temperature_value>
<temperature_degree>celsius</temperature_degree> <temperature_unit>celsius</temperature_unit>
<temperature_from_date>2019-06-14T21:15:28.583856443+02:00</temperature_from_date> <temperature_from_date>2019-06-14T21:15:28.583856443+02:00</temperature_from_date>
<temperature_till_date>2019-06-14T21:18:07.463893776+02:00</temperature_till_date> <temperature_till_date>2019-06-14T21:18:07.463893776+02:00</temperature_till_date>
<sensor_id>efcd755e-82d1-4789-a50b-355b8735b8d8</sensor_id> <sensor_id>efcd755e-82d1-4789-a50b-355b8735b8d8</sensor_id>

View File

@ -2,7 +2,7 @@
<temperature> <temperature>
<temperature_id>a469503b-fc16-4e72-8d29-7eeee08ba957</temperature_id> <temperature_id>a469503b-fc16-4e72-8d29-7eeee08ba957</temperature_id>
<temperature_value>24.562</temperature_value> <temperature_value>24.562</temperature_value>
<temperature_degree>celsius</temperature_degree> <temperature_unit>celsius</temperature_unit>
<temperature_from_date>2019-06-14T21:15:28.504051541+02:00</temperature_from_date> <temperature_from_date>2019-06-14T21:15:28.504051541+02:00</temperature_from_date>
<temperature_till_date>2019-06-14T21:18:07.384104493+02:00</temperature_till_date> <temperature_till_date>2019-06-14T21:18:07.384104493+02:00</temperature_till_date>
<sensor_id>84eac248-6927-4db6-b6f9-7891ce2d301e</sensor_id> <sensor_id>84eac248-6927-4db6-b6f9-7891ce2d301e</sensor_id>
@ -12,7 +12,7 @@
<temperature> <temperature>
<temperature_id>5f119ba3-bcea-4c3b-aabb-0406ea70f7e1</temperature_id> <temperature_id>5f119ba3-bcea-4c3b-aabb-0406ea70f7e1</temperature_id>
<temperature_value>24.375</temperature_value> <temperature_value>24.375</temperature_value>
<temperature_degree>celsius</temperature_degree> <temperature_unit>celsius</temperature_unit>
<temperature_from_date>2019-06-14T21:15:28.583856443+02:00</temperature_from_date> <temperature_from_date>2019-06-14T21:15:28.583856443+02:00</temperature_from_date>
<temperature_till_date>2019-06-14T21:15:28.583856443+02:00</temperature_till_date> <temperature_till_date>2019-06-14T21:15:28.583856443+02:00</temperature_till_date>
<sensor_id>efcd755e-82d1-4789-a50b-355b8735b8d8</sensor_id> <sensor_id>efcd755e-82d1-4789-a50b-355b8735b8d8</sensor_id>

View File

@ -87,7 +87,7 @@ func (s *DHT11) ReadHumidityContinously(ctx context.Context, round float64, humi
} }
// ReadTemperature measure the temperature // ReadTemperature measure the temperature
func (s *DHT11) ReadTemperature(degree types.Degree, round float64) (*types.Temperature, error) { func (s *DHT11) ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, 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)
@ -109,7 +109,7 @@ func (s *DHT11) ReadTemperature(degree types.Degree, round float64) (*types.Temp
} }
// Convert temperature degree // Convert temperature degree
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree) temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
if round != 0 { if round != 0 {
temperatureValue = math.Round(temperatureValue/round) * round temperatureValue = math.Round(temperatureValue/round) * round
@ -118,7 +118,7 @@ func (s *DHT11) ReadTemperature(degree types.Degree, round float64) (*types.Temp
temperature := &types.Temperature{ temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(), TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue, TemperatureValue: temperatureValue,
TemperatureDegree: degree, TemperatureUnit: degree,
TemperatureFromDate: time.Now(), TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(), TemperatureTillDate: time.Now(),
SensorID: s.SensorID, SensorID: s.SensorID,
@ -128,7 +128,7 @@ func (s *DHT11) ReadTemperature(degree types.Degree, round float64) (*types.Temp
} }
// ReadTemperatureWriteIntoChannel and write values into a channel // ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DHT11) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) { func (s *DHT11) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil { if wg != nil {
defer wg.Done() defer wg.Done()
} }
@ -142,7 +142,7 @@ func (s *DHT11) ReadTemperatureWriteIntoChannel(degree types.Degree, round float
} }
// ReadTemperatureContinously into a channel until context closed // ReadTemperatureContinously into a channel until context closed
func (s *DHT11) ReadTemperatureContinously(ctx context.Context, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { func (s *DHT11) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():

View File

@ -87,7 +87,7 @@ func (s *DHT22) ReadHumidityContinously(ctx context.Context, round float64, humi
} }
// ReadTemperature measure the temperature // ReadTemperature measure the temperature
func (s *DHT22) ReadTemperature(degree types.Degree, round float64) (*types.Temperature, error) { func (s *DHT22) ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, 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)
@ -109,7 +109,7 @@ func (s *DHT22) ReadTemperature(degree types.Degree, round float64) (*types.Temp
} }
// Convert temperature degree // Convert temperature degree
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree) temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
// round // round
if round != 0 { if round != 0 {
@ -119,7 +119,7 @@ func (s *DHT22) ReadTemperature(degree types.Degree, round float64) (*types.Temp
temperature := &types.Temperature{ temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(), TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue, TemperatureValue: temperatureValue,
TemperatureDegree: degree, TemperatureUnit: degree,
TemperatureFromDate: time.Now(), TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(), TemperatureTillDate: time.Now(),
SensorID: s.SensorID, SensorID: s.SensorID,
@ -129,7 +129,7 @@ func (s *DHT22) ReadTemperature(degree types.Degree, round float64) (*types.Temp
} }
// ReadTemperatureWriteIntoChannel and write values into a channel // ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DHT22) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) { func (s *DHT22) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil { if wg != nil {
defer wg.Done() defer wg.Done()
} }
@ -143,7 +143,7 @@ func (s *DHT22) ReadTemperatureWriteIntoChannel(degree types.Degree, round float
} }
// ReadTemperatureContinously into a channel until context closed // ReadTemperatureContinously into a channel until context closed
func (s *DHT22) ReadTemperatureContinously(ctx context.Context, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { func (s *DHT22) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():

View File

@ -31,7 +31,7 @@ func (s *DS18B20) GetSensor() *types.Sensor {
} }
// ReadTemperature measure the temperature // ReadTemperature measure the temperature
func (s *DS18B20) ReadTemperature(degree types.Degree, round float64) (*types.Temperature, error) { 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 {
@ -53,7 +53,7 @@ func (s *DS18B20) ReadTemperature(degree types.Degree, round float64) (*types.Te
temperatureValue := c / 1000 temperatureValue := c / 1000
// Convert temperature degree // Convert temperature degree
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree) temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
// round // round
if round != 0 { if round != 0 {
@ -63,7 +63,7 @@ func (s *DS18B20) ReadTemperature(degree types.Degree, round float64) (*types.Te
temperature := &types.Temperature{ temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(), TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue, TemperatureValue: temperatureValue,
TemperatureDegree: degree, TemperatureUnit: degree,
TemperatureFromDate: time.Now(), TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(), TemperatureTillDate: time.Now(),
SensorID: s.SensorID, SensorID: s.SensorID,
@ -74,7 +74,7 @@ func (s *DS18B20) ReadTemperature(degree types.Degree, round float64) (*types.Te
} }
// ReadTemperatureWriteIntoChannel and write values into a channel // ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) { func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil { if wg != nil {
defer wg.Done() defer wg.Done()
} }
@ -88,7 +88,7 @@ func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.Degree, round flo
} }
// ReadTemperatureContinously into a channel until context closed // ReadTemperatureContinously into a channel until context closed
func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():

View File

@ -18,7 +18,7 @@ type HumiditySensor interface {
// TemperatureSensor is a interface to describe required functions to measure temperatures // TemperatureSensor is a interface to describe required functions to measure temperatures
type TemperatureSensor interface { type TemperatureSensor interface {
GetSensorModel() types.SensorModel GetSensorModel() types.SensorModel
ReadTemperature(degree types.Degree, round float64) (*types.Temperature, error) ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, error)
ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup)
ReadTemperatureContinously(ctx context.Context, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error)
} }

View File

@ -55,7 +55,7 @@ func ReadHumiditiesContinuously(ctx context.Context, humiditySensors []HumidityS
} }
// ReadTemperatures returns a list of measured temperatures by temperature sensors // ReadTemperatures returns a list of measured temperatures by temperature sensors
func ReadTemperatures(temperatureSensors []TemperatureSensor, degree types.Degree, round float64) ([]*types.Temperature, error) { func ReadTemperatures(temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64) ([]*types.Temperature, error) {
temperatureChannel := make(chan *types.Temperature, len(temperatureSensors)) temperatureChannel := make(chan *types.Temperature, len(temperatureSensors))
errorChannel := make(chan error, len(temperatureSensors)) errorChannel := make(chan error, len(temperatureSensors))
@ -79,14 +79,14 @@ func ReadTemperatures(temperatureSensors []TemperatureSensor, degree types.Degre
} }
// ReadTemperaturesWriteIntoChannel reads the temperature values of temperature sensors and writes them into a channel // ReadTemperaturesWriteIntoChannel reads the temperature values of temperature sensors and writes them into a channel
func ReadTemperaturesWriteIntoChannel(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) { 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 { for _, temperatureSensor := range temperatureSensors {
temperatureSensor.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, wg) temperatureSensor.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, wg)
} }
} }
// ReadTemperaturesContinuously reads the temperature values of temperature sensors continuously and writes them into a chann // ReadTemperaturesContinuously reads the temperature values of temperature sensors continuously and writes them into a chann
func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) { func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []TemperatureSensor, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
@ -98,47 +98,47 @@ func ReadTemperaturesContinuously(ctx context.Context, temperatureSensors []Temp
} }
} }
func convertTemperatureMeasurementUnit(value float64, fromDegree types.Degree, toDegree types.Degree) float64 { func convertTemperatureMeasurementUnit(value float64, fromDegree types.TemperatureUnit, toDegree types.TemperatureUnit) float64 {
switch fromDegree { switch fromDegree {
// Celsius // Celsius
case types.DegreeCelsius: case types.TemperatureUnitCelsius:
switch toDegree { switch toDegree {
// Celsius -> Celsius // Celsius -> Celsius
case types.DegreeCelsius: case types.TemperatureUnitCelsius:
return value return value
// Celsius -> Fahrenheit // Celsius -> Fahrenheit
case types.DegreeFahrenheit: case types.TemperatureUnitFahrenheit:
return (value * 9 / 5) + 32 return (value * 9 / 5) + 32
// Celsius -> Kelvin // Celsius -> Kelvin
case types.DegreeKelvin: case types.TemperatureUnitKelvin:
return value + 273.15 return value + 273.15
} }
// Fahrenheit // Fahrenheit
case types.DegreeFahrenheit: case types.TemperatureUnitFahrenheit:
switch toDegree { switch toDegree {
// Fahrenheit -> Celsius // Fahrenheit -> Celsius
case types.DegreeCelsius: case types.TemperatureUnitCelsius:
return (value - 32) * 5 / 9 return (value - 32) * 5 / 9
// Fahrenheit -> Fahrenheit // Fahrenheit -> Fahrenheit
case types.DegreeFahrenheit: case types.TemperatureUnitFahrenheit:
return value return value
// Fahrenheit -> Kelvin // Fahrenheit -> Kelvin
case types.DegreeKelvin: case types.TemperatureUnitKelvin:
return (value-32)*5/9 + 273.15 return (value-32)*5/9 + 273.15
} }
case types.DegreeKelvin: case types.TemperatureUnitKelvin:
switch toDegree { switch toDegree {
// Kelvin -> Celsius // Kelvin -> Celsius
case types.DegreeCelsius: case types.TemperatureUnitCelsius:
return value - 273.15 return value - 273.15
// Kelvin -> Fahrenheit // Kelvin -> Fahrenheit
case types.DegreeFahrenheit: case types.TemperatureUnitFahrenheit:
return (value-273.15)*9/5 + 32 return (value-273.15)*9/5 + 32
// Kevin -> Kelvin // Kevin -> Kelvin
case types.DegreeKelvin: case types.TemperatureUnitKelvin:
return value return value
} }
} }
@ -146,14 +146,14 @@ func convertTemperatureMeasurementUnit(value float64, fromDegree types.Degree, t
return value return value
} }
func SelectTemperatureMeasurementUnit(unit string) (types.Degree, error) { func SelectTemperatureMeasurementUnit(unit string) (types.TemperatureUnit, error) {
switch unit { switch unit {
case "celsius": case "celsius":
return types.DegreeCelsius, nil return types.TemperatureUnitCelsius, nil
case "fahrenheit": case "fahrenheit":
return types.DegreeFahrenheit, nil return types.TemperatureUnitFahrenheit, nil
case "kelvin": case "kelvin":
return types.DegreeKelvin, nil return types.TemperatureUnitKelvin, nil
default: default:
return "", fmt.Errorf("Can not determine temperature measurement unit") return "", fmt.Errorf("Can not determine temperature measurement unit")
} }

View File

@ -6,26 +6,26 @@ import (
// 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"`
TemperatureDegree Degree `json:"temperature_degree" xml:"temperature_degree"` 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"`
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"`
} }
// Degree unit of measurement for temperature // TemperatureUnit of measurement for temperature
type Degree string type TemperatureUnit string
const ( const (
// DegreeCelsius indicates the temperature in Celsius // TemperatureUnitCelsius indicates the temperature in Celsius
DegreeCelsius Degree = "celsius" TemperatureUnitCelsius TemperatureUnit = "celsius"
// DegreeFahrenheit indicates the temperature in Fahrenheit // TemperatureUnitFahrenheit indicates the temperature in Fahrenheit
DegreeFahrenheit = "fahrenheit" TemperatureUnitFahrenheit = "fahrenheit"
// DegreeKelvin indicates the temperature in Kelvin // TemperatureUnitKelvin indicates the temperature in Kelvin
DegreeKelvin = "kelvin" TemperatureUnitKelvin = "kelvin"
) )