Skip to content

Commit

Permalink
Replaces use of deprecated functions with their successors.
Browse files Browse the repository at this point in the history
  • Loading branch information
chr-fritz committed Aug 17, 2023
1 parent 4207889 commit eeba1af
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 74 deletions.
2 changes: 1 addition & 1 deletion cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ PowerShell:
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell", "ps1"},
Args: cobra.ExactValidArgs(1),
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
Expand Down
146 changes: 73 additions & 73 deletions pkg/knx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,60 @@
package knx

import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"time"
"encoding/json"
"fmt"
"os"
"strings"
"time"

"github.com/ghodss/yaml"
"github.com/ghodss/yaml"
)

// Config defines the structure of the configuration file which defines which
// KNX Group Addresses were mapped into prometheus metrics.
type Config struct {
Connection Connection `json:",omitempty"`
// MetricsPrefix is a short prefix which will be added in front of the actual metric name.
MetricsPrefix string
AddressConfigs GroupAddressConfigSet
Connection Connection `json:",omitempty"`
// MetricsPrefix is a short prefix which will be added in front of the actual metric name.
MetricsPrefix string
AddressConfigs GroupAddressConfigSet
}

// ReadConfig reads the given configuration file and returns the parsed Config object.
func ReadConfig(configFile string) (*Config, error) {
content, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, fmt.Errorf("can not read group address configuration: %s", err)
}
config := Config{}
err = yaml.Unmarshal(content, &config)
if err != nil {
return nil, fmt.Errorf("can not read config file %s: %s", configFile, err)
}
return &config, nil
content, err := os.ReadFile(configFile)
if err != nil {
return nil, fmt.Errorf("can not read group address configuration: %s", err)
}
config := Config{}
err = yaml.Unmarshal(content, &config)
if err != nil {
return nil, fmt.Errorf("can not read config file %s: %s", configFile, err)
}
return &config, nil
}

// NameForGa returns the full metric name for the given GroupAddress.
func (c *Config) NameForGa(address GroupAddress) string {
gaConfig, ok := c.AddressConfigs[address]
if !ok {
return ""
}
return c.NameFor(gaConfig)
gaConfig, ok := c.AddressConfigs[address]
if !ok {
return ""
}
return c.NameFor(gaConfig)
}

// NameFor return s the full metric name for the given GroupAddressConfig.
func (c *Config) NameFor(gaConfig GroupAddressConfig) string {
return c.MetricsPrefix + gaConfig.Name
return c.MetricsPrefix + gaConfig.Name
}

// Connection contains the information about how to connect to the KNX system and how to identify itself.
type Connection struct {
// Type of the actual connection. Can be either Tunnel or Router
Type ConnectionType
// Endpoint defines the IP address or hostname and port to where it should connect.
Endpoint string
// PhysicalAddress defines how the knx-exporter should identify itself within the KNX system.
PhysicalAddress PhysicalAddress
// Type of the actual connection. Can be either Tunnel or Router
Type ConnectionType
// Endpoint defines the IP address or hostname and port to where it should connect.
Endpoint string
// PhysicalAddress defines how the knx-exporter should identify itself within the KNX system.
PhysicalAddress PhysicalAddress
}

type ConnectionType string
Expand All @@ -77,62 +77,62 @@ const Tunnel = ConnectionType("Tunnel")
const Router = ConnectionType("Router")

func (t ConnectionType) MarshalJSON() ([]byte, error) {
return json.Marshal(string(t))
return json.Marshal(string(t))
}

func (t *ConnectionType) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
switch strings.ToLower(str) {
case "tunnel":
*t = Tunnel
case "router":
*t = Router
default:
return fmt.Errorf("invalid connection type given: \"%s\"", str)
}
return nil
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
switch strings.ToLower(str) {
case "tunnel":
*t = Tunnel
case "router":
*t = Router
default:
return fmt.Errorf("invalid connection type given: \"%s\"", str)
}
return nil
}

type Duration time.Duration

func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
return json.Marshal(time.Duration(d).String())
}

func (d *Duration) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
duration, err := time.ParseDuration(str)
if err != nil {
return err
}
*d = Duration(duration)
return nil
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
duration, err := time.ParseDuration(str)
if err != nil {
return err
}
*d = Duration(duration)
return nil
}

// GroupAddressConfig defines all information to map a KNX group address to a prometheus metric.
type GroupAddressConfig struct {
// Name defines the prometheus metric name without the MetricsPrefix.
Name string
// Comment to identify the group address.
Comment string `json:",omitempty"`
// DPT defines the DPT at the knx bus. This is required to parse the values correctly.
DPT string
// MetricType is the type that prometheus uses when exporting it. i.e. gauge or counter
MetricType string
// Export the metric to prometheus
Export bool
// ReadActive allows the exporter to actively send `GroupValueRead` telegrams to actively poll the value instead waiting for it.
ReadActive bool `json:",omitempty"`
// MaxAge of a value until it will actively send a `GroupValueRead` telegram to read the value if ReadActive is set to true.
MaxAge Duration `json:",omitempty"`
// Labels defines static labels that should be set when exporting the metric using prometheus.
Labels map[string]string `json:",omitempty"`
// Name defines the prometheus metric name without the MetricsPrefix.
Name string
// Comment to identify the group address.
Comment string `json:",omitempty"`
// DPT defines the DPT at the knx bus. This is required to parse the values correctly.
DPT string
// MetricType is the type that prometheus uses when exporting it. i.e. gauge or counter
MetricType string
// Export the metric to prometheus
Export bool
// ReadActive allows the exporter to actively send `GroupValueRead` telegrams to actively poll the value instead waiting for it.
ReadActive bool `json:",omitempty"`
// MaxAge of a value until it will actively send a `GroupValueRead` telegram to read the value if ReadActive is set to true.
MaxAge Duration `json:",omitempty"`
// Labels defines static labels that should be set when exporting the metric using prometheus.
Labels map[string]string `json:",omitempty"`
}

// GroupAddressConfigSet is a shortcut type for the group address config map.
Expand Down

0 comments on commit eeba1af

Please sign in to comment.