Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support arguments for actions #87

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
Expand Down
18 changes: 18 additions & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
}

Expand All @@ -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 {
Expand All @@ -58,5 +74,7 @@ func LoadConfig(file string) (Config, error) {
cfg.LogLevel = "info"
}

cfg.Validate()

return cfg, nil
}
15 changes: 14 additions & 1 deletion app/fritzbox/fritzbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading