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"
"log"
"math"
"strconv"
"time"
)
@ -49,6 +48,14 @@ type Action struct {
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
func ConnectAMQP(uri string) error {
@ -114,40 +121,37 @@ func ConnectAMQP(uri string) error {
// 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 {
panic(err)
}
//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
/*if strings.Contains(content, "action") {
continue
}*/
var sToBeUpdated database.InfrastructureComponent
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()
ICUUID := payload.Properties.UUID
if ICUUID == "" {
log.Println("AMQP: Could not extract UUID of IC from content of received message, COMPONENT NOT UPDATED")
} else {
var sToBeUpdated database.InfrastructureComponent
db := database.GetDB()
err = db.Where("UUID = ?", ICUUID).Find(sToBeUpdated).Error
if err != nil {
log.Println("AMQP: Unable to find IC with UUID: ", ICUUID, " DB error message: ", err)
continue
}
var stateUpdateAt = message.Timestamp.UTC()
err = db.Model(&sToBeUpdated).Updates(map[string]interface{}{
//"Host": gjson.Get(content, "host"),
//"Type": gjson.Get(content, "model"),
"Uptime": uptime,
"State": state,
"Uptime": math.Round(payload.Uptime),
"State": payload.State,
"StateUpdateAt": stateUpdateAt.Format(time.RFC1123),
//"RawProperties": gjson.Get(content, "properties"),
}).Error