Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Z
z2m
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xAAL
Code
Go
z2m
Commits
a9391c02
Commit
a9391c02
authored
8 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Added cleaner MQTT display
parent
527b323e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
devices.go
+40
-0
40 additions, 0 deletions
devices.go
z2m.go
+7
-27
7 additions, 27 deletions
z2m.go
with
47 additions
and
27 deletions
devices.go
+
40
−
0
View file @
a9391c02
package
main
import
(
"log/slog"
"math"
"gitlab.imt-atlantique.fr/xaal/code/go/core/schemas"
"gitlab.imt-atlantique.fr/xaal/code/go/core/uuid"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
...
...
@@ -50,3 +53,40 @@ func newDevices(gw *gateway, bDevice *BridgeDevice) {
}
}
}
func
updateXAALDevice
(
bDevice
*
BridgeDevice
,
payload
map
[
string
]
interface
{})
{
slog
.
Info
(
"Updating device:"
,
"name"
,
bDevice
.
FriendlyName
)
for
_
,
dev
:=
range
bDevice
.
XAALDevice
{
for
key
,
value
:=
range
payload
{
if
key
==
"contact"
&&
dev
.
DevType
==
"contact.basic"
{
switch
value
{
case
true
:
dev
.
GetAttribute
(
"detected"
)
.
SetValue
(
false
)
case
false
:
dev
.
GetAttribute
(
"detected"
)
.
SetValue
(
true
)
}
}
if
key
==
"battery"
&&
dev
.
DevType
==
"battery.basic"
{
dev
.
GetAttribute
(
"level"
)
.
SetValue
(
value
)
}
if
key
==
"linkquality"
&&
dev
.
DevType
==
"linkquality.basic"
{
value
=
math
.
Round
(
value
.
(
float64
)
/
255
*
100
)
dev
.
GetAttribute
(
"level"
)
.
SetValue
(
value
)
}
if
key
==
"temperature"
&&
dev
.
DevType
==
"thermometer.basic"
{
dev
.
GetAttribute
(
"temperature"
)
.
SetValue
(
value
)
}
if
key
==
"power"
&&
dev
.
DevType
==
"powermeter.basic"
{
dev
.
GetAttribute
(
"power"
)
.
SetValue
(
value
)
}
if
key
==
"state"
&&
dev
.
DevType
==
"powerrelay.basic"
{
switch
value
{
case
"ON"
:
dev
.
GetAttribute
(
"power"
)
.
SetValue
(
true
)
case
"OFF"
:
dev
.
GetAttribute
(
"power"
)
.
SetValue
(
false
)
}
}
}
}
}
This diff is collapsed.
Click to expand it.
z2m.go
+
7
−
27
View file @
a9391c02
...
...
@@ -4,11 +4,11 @@ import (
"encoding/json"
"fmt"
"log/slog"
"math"
"slices"
"sort"
MQTT
"github.com/eclipse/paho.mqtt.golang"
"github.com/jedib0t/go-pretty/v6/table"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
)
...
...
@@ -107,28 +107,6 @@ func parseDeviceJSON(jsonData []byte) {
}
}
func
updateXAALDevice
(
bDevice
*
BridgeDevice
,
payload
map
[
string
]
interface
{})
{
slog
.
Info
(
"Updating device:"
,
"name"
,
bDevice
.
FriendlyName
)
for
_
,
dev
:=
range
bDevice
.
XAALDevice
{
for
key
,
value
:=
range
payload
{
if
key
==
"contact"
&&
dev
.
DevType
==
"contact.basic"
{
dev
.
GetAttribute
(
"detected"
)
.
SetValue
(
value
)
}
if
key
==
"battery"
&&
dev
.
DevType
==
"battery.basic"
{
dev
.
GetAttribute
(
"level"
)
.
SetValue
(
value
)
}
if
key
==
"linkquality"
&&
dev
.
DevType
==
"linkquality.basic"
{
value
=
math
.
Round
(
value
.
(
float64
)
/
255
*
100
)
dev
.
GetAttribute
(
"level"
)
.
SetValue
(
value
)
}
if
key
==
"temperature"
&&
dev
.
DevType
==
"thermometer.basic"
{
dev
.
GetAttribute
(
"temperature"
)
.
SetValue
(
value
)
}
}
}
}
func
deviceHandler
(
device
*
BridgeDevice
,
msg
MQTT
.
Message
)
{
var
data
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
(
msg
.
Payload
(),
&
data
)
...
...
@@ -151,11 +129,14 @@ func dumpMessage(msg MQTT.Message) {
keys
=
append
(
keys
,
key
)
}
sort
.
Strings
(
keys
)
tab
:=
table
.
NewWriter
()
tab
.
SetTitle
(
"MQTT update"
)
tab
.
SetStyle
(
table
.
StyleRounded
)
for
_
,
key
:=
range
keys
{
fmt
.
Printf
(
"%s =>
\t
%v
\n
"
,
key
,
data
[
key
])
tab
.
AppendRow
(
table
.
Row
{
key
,
data
[
key
]
}
)
}
fmt
.
Printf
(
"------------------
\n\n
"
)
fmt
.
Println
(
tab
.
Render
())
}
func
publishHander
(
client
MQTT
.
Client
,
msg
MQTT
.
Message
)
{
...
...
@@ -182,7 +163,6 @@ func publishHander(client MQTT.Client, msg MQTT.Message) {
deviceHandler
(
dev
,
msg
)
dumpMessage
(
msg
)
}
fmt
.
Printf
(
"------------------
\n\n
"
)
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment