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
e395ceb3
Commit
e395ceb3
authored
4 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Big refactoring
Code is quite production ready..
parent
ac745cb1
No related branches found
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
config.go
+2
-0
2 additions, 0 deletions
config.go
gateway.go
+15
-12
15 additions, 12 deletions
gateway.go
mqtt.go
+4
-4
4 additions, 4 deletions
mqtt.go
xaal.go
+2
-2
2 additions, 2 deletions
xaal.go
z2m.go
+3
-4
3 additions, 4 deletions
z2m.go
with
26 additions
and
22 deletions
config.go
+
2
−
0
View file @
e395ceb3
...
...
@@ -20,6 +20,8 @@ var (
"bridge/state"
,
"bridge/logging"
,
"bridge/config"
,
"bridge/event"
,
"bridge/log"
,
}
)
...
...
This diff is collapsed.
Click to expand it.
gateway.go
+
15
−
12
View file @
e395ceb3
...
...
@@ -19,9 +19,8 @@ type Gateway struct {
func
NewGW
(
cfg
*
Config
,
eng
*
xaal
.
Engine
)
*
Gateway
{
gw
:=
&
Gateway
{
Devices
:
make
(
map
[
string
]
*
Z2MDevice
)}
gw
.
Config
=
cfg
gw
.
Client
=
mqttSetup
(
cfg
,
gw
.
mqttPublishHander
)
// NOTE: Wondering if we can setup engine before
gw
.
Engine
=
eng
gw
.
Client
=
MQTTSetup
(
cfg
,
gw
.
MQTTHandler
)
return
gw
}
...
...
@@ -31,7 +30,7 @@ func (gw *Gateway) GetZDevice(name string) *Z2MDevice {
func
(
gw
*
Gateway
)
AddZDevice
(
zDev
*
Z2MDevice
)
{
for
_
,
xDev
:=
range
zDev
.
XAALDevices
{
dev
:=
xDev
.
g
etXAALDevice
()
dev
:=
xDev
.
G
etXAALDevice
()
slog
.
Debug
(
"Adding device"
,
"dev"
,
dev
.
String
())
gw
.
Engine
.
AddDevice
(
dev
)
}
...
...
@@ -41,7 +40,7 @@ func (gw *Gateway) AddZDevice(zDev *Z2MDevice) {
func
(
gw
*
Gateway
)
RemoveZDevice
(
zDev
*
Z2MDevice
)
{
for
_
,
xDev
:=
range
zDev
.
XAALDevices
{
dev
:=
xDev
.
g
etXAALDevice
()
dev
:=
xDev
.
G
etXAALDevice
()
slog
.
Debug
(
"Removing device"
,
"dev"
,
dev
.
String
())
gw
.
Engine
.
RemoveDevice
(
dev
)
}
...
...
@@ -58,17 +57,21 @@ func (gw *Gateway) GetZDeviceByTopic(topic string) *Z2MDevice {
return
gw
.
GetZDevice
(
name
)
}
//
mqttPublish
Hander handles all incoming MQTT messages
//
MQTT
Hand
l
er 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
(
gw
*
Gateway
)
mqttPublish
Hander
(
client
mqtt
.
Client
,
msg
mqtt
.
Message
)
{
// Else it will find the device with the topic and call the
HandleMessage
func
(
gw
*
Gateway
)
MQTT
Hand
l
er
(
client
mqtt
.
Client
,
msg
mqtt
.
Message
)
{
// we ignore some topics
if
slices
.
Contains
(
gw
.
Config
.
ignoredTopics
,
msg
.
Topic
())
{
return
}
// some messages are empty
if
len
(
msg
.
Payload
())
==
0
{
return
}
// Is it devices definitions ?
if
msg
.
Topic
()
==
gw
.
Config
.
topic
+
"/bridge/devices"
{
gw
.
json
ParseDevices
(
msg
)
gw
.
Z2M
ParseDevices
(
msg
)
}
else
{
zDev
:=
gw
.
GetZDeviceByTopic
(
msg
.
Topic
())
// mqttDumpMsg(msg)
...
...
@@ -78,14 +81,14 @@ func (gw *Gateway) mqttPublishHander(client mqtt.Client, msg mqtt.Message) {
}
}
// jsonParseDevices parses the bridge/devices json and creates new xAAL devices
// if they don't exist
func
(
gw
*
Gateway
)
jsonParseDevices
(
msg
mqtt
.
Message
)
{
// Z2MParseDevices parses the bridge/devices json and and sync xAAL with results
func
(
gw
*
Gateway
)
Z2MParseDevices
(
msg
mqtt
.
Message
)
{
var
devices
[]
Z2MDevice
slog
.
Debug
(
"Parsing devices"
)
err
:=
json
.
Unmarshal
(
msg
.
Payload
(),
&
devices
)
if
err
!=
nil
{
slog
.
Error
(
"Error decoding JSON"
,
"err"
,
err
)
return
}
// search for new devices
...
...
@@ -96,7 +99,7 @@ func (gw *Gateway) jsonParseDevices(msg mqtt.Message) {
}
slog
.
Debug
(
"Found new device"
,
"name"
,
zDev
.
FriendlyName
)
zDev
.
Dump
()
zDev
.
setup
XAALDevices
(
gw
)
zDev
.
Find
XAALDevices
(
gw
)
gw
.
AddZDevice
(
&
zDev
)
zDev
.
Sync
()
// zDev.Available()
...
...
This diff is collapsed.
Click to expand it.
mqtt.go
+
4
−
4
View file @
e395ceb3
...
...
@@ -10,8 +10,8 @@ import (
"github.com/jedib0t/go-pretty/v6/table"
)
//
mqtt
Setup creates a new MQTT client
func
mqtt
Setup
(
cfg
*
Config
,
publishHandler
mqtt
.
MessageHandler
)
mqtt
.
Client
{
//
MQTT
Setup creates a new MQTT client
func
MQTT
Setup
(
cfg
*
Config
,
publishHandler
mqtt
.
MessageHandler
)
mqtt
.
Client
{
// This JS style of creating a client is awfully verbose
opts
:=
mqtt
.
NewClientOptions
()
.
AddBroker
(
fmt
.
Sprintf
(
"tcp://%s:%d"
,
cfg
.
brokerHost
,
cfg
.
brokerPort
))
.
...
...
@@ -29,8 +29,8 @@ func mqttSetup(cfg *Config, publishHandler mqtt.MessageHandler) mqtt.Client {
return
client
}
//
mqtt
DumpMsg displays the MQTT message
func
mqtt
DumpMsg
(
msg
mqtt
.
Message
)
{
//
MQTT
DumpMsg displays the MQTT message
func
MQTT
DumpMsg
(
msg
mqtt
.
Message
)
{
var
data
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
(
msg
.
Payload
(),
&
data
)
if
err
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
xaal.go
+
2
−
2
View file @
e395ceb3
...
...
@@ -55,7 +55,7 @@ type ButtonRemote struct {
type
XAALDeviceInterface
interface
{
update
(
map
[
string
]
interface
{})
g
etXAALDevice
()
*
xaal
.
Device
G
etXAALDevice
()
*
xaal
.
Device
}
func
(
dev
*
XAALDevice
)
setup
()
{
...
...
@@ -67,7 +67,7 @@ func (dev *XAALDevice) setup() {
dev
.
Info
=
"z2m:"
+
zDev
.
Type
+
":"
+
zDev
.
FriendlyName
}
func
(
dev
*
XAALDevice
)
g
etXAALDevice
()
*
xaal
.
Device
{
func
(
dev
*
XAALDevice
)
G
etXAALDevice
()
*
xaal
.
Device
{
return
dev
.
Device
}
...
...
This diff is collapsed.
Click to expand it.
z2m.go
+
3
−
4
View file @
e395ceb3
...
...
@@ -79,7 +79,7 @@ func (zDev *Z2MDevice) HandleMessage(msg mqtt.Message) {
var
data
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
(
msg
.
Payload
(),
&
data
)
if
err
!=
nil
{
slog
.
Error
(
"Error decoding JSON"
,
"err"
,
err
)
slog
.
Error
(
"Error decoding JSON"
,
"err"
,
err
,
"topic"
,
msg
.
Topic
(),
"data"
,
msg
.
Payload
()
)
}
else
{
slog
.
Debug
(
"Updating device:"
,
"name"
,
zDev
.
FriendlyName
)
for
_
,
dev
:=
range
zDev
.
XAALDevices
{
...
...
@@ -89,8 +89,7 @@ func (zDev *Z2MDevice) HandleMessage(msg mqtt.Message) {
}
// creates new xAAL devices from a bridge device
func
(
zDev
*
Z2MDevice
)
setupXAALDevices
(
gw
*
Gateway
)
{
// TODO: Handle errors
func
(
zDev
*
Z2MDevice
)
FindXAALDevices
(
gw
*
Gateway
)
{
baseAddr
:=
gw
.
Config
.
baseAddr
ieeeAddr
,
_
:=
hexStringToInteger
(
zDev
.
IeeeAddress
)
baseAddr
,
_
=
baseAddr
.
Add
(
int64
(
ieeeAddr
))
...
...
@@ -123,7 +122,7 @@ func (zDev *Z2MDevice) setupXAALDevices(gw *Gateway) {
if
dev
!=
nil
{
zDev
.
XAALDevices
=
append
(
zDev
.
XAALDevices
,
dev
)
xaalDev
:=
dev
.
g
etXAALDevice
()
xaalDev
:=
dev
.
G
etXAALDevice
()
xaalDev
.
GroupID
=
grpAdd
}
}
...
...
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