AMQP: create update struct to unmarshal incoming message

This commit is contained in:
Sonja Happ 2020-09-30 17:11:20 +02:00
parent 3f654e60f1
commit dc0e0599c6

View file

@ -29,7 +29,6 @@ import (
"github.com/streadway/amqp" "github.com/streadway/amqp"
"log" "log"
"math" "math"
"strconv"
"time" "time"
) )
@ -49,6 +48,14 @@ type Action struct {
Results struct{} `json:"results"` Results struct{} `json:"results"`
} }
type Update struct {
State string `json:"state"`
Uptime float64 `json:"uptime"`
Properties struct {
UUID string `json:"uuid"`
} `json:"properties"`
}
var client AMQPclient var client AMQPclient
func ConnectAMQP(uri string) error { func ConnectAMQP(uri string) error {
@ -114,40 +121,37 @@ func ConnectAMQP(uri string) error {
// fmt.Println("AMQP: Unable to ack message:", err) // fmt.Println("AMQP: Unable to ack message:", err)
//} //}
var payload map[string]interface{} var payload Update
if err := json.Unmarshal(message.Body, &payload); err != nil { if err := json.Unmarshal(message.Body, &payload); err != nil {
panic(err) panic(err)
} }
//content := string(message.Body) //content := string(message.Body)
fmt.Println("APQM: message payload", message.Body) fmt.Println("APQM: message payload", string(message.Body))
// any action message sent by the VILLAScontroller should be ignored by the web backend // any action message sent by the VILLAScontroller should be ignored by the web backend
/*if strings.Contains(content, "action") { /*if strings.Contains(content, "action") {
continue continue
}*/ }*/
var sToBeUpdated database.InfrastructureComponent ICUUID := payload.Properties.UUID
db := database.GetDB()
ICUUID := fmt.Sprintf("%v", payload["properties.uuid"])
uptime_s := fmt.Sprintf("%v", payload["uptime"])
uptime, _ := strconv.ParseFloat(uptime_s, 64)
uptime = math.Round(uptime)
state := fmt.Sprintf("%v", payload["state"])
var stateUpdateAt = message.Timestamp.UTC()
if ICUUID == "" { if ICUUID == "" {
log.Println("AMQP: Could not extract UUID of IC from content of received message, COMPONENT NOT UPDATED") log.Println("AMQP: Could not extract UUID of IC from content of received message, COMPONENT NOT UPDATED")
} else { } else {
var sToBeUpdated database.InfrastructureComponent
db := database.GetDB()
err = db.Where("UUID = ?", ICUUID).Find(sToBeUpdated).Error err = db.Where("UUID = ?", ICUUID).Find(sToBeUpdated).Error
if err != nil { if err != nil {
log.Println("AMQP: Unable to find IC with UUID: ", ICUUID, " DB error message: ", err) log.Println("AMQP: Unable to find IC with UUID: ", ICUUID, " DB error message: ", err)
continue continue
} }
var stateUpdateAt = message.Timestamp.UTC()
err = db.Model(&sToBeUpdated).Updates(map[string]interface{}{ err = db.Model(&sToBeUpdated).Updates(map[string]interface{}{
//"Host": gjson.Get(content, "host"), //"Host": gjson.Get(content, "host"),
//"Type": gjson.Get(content, "model"), //"Type": gjson.Get(content, "model"),
"Uptime": uptime, "Uptime": math.Round(payload.Uptime),
"State": state, "State": payload.State,
"StateUpdateAt": stateUpdateAt.Format(time.RFC1123), "StateUpdateAt": stateUpdateAt.Format(time.RFC1123),
//"RawProperties": gjson.Get(content, "properties"), //"RawProperties": gjson.Get(content, "properties"),
}).Error }).Error