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
45cb7234
Commit
45cb7234
authored
5 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Added more test for refresh
parent
fd2edcb2
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
devices.go
+2
-38
2 additions, 38 deletions
devices.go
z2m.go
+28
-3
28 additions, 3 deletions
z2m.go
with
30 additions
and
41 deletions
devices.go
+
2
−
38
View file @
45cb7234
...
...
@@ -7,8 +7,8 @@ import (
"gitlab.imt-atlantique.fr/xaal/code/go/core/uuid"
)
// newDevices creates new xAAL devices from a bridge device
func
newDevices
(
gw
*
Gateway
,
zDev
*
Z2MDevice
)
{
// new
XAAL
Devices creates new xAAL devices from a bridge device
func
new
XAAL
Devices
(
gw
*
Gateway
,
zDev
*
Z2MDevice
)
{
// TODO: Handle errors
baseAddr
:=
gw
.
baseAddr
ieeeAddr
,
_
:=
hexStringToInteger
(
zDev
.
IeeeAddress
)
...
...
@@ -25,9 +25,6 @@ func newDevices(gw *Gateway, zDev *Z2MDevice) {
"linkquality"
:
NewLinkQuality
,
"battery"
:
NewBattery
,
}
// "switch": schemas.NewPowerrelay,
// "power": schemas.NewPowermeter,
// }
if
createFunc
,
ok
:=
deviceMap
[
expose
.
Name
];
ok
{
dev
=
createFunc
(
addr
,
zDev
)
...
...
@@ -53,40 +50,7 @@ func newDevices(gw *Gateway, zDev *Z2MDevice) {
func
updateXAALDevice
(
zDev
*
Z2MDevice
,
payload
map
[
string
]
interface
{})
{
slog
.
Info
(
"Updating device:"
,
"name"
,
zDev
.
FriendlyName
)
for
_
,
dev
:=
range
zDev
.
XAALDevices
{
dev
.
update
(
payload
)
}
// for _, dev := range zDev.XAALDevice {
// for key, value := range payload {
// if key == "contact" && dev.DevType == "contact.basic" {
// dev.GetAttribute("detected").SetValue(!value.(bool))
// }
// 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 == "humidity" && dev.DevType == "hygrometer.basic" {
// dev.GetAttribute("humidity").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
+
28
−
3
View file @
45cb7234
...
...
@@ -79,10 +79,34 @@ func (zDev *Z2MDevice) Send(payload interface{}) {
slog
.
Debug
(
"Sending"
,
"topic"
,
topic
,
"payload"
,
payload
)
client
:=
GetGW
()
.
client
if
token
:=
client
.
Publish
(
topic
,
0
,
false
,
payload
);
token
.
Wait
()
&&
token
.
Error
()
!=
nil
{
slog
.
Error
(
"Error
sending
"
,
":"
,
token
.
Error
())
slog
.
Error
(
"
SEND
Error"
,
":"
,
token
.
Error
())
}
}
func
(
zDev
*
Z2MDevice
)
Available
()
{
client
:=
GetGW
()
.
client
if
token
:=
client
.
Publish
(
zDev
.
GetTopic
()
+
"/availability"
,
0
,
false
,
`{"state": "online"}`
);
token
.
Wait
()
&&
token
.
Error
()
!=
nil
{
slog
.
Error
(
"REFRESH Error"
,
":"
,
token
.
Error
())
}
}
func
(
zDev
*
Z2MDevice
)
Sync
()
{
client
:=
GetGW
()
.
client
buf
:=
``
for
_
,
expose
:=
range
zDev
.
Definition
.
Exposes
{
if
expose
.
Access
==
1
||
expose
.
Access
==
3
||
expose
.
Access
==
5
||
expose
.
Access
==
7
{
buf
+=
fmt
.
Sprintf
(
`"%s": "",`
,
expose
.
Name
)
}
}
buf
=
strings
.
TrimSuffix
(
buf
,
","
)
buf
=
`{`
+
buf
+
`}`
fmt
.
Println
(
buf
)
if
token
:=
client
.
Publish
(
zDev
.
GetTopic
()
+
"/get"
,
0
,
false
,
buf
);
token
.
Wait
()
&&
token
.
Error
()
!=
nil
{
slog
.
Error
(
"REFRESH Error"
,
":"
,
token
.
Error
())
}
}
func
(
l
AccessLevel
)
String
()
string
{
return
[
...
]
string
{
"--"
,
"R"
,
"W"
,
"RW"
,
"Err"
,
"RN"
,
"Err"
,
"RWN"
}[
l
]
// return fmt.Sprintf("%d", l)
...
...
@@ -141,9 +165,10 @@ func jsonParseDevices(jsonData []byte) {
continue
}
zDev
.
dump
()
newDevices
(
gw
,
&
zDev
)
new
XAAL
Devices
(
gw
,
&
zDev
)
// TODO: filter if device already exists
gw
.
AddZDevice
(
&
zDev
)
// zDev.Sync()
}
}
...
...
@@ -192,8 +217,8 @@ func mqttPublishHander(client MQTT.Client, msg MQTT.Message) {
if
slices
.
Contains
(
ignoredTopics
,
msg
.
Topic
())
{
return
}
slog
.
Debug
(
"Received message on"
,
"topic"
,
msg
.
Topic
())
// Is it devices definitions ?
if
msg
.
Topic
()
==
mqttTopic
+
"/bridge/devices"
{
jsonParseDevices
(
msg
.
Payload
())
...
...
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