freifunkmanager/websocket/hd_node.go

61 lines
1.5 KiB
Go
Raw Normal View History

package websocket
import (
"github.com/mitchellh/mapstructure"
log "github.com/sirupsen/logrus"
2018-07-26 14:14:23 +02:00
wsLib "dev.sum7.eu/genofire/golang-lib/websocket"
"github.com/FreifunkBremen/freifunkmanager/runtime"
)
func (ws *WebsocketServer) nodeHandler(logger *log.Entry, msg *wsLib.Message) error {
2018-08-10 17:33:39 +02:00
if _, ok := ws.IsLoggedIn(msg); !ok {
msg.Answer(msg.Subject, false)
logger.Warn("not logged in")
return nil
}
2018-07-26 14:14:23 +02:00
var node runtime.Node
if err := mapstructure.Decode(msg.Body, &node); err != nil {
msg.Answer(msg.Subject, false)
logger.Warnf("not able to decode data: %s", err)
return nil
}
if node.NodeID == "" {
msg.Answer(msg.Subject, false)
logger.Warnf("not able to find nodeid")
logger.Debugf("%v", node)
return nil
}
2018-07-26 14:14:23 +02:00
originNode := runtime.Node{
NodeID: node.NodeID,
}
err := ws.db.First(&originNode)
originNode.Hostname = node.Hostname
originNode.Owner = node.Owner
originNode.Wireless = node.Wireless
originNode.Location = node.Location
if err.Error == nil {
err = ws.db.Save(&originNode)
} else {
logger.Debug(err.Error.Error(), err)
err = ws.db.Create(&originNode)
}
hasError := (err.Error != nil)
msg.Answer(msg.Subject, !hasError)
if hasError {
logger.Warnf("could not change %s: %s", node.NodeID, err.Error.Error())
return err.Error
}
2018-08-10 13:46:18 +02:00
ws.nodes.Lock()
nodeRespondd := ws.nodes.List[node.NodeID]
ws.nodes.Unlock()
node.Update(nodeRespondd, ws.ipPrefix)
ws.SendNode(&node)
logger.Infof("change %s", node.NodeID)
return nil
}