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
0e2ff75b
Commit
0e2ff75b
authored
8 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Cleanup / Better Access display
parent
f20cba1b
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
+14
-14
14 additions, 14 deletions
devices.go
z2m.go
+43
-19
43 additions, 19 deletions
z2m.go
with
57 additions
and
33 deletions
devices.go
+
14
−
14
View file @
0e2ff75b
...
...
@@ -10,22 +10,22 @@ import (
)
// setupDevice sets up the xAAL device with the bridge device information
func
setupDevice
(
dev
*
xaal
.
Device
,
bDev
ice
*
BridgeDevice
)
{
dev
.
VendorID
=
bDev
ice
.
Definition
.
Vendor
dev
.
ProductID
=
bDev
ice
.
Definition
.
Model
dev
.
HWID
=
bDev
ice
.
IeeeAddress
dev
.
Version
=
bDev
ice
.
SwBuildID
dev
.
Info
=
"z2m:"
+
bDev
ice
.
Type
+
":"
+
bDev
ice
.
FriendlyName
func
setupDevice
(
dev
*
xaal
.
Device
,
bDev
*
BridgeDevice
)
{
dev
.
VendorID
=
bDev
.
Definition
.
Vendor
dev
.
ProductID
=
bDev
.
Definition
.
Model
dev
.
HWID
=
bDev
.
IeeeAddress
dev
.
Version
=
bDev
.
SwBuildID
dev
.
Info
=
"z2m:"
+
bDev
.
Type
+
":"
+
bDev
.
FriendlyName
}
// newDevices creates new xAAL devices from a bridge device
func
newDevices
(
gw
*
gateway
,
bDev
ice
*
BridgeDevice
)
{
func
newDevices
(
gw
*
gateway
,
bDev
*
BridgeDevice
)
{
// TODO: Handle errors
baseAddr
:=
gw
.
baseAddr
ieeeAddr
,
_
:=
hexStringToInteger
(
bDev
ice
.
IeeeAddress
)
ieeeAddr
,
_
:=
hexStringToInteger
(
bDev
.
IeeeAddress
)
baseAddr
,
_
=
baseAddr
.
Add
(
int64
(
ieeeAddr
))
for
i
,
expose
:=
range
bDev
ice
.
Definition
.
Exposes
{
for
i
,
expose
:=
range
bDev
.
Definition
.
Exposes
{
addr
,
_
:=
baseAddr
.
Add
(
int64
(
i
))
var
dev
*
xaal
.
Device
...
...
@@ -48,17 +48,17 @@ func newDevices(gw *gateway, bDevice *BridgeDevice) {
}
if
dev
!=
nil
{
setupDevice
(
dev
,
bDev
ice
)
setupDevice
(
dev
,
bDev
)
dev
.
Dump
()
bDev
ice
.
XAALDevice
=
append
(
bDev
ice
.
XAALDevice
,
dev
)
bDev
.
XAALDevice
=
append
(
bDev
.
XAALDevice
,
dev
)
gw
.
engine
.
AddDevice
(
dev
)
}
}
}
func
updateXAALDevice
(
bDev
ice
*
BridgeDevice
,
payload
map
[
string
]
interface
{})
{
slog
.
Info
(
"Updating device:"
,
"name"
,
bDev
ice
.
FriendlyName
)
for
_
,
dev
:=
range
bDev
ice
.
XAALDevice
{
func
updateXAALDevice
(
bDev
*
BridgeDevice
,
payload
map
[
string
]
interface
{})
{
slog
.
Info
(
"Updating device:"
,
"name"
,
bDev
.
FriendlyName
)
for
_
,
dev
:=
range
bDev
.
XAALDevice
{
for
key
,
value
:=
range
payload
{
if
key
==
"contact"
&&
dev
.
DevType
==
"contact.basic"
{
dev
.
GetAttribute
(
"detected"
)
.
SetValue
(
!
value
.
(
bool
))
...
...
This diff is collapsed.
Click to expand it.
z2m.go
+
43
−
19
View file @
0e2ff75b
...
...
@@ -55,30 +55,46 @@ type Feature struct {
Access
int
`json:"access,omitempty"`
}
func
(
bDev
ice
*
BridgeDevice
)
GetTopic
()
string
{
return
mqttTopic
+
"/"
+
bDev
ice
.
FriendlyName
func
(
bDev
*
BridgeDevice
)
GetTopic
()
string
{
return
mqttTopic
+
"/"
+
bDev
.
FriendlyName
}
func
(
bDev
ice
*
BridgeDevice
)
SetTopic
()
string
{
return
bDev
ice
.
GetTopic
()
+
"/set"
func
(
bDev
*
BridgeDevice
)
SetTopic
()
string
{
return
bDev
.
GetTopic
()
+
"/set"
}
func
(
bDevices
*
BridgeDevice
)
dump
()
{
func
getAccessType
(
level
int
)
string
{
switch
level
{
case
1
:
return
"R"
case
2
:
return
"W"
case
3
:
return
"RW"
case
5
:
return
"RN"
case
7
:
return
"RWN"
}
return
""
}
func
(
bDev
*
BridgeDevice
)
dump
()
{
tab
:=
table
.
NewWriter
()
tab
.
SetTitle
(
"Def:"
+
bDev
ices
.
FriendlyName
)
tab
.
SetTitle
(
"Def:"
+
bDev
.
FriendlyName
)
tab
.
SetStyle
(
table
.
StyleRounded
)
tab
.
AppendRow
(
table
.
Row
{
"IeeeAddr"
,
bDev
ices
.
IeeeAddress
})
tab
.
AppendRow
(
table
.
Row
{
"Vendor"
,
bDev
ices
.
Definition
.
Vendor
})
tab
.
AppendRow
(
table
.
Row
{
"Model"
,
bDev
ices
.
Definition
.
Model
})
tab
.
AppendRow
(
table
.
Row
{
"Type"
,
bDev
ices
.
Type
})
tab
.
AppendRow
(
table
.
Row
{
"IeeeAddr"
,
bDev
.
IeeeAddress
})
tab
.
AppendRow
(
table
.
Row
{
"Vendor"
,
bDev
.
Definition
.
Vendor
})
tab
.
AppendRow
(
table
.
Row
{
"Model"
,
bDev
.
Definition
.
Model
})
tab
.
AppendRow
(
table
.
Row
{
"Type"
,
bDev
.
Type
})
fmt
.
Println
(
tab
.
Render
())
if
len
(
bDev
ices
.
Definition
.
Exposes
)
>
0
{
if
len
(
bDev
.
Definition
.
Exposes
)
>
0
{
expTab
:=
table
.
NewWriter
()
expTab
.
SetTitle
(
"Exp:"
+
bDev
ices
.
FriendlyName
)
expTab
.
SetTitle
(
"Exp:"
+
bDev
.
FriendlyName
)
expTab
.
SetStyle
(
table
.
StyleRounded
)
expTab
.
AppendHeader
(
table
.
Row
{
"Name"
,
"Type"
,
"Acc"
,
"Unit"
,
"Values"
,
"Features: Name[Type]-Acc-(Unit){Property}"
})
for
_
,
expose
:=
range
bDev
ices
.
Definition
.
Exposes
{
for
_
,
expose
:=
range
bDev
.
Definition
.
Exposes
{
values
:=
""
if
len
(
expose
.
Values
)
>
0
{
values
=
strings
.
Join
(
expose
.
Values
,
"
\n
"
)
...
...
@@ -86,16 +102,18 @@ func (bDevices *BridgeDevice) dump() {
features
:=
""
if
len
(
expose
.
Features
)
>
0
{
for
_
,
feature
:=
range
expose
.
Features
{
features
+=
fmt
.
Sprintf
(
"- %s[%s]-%
d
-(%s){%s}
\n
"
,
feature
.
Name
,
feature
.
Type
,
feature
.
Access
,
feature
.
Unit
,
feature
.
Property
)
features
+=
fmt
.
Sprintf
(
"- %s[%s]-%
s
-(%s){%s}
\n
"
,
feature
.
Name
,
feature
.
Type
,
getAccessType
(
feature
.
Access
)
,
feature
.
Unit
,
feature
.
Property
)
}
features
=
strings
.
TrimSuffix
(
features
,
"
\n
"
)
}
expTab
.
AppendRow
(
table
.
Row
{
expose
.
Name
,
expose
.
Type
,
expose
.
Access
,
expose
.
Unit
,
values
,
features
})
expTab
.
AppendRow
(
table
.
Row
{
expose
.
Name
,
expose
.
Type
,
getAccessType
(
expose
.
Access
)
,
expose
.
Unit
,
values
,
features
})
}
fmt
.
Println
(
expTab
.
Render
())
}
}
// jsonParseDevices parses the bridge/devices json and creates new xAAL devices
// if they don't exist
func
jsonParseDevices
(
jsonData
[]
byte
)
{
var
devices
[]
BridgeDevice
err
:=
json
.
Unmarshal
([]
byte
(
jsonData
),
&
devices
)
...
...
@@ -104,7 +122,6 @@ func jsonParseDevices(jsonData []byte) {
}
gw
:=
GetGW
()
for
_
,
device
:=
range
devices
{
newFlag
:=
true
for
_
,
known
:=
range
gw
.
devices
{
...
...
@@ -123,16 +140,18 @@ func jsonParseDevices(jsonData []byte) {
}
}
func
mqttDeviceHandler
(
device
*
BridgeDevice
,
msg
MQTT
.
Message
)
{
// mqttDeviceHandler updates the xAAL device with the MQTT message
func
mqttDeviceHandler
(
bDev
*
BridgeDevice
,
msg
MQTT
.
Message
)
{
var
data
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
(
msg
.
Payload
(),
&
data
)
if
err
!=
nil
{
slog
.
Error
(
"Error decoding JSON"
,
"err"
,
err
)
}
else
{
updateXAALDevice
(
device
,
data
)
updateXAALDevice
(
bDev
,
data
)
}
}
// mqttDumpMsg displays the MQTT message
func
mqttDumpMsg
(
msg
MQTT
.
Message
)
{
var
data
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
(
msg
.
Payload
(),
&
data
)
...
...
@@ -158,13 +177,17 @@ func mqttDumpMsg(msg MQTT.Message) {
fmt
.
Println
(
tab
.
Render
())
}
// mqttPublishHander handles all incoming MQTT messages
// If the topic is /bridge/devices it will parse the json and create new devices
// Else it will find the device with the topic and call the mqttDeviceHandler
func
mqttPublishHander
(
client
MQTT
.
Client
,
msg
MQTT
.
Message
)
{
// we ignore some topics
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
())
}
else
{
...
...
@@ -182,6 +205,7 @@ func mqttPublishHander(client MQTT.Client, msg MQTT.Message) {
}
}
// mqttSetup creates a new MQTT client
func
mqttSetup
(
mqttBroker
string
,
port
int
)
MQTT
.
Client
{
// This JS style of creating a client is awfully verbose
opts
:=
MQTT
.
NewClientOptions
()
.
...
...
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