From 70cdb53b49975025ae622383e7fb21fa1216c420 Mon Sep 17 00:00:00 2001 From: nrbffs <38812627+nrbffs@users.noreply.github.com> Date: Sun, 17 Nov 2019 10:48:42 +0100 Subject: [PATCH] meshviewer-ffrgb output: add custom fields This will include all configured custom fields in the meshviewer-ffrgb output under the "custom_fields" key. --- output/meshviewer-ffrgb/struct.go | 61 ++++++++++++++------------ output/meshviewer-ffrgb/struct_test.go | 6 +++ 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/output/meshviewer-ffrgb/struct.go b/output/meshviewer-ffrgb/struct.go index 5ca1946..6807bf1 100644 --- a/output/meshviewer-ffrgb/struct.go +++ b/output/meshviewer-ffrgb/struct.go @@ -14,33 +14,34 @@ type Meshviewer struct { } type Node struct { - Firstseen jsontime.Time `json:"firstseen"` - Lastseen jsontime.Time `json:"lastseen"` - IsOnline bool `json:"is_online"` - IsGateway bool `json:"is_gateway"` - Clients uint32 `json:"clients"` - ClientsWifi24 uint32 `json:"clients_wifi24"` - ClientsWifi5 uint32 `json:"clients_wifi5"` - ClientsOthers uint32 `json:"clients_other"` - RootFSUsage float64 `json:"rootfs_usage"` - LoadAverage float64 `json:"loadavg"` - MemoryUsage *float64 `json:"memory_usage,omitempty"` - Uptime jsontime.Time `json:"uptime,omitempty"` - GatewayNexthop string `json:"gateway_nexthop,omitempty"` - GatewayIPv4 string `json:"gateway,omitempty"` - GatewayIPv6 string `json:"gateway6,omitempty"` - NodeID string `json:"node_id"` - MAC string `json:"mac"` - Addresses []string `json:"addresses"` - SiteCode string `json:"-"` - DomainCode string `json:"domain"` - Hostname string `json:"hostname"` - Owner string `json:"owner,omitempty"` - Location *Location `json:"location,omitempty"` - Firmware Firmware `json:"firmware,omitempty"` - Autoupdater Autoupdater `json:"autoupdater"` - Nproc int `json:"nproc"` - Model string `json:"model,omitempty"` + Firstseen jsontime.Time `json:"firstseen"` + Lastseen jsontime.Time `json:"lastseen"` + IsOnline bool `json:"is_online"` + IsGateway bool `json:"is_gateway"` + Clients uint32 `json:"clients"` + ClientsWifi24 uint32 `json:"clients_wifi24"` + ClientsWifi5 uint32 `json:"clients_wifi5"` + ClientsOthers uint32 `json:"clients_other"` + RootFSUsage float64 `json:"rootfs_usage"` + LoadAverage float64 `json:"loadavg"` + MemoryUsage *float64 `json:"memory_usage,omitempty"` + Uptime jsontime.Time `json:"uptime,omitempty"` + GatewayNexthop string `json:"gateway_nexthop,omitempty"` + GatewayIPv4 string `json:"gateway,omitempty"` + GatewayIPv6 string `json:"gateway6,omitempty"` + NodeID string `json:"node_id"` + MAC string `json:"mac"` + Addresses []string `json:"addresses"` + SiteCode string `json:"-"` + DomainCode string `json:"domain"` + Hostname string `json:"hostname"` + Owner string `json:"owner,omitempty"` + Location *Location `json:"location,omitempty"` + Firmware Firmware `json:"firmware,omitempty"` + Autoupdater Autoupdater `json:"autoupdater"` + Nproc int `json:"nproc"` + Model string `json:"model,omitempty"` + CustomFields map[string]interface{} `json:"custom_fields,omitempty"` } // Firmware out of software @@ -154,6 +155,12 @@ func NewNode(nodes *runtime.Nodes, n *runtime.Node) *Node { node.GatewayIPv6 = statistic.GatewayIPv6 } } + if customFields := n.CustomFields; customFields != nil { + node.CustomFields = make(map[string]interface{}) + for fieldName, fieldValue := range customFields { + node.CustomFields[fieldName] = fieldValue + } + } // fix site to domain - if empty if node.DomainCode == "" { diff --git a/output/meshviewer-ffrgb/struct_test.go b/output/meshviewer-ffrgb/struct_test.go index 66f0b4f..a049d2a 100644 --- a/output/meshviewer-ffrgb/struct_test.go +++ b/output/meshviewer-ffrgb/struct_test.go @@ -58,6 +58,10 @@ func TestRegister(t *testing.T) { }, }, }, + CustomFields: map[string]interface{}{ + "custom_fields": "are_custom", + "custom_int": 3, + }, }) assert.NotNil(node) assert.NotNil(node.Addresses) @@ -66,4 +70,6 @@ func TestRegister(t *testing.T) { assert.Equal(13.3, node.Location.Longitude) assert.Equal(8.7, node.Location.Latitude) assert.Equal(0.74, *node.MemoryUsage) + assert.Equal("are_custom", node.CustomFields["custom_fields"]) + assert.Equal(3, node.CustomFields["custom_int"]) }