RWMutex; update lastValues all at once
This commit is contained in:
parent
fa7b18bc2d
commit
8408517b7d
2 changed files with 18 additions and 11 deletions
21
main.go
21
main.go
|
@ -42,7 +42,7 @@ var (
|
||||||
scrapeInterval = flag.Duration("scrape-interval", 15*time.Second, "Period of modbus scraping loop")
|
scrapeInterval = flag.Duration("scrape-interval", 15*time.Second, "Period of modbus scraping loop")
|
||||||
|
|
||||||
promHandler = promhttp.Handler()
|
promHandler = promhttp.Handler()
|
||||||
scrapeMu sync.Mutex
|
scrapeMu sync.RWMutex
|
||||||
lastScrape time.Time
|
lastScrape time.Time
|
||||||
lastValues = make(map[uint16]float64)
|
lastValues = make(map[uint16]float64)
|
||||||
|
|
||||||
|
@ -128,7 +128,7 @@ func dialInverter() (*sungrowConn, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called under scrapeMu.
|
// Called under scrapeMu.
|
||||||
func readRegs(client modbus.Client, start, qty uint16) error {
|
func readRegs(vals map[uint16]float64, client modbus.Client, start, qty uint16) error {
|
||||||
data, err := client.ReadInputRegisters(start, qty)
|
data, err := client.ReadInputRegisters(start, qty)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("read input registers %d-%d: %v", start+1, start+qty, err)
|
return fmt.Errorf("read input registers %d-%d: %v", start+1, start+qty, err)
|
||||||
|
@ -149,7 +149,7 @@ func readRegs(client modbus.Client, start, qty uint16) error {
|
||||||
return fmt.Errorf("parse input register data at %d: %v", addr, err)
|
return fmt.Errorf("parse input register data at %d: %v", addr, err)
|
||||||
}
|
}
|
||||||
//fmt.Printf("%s: %v %s\n", reg.name, val, reg.unit)
|
//fmt.Printf("%s: %v %s\n", reg.name, val, reg.unit)
|
||||||
lastValues[addr] = val
|
vals[addr] = val
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -168,17 +168,20 @@ func scrape() error {
|
||||||
|
|
||||||
client := modbus.NewClient(handler)
|
client := modbus.NewClient(handler)
|
||||||
|
|
||||||
|
vals := make(map[uint16]float64)
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
scrapeStart.SetToCurrentTime()
|
scrapeStart.SetToCurrentTime()
|
||||||
if err := readRegs(client, 5000, 50); err != nil {
|
if err := readRegs(vals, client, 5000, 50); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := readRegs(client, 5050, 50); err != nil {
|
if err := readRegs(vals, client, 5050, 50); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := readRegs(client, 5100, 50); err != nil {
|
if err := readRegs(vals, client, 5100, 50); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastValues = vals
|
||||||
scrapeEnd.SetToCurrentTime()
|
scrapeEnd.SetToCurrentTime()
|
||||||
lastScrape = time.Now()
|
lastScrape = time.Now()
|
||||||
scrapeDuration.Set(time.Since(start).Seconds())
|
scrapeDuration.Set(time.Since(start).Seconds())
|
||||||
|
@ -223,7 +226,11 @@ func main() {
|
||||||
Name: reg.name,
|
Name: reg.name,
|
||||||
Help: fmt.Sprintf("addr: %d, unit: %s", addr, reg.unit),
|
Help: fmt.Sprintf("addr: %d, unit: %s", addr, reg.unit),
|
||||||
},
|
},
|
||||||
func() float64 { return lastValues[addr] },
|
func() float64 {
|
||||||
|
scrapeMu.RLock()
|
||||||
|
defer scrapeMu.RUnlock()
|
||||||
|
return lastValues[addr]
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,7 +75,7 @@ var sungrowInputRegs = map[uint16]*register{
|
||||||
5148: {"frequency_2", frequency2, 0.01, "Hz"},
|
5148: {"frequency_2", frequency2, 0.01, "Hz"},
|
||||||
}
|
}
|
||||||
|
|
||||||
var errSkippableRead = errors.New("skip read")
|
var errSkippableRead = errors.New("skippable misread")
|
||||||
|
|
||||||
func frequency2(data []byte) (float64, error) {
|
func frequency2(data []byte) (float64, error) {
|
||||||
n := binary.BigEndian.Uint16(data)
|
n := binary.BigEndian.Uint16(data)
|
||||||
|
@ -83,9 +83,9 @@ func frequency2(data []byte) (float64, error) {
|
||||||
// probably a misread
|
// probably a misread
|
||||||
return 0, fmt.Errorf("likely misread: frequency_2 read %v", n)
|
return 0, fmt.Errorf("likely misread: frequency_2 read %v", n)
|
||||||
}
|
}
|
||||||
if n == 100 {
|
if n <= 100 {
|
||||||
// this translates as 1 Hz, which is unlikely
|
// 100 translates as 1 Hz, which is... unlikely
|
||||||
return 0, fmt.Errorf("%w: frequency_2 is 1.00Hz", errSkippableRead)
|
return 0, fmt.Errorf("%w: frequency_2 is %d", errSkippableRead, n)
|
||||||
}
|
}
|
||||||
return float64(n), nil
|
return float64(n), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue