Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ACS
Public
VILLASframework
VILLASweb-backend-go
Commits
dc0e0599
Commit
dc0e0599
authored
Sep 30, 2020
by
Sonja Happ
Browse files
AMQP: create update struct to unmarshal incoming message
parent
3f654e60
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
amqp/amqpclient.go
View file @
dc0e0599
...
...
@@ -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"
:
u
ptime
,
"State"
:
s
tate
,
"Uptime"
:
math
.
Round
(
payload
.
U
ptime
)
,
"State"
:
payload
.
S
tate
,
"StateUpdateAt"
:
stateUpdateAt
.
Format
(
time
.
RFC1123
),
//"RawProperties": gjson.Get(content, "properties"),
})
.
Error
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment