From e292e304c382fddd4162d2bd0ebdd8f0f5c0c1bd Mon Sep 17 00:00:00 2001 From: Nils Jakobi Date: Sun, 7 Feb 2021 21:29:30 +0100 Subject: [PATCH] made callUnifiAPI, processAPIs, processUNMSAPI functions for config type --- go.mod | 2 -- go.sum | 16 ---------------- main.go | 26 +++++++++++--------------- 3 files changed, 11 insertions(+), 33 deletions(-) diff --git a/go.mod b/go.mod index 185622d..13f5908 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api go 1.16 - -require git.nils.zone/nils/prettify v0.0.4 diff --git a/go.sum b/go.sum index 8ec57a6..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,16 +0,0 @@ -git.nils.zone/nils/prettify v0.0.4 h1:y014Ejp0yhFAc9vu5U0nJRFxtXN6k6eWW6LiUkwn4NQ= -git.nils.zone/nils/prettify v0.0.4/go.mod h1:q4ydEhSvXuywHe5U6uSEoEeS9f2Upz/n8z0MJylLz5c= -github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4= -github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w= -github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= -github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= -github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e h1:0aewS5NTyxftZHSnFaJmWE5oCCrj4DyEXkAiMa1iZJM= -github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI= -github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= -github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= -github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go index dc69628..0bd87bb 100644 --- a/main.go +++ b/main.go @@ -14,13 +14,9 @@ import ( "strconv" "strings" "time" - - _ "git.nils.zone/nils/prettify" ) -const ( - iso8601 = "2006-01-02T15:04:05-0700" -) +const iso8601 = "2006-01-02T15:04:05-0700" // flags var configPath = flag.String("configPath", "config.json", "Path to config.json") @@ -38,7 +34,7 @@ func main() { log.Fatalln("Please specify path to config.json flag '-configPath'") } // start API processing (runs in a loop) - go processAPIs(c) + go c.processAPIs() // start webserver on Port 3000 serveJSON() @@ -64,7 +60,7 @@ func itob(i int) bool { } //Unifi Controller API processing -func processUcAPIs(c config) ([]node, []link) { +func (c config) processUcAPIs() ([]node, []link) { //get list of Unifi devices to display var nodes []node var links []link @@ -148,7 +144,7 @@ func processUcAPIs(c config) ([]node, []link) { } //UNMS API processing (Richtfunk) -func processUNMSAPI(c config) ([]node, []link) { +func (c config) processUNMSAPI() ([]node, []link) { // Variables for runtime var links []link var nodes []node @@ -161,7 +157,7 @@ func processUNMSAPI(c config) ([]node, []link) { // API CALL 1 log.Println("calling API 1") var u []unifiAPIResponse - err = callUnifiAPI(c, "/devices", &u) + err = c.callUnifiAPI("/devices", &u) if err != nil { log.Fatalln(err) } @@ -185,13 +181,13 @@ func processUNMSAPI(c config) ([]node, []link) { // API CALL 2 log.Println("calling API 2 for device", d.Devices[i].Name) var details unifiAPIDetails - callUnifiAPI(c, "/devices/erouters/"+dev.Identification.ID, &details) + c.callUnifiAPI("/devices/erouters/"+dev.Identification.ID, &details) // END OF API CALL 2 // API CALL 3 log.Println("calling API 3 for device", d.Devices[i].Name) var airmaxes []unifiAPIAirmax - callUnifiAPI(c, "/devices/airmaxes/"+dev.Identification.ID+"/stations", &airmaxes) + c.callUnifiAPI("/devices/airmaxes/"+dev.Identification.ID+"/stations", &airmaxes) // check if remote mac address is part of our published network for i := range airmaxes { if isRemoteMACpublished(airmaxes[i].DeviceIdentification.MAC, d.Devices) == true { @@ -238,19 +234,19 @@ func processUNMSAPI(c config) ([]node, []link) { return nodes, links } -func processAPIs(c config) { +func (c config) processAPIs() { tick := time.Tick(delay) for range tick { var nodes []node var links []link if c.Unms.Enabled == true { - unmsNodes, unmsLinks := processUNMSAPI(c) + unmsNodes, unmsLinks := c.processUNMSAPI() nodes = append(nodes, unmsNodes...) links = append(links, unmsLinks...) } if c.Unifi.Enabled == true { - ucNodes, ucLinks := processUcAPIs(c) + ucNodes, ucLinks := c.processUcAPIs() nodes = append(nodes, ucNodes...) links = append(links, ucLinks...) } @@ -299,7 +295,7 @@ func getDevices(url string) (devices, error) { return d, nil } -func callUnifiAPI(c config, url string, i interface{}) error { +func (c config) callUnifiAPI(url string, i interface{}) error { request, err := http.NewRequest(http.MethodGet, c.Unms.UnmsAPIURL+url, nil) if err != nil { return errors.New(fmt.Sprint("can't set request", c.Unms.UnmsAPIURL+url))