Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
apps
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
apps
Commits
8515e1e8
Commit
8515e1e8
authored
3 weeks ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Added Send is_alive, added attributes extract ...
parent
6fc06de5
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
misc/devices_counter.go
+69
-13
69 additions, 13 deletions
misc/devices_counter.go
with
69 additions
and
13 deletions
misc/devices_counter.go
+
69
−
13
View file @
8515e1e8
...
@@ -5,7 +5,7 @@ import (
...
@@ -5,7 +5,7 @@ import (
"strings"
"strings"
"time"
"time"
"git
hub.com/davecgh/go-spew/spew
"
"git
lab.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/uuid"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
)
)
...
@@ -13,10 +13,11 @@ import (
...
@@ -13,10 +13,11 @@ import (
type
Counter
struct
{
type
Counter
struct
{
Type
string
Type
string
Until
time
.
Time
Until
time
.
Time
State
bool
}
}
var
(
var
(
devices
map
[
uuid
.
UUID
]
Counter
devices
map
[
uuid
.
UUID
]
*
Counter
deviceIcons
=
map
[
string
]
string
{
deviceIcons
=
map
[
string
]
string
{
"lamp"
:
"💡"
,
"lamp"
:
"💡"
,
"contact"
:
"🚪"
,
"contact"
:
"🚪"
,
...
@@ -27,17 +28,18 @@ var (
...
@@ -27,17 +28,18 @@ var (
func
getCounter
(
msg
*
xaal
.
Message
)
*
Counter
{
func
getCounter
(
msg
*
xaal
.
Message
)
*
Counter
{
cnt
,
found
:=
devices
[
msg
.
Source
]
cnt
,
found
:=
devices
[
msg
.
Source
]
if
found
{
if
found
{
return
&
cnt
return
cnt
}
}
return
nil
return
nil
}
}
func
addCounter
(
msg
*
xaal
.
Message
,
prefix
string
)
*
Counter
{
func
addCounter
(
msg
*
xaal
.
Message
,
prefix
string
)
*
Counter
{
cnt
:=
Counter
{
cnt
:=
&
Counter
{
Type
:
prefix
,
Type
:
prefix
,
State
:
false
,
}
}
devices
[
msg
.
Source
]
=
cnt
devices
[
msg
.
Source
]
=
cnt
return
&
cnt
return
cnt
}
}
func
filterCounter
(
msg
*
xaal
.
Message
)
string
{
func
filterCounter
(
msg
*
xaal
.
Message
)
string
{
...
@@ -51,6 +53,33 @@ func filterCounter(msg *xaal.Message) string {
...
@@ -51,6 +53,33 @@ func filterCounter(msg *xaal.Message) string {
return
prefix
return
prefix
}
}
func
getAttribute
(
msg
*
xaal
.
Message
,
attr
string
)
bool
{
if
value
,
ok
:=
msg
.
Body
[
attr
]
.
(
bool
);
ok
{
return
value
}
return
false
}
func
updateCounter
(
msg
*
xaal
.
Message
,
prefix
string
)
{
// we can extract from msg, but already done so use it
msg
.
Dump
()
cnt
:=
getCounter
(
msg
)
// Lamps
if
prefix
==
"lamp"
{
cnt
.
State
=
getAttribute
(
msg
,
"light"
)
}
// Contacts
if
prefix
==
"contact"
{
cnt
.
State
=
getAttribute
(
msg
,
"detected"
)
}
// motion sensors
if
prefix
==
"motion"
{
cnt
.
State
=
getAttribute
(
msg
,
"presence"
)
}
slog
.
Debug
(
"Update counter"
,
"cnt"
,
cnt
)
}
func
handleMessage
(
msg
*
xaal
.
Message
)
{
func
handleMessage
(
msg
*
xaal
.
Message
)
{
// t's filter the messages we are interested in
// t's filter the messages we are interested in
prefix
:=
filterCounter
(
msg
)
prefix
:=
filterCounter
(
msg
)
...
@@ -58,16 +87,38 @@ func handleMessage(msg *xaal.Message) {
...
@@ -58,16 +87,38 @@ func handleMessage(msg *xaal.Message) {
return
return
}
}
msg
.
Dump
()
cnt
:=
getCounter
(
msg
)
if
cnt
==
nil
{
cnt
=
addCounter
(
msg
,
prefix
)
}
if
msg
.
IsAlive
()
{
if
msg
.
IsAlive
()
{
if
timeout
,
ok
:=
msg
.
Body
[
"timeout"
]
.
(
uint64
);
ok
{
if
timeout
,
ok
:=
msg
.
Body
[
"timeout"
]
.
(
uint64
);
ok
{
cnt
:=
getCounter
(
msg
)
if
cnt
==
nil
{
cnt
=
addCounter
(
msg
,
prefix
)
}
cnt
.
Until
=
time
.
Now
()
.
Add
(
time
.
Duration
(
timeout
)
*
time
.
Second
)
cnt
.
Until
=
time
.
Now
()
.
Add
(
time
.
Duration
(
timeout
)
*
time
.
Second
)
}
}
}
}
if
msg
.
IsAttributesChange
()
||
msg
.
IsGetAttributes
()
{
updateCounter
(
msg
,
prefix
)
}
}
func
countPrefix
(
prefix
string
)
int
{
total
:=
0
for
_
,
cnt
:=
range
devices
{
if
cnt
.
Type
==
prefix
&&
cnt
.
State
{
total
++
}
}
return
total
}
func
boot
(
eng
*
xaal
.
Engine
)
{
addr
:=
uuid
.
Random
()
dev
:=
schemas
.
NewHmi
(
addr
)
body
:=
make
(
xaal
.
MessageBody
)
body
[
"dev_types"
]
=
[]
string
{
"any.any"
}
msg
:=
eng
.
MessagesFactory
.
BuildMessage
(
dev
,
[]
uuid
.
UUID
{
uuid
.
Nil
},
xaal
.
MSGTypeRequest
,
string
(
xaal
.
MSGActionIsAlive
),
body
)
eng
.
Send
(
msg
)
}
}
func
init
()
{
func
init
()
{
...
@@ -78,10 +129,15 @@ func main() {
...
@@ -78,10 +129,15 @@ func main() {
eng
:=
xaal
.
NewEngine
()
eng
:=
xaal
.
NewEngine
()
eng
.
DisableMessageFilter
()
eng
.
DisableMessageFilter
()
devices
=
make
(
map
[
uuid
.
UUID
]
Counter
)
devices
=
make
(
map
[
uuid
.
UUID
]
*
Counter
)
eng
.
Subscribe
(
handleMessage
)
eng
.
Subscribe
(
handleMessage
)
boot
(
eng
)
eng
.
Run
()
eng
.
Run
()
// Let's count the devices
// Let's count the devices
slog
.
Info
(
"Devices"
,
"count"
,
len
(
devices
))
slog
.
Info
(
"Devices"
,
"count"
,
len
(
devices
))
spew
.
Dump
(
devices
)
// spew.Dump(devices)
slog
.
Info
(
"Lamps"
,
"cnt"
,
countPrefix
(
"lamp"
))
slog
.
Info
(
"Motion"
,
"cnt"
,
countPrefix
(
"motion"
))
slog
.
Info
(
"Contact"
,
"cnt"
,
countPrefix
(
"contact"
))
}
}
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