package main import ( "encoding/json" "log" "time" ) func getMeshviewerJSON(url string) (mvDevices, error) { // get devices from JSON file jsonFile := getFile(url) // read file to bytes // variable for d var n mvDevices //var l []link // unmarshal to struct err := json.Unmarshal(jsonFile, &n) if err != nil { log.Println("can´t get Meshviewer Json file from " + url) log.Println(err) } return n, nil } func checkMeshviewerLink(s string) bool { mvNodes, _ := getMeshviewer() for i := range mvNodes { if mvNodes[i].MAC == s { return true } } return false } func addmvDevices(d mvDevices) ([]node, []link) { var nodes []node var links []link for i := range d.Nodes { mvNode := d.Nodes[i] if findNodeID(mvNode.NodeID) { mvNode.Clients = 0 mvNode.ClientsWifi24 = 0 mvNode.ClientsWifi5 = 0 mvNode.ClientsOther = 0 } if mvNode.Location.Latitude == 0 { nodes = append(nodes, node{ Firstseen: mvNode.Firstseen, Lastseen: mvNode.Lastseen, IsOnline: mvNode.IsOnline, IsGateway: mvNode.IsGateway, Clients: mvNode.Clients, ClientsWifi24: mvNode.ClientsWifi24, ClientsWifi5: mvNode.ClientsWifi5, ClientsOther: mvNode.ClientsOther, RootFSUsage: int(mvNode.RootfsUsage), LoadAVG: mvNode.Loadavg, MemoryUsage: mvNode.MemoryUsage, Uptime: mvNode.Uptime, GatewayNexthop: mvNode.GatewayNexthop, Gateway: mvNode.Gateway, NodeID: mvNode.NodeID, MAC: mvNode.Mac, Adresses: mvNode.Addresses, Domain: mvNode.Domain, Hostname: mvNode.Hostname, Owner: mvNode.Owner, Firmware: firmware{ Base: mvNode.Firmware.Base, Release: mvNode.Firmware.Release, }, Autoupdater: autoupdater{ Enabled: mvNode.Autoupdater.Enabled, Branch: mvNode.Autoupdater.Branch, }, NProc: mvNode.Nproc, Model: mvNode.Model, }) } else { nodes = append(nodes, node{ Firstseen: mvNode.Firstseen, Lastseen: mvNode.Lastseen, IsOnline: mvNode.IsOnline, IsGateway: mvNode.IsGateway, Clients: mvNode.Clients, ClientsWifi24: mvNode.ClientsWifi24, ClientsWifi5: mvNode.ClientsWifi5, ClientsOther: mvNode.ClientsOther, RootFSUsage: int(mvNode.RootfsUsage), LoadAVG: mvNode.Loadavg, MemoryUsage: mvNode.MemoryUsage, Uptime: mvNode.Uptime, GatewayNexthop: mvNode.GatewayNexthop, Gateway: mvNode.Gateway, Location: &mvNode.Location, NodeID: mvNode.NodeID, MAC: mvNode.Mac, Adresses: mvNode.Addresses, Domain: mvNode.Domain, Hostname: mvNode.Hostname, Owner: mvNode.Owner, Firmware: firmware{ Base: mvNode.Firmware.Base, Release: mvNode.Firmware.Release, }, Autoupdater: autoupdater{ Enabled: mvNode.Autoupdater.Enabled, Branch: mvNode.Autoupdater.Branch, }, NProc: mvNode.Nproc, Model: mvNode.Model, }) } } for i := range d.Links { mvNode := d.Links[i] links = append(links, link{ Type: mvNode.Type, Source: mvNode.Source, Target: mvNode.Target, SourceTQ: mvNode.SourceTq, TargetTQ: mvNode.TargetTq, SourceAddr: mvNode.SourceAddr, TargetAddr: mvNode.TargetAddr, }) } return nodes, links } func getMeshviewer() ([]node, []link) { cacheMutex.Lock() defer cacheMutex.Unlock() // Überprüfen, ob die Daten kürzlich aktualisiert wurden if time.Since(lastFetchTime) < fetchInterval { return cacheNodes, cacheLinks } var nodes []node var links []link for i := range conf.Meshviewer.Files { m, err := getMeshviewerJSON(conf.Meshviewer.Files[i].URL) if err != nil { return cacheNodes, cacheLinks } mvNodes, mvLinks := addmvDevices(m) nodes = append(nodes, mvNodes...) links = append(links, mvLinks...) } // Cache aktualisieren cacheNodes = nodes cacheLinks = links lastFetchTime = time.Now() return cacheNodes, cacheLinks }