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
8d6f9383
Commit
8d6f9383
authored
5 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Added config file parsing
Still a work in progress, but config parser is Ok.
parent
c1e81132
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
config.go
+61
-1
61 additions, 1 deletion
config.go
main.go
+5
-9
5 additions, 9 deletions
main.go
z2m.go
+3
-5
3 additions, 5 deletions
z2m.go
with
69 additions
and
15 deletions
config.go
+
61
−
1
View file @
8d6f9383
package
main
package
main
import
"github.com/google/uuid"
import
(
"gitlab.imt-atlantique.fr/xaal/code/go/core/uuid"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
"gopkg.in/ini.v1"
)
var
(
var
(
mqttTopic
=
"zigbee2mqtt"
mqttTopic
=
"zigbee2mqtt"
mqttClientID
=
"z2m-"
+
uuid
.
New
()
.
String
()
mqttClientID
=
"z2m-"
+
uuid
.
New
()
.
String
()
)
)
type
Config
struct
{
brokerHost
string
topic
string
logLevel
string
brokerPort
int
baseAddr
uuid
.
UUID
}
func
parseConfig
()
(
*
Config
,
error
)
{
// Default configuration
config
:=
Config
{
brokerHost
:
"mqtt"
,
topic
:
"zigbee2mqtt"
,
logLevel
:
"INFO"
,
brokerPort
:
1883
}
// Parse the configuration file
cfg
,
err
:=
ini
.
Load
(
xaal
.
GetConfigDir
()
+
"/z2m.ini"
)
if
err
!=
nil
{
return
nil
,
err
}
sec
:=
cfg
.
Section
(
ini
.
DefaultSection
)
// NOTE: This parsing is painful to write, I should look for a better way to do it
// broker
broker
,
err
:=
sec
.
GetKey
(
"broker"
)
if
err
==
nil
{
config
.
brokerHost
=
broker
.
String
()
}
// topic
topic
,
err
:=
sec
.
GetKey
(
"topic"
)
if
err
==
nil
{
config
.
topic
=
topic
.
String
()
}
// log level
level
,
err
:=
sec
.
GetKey
(
"log_level"
)
if
err
==
nil
{
config
.
logLevel
=
level
.
String
()
}
// port
port
,
err
:=
sec
.
GetKey
(
"port"
)
if
err
==
nil
{
value
,
err
:=
port
.
Int
()
if
err
!=
nil
{
return
nil
,
err
}
config
.
brokerPort
=
value
}
// base address
addr
,
err
:=
sec
.
GetKey
(
"base_addr"
)
if
err
==
nil
{
baseAddr
,
err
:=
uuid
.
FromString
(
addr
.
String
())
if
err
!=
nil
{
return
nil
,
err
}
config
.
baseAddr
=
baseAddr
}
return
&
config
,
nil
}
This diff is collapsed.
Click to expand it.
main.go
+
5
−
9
View file @
8d6f9383
package
main
package
main
import
(
import
(
"flag"
"fmt"
"log/slog"
"log/slog"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
)
)
func
main
()
{
func
main
()
{
mqttBroker
:=
flag
.
String
(
"broker"
,
""
,
"The MQTT broker URL"
)
cfg
,
err
:=
parseConfig
()
flag
.
Parse
()
if
err
!=
nil
{
slog
.
Error
(
"Error parsing configuration file: "
,
"err"
,
err
)
if
*
mqttBroker
==
""
{
fmt
.
Println
(
"Error: MQTT broker URL is required"
)
return
return
}
}
// spew.Dump(cfg)
xaal
.
SetupLogger
()
xaal
.
SetupLogger
()
client
:=
mqttSetup
(
*
mqttBroker
,
1883
)
client
:=
mqttSetup
(
cfg
.
brokerHost
,
cfg
.
brokerPort
)
// client.Publish("zigbee2mqtt/0x70b3d52b600f89d3/set", 0, false, `{"state": "toggle"}`)
eng
:=
xaal
.
NewEngine
()
eng
:=
xaal
.
NewEngine
()
gw
:=
NewGW
(
client
)
gw
:=
NewGW
(
client
)
...
...
This diff is collapsed.
Click to expand it.
z2m.go
+
3
−
5
View file @
8d6f9383
...
@@ -91,11 +91,10 @@ func (zDev *Z2MDevice) HandleMessage(msg MQTT.Message) {
...
@@ -91,11 +91,10 @@ func (zDev *Z2MDevice) HandleMessage(msg MQTT.Message) {
if
err
!=
nil
{
if
err
!=
nil
{
slog
.
Error
(
"Error decoding JSON"
,
"err"
,
err
)
slog
.
Error
(
"Error decoding JSON"
,
"err"
,
err
)
}
else
{
}
else
{
slog
.
Info
(
"Updating device:"
,
"name"
,
zDev
.
FriendlyName
)
slog
.
Debug
(
"Updating device:"
,
"name"
,
zDev
.
FriendlyName
)
for
_
,
dev
:=
range
zDev
.
XAALDevices
{
for
_
,
dev
:=
range
zDev
.
XAALDevices
{
dev
.
update
(
data
)
dev
.
update
(
data
)
}
}
}
}
}
}
...
@@ -259,14 +258,13 @@ func mqttPublishHander(client MQTT.Client, msg MQTT.Message) {
...
@@ -259,14 +258,13 @@ func mqttPublishHander(client MQTT.Client, msg MQTT.Message) {
if
slices
.
Contains
(
ignoredTopics
,
msg
.
Topic
())
{
if
slices
.
Contains
(
ignoredTopics
,
msg
.
Topic
())
{
return
return
}
}
slog
.
Debug
(
"Received message on"
,
"topic"
,
msg
.
Topic
())
// slog.Debug("Received message on", "topic", msg.Topic())
// Is it devices definitions ?
// Is it devices definitions ?
if
msg
.
Topic
()
==
mqttTopic
+
"/bridge/devices"
{
if
msg
.
Topic
()
==
mqttTopic
+
"/bridge/devices"
{
jsonParseDevices
(
msg
.
Payload
())
jsonParseDevices
(
msg
.
Payload
())
}
else
{
}
else
{
dev
:=
GetGW
()
.
GetZDeviceByTopic
(
msg
.
Topic
())
dev
:=
GetGW
()
.
GetZDeviceByTopic
(
msg
.
Topic
())
mqttDumpMsg
(
msg
)
//
mqttDumpMsg(msg)
if
dev
!=
nil
{
if
dev
!=
nil
{
dev
.
HandleMessage
(
msg
)
dev
.
HandleMessage
(
msg
)
}
}
...
...
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