2017-05-20 14:46:29 +02:00
|
|
|
package meshviewerFFRGB
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-01-03 15:30:28 +01:00
|
|
|
"log"
|
2017-05-20 14:46:29 +02:00
|
|
|
"strings"
|
|
|
|
|
2018-01-07 21:00:56 +01:00
|
|
|
"github.com/FreifunkBremen/yanic/lib/jsontime"
|
2017-05-20 14:46:29 +02:00
|
|
|
"github.com/FreifunkBremen/yanic/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
func transform(nodes *runtime.Nodes) *Meshviewer {
|
|
|
|
|
|
|
|
meshviewer := &Meshviewer{
|
|
|
|
Timestamp: jsontime.Now(),
|
|
|
|
Nodes: make([]*Node, 0),
|
|
|
|
Links: make([]*Link, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
links := make(map[string]*Link)
|
2018-01-03 15:30:28 +01:00
|
|
|
typeList := make(map[string]string)
|
2017-05-20 14:46:29 +02:00
|
|
|
|
|
|
|
nodes.RLock()
|
|
|
|
defer nodes.RUnlock()
|
|
|
|
|
|
|
|
for _, nodeOrigin := range nodes.List {
|
|
|
|
node := NewNode(nodes, nodeOrigin)
|
|
|
|
meshviewer.Nodes = append(meshviewer.Nodes, node)
|
|
|
|
|
2017-11-01 20:29:00 +01:00
|
|
|
if !nodeOrigin.Online {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-05-20 14:46:29 +02:00
|
|
|
if nodeinfo := nodeOrigin.Nodeinfo; nodeinfo != nil {
|
|
|
|
if meshes := nodeinfo.Network.Mesh; meshes != nil {
|
|
|
|
for _, mesh := range meshes {
|
|
|
|
for _, mac := range mesh.Interfaces.Wireless {
|
|
|
|
typeList[mac] = "wifi"
|
|
|
|
}
|
|
|
|
for _, mac := range mesh.Interfaces.Tunnel {
|
|
|
|
typeList[mac] = "vpn"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, linkOrigin := range nodes.NodeLinks(nodeOrigin) {
|
|
|
|
var key string
|
|
|
|
// keep source and target in the same order
|
|
|
|
switchSourceTarget := strings.Compare(linkOrigin.SourceMAC, linkOrigin.TargetMAC) > 0
|
|
|
|
if switchSourceTarget {
|
|
|
|
key = fmt.Sprintf("%s-%s", linkOrigin.SourceMAC, linkOrigin.TargetMAC)
|
|
|
|
} else {
|
|
|
|
key = fmt.Sprintf("%s-%s", linkOrigin.TargetMAC, linkOrigin.SourceMAC)
|
|
|
|
}
|
|
|
|
if link := links[key]; link != nil {
|
|
|
|
if switchSourceTarget {
|
|
|
|
link.TargetTQ = float32(linkOrigin.TQ) / 255.0
|
2018-01-03 15:30:28 +01:00
|
|
|
if link.Type == "other" {
|
|
|
|
link.Type = typeList[linkOrigin.TargetMAC]
|
|
|
|
} else if link.Type != typeList[linkOrigin.TargetMAC] {
|
|
|
|
log.Printf("different linktypes %s:%s current: %s source: %s target: %s", linkOrigin.SourceMAC, linkOrigin.TargetMAC, link.Type, typeList[linkOrigin.SourceMAC], typeList[linkOrigin.TargetMAC])
|
|
|
|
}
|
2017-05-20 14:46:29 +02:00
|
|
|
} else {
|
|
|
|
link.SourceTQ = float32(linkOrigin.TQ) / 255.0
|
2018-01-03 15:30:28 +01:00
|
|
|
if link.Type == "other" {
|
|
|
|
link.Type = typeList[linkOrigin.SourceMAC]
|
|
|
|
} else if link.Type != typeList[linkOrigin.SourceMAC] {
|
|
|
|
log.Printf("different linktypes %s:%s current: %s source: %s target: %s", linkOrigin.SourceMAC, linkOrigin.TargetMAC, link.Type, typeList[linkOrigin.SourceMAC], typeList[linkOrigin.TargetMAC])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if link.Type == "" {
|
|
|
|
link.Type = "other"
|
2017-05-20 14:46:29 +02:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tq := float32(linkOrigin.TQ) / 255.0
|
|
|
|
link := &Link{
|
2018-01-03 15:30:28 +01:00
|
|
|
Type: typeList[linkOrigin.SourceMAC],
|
2017-05-20 14:46:29 +02:00
|
|
|
Source: linkOrigin.SourceID,
|
|
|
|
SourceMAC: linkOrigin.SourceMAC,
|
|
|
|
Target: linkOrigin.TargetID,
|
|
|
|
TargetMAC: linkOrigin.TargetMAC,
|
|
|
|
SourceTQ: tq,
|
|
|
|
TargetTQ: tq,
|
|
|
|
}
|
|
|
|
if switchSourceTarget {
|
2018-01-03 15:30:28 +01:00
|
|
|
link.Type = typeList[linkOrigin.TargetMAC]
|
2017-05-20 14:46:29 +02:00
|
|
|
link.Source = linkOrigin.TargetID
|
|
|
|
link.SourceMAC = linkOrigin.TargetMAC
|
|
|
|
link.Target = linkOrigin.SourceID
|
|
|
|
link.TargetMAC = linkOrigin.SourceMAC
|
|
|
|
}
|
2018-01-03 15:30:28 +01:00
|
|
|
if link.Type == "" {
|
|
|
|
link.Type = "other"
|
|
|
|
}
|
2017-05-20 14:46:29 +02:00
|
|
|
links[key] = link
|
|
|
|
meshviewer.Links = append(meshviewer.Links, link)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return meshviewer
|
|
|
|
}
|