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
8505f0af
Commit
8505f0af
authored
2 weeks ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Work in progress
parent
8515e1e8
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Makefile
+1
-1
1 addition, 1 deletion
Makefile
misc/devices_counter.go
+32
-19
32 additions, 19 deletions
misc/devices_counter.go
with
33 additions
and
20 deletions
Makefile
+
1
−
1
View file @
8505f0af
all
:
tidy
dumper chandelier lamp lamp_schem
all
:
dumper chandelier lamp lamp_schem
tidy
:
go mod tidy
...
...
This diff is collapsed.
Click to expand it.
misc/devices_counter.go
+
32
−
19
View file @
8505f0af
...
...
@@ -11,9 +11,9 @@ import (
)
type
Counter
struct
{
Type
string
Until
time
.
Time
State
bool
Prefix
string
Until
time
.
Time
State
bool
}
var
(
...
...
@@ -35,8 +35,8 @@ func getCounter(msg *xaal.Message) *Counter {
func
addCounter
(
msg
*
xaal
.
Message
,
prefix
string
)
*
Counter
{
cnt
:=
&
Counter
{
Type
:
prefix
,
State
:
false
,
Prefix
:
prefix
,
State
:
false
,
}
devices
[
msg
.
Source
]
=
cnt
return
cnt
...
...
@@ -62,9 +62,9 @@ func getAttribute(msg *xaal.Message, attr string) bool {
func
updateCounter
(
msg
*
xaal
.
Message
,
prefix
string
)
{
// we can extract from msg, but already done so use it
msg
.
Dump
()
cnt
:=
getCounter
(
msg
)
old
:=
cnt
.
State
// Lamps
if
prefix
==
"lamp"
{
cnt
.
State
=
getAttribute
(
msg
,
"light"
)
...
...
@@ -77,11 +77,29 @@ func updateCounter(msg *xaal.Message, prefix string) {
if
prefix
==
"motion"
{
cnt
.
State
=
getAttribute
(
msg
,
"presence"
)
}
slog
.
Debug
(
"Update counter"
,
"cnt"
,
cnt
)
if
cnt
.
State
!=
old
{
display
()
}
// slog.Debug("Update counter", "cnt", cnt)
}
func
updateAlive
(
msg
*
xaal
.
Message
)
{
cnt
:=
getCounter
(
msg
)
if
timeout
,
ok
:=
msg
.
Body
[
"timeout"
]
.
(
uint64
);
ok
{
cnt
.
Until
=
time
.
Now
()
.
Add
(
time
.
Duration
(
timeout
)
*
time
.
Second
)
}
}
func
display
()
{
slog
.
Info
(
"Devices"
,
"count"
,
len
(
devices
))
slog
.
Info
(
"Lamps"
,
"cnt"
,
countPrefix
(
"lamp"
))
slog
.
Info
(
"Motion"
,
"cnt"
,
countPrefix
(
"motion"
))
slog
.
Info
(
"Contact"
,
"cnt"
,
countPrefix
(
"contact"
))
}
func
handleMessage
(
msg
*
xaal
.
Message
)
{
// t's filter the messages we are interested in
//
le
t's filter the messages we are interested in
prefix
:=
filterCounter
(
msg
)
if
prefix
==
""
{
return
...
...
@@ -93,26 +111,26 @@ func handleMessage(msg *xaal.Message) {
}
if
msg
.
IsAlive
()
{
if
timeout
,
ok
:=
msg
.
Body
[
"timeout"
]
.
(
uint64
);
ok
{
cnt
.
Until
=
time
.
Now
()
.
Add
(
time
.
Duration
(
timeout
)
*
time
.
Second
)
}
updateAlive
(
msg
)
}
if
msg
.
IsAttributesChange
()
||
msg
.
IsGetAttributes
()
{
updateCounter
(
msg
,
prefix
)
}
}
// countPrefix counts the number of devices with a given prefix
func
countPrefix
(
prefix
string
)
int
{
total
:=
0
for
_
,
cnt
:=
range
devices
{
if
cnt
.
Type
==
prefix
&&
cnt
.
State
{
if
cnt
.
Prefix
==
prefix
&&
cnt
.
State
{
total
++
}
}
return
total
}
func
boot
(
eng
*
xaal
.
Engine
)
{
// isAlive sends a message to all devices asking if they are alive
func
isAlive
(
eng
*
xaal
.
Engine
)
{
addr
:=
uuid
.
Random
()
dev
:=
schemas
.
NewHmi
(
addr
)
body
:=
make
(
xaal
.
MessageBody
)
...
...
@@ -131,13 +149,8 @@ func main() {
devices
=
make
(
map
[
uuid
.
UUID
]
*
Counter
)
eng
.
Subscribe
(
handleMessage
)
boot
(
eng
)
isAlive
(
eng
)
eng
.
Run
()
// Let's count the devices
slog
.
Info
(
"Devices"
,
"count"
,
len
(
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