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"
|
|
|
|
)
|
|
|
|
|
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-15 09:49:46 +00:00
|
|
|
// GetEnabled returns a boolen if the sensor is enabled or disabled
|
|
|
|
func (s *DS18B20) GetEnabled() bool {
|
|
|
|
return s.SensorEnabled
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|