2019-02-22 12:08:58 +00:00
|
|
|
package sensor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
"github.com/yryz/ds18b20"
|
|
|
|
)
|
|
|
|
|
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-02-24 21:46:36 +00:00
|
|
|
func (s *DS18B20) ReadTemperature() (*types.Temperature, error) {
|
2019-02-22 12:08:58 +00:00
|
|
|
|
|
|
|
t, err := ds18b20.Temperature(*s.WireID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Can not read from Sensor %v (UUID: %v, Wire-ID: %v): %v", s.SensorName, s.SensorID, s.WireID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
temperature := &types.Temperature{
|
2019-06-13 20:22:12 +00:00
|
|
|
TemperatureID: uuid.NewV4().String(),
|
|
|
|
TemperatureValue: t,
|
|
|
|
TemperatureFromDate: time.Now(),
|
|
|
|
TemperatureTillDate: time.Now(),
|
|
|
|
SensorID: s.SensorID,
|
2019-02-22 12:08:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return temperature, nil
|
|
|
|
|
|
|
|
}
|