Skip to content

Commit

Permalink
feat: add a command to interact with the diagnostic rpc
Browse files Browse the repository at this point in the history
This wil allow to query the diagnostic state of the device and also to
query the filter schema from the device.

Signed-off-by: Christian Ege <[email protected]>
  • Loading branch information
graugans committed Aug 22, 2023
1 parent 56a12c5 commit 71817e1
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
85 changes: 85 additions & 0 deletions cmd/ovp8xx/cmd/diagnostic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright © 2023 Christian Ege <[email protected]>
*/
package cmd

import (
"fmt"

"github.com/graugans/go-ovp8xx/pkg/ovp8xx"
"github.com/spf13/cobra"
)

func diagnosticGetFilteredCommand(cmd *cobra.Command, args []string) error {

filter, err := cmd.Flags().GetString("filter")
if err != nil {
return err
}
host, err := rootCmd.PersistentFlags().GetString("ip")
if err != nil {
return err
}

o3r := ovp8xx.NewClient(
ovp8xx.WithHost(host),
)

diag := o3r.GetDiagnosticClient()
result, err := diag.GetFiltered(
*ovp8xx.NewConfig(
ovp8xx.WitJSONString(filter),
),
)
if err != nil {
return err
} else {
fmt.Printf("%s\n", result)
}
return nil
}

func diagnosticGetFilterSchemaCommand(cmd *cobra.Command, args []string) error {

host, err := rootCmd.PersistentFlags().GetString("ip")
if err != nil {
return err
}

o3r := ovp8xx.NewClient(
ovp8xx.WithHost(host),
)

diag := o3r.GetDiagnosticClient()
result, err := diag.GetFilterSchema()
if err != nil {
return err
} else {
fmt.Printf("%s\n", result)
}
return nil
}

var diagnosticCmd = &cobra.Command{
Use: "diagnostic",
Short: "Interact with diagnostic subsystem of the OVP8xx",
}

var diagnosticGetFilteredCmd = &cobra.Command{
Use: "getFiltered",
Short: "Retrieve the filtered diagnostic JSON from the device",
RunE: diagnosticGetFilteredCommand,
}

var diagnosticGetFilterSchemaCmd = &cobra.Command{
Use: "getFilterSchema",
Short: "Retrieve the diagnostic filter schema JSON from the device",
RunE: diagnosticGetFilterSchemaCommand,
}

func init() {
rootCmd.AddCommand(diagnosticCmd)
diagnosticCmd.AddCommand(diagnosticGetFilteredCmd)
diagnosticCmd.AddCommand(diagnosticGetFilterSchemaCmd)
diagnosticGetFilteredCmd.Flags().String("filter", "{\"state\": \"active\"}", "A JSON filter representation")
}
11 changes: 11 additions & 0 deletions pkg/ovp8xx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ type (
host string
url string
}
DiagnosisClient struct {
host string
url string
}
)

func NewClient(opts ...ClientOption) *Client {
Expand All @@ -31,3 +35,10 @@ func WithHost(host string) ClientOption {
c.host = host
}
}

func (device *Client) GetDiagnosticClient() *DiagnosisClient {
client := &DiagnosisClient{}
client.host = device.host
client.url = fmt.Sprintf("http://%s/api/rpc/v1/com.ifm.diagnostic/", client.host)
return client
}
40 changes: 40 additions & 0 deletions pkg/ovp8xx/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,43 @@ func (device *Client) Reboot() error {

return client.Call("reboot", nil, nil)
}

func (device *DiagnosisClient) GetFiltered(conf Config) (Config, error) {
client, err := xmlrpc.NewClient(device.url)
if err != nil {
return *NewConfig(), err
}
defer client.Close()

arg := &struct {
Data string
}{Data: conf.String()}

result := &struct {
JSON string
}{}

if err = client.Call("getFiltered", arg, result); err != nil {
return *NewConfig(), err
}

return *NewConfig(WitJSONString(result.JSON)), nil
}

func (device *DiagnosisClient) GetFilterSchema() (Config, error) {
client, err := xmlrpc.NewClient(device.url)
if err != nil {
return *NewConfig(), err
}
defer client.Close()

result := &struct {
JSON string
}{}

if err = client.Call("getFilterSchema", nil, result); err != nil {
return *NewConfig(), err
}

return *NewConfig(WitJSONString(result.JSON)), nil
}

0 comments on commit 71817e1

Please sign in to comment.