refac(pkg/sensor): temperature measurement unit
This commit is contained in:
@ -87,7 +87,7 @@ func (s *DHT11) ReadHumidityContinously(ctx context.Context, round float64, humi
|
||||
}
|
||||
|
||||
// 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()
|
||||
if err != nil {
|
||||
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
|
||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree)
|
||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
|
||||
|
||||
if round != 0 {
|
||||
temperatureValue = math.Round(temperatureValue/round) * round
|
||||
@ -118,7 +118,7 @@ func (s *DHT11) ReadTemperature(degree types.Degree, round float64) (*types.Temp
|
||||
temperature := &types.Temperature{
|
||||
TemperatureID: uuid.NewV4().String(),
|
||||
TemperatureValue: temperatureValue,
|
||||
TemperatureDegree: degree,
|
||||
TemperatureUnit: degree,
|
||||
TemperatureFromDate: time.Now(),
|
||||
TemperatureTillDate: time.Now(),
|
||||
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
|
||||
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 {
|
||||
defer wg.Done()
|
||||
}
|
||||
@ -142,7 +142,7 @@ func (s *DHT11) ReadTemperatureWriteIntoChannel(degree types.Degree, round float
|
||||
}
|
||||
|
||||
// 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 {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
@ -87,7 +87,7 @@ func (s *DHT22) ReadHumidityContinously(ctx context.Context, round float64, humi
|
||||
}
|
||||
|
||||
// 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()
|
||||
if err != nil {
|
||||
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
|
||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree)
|
||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
|
||||
|
||||
// round
|
||||
if round != 0 {
|
||||
@ -119,7 +119,7 @@ func (s *DHT22) ReadTemperature(degree types.Degree, round float64) (*types.Temp
|
||||
temperature := &types.Temperature{
|
||||
TemperatureID: uuid.NewV4().String(),
|
||||
TemperatureValue: temperatureValue,
|
||||
TemperatureDegree: degree,
|
||||
TemperatureUnit: degree,
|
||||
TemperatureFromDate: time.Now(),
|
||||
TemperatureTillDate: time.Now(),
|
||||
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
|
||||
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 {
|
||||
defer wg.Done()
|
||||
}
|
||||
@ -143,7 +143,7 @@ func (s *DHT22) ReadTemperatureWriteIntoChannel(degree types.Degree, round float
|
||||
}
|
||||
|
||||
// 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 {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
@ -31,7 +31,7 @@ func (s *DS18B20) GetSensor() *types.Sensor {
|
||||
}
|
||||
|
||||
// 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"))
|
||||
if err != nil {
|
||||
@ -53,7 +53,7 @@ func (s *DS18B20) ReadTemperature(degree types.Degree, round float64) (*types.Te
|
||||
temperatureValue := c / 1000
|
||||
|
||||
// Convert temperature degree
|
||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.DegreeCelsius, degree)
|
||||
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
|
||||
|
||||
// round
|
||||
if round != 0 {
|
||||
@ -63,7 +63,7 @@ func (s *DS18B20) ReadTemperature(degree types.Degree, round float64) (*types.Te
|
||||
temperature := &types.Temperature{
|
||||
TemperatureID: uuid.NewV4().String(),
|
||||
TemperatureValue: temperatureValue,
|
||||
TemperatureDegree: degree,
|
||||
TemperatureUnit: degree,
|
||||
TemperatureFromDate: time.Now(),
|
||||
TemperatureTillDate: time.Now(),
|
||||
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
|
||||
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 {
|
||||
defer wg.Done()
|
||||
}
|
||||
@ -88,7 +88,7 @@ func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.Degree, round flo
|
||||
}
|
||||
|
||||
// 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 {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
@ -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(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)
|
||||
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)
|
||||
}
|
||||
|
@ -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, 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))
|
||||
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
|
||||
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 {
|
||||
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.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 {
|
||||
select {
|
||||
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 {
|
||||
// Celsius
|
||||
case types.DegreeCelsius:
|
||||
case types.TemperatureUnitCelsius:
|
||||
switch toDegree {
|
||||
// Celsius -> Celsius
|
||||
case types.DegreeCelsius:
|
||||
case types.TemperatureUnitCelsius:
|
||||
return value
|
||||
// Celsius -> Fahrenheit
|
||||
case types.DegreeFahrenheit:
|
||||
case types.TemperatureUnitFahrenheit:
|
||||
return (value * 9 / 5) + 32
|
||||
// Celsius -> Kelvin
|
||||
case types.DegreeKelvin:
|
||||
case types.TemperatureUnitKelvin:
|
||||
return value + 273.15
|
||||
}
|
||||
|
||||
// Fahrenheit
|
||||
case types.DegreeFahrenheit:
|
||||
case types.TemperatureUnitFahrenheit:
|
||||
switch toDegree {
|
||||
// Fahrenheit -> Celsius
|
||||
case types.DegreeCelsius:
|
||||
case types.TemperatureUnitCelsius:
|
||||
return (value - 32) * 5 / 9
|
||||
// Fahrenheit -> Fahrenheit
|
||||
case types.DegreeFahrenheit:
|
||||
case types.TemperatureUnitFahrenheit:
|
||||
return value
|
||||
// Fahrenheit -> Kelvin
|
||||
case types.DegreeKelvin:
|
||||
case types.TemperatureUnitKelvin:
|
||||
return (value-32)*5/9 + 273.15
|
||||
}
|
||||
|
||||
case types.DegreeKelvin:
|
||||
case types.TemperatureUnitKelvin:
|
||||
switch toDegree {
|
||||
// Kelvin -> Celsius
|
||||
case types.DegreeCelsius:
|
||||
case types.TemperatureUnitCelsius:
|
||||
return value - 273.15
|
||||
// Kelvin -> Fahrenheit
|
||||
case types.DegreeFahrenheit:
|
||||
case types.TemperatureUnitFahrenheit:
|
||||
return (value-273.15)*9/5 + 32
|
||||
// Kevin -> Kelvin
|
||||
case types.DegreeKelvin:
|
||||
case types.TemperatureUnitKelvin:
|
||||
return value
|
||||
}
|
||||
}
|
||||
@ -146,14 +146,14 @@ func convertTemperatureMeasurementUnit(value float64, fromDegree types.Degree, t
|
||||
return value
|
||||
}
|
||||
|
||||
func SelectTemperatureMeasurementUnit(unit string) (types.Degree, error) {
|
||||
func SelectTemperatureMeasurementUnit(unit string) (types.TemperatureUnit, error) {
|
||||
switch unit {
|
||||
case "celsius":
|
||||
return types.DegreeCelsius, nil
|
||||
return types.TemperatureUnitCelsius, nil
|
||||
case "fahrenheit":
|
||||
return types.DegreeFahrenheit, nil
|
||||
return types.TemperatureUnitFahrenheit, nil
|
||||
case "kelvin":
|
||||
return types.DegreeKelvin, nil
|
||||
return types.TemperatureUnitKelvin, nil
|
||||
default:
|
||||
return "", fmt.Errorf("Can not determine temperature measurement unit")
|
||||
}
|
||||
|
Reference in New Issue
Block a user