diff --git a/README.md b/README.md index 0c5ba7f..e15790e 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,14 @@ Convert the FritzBox tr-064 data to MQTT messages. { "name": "Uptime" }, { "name": "ConnectionStatus", "mapEnum": { "Connected": 1, "__default": 0 } } ] + }, + { + "service": "urn:dslforum-org:service:Hosts:1", + "action": "GetSpecificHostEntry", + "args": [ + { "name": "NewMACAddress", "value": "12:34:56:78:9A:BC" } + ], + "alias": "host1" } ] } diff --git a/app/config/config.go b/app/config/config.go index 83c5c63..d2511ec 100644 --- a/app/config/config.go +++ b/app/config/config.go @@ -11,6 +11,12 @@ type ValueMapping struct { Name string `json:"name"` MapEnum map[string]interface{} `json:"mapEnum,omitempty"` } + +type Argument struct { + Name string `json:"name"` + Value string `json:"value"` +} + type Fritzbox struct { PollingInterval int `json:"polling-interval"` Host string `json:"host"` @@ -20,7 +26,9 @@ type Fritzbox struct { Message []struct { Service string `json:"service"` Action string `json:"action"` + Args []Argument `json:"args"` Values []ValueMapping `json:"values"` + Alias string `json:"alias,omitempty"` } `json:"message"` } @@ -35,6 +43,14 @@ type Config struct { LogLevel string `json:"loglevel,omitempty"` } +func (cfg Config) Validate() { + for _, msg := range cfg.Fritzbox.Message { + if len(msg.Args) > 1 { + panic("Currently only one argument is supported check configuration for " + msg.Service + " " + msg.Action) + } + } +} + func LoadConfig(file string) (Config, error) { data, err := os.ReadFile(file) if err != nil { @@ -58,5 +74,7 @@ func LoadConfig(file string) (Config, error) { cfg.LogLevel = "info" } + cfg.Validate() + return cfg, nil } diff --git a/app/fritzbox/fritzbox.go b/app/fritzbox/fritzbox.go index a63511f..fcedfcf 100644 --- a/app/fritzbox/fritzbox.go +++ b/app/fritzbox/fritzbox.go @@ -64,10 +64,23 @@ func LoadData(cfg config.Config) map[string]interface{} { actionArg := fritzbox_upnp.ActionArgument{ Name: "", } + + if len(msg.Args) > 0 { + actionArg = fritzbox_upnp.ActionArgument{ + Name: msg.Args[0].Name, + Value: msg.Args[0].Value, + } + } + result, err := action.Call(&actionArg) if err == nil { for key, value := range result { - dataMap[key] = mapEnumValue(msg.Values, key, value) + keyName := key + if msg.Alias != "" { + keyName = msg.Alias + "." + key + } + + dataMap[keyName] = mapEnumValue(msg.Values, key, value) } } else { logger.Error("Error calling action", err)