2018-06-30 16:20:54 +02:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
2018-08-10 17:17:00 +02:00
|
|
|
"time"
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2018-07-26 14:14:23 +02:00
|
|
|
wsLib "dev.sum7.eu/genofire/golang-lib/websocket"
|
2018-07-15 21:04:17 +02:00
|
|
|
|
|
|
|
"github.com/FreifunkBremen/freifunkmanager/runtime"
|
2018-06-30 16:20:54 +02:00
|
|
|
)
|
|
|
|
|
2018-07-15 21:04:17 +02:00
|
|
|
var wifi24Channels []uint32
|
|
|
|
var wifi5Channels []uint32
|
|
|
|
|
2018-06-30 16:20:54 +02:00
|
|
|
func (ws *WebsocketServer) connectHandler(logger *log.Entry, msg *wsLib.Message) error {
|
2018-07-26 14:14:23 +02:00
|
|
|
var nodes []*runtime.Node
|
|
|
|
var count int
|
|
|
|
|
2018-08-10 17:17:00 +02:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
ws.db.Find(&nodes).Count(&count)
|
2018-07-26 14:14:23 +02:00
|
|
|
|
|
|
|
ws.nodes.Lock()
|
|
|
|
i := 0
|
2018-08-10 13:46:18 +02:00
|
|
|
for _, node := range nodes {
|
2018-08-10 17:17:00 +02:00
|
|
|
if node.Blacklist != nil && node.Blacklist.After(now.Add(-ws.blacklistFor)) {
|
|
|
|
continue
|
|
|
|
}
|
2018-08-10 13:46:18 +02:00
|
|
|
node.Update(ws.nodes.List[node.NodeID], ws.ipPrefix)
|
|
|
|
msg.From.Write(&wsLib.Message{Subject: MessageTypeNode, Body: node})
|
2018-07-26 14:14:23 +02:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
ws.nodes.Unlock()
|
2018-07-15 21:04:17 +02:00
|
|
|
msg.From.Write(&wsLib.Message{Subject: MessageTypeChannelsWifi24, Body: wifi24Channels})
|
|
|
|
msg.From.Write(&wsLib.Message{Subject: MessageTypeChannelsWifi5, Body: wifi5Channels})
|
2018-07-26 14:14:23 +02:00
|
|
|
logger.Debugf("done - fetch %d nodes and send %d", count, i)
|
2018-06-30 16:20:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-15 21:04:17 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
for ch, channel := range runtime.ChannelList {
|
|
|
|
if runtime.ChannelEU && !channel.AllowedInEU {
|
|
|
|
continue
|
|
|
|
}
|
2018-08-11 12:34:19 +02:00
|
|
|
if channel.Frequency > runtime.FREQ_THREASHOLD {
|
2018-07-15 21:04:17 +02:00
|
|
|
wifi5Channels = append(wifi5Channels, ch)
|
|
|
|
} else {
|
|
|
|
wifi24Channels = append(wifi24Channels, ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|