2018-06-30 16:20:54 +02:00
|
|
|
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"
|
2018-06-30 16:20:54 +02:00
|
|
|
|
|
|
|
"github.com/FreifunkBremen/freifunkmanager/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ws *WebsocketServer) nodeHandler(logger *log.Entry, msg *wsLib.Message) error {
|
2018-07-26 14:14:23 +02:00
|
|
|
if !ws.IsLoggedIn(msg) {
|
2018-06-30 16:20:54 +02:00
|
|
|
msg.Answer(msg.Subject, false)
|
|
|
|
logger.Warn("not logged in")
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-26 14:14:23 +02:00
|
|
|
var node runtime.Node
|
2018-06-30 16:20:54 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
ws.SendNode(&node, true)
|
2018-06-30 16:20:54 +02:00
|
|
|
logger.Infof("change %s", node.NodeID)
|
|
|
|
return nil
|
|
|
|
}
|