PKGBUILD/pkg/sensor/ds18b20.go

33 lines
650 B
Go
Raw Normal View History

2019-02-22 12:08:58 +00:00
package sensor
import (
"fmt"
"time"
2019-02-24 21:46:36 +00:00
"git.cryptic.systems/fh-trier/go-flucky/pkg/types"
2019-02-22 12:08:58 +00:00
uuid "github.com/satori/go.uuid"
"github.com/yryz/ds18b20"
)
type DS18B20 struct {
*types.Sensor
}
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,
TemperatureDate: time.Now(),
SensorID: s.SensorID,
}
return temperature, nil
}