Skip to content

Commit

Permalink
Speed up loading temperatures
Browse files Browse the repository at this point in the history
  • Loading branch information
l3akage committed Apr 15, 2019
1 parent 1b83ba3 commit 8ff6e59
Showing 1 changed file with 57 additions and 36 deletions.
93 changes: 57 additions & 36 deletions onewire_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
Expand All @@ -32,55 +33,75 @@ type Temp struct {
type onewireCollector struct {
}

func getTemperatures() ([]Temp, error) {
func getTemperatureFromDevice(device os.FileInfo) Temp {
reg, err := regexp.Compile("[^0-9]+")
if err != nil {
log.Fatal(err)
}
for i := 1; i <= 5; i++ {
content, err := ioutil.ReadFile("/sys/bus/w1/devices/" + device.Name() + "/w1_slave")
if err != nil {
log.Infof("Error reading device %s\n", device.Name())
continue
}
lines := strings.Split(string(content), "\n")
if len(lines) != 3 {
log.Infof("Unknown format for device %s\n", device.Name())
continue
}
if !strings.Contains(lines[0], "YES") {
log.Infof("CRC invalid for device %s\n", device.Name())
continue
}
data := strings.SplitAfter(lines[1], "t=")
if len(data) != 2 {
log.Infof("Temp value not found for device %s\n", device.Name())
continue
}
strValue := reg.ReplaceAllString(data[1], "")

tempInt, err := strconv.ParseFloat(strValue, 64)
if err != nil {
continue
}
if tempInt == 85000 {
continue
}
return Temp{
ID: device.Name(),
Value: tempInt / 1000.0,
}
}
return Temp{}
}

func getTemperatures() ([]Temp, error) {
devices, err := ioutil.ReadDir("/sys/bus/w1/devices/")
if err != nil {
return nil, err
}
var values []Temp
var wg sync.WaitGroup
valueChan := make(chan Temp)
for _, device := range devices {
if _, err := os.Stat("/sys/bus/w1/devices/" + device.Name() + "/w1_slave"); err != nil {
continue
}
for i := 1; i <= 5; i++ {
content, err := ioutil.ReadFile("/sys/bus/w1/devices/" + device.Name() + "/w1_slave")
if err != nil {
log.Infof("Error reading device %s\n", device.Name())
continue
}
lines := strings.Split(string(content), "\n")
if len(lines) != 3 {
log.Infof("Unknown format for device %s\n", device.Name())
continue
}
if !strings.Contains(lines[0], "YES") {
log.Infof("CRC invalid for device %s\n", device.Name())
continue
}
data := strings.SplitAfter(lines[1], "t=")
if len(data) != 2 {
log.Infof("Temp value not found for device %s\n", device.Name())
continue
}
strValue := reg.ReplaceAllString(data[1], "")

tempInt, err := strconv.ParseFloat(strValue, 64)
if err != nil {
continue
}
if tempInt == 85000 {
continue
}
values = append(values, Temp{
ID: device.Name(),
Value: tempInt / 1000.0,
})
break
wg.Add(1)
go func(device os.FileInfo) {
defer wg.Done()
valueChan <- getTemperatureFromDevice(device)
}(device)
}
go func() {
wg.Wait()
close(valueChan)
}()
var values []Temp
for t := range valueChan {
if t == (Temp{}) {
continue
}
values = append(values, t)
}
return values, nil
}
Expand Down

0 comments on commit 8ff6e59

Please sign in to comment.