2019-02-22 12:08:58 +00:00
|
|
|
package sensor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-06-17 21:37:48 +00:00
|
|
|
"io/ioutil"
|
2020-05-03 12:04:08 +00:00
|
|
|
"os"
|
2019-06-17 21:37:48 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-05-03 12:04:08 +00:00
|
|
|
"sync"
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2020-06-10 19:13:05 +00:00
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/internal/format"
|
|
|
|
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
|
2020-05-03 12:04:08 +00:00
|
|
|
uuid "github.com/satori/go.uuid"
|
2019-02-22 12:08:58 +00:00
|
|
|
)
|
|
|
|
|
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
|
2020-05-03 12:04:08 +00:00
|
|
|
mutex *sync.Mutex
|
2019-02-22 12:08:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
// Read measured values
|
|
|
|
func (ds18b20 *DS18B20) Read() ([]*types.MeasuredValue, error) {
|
2020-01-10 21:03:49 +00:00
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
// Lock multiple access
|
|
|
|
ds18b20.mutex.Lock()
|
|
|
|
defer ds18b20.mutex.Unlock()
|
2019-06-13 19:25:32 +00:00
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
if ds18b20.WireID == nil {
|
|
|
|
return nil, fmt.Errorf("WireID is not specified")
|
|
|
|
}
|
2019-02-22 12:08:58 +00:00
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
socketPath := filepath.Join("/sys/bus/w1/devices", *ds18b20.WireID, "/w1_slave")
|
|
|
|
if _, err := os.Stat(socketPath); os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("Socket path not found: %v", socketPath)
|
2019-06-27 07:17:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 12:04:08 +00:00
|
|
|
data, err := ioutil.ReadFile(socketPath)
|
2019-02-22 12:08:58 +00:00
|
|
|
if err != nil {
|
2020-05-03 12:04:08 +00:00
|
|
|
return nil, fmt.Errorf("Can not read data from sensor %v", ds18b20.Name)
|
2019-06-17 21:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
raw := string(data)
|
|
|
|
|
|
|
|
i := strings.LastIndex(raw, "t=")
|
|
|
|
if i == -1 {
|
2020-01-10 18:42:19 +00:00
|
|
|
return nil, errorReadData
|
2019-06-17 21:37:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
c, err := strconv.ParseFloat(raw[i+2:len(raw)-1], 64)
|
2019-06-17 21:37:48 +00:00
|
|
|
if err != nil {
|
2020-01-10 18:42:19 +00:00
|
|
|
return nil, errorParseData
|
2019-02-22 12:08:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 21:02:11 +00:00
|
|
|
temperatureValue := c / 1000
|
|
|
|
|
2019-07-02 20:33:01 +00:00
|
|
|
measuredValues := []*types.MeasuredValue{
|
2020-05-03 12:04:08 +00:00
|
|
|
{
|
2019-07-02 20:33:01 +00:00
|
|
|
ID: uuid.NewV4().String(),
|
|
|
|
Value: float64(temperatureValue),
|
2020-09-21 17:36:42 +00:00
|
|
|
ValueType: types.Temperature,
|
2020-05-31 22:52:54 +00:00
|
|
|
Date: format.FormatedTime(),
|
2020-05-03 12:04:08 +00:00
|
|
|
SensorID: ds18b20.ID,
|
2019-06-25 20:22:34 +00:00
|
|
|
},
|
2019-02-22 12:08:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-25 20:22:34 +00:00
|
|
|
return measuredValues, nil
|
2020-01-10 18:42:19 +00:00
|
|
|
}
|