-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility for GroupValueRead at startup
- Loading branch information
simon mittelberger
committed
Aug 1, 2023
1 parent
5fb05a3
commit 1e88678
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package knx | ||
|
||
import ( | ||
"reflect" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/sirupsen/logrus" | ||
"github.com/vapourismo/knx-go/knx" | ||
"github.com/vapourismo/knx-go/knx/cemi" | ||
) | ||
|
||
// StartupReader defines the interface for active polling for metrics values against the knx system at startup. | ||
type StartupReader interface { | ||
// Run starts the startup reading. | ||
Run() | ||
// Close stops the startup reading. | ||
Close() | ||
} | ||
|
||
type startupReader struct { | ||
client GroupClient | ||
config *Config | ||
messageCounter *prometheus.CounterVec | ||
snapshotHandler MetricSnapshotHandler | ||
metricsToRead GroupAddressConfigSet | ||
ticker *time.Ticker | ||
} | ||
|
||
// NewStartupReader creates a new StartupReader instance using the given MetricsExporter for connection handling and metrics observing. | ||
func NewStartupReader(config *Config, client GroupClient, metricsHandler MetricSnapshotHandler, messageCounter *prometheus.CounterVec) StartupReader { | ||
metricsToRead := getMetricsToRead(config) | ||
return &startupReader{ | ||
client: client, | ||
config: config, | ||
messageCounter: messageCounter, | ||
snapshotHandler: metricsHandler, | ||
metricsToRead: metricsToRead, | ||
} | ||
} | ||
|
||
func (s *startupReader) Run() { | ||
readInterval := time.Duration(s.config.ReadStartupInterval) | ||
if readInterval.Milliseconds() <= 0 { | ||
readInterval = time.Duration(200 * time.Millisecond) | ||
} | ||
logrus.Infof("start reading addresses after startup in %dms intervals.", readInterval.Milliseconds()) | ||
s.ticker = time.NewTicker(readInterval) | ||
c := s.ticker.C | ||
go func(s *startupReader) { | ||
addressesToRead := reflect.ValueOf(s.metricsToRead).MapKeys() | ||
for range c { | ||
if len(addressesToRead) == 0 { | ||
break | ||
} | ||
addressToRead := addressesToRead[0].Interface().(GroupAddress) | ||
s.sendReadMessage(addressToRead) | ||
addressesToRead = addressesToRead[1:] | ||
} | ||
s.ticker.Stop() | ||
}(s) | ||
} | ||
|
||
func (s *startupReader) Close() { | ||
if s.ticker != nil { | ||
s.ticker.Stop() | ||
} | ||
} | ||
|
||
func (s *startupReader) sendReadMessage(address GroupAddress) { | ||
event := knx.GroupEvent{ | ||
Command: knx.GroupRead, | ||
Destination: cemi.GroupAddr(address), | ||
Source: cemi.IndividualAddr(s.config.Connection.PhysicalAddress), | ||
} | ||
|
||
if e := s.client.Send(event); e != nil { | ||
logrus.Errorf("can not send read request for %s: %s", address.String(), e) | ||
} | ||
s.messageCounter.WithLabelValues("sent", "true").Inc() | ||
} | ||
|
||
func getMetricsToRead(config *Config) GroupAddressConfigSet { | ||
toRead := make(GroupAddressConfigSet) | ||
for address, addressConfig := range config.AddressConfigs { | ||
if !addressConfig.Export || !addressConfig.ReadStartup { | ||
continue | ||
} | ||
|
||
toRead[address] = GroupAddressConfig{ | ||
Name: config.NameFor(addressConfig), | ||
ReadStartup: true, | ||
} | ||
} | ||
return toRead | ||
} |