fix(pkg/sensor): temperature measurement unit

This commit is contained in:
Markus Pesch 2019-06-23 13:33:09 +02:00
parent 1e25b55789
commit 3bb10a4f78
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
22 changed files with 154 additions and 42 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/go-flucky/flucky/pkg/config"
"github.com/go-flucky/flucky/pkg/logfile"
"github.com/go-flucky/flucky/pkg/sensor"
"github.com/go-flucky/flucky/pkg/types"
"github.com/spf13/cobra"
)
@ -34,7 +35,7 @@ var readTemperatureCmd = &cobra.Command{
temperatureSensors = cnf.GetTemperatureSensorsByName(args)
}
temperatures, err := sensor.ReadTemperatures(temperatureSensors, round)
temperatures, err := sensor.ReadTemperatures(temperatureSensors, types.DegreeCelsius, round)
if err != nil {
log.Fatalln(err)
}

View File

@ -38,7 +38,7 @@ func Start(cnf *config.Configuration, cleanCacheInterval time.Duration, compress
childContext, cancel := context.WithCancel(ctx)
// go sensor.ReadHumiditiesContinuously(cnf.GetHumiditySensors(config.ENABLED), humidityChannel, errorChannel)
go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), round, temperatureChannel, errorChannel)
go sensor.ReadTemperaturesContinuously(childContext, cnf.GetTemperatureSensors(config.ENABLED), types.DegreeCelsius, round, temperatureChannel, errorChannel)
temperatureCache := make([]*types.Temperature, 0)

View File

@ -8,6 +8,7 @@ import (
"strconv"
"time"
"github.com/go-flucky/flucky/pkg/sensor"
"github.com/go-flucky/flucky/pkg/types"
)
@ -63,7 +64,7 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
for _, record := range records {
times := make([]time.Time, 0)
for _, j := range []int{2, 3} {
for _, j := range []int{3, 4} {
time, err := time.Parse(timeFormat, record[j])
if err != nil {
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[j], err)
@ -76,25 +77,31 @@ func (cl *csvLogfile) ReadTemperatures() ([]*types.Temperature, error) {
return nil, fmt.Errorf("%v %v: %v", errorParseFloat, record[1], err)
}
measurementUnit, err := sensor.SelectTemperatureMeasurementUnit(record[2])
if err != nil {
return nil, fmt.Errorf("%v %v: %v", errorParseMeasurementUnit, record[2], err)
}
temperature := &types.Temperature{
TemperatureID: record[0],
TemperatureValue: temperatureValue,
TemperatureFromDate: times[0],
TemperatureTillDate: times[1],
SensorID: record[4],
TemperatureID: record[0], // 0
TemperatureValue: temperatureValue, // 1
TemperatureDegree: measurementUnit, // 2
TemperatureFromDate: times[0], // 3
TemperatureTillDate: times[1], // 4
SensorID: record[5], // 5
}
// Creation date
temperatureCreationDate, err := time.Parse(timeFormat, record[5])
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[6] != "" {
temperatureUpdateDate, err := time.Parse(timeFormat, record[6])
if record[7] != "" {
temperatureUpdateDate, err := time.Parse(timeFormat, record[7])
if err != nil {
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[6], err)
return nil, fmt.Errorf("%v %v: %v", errorParseTime, record[7], err)
}
temperature.UpdateDate = &temperatureUpdateDate
@ -151,6 +158,7 @@ func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error
record = []string{
fmt.Sprintf("%v", temperature.TemperatureID),
fmt.Sprintf("%v", temperature.TemperatureValue),
fmt.Sprintf("%v", temperature.TemperatureDegree),
fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)),
fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
fmt.Sprintf("%v", temperature.SensorID),
@ -161,6 +169,7 @@ func (cl *csvLogfile) WriteTemperatures(temperatures []*types.Temperature) error
record = []string{
fmt.Sprintf("%v", temperature.TemperatureID),
fmt.Sprintf("%v", temperature.TemperatureValue),
fmt.Sprintf("%v", temperature.TemperatureDegree),
fmt.Sprintf("%v", temperature.TemperatureFromDate.Format(timeFormat)),
fmt.Sprintf("%v", temperature.TemperatureTillDate.Format(timeFormat)),
fmt.Sprintf("%v", temperature.SensorID),

View File

@ -13,6 +13,7 @@ var errorLogfileUnmarshal = errors.New("Can not unmarshal values")
var errorLogfileWrite = errors.New("Can not write with given writer")
var errorParseFloat = errors.New("Can not parse float")
var errorParseMeasurementUnit = errors.New("Can not parse mesaurement unit")
var errorParseTime = errors.New("Can not parse time")
var errorNoValidHumidityID = errors.New("No valid humidity id detected or available")

View File

@ -27,8 +27,8 @@ func TestConvert(t *testing.T) {
markupLanguages := []string{"csv", "json", "xml"}
for _, markupLanguageFrom := range markupLanguages {
testDir, cleanup := createTestDir(t)
defer cleanup()
testDir, _ := createTestDir(t)
//defer cleanup()
for _, markupLanguageTo := range markupLanguages {

View File

@ -1,2 +1,2 @@
a469503b-fc16-4e72-8d29-7eeee08ba957,24.562,2019-06-14 21:15:28.504051541 +0200,2019-06-14 21:18:07.384104493 +0200,84eac248-6927-4db6-b6f9-7891ce2d301e,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587076 +0200
5f119ba3-bcea-4c3b-aabb-0406ea70f7e1,24.375,2019-06-14 21:15:28.583856443 +0200,2019-06-14 21:18:07.463893776 +0200,efcd755e-82d1-4789-a50b-355b8735b8d8,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587701 +0200
a469503b-fc16-4e72-8d29-7eeee08ba957,24.562,celsius,2019-06-14 21:15:28.504051541 +0200,2019-06-14 21:18:07.384104493 +0200,84eac248-6927-4db6-b6f9-7891ce2d301e,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587076 +0200
5f119ba3-bcea-4c3b-aabb-0406ea70f7e1,24.375,celsius,2019-06-14 21:15:28.583856443 +0200,2019-06-14 21:18:07.463893776 +0200,efcd755e-82d1-4789-a50b-355b8735b8d8,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587701 +0200

1 a469503b-fc16-4e72-8d29-7eeee08ba957 24.562 celsius 2019-06-14 21:15:28.504051541 +0200 2019-06-14 21:18:07.384104493 +0200 84eac248-6927-4db6-b6f9-7891ce2d301e 2019-06-14 21:18:07.465885864 +0200 2019-06-14 21:18:07.46587076 +0200
2 5f119ba3-bcea-4c3b-aabb-0406ea70f7e1 24.375 celsius 2019-06-14 21:15:28.583856443 +0200 2019-06-14 21:18:07.463893776 +0200 efcd755e-82d1-4789-a50b-355b8735b8d8 2019-06-14 21:18:07.465885864 +0200 2019-06-14 21:18:07.46587701 +0200

View File

@ -1,2 +1,2 @@
a469503b-fc16-4e72-8d29-7eeee08ba957,24.562,2019-06-14 21:15:28.504051541 +0200,2019-06-14 21:18:07.384104493 +0200,84eac248-6927-4db6-b6f9-7891ce2d301e,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587076 +0200
5f119ba3-bcea-4c3b-aabb-0406ea70f7e1,24.375,2019-06-14 21:15:28.583856443 +0200,2019-06-14 21:15:28.583856443 +0200,efcd755e-82d1-4789-a50b-355b8735b8d8,2019-06-14 21:18:07.465885864 +0200,
a469503b-fc16-4e72-8d29-7eeee08ba957,24.562,celsius,2019-06-14 21:15:28.504051541 +0200,2019-06-14 21:18:07.384104493 +0200,84eac248-6927-4db6-b6f9-7891ce2d301e,2019-06-14 21:18:07.465885864 +0200,2019-06-14 21:18:07.46587076 +0200
5f119ba3-bcea-4c3b-aabb-0406ea70f7e1,24.375,celsius,2019-06-14 21:15:28.583856443 +0200,2019-06-14 21:15:28.583856443 +0200,efcd755e-82d1-4789-a50b-355b8735b8d8,2019-06-14 21:18:07.465885864 +0200,

1 a469503b-fc16-4e72-8d29-7eeee08ba957 24.562 celsius 2019-06-14 21:15:28.504051541 +0200 2019-06-14 21:18:07.384104493 +0200 84eac248-6927-4db6-b6f9-7891ce2d301e 2019-06-14 21:18:07.465885864 +0200 2019-06-14 21:18:07.46587076 +0200
2 5f119ba3-bcea-4c3b-aabb-0406ea70f7e1 24.375 celsius 2019-06-14 21:15:28.583856443 +0200 2019-06-14 21:15:28.583856443 +0200 efcd755e-82d1-4789-a50b-355b8735b8d8 2019-06-14 21:18:07.465885864 +0200

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -2,6 +2,7 @@
{
"temperature_id": "a469503b-fc16-4e72-8d29-7eeee08ba957",
"temperature_value": "24.562",
"temperature_degree": "celsius",
"temperature_from_date": "2019-06-14T21:15:28.504051541+02:00",
"temperature_till_date": "2019-06-14T21:18:07.384104493+02:00",
"sensor_id": "84eac248-6927-4db6-b6f9-7891ce2d301e",
@ -11,6 +12,7 @@
{
"temperature_id": "5f119ba3-bcea-4c3b-aabb-0406ea70f7e1",
"temperature_value": "24.375",
"temperature_degree": "celsius",
"temperature_from_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",

View File

@ -2,6 +2,7 @@
<temperature>
<temperature_id>a469503b-fc16-4e72-8d29-7eeee08ba957</temperature_id>
<temperature_value>24.562</temperature_value>
<temperature_degree>celsius</temperature_degree>
<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>
<sensor_id>84eac248-6927-4db6-b6f9-7891ce2d301e</sensor_id>
@ -11,6 +12,7 @@
<temperature>
<temperature_id>5f119ba3-bcea-4c3b-aabb-0406ea70f7e1</temperature_id>
<temperature_value>24.375</temperature_value>
<temperature_degree>celsius</temperature_degree>
<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>
<sensor_id>efcd755e-82d1-4789-a50b-355b8735b8d8</sensor_id>

View File

@ -2,6 +2,7 @@
<temperature>
<temperature_id>a469503b-fc16-4e72-8d29-7eeee08ba957</temperature_id>
<temperature_value>24.562</temperature_value>
<temperature_degree>celsius</temperature_degree>
<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>
<sensor_id>84eac248-6927-4db6-b6f9-7891ce2d301e</sensor_id>
@ -11,6 +12,7 @@
<temperature>
<temperature_id>5f119ba3-bcea-4c3b-aabb-0406ea70f7e1</temperature_id>
<temperature_value>24.375</temperature_value>
<temperature_degree>celsius</temperature_degree>
<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>
<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
func (s *DHT11) ReadTemperature(round float64) (*types.Temperature, error) {
func (s *DHT11) ReadTemperature(degree types.Degree, round float64) (*types.Temperature, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
@ -108,6 +108,9 @@ func (s *DHT11) ReadTemperature(round float64) (*types.Temperature, error) {
return nil, fmt.Errorf("Read error: %v", err)
}
// Convert temperature degree
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree)
if round != 0 {
temperatureValue = math.Round(temperatureValue/round) * round
}
@ -115,6 +118,7 @@ func (s *DHT11) ReadTemperature(round float64) (*types.Temperature, error) {
temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
TemperatureDegree: degree,
TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(),
SensorID: s.SensorID,
@ -124,12 +128,12 @@ func (s *DHT11) ReadTemperature(round float64) (*types.Temperature, error) {
}
// ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DHT11) ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DHT11) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
temperature, err := s.ReadTemperature(round)
temperature, err := s.ReadTemperature(degree, round)
if err != nil {
errorChannel <- err
return
@ -138,14 +142,14 @@ func (s *DHT11) ReadTemperatureWriteIntoChannel(round float64, temperatureChanne
}
// ReadTemperatureContinously into a channel until context closed
func (s *DHT11) ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func (s *DHT11) ReadTemperatureContinously(ctx context.Context, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
return
default:
s.ReadTemperatureWriteIntoChannel(round, temperatureChannel, errorChannel, nil)
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
}
}
}

View File

@ -87,7 +87,7 @@ func (s *DHT22) ReadHumidityContinously(ctx context.Context, round float64, humi
}
// ReadTemperature measure the temperature
func (s *DHT22) ReadTemperature(round float64) (*types.Temperature, error) {
func (s *DHT22) ReadTemperature(degree types.Degree, round float64) (*types.Temperature, error) {
err := dht.HostInit()
if err != nil {
return nil, fmt.Errorf("HostInit error: %v", err)
@ -108,6 +108,9 @@ func (s *DHT22) ReadTemperature(round float64) (*types.Temperature, error) {
return nil, fmt.Errorf("Read error: %v", err)
}
// Convert temperature degree
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree)
// round
if round != 0 {
temperatureValue = math.Round(temperatureValue/round) * round
@ -116,6 +119,7 @@ func (s *DHT22) ReadTemperature(round float64) (*types.Temperature, error) {
temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
TemperatureDegree: degree,
TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(),
SensorID: s.SensorID,
@ -125,12 +129,12 @@ func (s *DHT22) ReadTemperature(round float64) (*types.Temperature, error) {
}
// ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DHT22) ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DHT22) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
temperature, err := s.ReadTemperature(round)
temperature, err := s.ReadTemperature(degree, round)
if err != nil {
errorChannel <- err
return
@ -139,14 +143,14 @@ func (s *DHT22) ReadTemperatureWriteIntoChannel(round float64, temperatureChanne
}
// ReadTemperatureContinously into a channel until context closed
func (s *DHT22) ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func (s *DHT22) ReadTemperatureContinously(ctx context.Context, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
return
default:
s.ReadTemperatureWriteIntoChannel(round, temperatureChannel, errorChannel, nil)
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
}
}
}

View File

@ -31,7 +31,7 @@ func (s *DS18B20) GetSensor() *types.Sensor {
}
// ReadTemperature measure the temperature
func (s *DS18B20) ReadTemperature(round float64) (*types.Temperature, error) {
func (s *DS18B20) ReadTemperature(degree types.Degree, round float64) (*types.Temperature, error) {
data, err := ioutil.ReadFile(filepath.Join("/sys/bus/w1/devices", *s.WireID, "/w1_slave"))
if err != nil {
@ -52,6 +52,9 @@ func (s *DS18B20) ReadTemperature(round float64) (*types.Temperature, error) {
temperatureValue := c / 1000
// Convert temperature degree
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree)
// round
if round != 0 {
temperatureValue = math.Round(temperatureValue/round) * round
@ -60,6 +63,7 @@ func (s *DS18B20) ReadTemperature(round float64) (*types.Temperature, error) {
temperature := &types.Temperature{
TemperatureID: uuid.NewV4().String(),
TemperatureValue: temperatureValue,
TemperatureDegree: degree,
TemperatureFromDate: time.Now(),
TemperatureTillDate: time.Now(),
SensorID: s.SensorID,
@ -70,12 +74,12 @@ func (s *DS18B20) ReadTemperature(round float64) (*types.Temperature, error) {
}
// ReadTemperatureWriteIntoChannel and write values into a channel
func (s *DS18B20) ReadTemperatureWriteIntoChannel(round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
temperature, err := s.ReadTemperature(round)
temperature, err := s.ReadTemperature(degree, round)
if err != nil {
errorChannel <- err
return
@ -84,14 +88,14 @@ func (s *DS18B20) ReadTemperatureWriteIntoChannel(round float64, temperatureChan
}
// ReadTemperatureContinously into a channel until context closed
func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, degree types.Degree, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
for {
select {
case <-ctx.Done():
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
return
default:
s.ReadTemperatureWriteIntoChannel(round, temperatureChannel, errorChannel, nil)
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
}
}
}

View File

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

View File

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

View File

@ -8,9 +8,24 @@ import (
type Temperature struct {
TemperatureID string `json:"temperature_id" xml:"temperature_id"`
TemperatureValue float64 `json:"temperature_value,string" xml:"temperature_value,string"`
TemperatureDegree Degree `json:"temperature_degree" xml:"temperature_degree"`
TemperatureFromDate time.Time `json:"temperature_from_date" xml:"temperature_from_date"`
TemperatureTillDate time.Time `json:"temperature_till_date" xml:"temperature_till_date"`
SensorID string `json:"sensor_id" xml:"sensor_id"`
CreationDate *time.Time `json:"creation_date" xml:"creation_date"`
UpdateDate *time.Time `json:"update_date" xml:"update_date"`
}
// Degree unit of measurement for temperature
type Degree string
const (
// DegreeCelsius indicates the temperature in Celsius
DegreeCelsius Degree = "celsius"
// DegreeFahrenheit indicates the temperature in Fahrenheit
DegreeFahrenheit = "fahrenheit"
// DegreeKelvin indicates the temperature in Kelvin
DegreeKelvin = "kelvin"
)