46 lines
1023 B
Go
46 lines
1023 B
Go
package sensor
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/go-flucky/flucky/pkg/types"
|
|
uuid "github.com/satori/go.uuid"
|
|
"github.com/yryz/ds18b20"
|
|
)
|
|
|
|
// DS18B20 is a sensor to measure humidity and temperature.
|
|
type DS18B20 struct {
|
|
*types.Sensor
|
|
}
|
|
|
|
// 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
|
|
func (s *DS18B20) ReadTemperature() (*types.Temperature, error) {
|
|
|
|
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{
|
|
TemperatureID: uuid.NewV4().String(),
|
|
TemperatureValue: t,
|
|
TemperatureFromDate: time.Now(),
|
|
TemperatureTillDate: time.Now(),
|
|
SensorID: s.SensorID,
|
|
}
|
|
|
|
return temperature, nil
|
|
|
|
}
|