2019-02-22 12:08:58 +00:00
|
|
|
package sensor
|
|
|
|
|
|
|
|
import (
|
2019-06-17 21:37:48 +00:00
|
|
|
"context"
|
2019-02-22 12:08:58 +00:00
|
|
|
"fmt"
|
2019-06-17 21:37:48 +00:00
|
|
|
"io/ioutil"
|
2019-06-18 21:02:11 +00:00
|
|
|
"math"
|
2019-06-17 21:37:48 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2019-02-22 12:08:58 +00:00
|
|
|
"time"
|
|
|
|
|
2019-06-15 13:58:41 +00:00
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
2019-02-22 12:08:58 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
|
|
|
)
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// DS18B20 is a sensor to measure humidity and temperature.
|
2019-02-22 12:08:58 +00:00
|
|
|
type DS18B20 struct {
|
|
|
|
*types.Sensor
|
|
|
|
}
|
|
|
|
|
2019-06-13 19:25:32 +00:00
|
|
|
// GetSensorModel returns the sensor model
|
|
|
|
func (s *DS18B20) GetSensorModel() types.SensorModel {
|
|
|
|
return s.Sensor.SensorModel
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSensor return the sensor struct
|
|
|
|
func (s *DS18B20) GetSensor() *types.Sensor {
|
|
|
|
return s.Sensor
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadTemperature measure the temperature
|
2019-06-23 12:17:57 +00:00
|
|
|
func (s *DS18B20) ReadTemperature(degree types.TemperatureUnit, round float64) (*types.Temperature, error) {
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2019-06-17 21:37:48 +00:00
|
|
|
data, err := ioutil.ReadFile(filepath.Join("/sys/bus/w1/devices", *s.WireID, "/w1_slave"))
|
2019-02-22 12:08:58 +00:00
|
|
|
if err != nil {
|
2019-06-17 21:37:48 +00:00
|
|
|
return nil, fmt.Errorf("Can not read data from sensor %v", s.SensorName)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := string(data)
|
|
|
|
|
|
|
|
i := strings.LastIndex(raw, "t=")
|
|
|
|
if i == -1 {
|
|
|
|
return nil, ErrReadSensor
|
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
c, err := strconv.ParseFloat(raw[i+2:len(raw)-1], 64)
|
2019-06-17 21:37:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ErrParseData
|
2019-02-22 12:08:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
temperatureValue := c / 1000
|
|
|
|
|
2019-06-23 11:33:09 +00:00
|
|
|
// Convert temperature degree
|
2019-06-23 12:17:57 +00:00
|
|
|
temperatureValue = convertTemperatureMeasurementUnit(temperatureValue, types.TemperatureUnitCelsius, degree)
|
2019-06-23 11:33:09 +00:00
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
// round
|
|
|
|
if round != 0 {
|
|
|
|
temperatureValue = math.Round(temperatureValue/round) * round
|
|
|
|
}
|
|
|
|
|
2019-02-22 12:08:58 +00:00
|
|
|
temperature := &types.Temperature{
|
2019-06-13 20:22:12 +00:00
|
|
|
TemperatureID: uuid.NewV4().String(),
|
2019-06-18 21:02:11 +00:00
|
|
|
TemperatureValue: temperatureValue,
|
2019-06-23 12:17:57 +00:00
|
|
|
TemperatureUnit: degree,
|
2019-06-13 20:22:12 +00:00
|
|
|
TemperatureFromDate: time.Now(),
|
|
|
|
TemperatureTillDate: time.Now(),
|
|
|
|
SensorID: s.SensorID,
|
2019-02-22 12:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return temperature, nil
|
|
|
|
|
|
|
|
}
|
2019-06-17 21:37:48 +00:00
|
|
|
|
|
|
|
// ReadTemperatureWriteIntoChannel and write values into a channel
|
2019-06-23 12:17:57 +00:00
|
|
|
func (s *DS18B20) ReadTemperatureWriteIntoChannel(degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error, wg *sync.WaitGroup) {
|
2019-06-17 21:37:48 +00:00
|
|
|
if wg != nil {
|
|
|
|
defer wg.Done()
|
|
|
|
}
|
|
|
|
|
2019-06-23 11:33:09 +00:00
|
|
|
temperature, err := s.ReadTemperature(degree, round)
|
2019-06-17 21:37:48 +00:00
|
|
|
if err != nil {
|
|
|
|
errorChannel <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
temperatureChannel <- temperature
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadTemperatureContinously into a channel until context closed
|
2019-06-23 12:17:57 +00:00
|
|
|
func (s *DS18B20) ReadTemperatureContinously(ctx context.Context, degree types.TemperatureUnit, round float64, temperatureChannel chan<- *types.Temperature, errorChannel chan<- error) {
|
2019-06-17 21:37:48 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
errorChannel <- fmt.Errorf("%v: Context closed: %v", s.SensorName, ctx.Err())
|
|
|
|
return
|
|
|
|
default:
|
2019-06-23 11:33:09 +00:00
|
|
|
s.ReadTemperatureWriteIntoChannel(degree, round, temperatureChannel, errorChannel, nil)
|
2019-06-17 21:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|