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
fc2b7668
Commit
fc2b7668
authored
2 weeks ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Rewrite the code to avoid wrapping and globals
parent
1c07b20e
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
+86
-72
86 additions, 72 deletions
misc/devices_counter.go
with
86 additions
and
72 deletions
misc/devices_counter.go
+
86
−
72
View file @
fc2b7668
...
@@ -17,8 +17,13 @@ type Counter struct {
...
@@ -17,8 +17,13 @@ type Counter struct {
State
bool
State
bool
}
}
type
CounterDevice
struct
{
devices
map
[
uuid
.
UUID
]
*
Counter
engine
*
xaal
.
Engine
device
*
xaal
.
Device
}
var
(
var
(
devices
map
[
uuid
.
UUID
]
*
Counter
deviceIcons
=
map
[
string
]
string
{
deviceIcons
=
map
[
string
]
string
{
"lamp"
:
" "
,
"lamp"
:
" "
,
"motion"
:
" "
,
"motion"
:
" "
,
...
@@ -26,44 +31,39 @@ var (
...
@@ -26,44 +31,39 @@ var (
}
}
)
)
func
getCounter
(
msg
*
xaal
.
Message
)
*
Counter
{
// ----------------------------------------------------------------------------
cnt
,
found
:=
devices
[
msg
.
Source
]
// CounterDevice
// -----------------------------------------------------------------------------
func
NewCounterDevice
(
eng
*
xaal
.
Engine
)
*
CounterDevice
{
dev
:=
schemas
.
NewHmi
(
uuid
.
Random
())
return
&
CounterDevice
{
devices
:
make
(
map
[
uuid
.
UUID
]
*
Counter
),
engine
:
eng
,
device
:
dev
,
}
}
func
(
d
*
CounterDevice
)
getCounter
(
msg
*
xaal
.
Message
)
*
Counter
{
cnt
,
found
:=
d
.
devices
[
msg
.
Source
]
if
found
{
if
found
{
return
cnt
return
cnt
}
}
return
nil
return
nil
}
}
func
addCounter
(
msg
*
xaal
.
Message
,
prefix
string
)
*
Counter
{
func
(
d
*
CounterDevice
)
addCounter
(
msg
*
xaal
.
Message
,
prefix
string
)
*
Counter
{
cnt
:=
&
Counter
{
cnt
:=
&
Counter
{
Prefix
:
prefix
,
Prefix
:
prefix
,
State
:
false
,
State
:
false
,
}
}
devices
[
msg
.
Source
]
=
cnt
d
.
devices
[
msg
.
Source
]
=
cnt
return
cnt
return
cnt
}
}
func
filterCounter
(
msg
*
xaal
.
Message
)
string
{
func
(
d
*
CounterDevice
)
updateCounter
(
msg
*
xaal
.
Message
,
prefix
string
)
{
// let's split the msg.DevType on points and keep the first part
tmp
:=
strings
.
Split
(
msg
.
DevType
,
"."
)
prefix
:=
tmp
[
0
]
_
,
found
:=
deviceIcons
[
prefix
]
if
!
found
{
return
""
}
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
// we can extract from msg, but already done so use it
cnt
:=
getCounter
(
msg
)
cnt
:=
d
.
getCounter
(
msg
)
old
:=
cnt
.
State
old
:=
cnt
.
State
// Lamps
// Lamps
...
@@ -80,58 +80,43 @@ func updateCounter(msg *xaal.Message, prefix string) {
...
@@ -80,58 +80,43 @@ func updateCounter(msg *xaal.Message, prefix string) {
}
}
if
cnt
.
State
!=
old
{
if
cnt
.
State
!=
old
{
display
()
d
.
display
()
}
}
}
}
func
updateAlive
(
msg
*
xaal
.
Message
)
{
func
(
d
*
CounterDevice
)
updateAlive
(
msg
*
xaal
.
Message
)
{
cnt
:=
getCounter
(
msg
)
cnt
:=
d
.
getCounter
(
msg
)
if
timeout
,
ok
:=
msg
.
Body
[
"timeout"
]
.
(
uint64
);
ok
{
if
timeout
,
ok
:=
msg
.
Body
[
"timeout"
]
.
(
uint64
);
ok
{
cnt
.
Until
=
time
.
Now
()
.
Add
(
time
.
Duration
(
timeout
)
*
time
.
Second
)
cnt
.
Until
=
time
.
Now
()
.
Add
(
time
.
Duration
(
timeout
)
*
time
.
Second
)
}
}
}
}
// display outputs the result on stdout and flush
func
(
d
*
CounterDevice
)
handleMessage
(
msg
*
xaal
.
Message
)
{
func
display
()
{
// slog.Info("Devices", "count", len(devices))
displayPrefix
(
"lamp"
)
fmt
.
Print
(
" "
)
displayPrefix
(
"motion"
)
fmt
.
Print
(
" "
)
displayPrefix
(
"contact"
)
fmt
.
Print
(
"
\n
"
)
}
func
displayPrefix
(
prefix
string
)
string
{
return
fmt
.
Sprintf
(
"%s%d"
,
deviceIcons
[
prefix
],
countPrefix
(
prefix
))
}
func
handleMessage
(
eng
*
xaal
.
Engine
,
msg
*
xaal
.
Message
)
{
// let's filter the messages we are interested in
// let's filter the messages we are interested in
prefix
:=
filterCounter
(
msg
)
prefix
:=
getPrefix
(
msg
)
if
prefix
==
""
{
if
prefix
==
""
{
return
return
}
}
cnt
:=
getCounter
(
msg
)
cnt
:=
d
.
getCounter
(
msg
)
if
cnt
==
nil
{
if
cnt
==
nil
{
cnt
=
addCounter
(
msg
,
prefix
)
cnt
=
d
.
addCounter
(
msg
,
prefix
)
sendGetAttritube
(
eng
,
msg
.
Source
)
d
.
sendGetAttritube
(
msg
.
Source
)
}
}
if
msg
.
IsAlive
()
{
if
msg
.
IsAlive
()
{
updateAlive
(
msg
)
d
.
updateAlive
(
msg
)
}
}
if
msg
.
IsAttributesChange
()
||
msg
.
IsGetAttributes
()
{
if
msg
.
IsAttributesChange
()
||
msg
.
IsGetAttributes
()
{
updateCounter
(
msg
,
prefix
)
d
.
updateCounter
(
msg
,
prefix
)
}
}
}
}
// countPrefix counts the number of devices with a given prefix
// countPrefix counts the number of devices with a given prefix
func
countPrefix
(
prefix
string
)
int
{
func
(
d
*
CounterDevice
)
countPrefix
(
prefix
string
)
int
{
total
:=
0
total
:=
0
for
_
,
cnt
:=
range
devices
{
for
_
,
cnt
:=
range
d
.
devices
{
if
cnt
.
Prefix
==
prefix
&&
cnt
.
State
{
if
cnt
.
Prefix
==
prefix
&&
cnt
.
State
{
total
++
total
++
}
}
...
@@ -139,21 +124,56 @@ func countPrefix(prefix string) int {
...
@@ -139,21 +124,56 @@ func countPrefix(prefix string) int {
return
total
return
total
}
}
func
sendGetAttritube
(
eng
*
xaal
.
Engine
,
addr
uuid
.
UUID
)
{
// isAlive sends a message to all devices asking if they are alive
dev
:=
schemas
.
NewHmi
(
uuid
.
Random
())
func
(
d
*
CounterDevice
)
sendIsAlive
()
{
body
:=
make
(
xaal
.
MessageBody
)
body
:=
make
(
xaal
.
MessageBody
)
msg
:=
eng
.
MessagesFactory
.
BuildMessage
(
dev
,
[]
uuid
.
UUID
{
addr
},
xaal
.
MSGTypeRequest
,
string
(
xaal
.
MSGActionGetAttributes
),
body
)
body
[
"dev_types"
]
=
[]
string
{
"any.any"
}
eng
.
Send
(
msg
)
msg
:=
d
.
engine
.
MessagesFactory
.
BuildMessage
(
d
.
device
,
[]
uuid
.
UUID
{
uuid
.
Nil
},
xaal
.
MSGTypeRequest
,
string
(
xaal
.
MSGActionIsAlive
),
body
)
d
.
engine
.
Send
(
msg
)
}
}
// isAlive sends a message to all devices asking if they are alive
// sendGetAttritube sends a message to all devices asking for their attributes
func
isAlive
(
eng
*
xaal
.
Engine
)
{
func
(
d
*
CounterDevice
)
sendGetAttritube
(
addr
uuid
.
UUID
)
{
addr
:=
uuid
.
Random
()
dev
:=
schemas
.
NewHmi
(
addr
)
body
:=
make
(
xaal
.
MessageBody
)
body
:=
make
(
xaal
.
MessageBody
)
body
[
"dev_types"
]
=
[]
string
{
"any.any"
}
msg
:=
d
.
engine
.
MessagesFactory
.
BuildMessage
(
d
.
device
,
[]
uuid
.
UUID
{
addr
},
xaal
.
MSGTypeRequest
,
string
(
xaal
.
MSGActionGetAttributes
),
body
)
msg
:=
eng
.
MessagesFactory
.
BuildMessage
(
dev
,
[]
uuid
.
UUID
{
uuid
.
Nil
},
xaal
.
MSGTypeRequest
,
string
(
xaal
.
MSGActionIsAlive
),
body
)
d
.
engine
.
Send
(
msg
)
eng
.
Send
(
msg
)
}
// display outputs the result on stdout and flush
func
(
d
*
CounterDevice
)
display
()
{
// slog.Info("Devices", "count", len(devices))
prefixs
:=
[]
string
{
"lamp"
,
"motion"
,
"contact"
}
for
_
,
prefix
:=
range
prefixs
{
fmt
.
Print
(
d
.
displayPrefix
(
prefix
))
fmt
.
Print
(
" "
)
}
fmt
.
Print
(
"
\n
"
)
}
func
(
d
*
CounterDevice
)
displayPrefix
(
prefix
string
)
string
{
return
fmt
.
Sprintf
(
"%s%d"
,
deviceIcons
[
prefix
],
d
.
countPrefix
(
prefix
))
}
// ----------------------------------------------------------------------------
// Message handling functions
// -----------------------------------------------------------------------------
func
getPrefix
(
msg
*
xaal
.
Message
)
string
{
// let's split the msg.DevType on points and keep the first part
tmp
:=
strings
.
Split
(
msg
.
DevType
,
"."
)
prefix
:=
tmp
[
0
]
_
,
found
:=
deviceIcons
[
prefix
]
if
!
found
{
return
""
}
return
prefix
}
func
getAttribute
(
msg
*
xaal
.
Message
,
attr
string
)
bool
{
if
value
,
ok
:=
msg
.
Body
[
attr
]
.
(
bool
);
ok
{
return
value
}
return
false
}
}
func
init
()
{
func
init
()
{
...
@@ -163,15 +183,9 @@ func init() {
...
@@ -163,15 +183,9 @@ func init() {
func
main
()
{
func
main
()
{
eng
:=
xaal
.
NewEngine
()
eng
:=
xaal
.
NewEngine
()
eng
.
DisableMessageFilter
()
eng
.
DisableMessageFilter
()
dev
:=
NewCounterDevice
(
eng
)
devices
=
make
(
map
[
uuid
.
UUID
]
*
Counter
)
eng
.
Subscribe
(
dev
.
handleMessage
)
handleMessageWrapper
:=
func
(
msg
*
xaal
.
Message
)
{
dev
.
sendIsAlive
()
handleMessage
(
eng
,
msg
)
}
eng
.
Subscribe
(
handleMessageWrapper
)
isAlive
(
eng
)
eng
.
Run
()
eng
.
Run
()
// Let's count the devices
}
}
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