made callUnifiAPI, processAPIs, processUNMSAPI functions for config type
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing

This commit is contained in:
Nils Stinnesbeck 2021-02-07 21:29:30 +01:00
parent e98b5f1267
commit e292e304c3
Signed by: nils
GPG Key ID: 86D4882C6C6CA48B
3 changed files with 11 additions and 33 deletions

2
go.mod
View File

@ -1,5 +1,3 @@
module git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api module git.freifunk-rhein-sieg.net/Freifunk-Troisdorf/ubnt-freifunk-map-api
go 1.16 go 1.16
require git.nils.zone/nils/prettify v0.0.4

16
go.sum
View File

@ -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=

26
main.go
View File

@ -14,13 +14,9 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
_ "git.nils.zone/nils/prettify"
) )
const ( const iso8601 = "2006-01-02T15:04:05-0700"
iso8601 = "2006-01-02T15:04:05-0700"
)
// flags // flags
var configPath = flag.String("configPath", "config.json", "Path to config.json") 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'") log.Fatalln("Please specify path to config.json flag '-configPath'")
} }
// start API processing (runs in a loop) // start API processing (runs in a loop)
go processAPIs(c) go c.processAPIs()
// start webserver on Port 3000 // start webserver on Port 3000
serveJSON() serveJSON()
@ -64,7 +60,7 @@ func itob(i int) bool {
} }
//Unifi Controller API processing //Unifi Controller API processing
func processUcAPIs(c config) ([]node, []link) { func (c config) processUcAPIs() ([]node, []link) {
//get list of Unifi devices to display //get list of Unifi devices to display
var nodes []node var nodes []node
var links []link var links []link
@ -148,7 +144,7 @@ func processUcAPIs(c config) ([]node, []link) {
} }
//UNMS API processing (Richtfunk) //UNMS API processing (Richtfunk)
func processUNMSAPI(c config) ([]node, []link) { func (c config) processUNMSAPI() ([]node, []link) {
// Variables for runtime // Variables for runtime
var links []link var links []link
var nodes []node var nodes []node
@ -161,7 +157,7 @@ func processUNMSAPI(c config) ([]node, []link) {
// API CALL 1 // API CALL 1
log.Println("calling API 1") log.Println("calling API 1")
var u []unifiAPIResponse var u []unifiAPIResponse
err = callUnifiAPI(c, "/devices", &u) err = c.callUnifiAPI("/devices", &u)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
@ -185,13 +181,13 @@ func processUNMSAPI(c config) ([]node, []link) {
// API CALL 2 // API CALL 2
log.Println("calling API 2 for device", d.Devices[i].Name) log.Println("calling API 2 for device", d.Devices[i].Name)
var details unifiAPIDetails var details unifiAPIDetails
callUnifiAPI(c, "/devices/erouters/"+dev.Identification.ID, &details) c.callUnifiAPI("/devices/erouters/"+dev.Identification.ID, &details)
// END OF API CALL 2 // END OF API CALL 2
// API CALL 3 // API CALL 3
log.Println("calling API 3 for device", d.Devices[i].Name) log.Println("calling API 3 for device", d.Devices[i].Name)
var airmaxes []unifiAPIAirmax 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 // check if remote mac address is part of our published network
for i := range airmaxes { for i := range airmaxes {
if isRemoteMACpublished(airmaxes[i].DeviceIdentification.MAC, d.Devices) == true { if isRemoteMACpublished(airmaxes[i].DeviceIdentification.MAC, d.Devices) == true {
@ -238,19 +234,19 @@ func processUNMSAPI(c config) ([]node, []link) {
return nodes, links return nodes, links
} }
func processAPIs(c config) { func (c config) processAPIs() {
tick := time.Tick(delay) tick := time.Tick(delay)
for range tick { for range tick {
var nodes []node var nodes []node
var links []link var links []link
if c.Unms.Enabled == true { if c.Unms.Enabled == true {
unmsNodes, unmsLinks := processUNMSAPI(c) unmsNodes, unmsLinks := c.processUNMSAPI()
nodes = append(nodes, unmsNodes...) nodes = append(nodes, unmsNodes...)
links = append(links, unmsLinks...) links = append(links, unmsLinks...)
} }
if c.Unifi.Enabled == true { if c.Unifi.Enabled == true {
ucNodes, ucLinks := processUcAPIs(c) ucNodes, ucLinks := c.processUcAPIs()
nodes = append(nodes, ucNodes...) nodes = append(nodes, ucNodes...)
links = append(links, ucLinks...) links = append(links, ucLinks...)
} }
@ -299,7 +295,7 @@ func getDevices(url string) (devices, error) {
return d, nil 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) request, err := http.NewRequest(http.MethodGet, c.Unms.UnmsAPIURL+url, nil)
if err != nil { if err != nil {
return errors.New(fmt.Sprint("can't set request", c.Unms.UnmsAPIURL+url)) return errors.New(fmt.Sprint("can't set request", c.Unms.UnmsAPIURL+url))