PKGBUILD/pkg/sensor/ds18b20.go

51 lines
1.1 KiB
Go
Raw Normal View History

2019-02-22 12:08:58 +00:00
package sensor
import (
"fmt"
"time"
uuid "github.com/satori/go.uuid"
2019-05-12 09:57:53 +00:00
"github.com/volker-raschek/flucky/pkg/types"
2019-02-22 12:08:58 +00:00
"github.com/yryz/ds18b20"
)
// DS18B20 is a sensor to measure humidity and temperature.
2019-02-22 12:08:58 +00:00
type DS18B20 struct {
*types.Sensor
}
// GetEnabled returns a boolen if the sensor is enabled or disabled
func (s *DS18B20) GetEnabled() bool {
return s.SensorEnabled
}
// 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{
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
}