freifunkmanager/runtime/node.go

136 lines
3.4 KiB
Go
Raw Normal View History

2017-05-07 03:37:30 +02:00
package runtime
import (
2018-07-27 13:40:04 +02:00
"fmt"
2017-05-07 03:37:30 +02:00
"net"
2018-07-24 15:07:11 +02:00
"strings"
2017-05-07 03:37:30 +02:00
2018-06-30 01:45:51 +02:00
yanicData "github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/lib/jsontime"
2017-05-07 03:37:30 +02:00
yanicRuntime "github.com/FreifunkBremen/yanic/runtime"
)
type Node struct {
2018-07-26 14:14:23 +02:00
Lastseen jsontime.Time `json:"lastseen" mapstructure:"-"`
NodeID string `json:"node_id" gorm:"primary_key" mapstructure:"node_id"`
Hostname string `json:"hostname"`
Location yanicData.Location `json:"location" gorm:"embedded;embedded_prefix:location_"`
Wireless yanicData.Wireless `json:"wireless" gorm:"embedded;embedded_prefix:wireless_"`
Owner string `json:"owner"`
Blacklist bool `json:"blacklist" gorm:"-" mapstructure:"-"`
Address string `json:"ip" mapstructure:"-"`
Stats struct {
2018-06-30 01:45:51 +02:00
Wireless yanicData.WirelessStatistics `json:"wireless"`
Clients yanicData.Clients `json:"clients"`
} `json:"statistics" mapstructure:"-"`
2017-05-07 03:37:30 +02:00
}
2018-07-24 15:07:11 +02:00
func NewNode(nodeOrigin *yanicRuntime.Node, ipPrefix string) *Node {
if nodeinfo := nodeOrigin.Nodeinfo; nodeinfo != nil {
2017-05-07 03:37:30 +02:00
node := &Node{
Hostname: nodeinfo.Hostname,
NodeID: nodeinfo.NodeID,
2018-06-30 01:45:51 +02:00
}
for _, ip := range nodeinfo.Network.Addresses {
2018-07-24 15:07:11 +02:00
if !strings.HasPrefix(ip, ipPrefix) {
continue
}
2018-06-30 01:45:51 +02:00
ipAddr := net.ParseIP(ip)
2018-07-26 14:14:23 +02:00
if node.Address == "" || ipAddr.IsGlobalUnicast() {
node.Address = ipAddr.String()
2018-06-30 01:45:51 +02:00
}
2017-05-07 03:37:30 +02:00
}
if owner := nodeinfo.Owner; owner != nil {
node.Owner = owner.Contact
}
if location := nodeinfo.Location; location != nil {
node.Location = *location
}
if wireless := nodeinfo.Wireless; wireless != nil {
node.Wireless = *wireless
}
if stats := nodeOrigin.Statistics; stats != nil {
node.Stats.Clients = stats.Clients
node.Stats.Wireless = stats.Wireless
}
2017-05-07 03:37:30 +02:00
return node
}
return nil
}
2018-07-26 14:14:23 +02:00
func (n *Node) GetAddress() net.TCPAddr {
return net.TCPAddr{IP: net.ParseIP(n.Address), Port: 22}
2017-05-07 03:37:30 +02:00
}
func (n *Node) Update(node *yanicRuntime.Node) {
if nodeinfo := node.Nodeinfo; nodeinfo != nil {
n.Hostname = nodeinfo.Hostname
n.NodeID = nodeinfo.NodeID
2018-07-26 14:14:23 +02:00
n.Address = node.Address.IP.String()
2017-05-07 03:37:30 +02:00
if owner := nodeinfo.Owner; owner != nil {
n.Owner = owner.Contact
}
if location := nodeinfo.Location; location != nil {
n.Location = *location
}
if wireless := nodeinfo.Wireless; wireless != nil {
n.Wireless = *wireless
}
}
}
func (n *Node) IsEqual(node *Node) bool {
if n.NodeID != node.NodeID {
return false
}
2018-07-27 13:40:04 +02:00
/*
if n.Address != node.Address {
return false
}
*/
2017-05-07 03:37:30 +02:00
if n.Hostname != node.Hostname {
return false
}
if n.Owner != node.Owner {
return false
}
2017-07-07 08:12:25 +02:00
if !locationEqual(n.Location, node.Location) {
2017-05-07 03:37:30 +02:00
return false
}
2017-07-07 08:12:25 +02:00
if !wirelessEqual(n.Wireless, node.Wireless) {
2017-05-07 03:37:30 +02:00
return false
}
return true
}
2018-07-27 13:40:04 +02:00
const LOCATION_EQUAL_FORMAT_STRING = "%.6f"
2018-06-30 01:45:51 +02:00
func locationEqual(a, b yanicData.Location) bool {
2018-07-27 13:40:04 +02:00
if fmt.Sprintf(LOCATION_EQUAL_FORMAT_STRING, a.Latitude) != fmt.Sprintf(LOCATION_EQUAL_FORMAT_STRING, b.Latitude) {
2017-05-07 03:37:30 +02:00
return false
}
2018-07-27 13:40:04 +02:00
if fmt.Sprintf(LOCATION_EQUAL_FORMAT_STRING, a.Longitude) != fmt.Sprintf(LOCATION_EQUAL_FORMAT_STRING, b.Longitude) {
2017-05-07 03:37:30 +02:00
return false
}
2018-07-27 13:40:04 +02:00
2017-05-07 03:37:30 +02:00
if a.Altitude != b.Altitude {
return false
}
2018-07-27 13:40:04 +02:00
2017-05-07 03:37:30 +02:00
return true
}
2018-06-30 01:45:51 +02:00
func wirelessEqual(a, b yanicData.Wireless) bool {
2017-05-07 03:37:30 +02:00
if a.Channel24 != b.Channel24 {
return false
}
if a.Channel5 != b.Channel5 {
return false
}
if a.TxPower24 != b.TxPower24 {
return false
}
if a.TxPower5 != b.TxPower5 {
return false
}
return true
}