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
7bf106af
Commit
7bf106af
authored
8 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Slash the whole old API. This should do the trick w/ light/ switches..
parent
cb04378c
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
devices.go
+54
-62
54 additions, 62 deletions
devices.go
xaal.go
+117
-0
117 additions, 0 deletions
xaal.go
z2m.go
+1
-2
1 addition, 2 deletions
z2m.go
with
172 additions
and
64 deletions
devices.go
+
54
−
62
View file @
7bf106af
...
...
@@ -2,22 +2,10 @@ 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"
)
// setupDevice sets up the xAAL device with the bridge device information
func
setupDevice
(
dev
*
xaal
.
Device
,
zDev
*
Z2MDevice
)
{
dev
.
VendorID
=
zDev
.
Definition
.
Vendor
dev
.
ProductID
=
zDev
.
Definition
.
Model
dev
.
HWID
=
zDev
.
IeeeAddress
dev
.
Version
=
zDev
.
SwBuildID
dev
.
Info
=
"z2m:"
+
zDev
.
Type
+
":"
+
zDev
.
FriendlyName
}
// newDevices creates new xAAL devices from a bridge device
func
newDevices
(
gw
*
Gateway
,
zDev
*
Z2MDevice
)
{
// TODO: Handle errors
...
...
@@ -28,65 +16,69 @@ func newDevices(gw *Gateway, zDev *Z2MDevice) {
for
i
,
expose
:=
range
zDev
.
Definition
.
Exposes
{
addr
,
_
:=
baseAddr
.
Add
(
int64
(
i
))
var
dev
*
xaal
.
Device
deviceMap
:=
map
[
string
]
func
(
uuid
.
UUID
)
*
xaal
.
Device
{
"contact"
:
schemas
.
NewContact
,
"battery"
:
schemas
.
NewBattery
,
"switch"
:
schemas
.
NewPowerrelay
,
"temperature"
:
schemas
.
NewThermometer
,
"humidity"
:
schemas
.
NewHygrometer
,
"power"
:
schemas
.
NewPowermeter
,
"linkquality"
:
schemas
.
NewLinkquality
,
deviceMap
:=
map
[
string
]
func
(
uuid
.
UUID
,
*
Z2MDevice
){
"contact"
:
NewContact
,
"temperature"
:
NewThermometer
,
"humidity"
:
NewHygrometer
,
"linkquality"
:
NewLinkQuality
,
"battery"
:
NewBattery
,
}
// "switch": schemas.NewPowerrelay,
// "power": schemas.NewPowermeter,
// }
if
createFunc
,
ok
:=
deviceMap
[
expose
.
Name
];
ok
{
dev
=
createFunc
(
addr
)
}
else
if
expose
.
Type
==
"switch"
{
// I don't know why but some devices have a switch type
// but name
dev
=
schemas
.
NewPowerrelay
(
addr
)
createFunc
(
addr
,
zDev
)
}
// else if expose.Type == "switch" {
// // I don't know why but some devices have a switch type
// // but name
// dev = schemas.NewPowerrelay(addr)
// }
if
dev
!=
nil
{
setupDevice
(
dev
,
zDev
)
// dev.Dump()
zDev
.
XAALDevice
=
append
(
zDev
.
XAALDevice
,
dev
)
gw
.
engine
.
AddDevice
(
dev
)
}
// if dev != nil {
// zDev.XAALDevice = append(zDev.XAALDevice, dev)
// // gw.engine.AddDevice(dev)
// }
}
}
func
updateXAALDevice
(
zDev
*
Z2MDevice
,
payload
map
[
string
]
interface
{})
{
slog
.
Info
(
"Updating device:"
,
"name"
,
zDev
.
FriendlyName
)
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
)
}
}
}
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.
xaal.go
0 → 100644
+
117
−
0
View file @
7bf106af
package
main
import
(
"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"
)
type
XAALDevice
struct
{
*
xaal
.
Device
}
type
Contact
struct
{
XAALDevice
}
type
Thermometer
struct
{
XAALDevice
}
type
Hygrometer
struct
{
XAALDevice
}
type
LinkQuality
struct
{
XAALDevice
}
type
Battery
struct
{
XAALDevice
}
type
XAALDeviceInterface
interface
{
update
(
map
[
string
]
interface
{})
}
func
(
dev
*
XAALDevice
)
setup
(
zDev
*
Z2MDevice
)
{
dev
.
VendorID
=
zDev
.
Definition
.
Vendor
dev
.
ProductID
=
zDev
.
Definition
.
Model
dev
.
HWID
=
zDev
.
IeeeAddress
dev
.
Version
=
zDev
.
SwBuildID
dev
.
Info
=
"z2m:"
+
zDev
.
Type
+
":"
+
zDev
.
FriendlyName
GetGW
()
.
engine
.
AddDevice
(
dev
.
Device
)
}
// ----------------------------------------------------------------------------
// Contact
// ----------------------------------------------------------------------------
func
NewContact
(
addr
uuid
.
UUID
,
zDev
*
Z2MDevice
)
{
dev
:=
&
Contact
{
XAALDevice
{
schemas
.
NewContact
(
addr
)}}
dev
.
setup
(
zDev
)
zDev
.
XAALDevices
=
append
(
zDev
.
XAALDevices
,
dev
)
}
func
(
dev
*
Contact
)
update
(
payload
map
[
string
]
interface
{})
{
value
:=
payload
[
"contact"
]
.
(
bool
)
dev
.
GetAttribute
(
"detected"
)
.
SetValue
(
!
value
)
}
// ----------------------------------------------------------------------------
// Thermometer
// ----------------------------------------------------------------------------
func
NewThermometer
(
addr
uuid
.
UUID
,
zDev
*
Z2MDevice
)
{
dev
:=
&
Thermometer
{
XAALDevice
{
schemas
.
NewThermometer
(
addr
)}}
dev
.
setup
(
zDev
)
zDev
.
XAALDevices
=
append
(
zDev
.
XAALDevices
,
dev
)
}
func
(
dev
*
Thermometer
)
update
(
payload
map
[
string
]
interface
{})
{
value
:=
payload
[
"temperature"
]
.
(
float64
)
dev
.
GetAttribute
(
"temperature"
)
.
SetValue
(
value
)
}
// ----------------------------------------------------------------------------
// Hygrometer
// ----------------------------------------------------------------------------
func
NewHygrometer
(
addr
uuid
.
UUID
,
zDev
*
Z2MDevice
)
{
dev
:=
&
Hygrometer
{
XAALDevice
{
schemas
.
NewHygrometer
(
addr
)}}
dev
.
setup
(
zDev
)
zDev
.
XAALDevices
=
append
(
zDev
.
XAALDevices
,
dev
)
}
func
(
dev
*
Hygrometer
)
update
(
payload
map
[
string
]
interface
{})
{
value
:=
payload
[
"humidity"
]
.
(
float64
)
dev
.
GetAttribute
(
"humidity"
)
.
SetValue
(
value
)
}
// ----------------------------------------------------------------------------
// LinkQuality
// ----------------------------------------------------------------------------
func
NewLinkQuality
(
addr
uuid
.
UUID
,
zDev
*
Z2MDevice
)
{
dev
:=
&
LinkQuality
{
XAALDevice
{
schemas
.
NewLinkquality
(
addr
)}}
dev
.
setup
(
zDev
)
zDev
.
XAALDevices
=
append
(
zDev
.
XAALDevices
,
dev
)
}
func
(
dev
*
LinkQuality
)
update
(
payload
map
[
string
]
interface
{})
{
value
:=
payload
[
"linkquality"
]
.
(
float64
)
value
=
math
.
Round
(
value
/
255
*
100
)
dev
.
GetAttribute
(
"level"
)
.
SetValue
(
value
)
}
// ----------------------------------------------------------------------------
// Battery
// ----------------------------------------------------------------------------
func
NewBattery
(
addr
uuid
.
UUID
,
zDev
*
Z2MDevice
)
{
dev
:=
&
Battery
{
XAALDevice
{
schemas
.
NewBattery
(
addr
)}}
dev
.
setup
(
zDev
)
zDev
.
XAALDevices
=
append
(
zDev
.
XAALDevices
,
dev
)
}
func
(
dev
*
Battery
)
update
(
payload
map
[
string
]
interface
{})
{
value
:=
payload
[
"battery"
]
.
(
float64
)
dev
.
GetAttribute
(
"level"
)
.
SetValue
(
value
)
}
This diff is collapsed.
Click to expand it.
z2m.go
+
1
−
2
View file @
7bf106af
...
...
@@ -10,7 +10,6 @@ import (
MQTT
"github.com/eclipse/paho.mqtt.golang"
"github.com/jedib0t/go-pretty/v6/table"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
)
const
(
...
...
@@ -47,7 +46,7 @@ type Z2MDevice struct {
SwBuildID
string
`json:"software_build_id"`
FriendlyName
string
`json:"friendly_name"`
// Manufacturer string `json:"manufacturer"` // TODO: This is quite the same as Vendor / Model
XAALDevice
[]
*
xaal
.
Devi
ce
XAALDevice
s
[]
XAALDeviceInterfa
ce
}
type
Expose
struct
{
...
...
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