Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Python
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
Python
Commits
28f1f018
Commit
28f1f018
authored
7 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Formating, fix ruff errors
parent
4a6b76c2
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
libs/lib/xaal/lib/aionetwork.py
+22
-19
22 additions, 19 deletions
libs/lib/xaal/lib/aionetwork.py
libs/lib/xaal/lib/bindings.py
+22
-24
22 additions, 24 deletions
libs/lib/xaal/lib/bindings.py
libs/lib/xaal/lib/devices.py
+35
-24
35 additions, 24 deletions
libs/lib/xaal/lib/devices.py
with
79 additions
and
67 deletions
libs/lib/xaal/lib/aionetwork.py
+
22
−
19
View file @
28f1f018
...
...
@@ -6,9 +6,9 @@ import logging
logger
=
logging
.
getLogger
(
__name__
)
class
AsyncNetworkConnector
(
object
):
def
__init__
(
self
,
addr
,
port
,
hops
,
bind_addr
=
'
0.0.0.0
'
):
class
AsyncNetworkConnector
(
object
):
def
__init__
(
self
,
addr
,
port
,
hops
,
bind_addr
=
"
0.0.0.0
"
):
self
.
addr
=
addr
self
.
port
=
port
self
.
hops
=
hops
...
...
@@ -18,14 +18,15 @@ class AsyncNetworkConnector(object):
async
def
connect
(
self
):
loop
=
asyncio
.
get_running_loop
()
on_con_lost
=
loop
.
create_future
()
self
.
transport
,
self
.
protocol
=
await
loop
.
create_datagram_endpoint
(
lambda
:
XAALServerProtocol
(
on_con_lost
,
self
.
receive
),
sock
=
self
.
new_sock
())
self
.
transport
,
self
.
protocol
=
await
loop
.
create_datagram_endpoint
(
lambda
:
XAALServerProtocol
(
on_con_lost
,
self
.
receive
),
sock
=
self
.
new_sock
()
)
# In some conditions (containers), transport is connected but IGMP is delayed (up to 10ms)
# so we need to wait for IGMP to be really sent.
await
asyncio
.
sleep
(
0.05
)
def
new_sock
(
self
):
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
,
socket
.
IPPROTO_UDP
)
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_DGRAM
,
socket
.
IPPROTO_UDP
)
try
:
# Linux + MacOS + BSD
sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEPORT
,
1
)
...
...
@@ -33,42 +34,44 @@ class AsyncNetworkConnector(object):
# Windows
sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
sock
.
bind
((
self
.
bind_addr
,
self
.
port
))
mreq
=
struct
.
pack
(
"
=4s4s
"
,
socket
.
inet_aton
(
self
.
addr
),
socket
.
inet_aton
(
self
.
bind_addr
))
sock
.
setsockopt
(
socket
.
IPPROTO_IP
,
socket
.
IP_ADD_MEMBERSHIP
,
mreq
)
sock
.
setsockopt
(
socket
.
IPPROTO_IP
,
socket
.
IP_MULTICAST_TTL
,
10
)
mreq
=
struct
.
pack
(
"
=4s4s
"
,
socket
.
inet_aton
(
self
.
addr
),
socket
.
inet_aton
(
self
.
bind_addr
)
)
sock
.
setsockopt
(
socket
.
IPPROTO_IP
,
socket
.
IP_ADD_MEMBERSHIP
,
mreq
)
sock
.
setsockopt
(
socket
.
IPPROTO_IP
,
socket
.
IP_MULTICAST_TTL
,
10
)
sock
.
setblocking
(
False
)
return
sock
def
send
(
self
,
data
):
self
.
protocol
.
datagram_send
(
data
,
self
.
addr
,
self
.
port
)
def
send
(
self
,
data
):
self
.
protocol
.
datagram_send
(
data
,
self
.
addr
,
self
.
port
)
def
receive
(
self
,
data
):
def
receive
(
self
,
data
):
self
.
_rx_queue
.
put_nowait
(
data
)
async
def
get_data
(
self
):
return
await
self
.
_rx_queue
.
get
()
class
XAALServerProtocol
(
asyncio
.
Protocol
):
def
__init__
(
self
,
on_con_lost
,
on_dtg_recv
):
def
__init__
(
self
,
on_con_lost
,
on_dtg_recv
):
self
.
on_con_lost
=
on_con_lost
self
.
on_dtg_recv
=
on_dtg_recv
def
connection_made
(
self
,
transport
):
logger
.
info
(
"
xAAL network connected
"
)
self
.
transport
=
transport
def
error_received
(
self
,
exc
):
print
(
'
Error received:
'
,
exc
)
print
(
"
Error received:
"
,
exc
)
logger
.
warning
(
f
"
Error received:
{
exc
}
"
)
def
connection_lost
(
self
,
exc
):
logger
.
info
(
f
"
Connexion closed:
{
exc
}
"
)
self
.
on_con_lost
.
set_result
(
True
)
def
datagram_send
(
self
,
data
,
ip
,
port
):
self
.
transport
.
sendto
(
data
,(
ip
,
port
))
def
datagram_send
(
self
,
data
,
ip
,
port
):
self
.
transport
.
sendto
(
data
,
(
ip
,
port
))
def
datagram_received
(
self
,
data
,
addr
):
#print(f"pkt from {addr}")
#
print(f"pkt from {addr}")
self
.
on_dtg_recv
(
data
)
This diff is collapsed.
Click to expand it.
libs/lib/xaal/lib/bindings.py
+
22
−
24
View file @
28f1f018
...
...
@@ -2,18 +2,19 @@ import uuid
from
.exceptions
import
UUIDError
class
UUID
:
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
__uuid
=
uuid
.
UUID
(
*
args
,
**
kwargs
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
__uuid
=
uuid
.
UUID
(
*
args
,
**
kwargs
)
@staticmethod
def
random_base
(
digit
=
2
):
"""
zeros the last digits of a random uuid, usefull w/ you want to forge some addresses
two digit is great.
two digit is great.
"""
if
(
digit
>
0
)
and
(
digit
<
13
):
if
(
digit
>
0
)
and
(
digit
<
13
):
tmp
=
str
(
uuid
.
uuid1
())
st
=
"
%s%s
"
%
(
tmp
[:
-
digit
],
'
0
'
*
digit
)
st
=
"
%s%s
"
%
(
tmp
[:
-
digit
],
"
0
"
*
digit
)
return
UUID
(
st
)
else
:
raise
UUIDError
...
...
@@ -23,27 +24,27 @@ class UUID:
tmp
=
uuid
.
uuid1
().
int
return
UUID
(
int
=
tmp
)
def
__add__
(
self
,
value
):
def
__add__
(
self
,
value
):
tmp
=
self
.
__uuid
.
int
+
value
return
UUID
(
int
=
tmp
)
def
__sub__
(
self
,
value
):
def
__sub__
(
self
,
value
):
tmp
=
self
.
__uuid
.
int
-
value
return
UUID
(
int
=
tmp
)
def
__eq__
(
self
,
value
):
def
__eq__
(
self
,
value
):
return
self
.
__uuid
==
value
def
__lt__
(
self
,
value
):
def
__lt__
(
self
,
value
):
return
self
.
__uuid
.
int
<
value
def
__gt__
(
self
,
value
):
def
__gt__
(
self
,
value
):
return
self
.
__uuid
.
int
>
value
def
__str__
(
self
):
return
str
(
self
.
__uuid
)
def
__repr__
(
self
):
# pragma: no cover
def
__repr__
(
self
):
# pragma: no cover
return
f
"
UUID(
'
{
self
.
__uuid
}
'
)
"
def
__hash__
(
self
):
...
...
@@ -51,8 +52,8 @@ class UUID:
def
get
(
self
):
return
self
.
__uuid
def
set
(
self
,
value
):
def
set
(
self
,
value
):
self
.
__uuid
=
value
@property
...
...
@@ -64,21 +65,20 @@ class UUID:
return
self
.
__uuid
.
bytes
class
URL
:
def
__init__
(
self
,
value
):
def
__init__
(
self
,
value
):
self
.
__url
=
value
def
__eq__
(
self
,
value
):
def
__eq__
(
self
,
value
):
return
self
.
__url
==
value
def
__str__
(
self
):
return
str
(
self
.
__url
)
def
__repr__
(
self
):
# pragma: no cover
return
f
"
URL(
'
{
self
.
__url
}
'
)
"
def
set
(
self
,
value
):
def
__repr__
(
self
):
# pragma: no cover
return
f
"
URL(
'
{
self
.
__url
}
'
)
"
def
set
(
self
,
value
):
self
.
__url
=
value
def
get
(
self
):
...
...
@@ -93,6 +93,4 @@ class URL:
return
self
.
__url
classes
=
[
UUID
,
URL
]
classes
=
[
UUID
,
URL
]
This diff is collapsed.
Click to expand it.
libs/lib/xaal/lib/devices.py
+
35
−
24
View file @
28f1f018
...
...
@@ -18,6 +18,7 @@
# along with xAAL. If not, see <http://www.gnu.org/licenses/>.
#
import
time
from
.
import
config
from
.
import
tools
...
...
@@ -28,7 +29,6 @@ from tabulate import tabulate
import
logging
logger
=
logging
.
getLogger
(
__name__
)
import
time
class
Attribute
(
object
):
...
...
@@ -58,7 +58,7 @@ class Attribute(object):
class
Attributes
(
list
):
"""
Devices owns a attributes list. This list also have dict-like access
"""
def
__getitem__
(
self
,
value
):
def
__getitem__
(
self
,
value
):
if
isinstance
(
value
,
int
):
return
list
.
__getitem__
(
self
,
value
)
for
k
in
self
:
...
...
@@ -66,7 +66,7 @@ class Attributes(list):
return
k
.
value
raise
KeyError
(
value
)
def
__setitem__
(
self
,
name
,
value
):
def
__setitem__
(
self
,
name
,
value
):
if
isinstance
(
name
,
int
):
return
list
.
__setitem__
(
self
,
name
,
value
)
for
k
in
self
:
...
...
@@ -84,7 +84,7 @@ class Device(object):
'
alive_period
'
,
'
next_alive
'
,
'
__attributes
'
,
'
methods
'
,
'
engine
'
]
def
__init__
(
self
,
dev_type
,
addr
=
None
,
engine
=
None
):
def
__init__
(
self
,
dev_type
,
addr
=
None
,
engine
=
None
):
# xAAL internal attributes for a device
self
.
dev_type
=
dev_type
# xaal dev_type
self
.
address
=
addr
# xaal addr
...
...
@@ -138,7 +138,7 @@ class Device(object):
@address.setter
def
address
(
self
,
value
):
if
value
==
None
:
if
value
is
None
:
self
.
__address
=
None
return
if
not
tools
.
is_valid_address
(
value
):
...
...
@@ -150,14 +150,14 @@ class Device(object):
return
self
.
__url
@url.setter
def
url
(
self
,
value
):
if
value
==
None
:
def
url
(
self
,
value
):
if
value
is
None
:
self
.
__url
=
None
else
:
self
.
__url
=
bindings
.
URL
(
value
)
# attributes
def
new_attribute
(
self
,
name
,
default
=
None
):
def
new_attribute
(
self
,
name
,
default
=
None
):
attr
=
Attribute
(
name
,
self
,
default
)
self
.
add_attribute
(
attr
)
return
attr
...
...
@@ -167,12 +167,12 @@ class Device(object):
self
.
__attributes
.
append
(
attr
)
attr
.
device
=
self
def
del_attribute
(
self
,
attr
):
def
del_attribute
(
self
,
attr
):
if
attr
:
attr
.
device
=
None
self
.
__attributes
.
remove
(
attr
)
def
get_attribute
(
self
,
name
):
def
get_attribute
(
self
,
name
):
for
attr
in
self
.
__attributes
:
if
attr
.
name
==
name
:
return
attr
...
...
@@ -183,13 +183,13 @@ class Device(object):
return
self
.
__attributes
@attributes.setter
def
attributes
(
self
,
values
):
def
attributes
(
self
,
values
):
if
isinstance
(
values
,
Attributes
):
self
.
__attributes
=
values
else
:
raise
DeviceError
(
"
Invalid attributes list, use class Attributes)
"
)
def
add_method
(
self
,
name
,
func
):
def
add_method
(
self
,
name
,
func
):
self
.
methods
.
update
({
name
:
func
})
def
get_methods
(
self
):
...
...
@@ -239,17 +239,28 @@ class Device(object):
#####################################################
def
_get_description
(
self
):
result
=
{}
if
self
.
vendor_id
:
result
[
'
vendor_id
'
]
=
self
.
vendor_id
if
self
.
product_id
:
result
[
'
product_id
'
]
=
self
.
product_id
if
self
.
version
:
result
[
'
version
'
]
=
self
.
version
if
self
.
url
:
result
[
'
url
'
]
=
self
.
url
if
self
.
schema
:
result
[
'
schema
'
]
=
self
.
schema
if
self
.
info
:
result
[
'
info
'
]
=
self
.
info
if
self
.
hw_id
:
result
[
'
hw_id
'
]
=
self
.
hw_id
if
self
.
group_id
:
result
[
'
group_id
'
]
=
self
.
group_id
if
self
.
unsupported_methods
:
result
[
'
unsupported_methods
'
]
=
self
.
unsupported_methods
if
self
.
unsupported_notifications
:
result
[
'
unsupported_notifications
'
]
=
self
.
unsupported_notifications
if
self
.
unsupported_attributes
:
result
[
'
unsupported_attributes
'
]
=
self
.
unsupported_attributes
if
self
.
vendor_id
:
result
[
'
vendor_id
'
]
=
self
.
vendor_id
if
self
.
product_id
:
result
[
'
product_id
'
]
=
self
.
product_id
if
self
.
version
:
result
[
'
version
'
]
=
self
.
version
if
self
.
url
:
result
[
'
url
'
]
=
self
.
url
if
self
.
schema
:
result
[
'
schema
'
]
=
self
.
schema
if
self
.
info
:
result
[
'
info
'
]
=
self
.
info
if
self
.
hw_id
:
result
[
'
hw_id
'
]
=
self
.
hw_id
if
self
.
group_id
:
result
[
'
group_id
'
]
=
self
.
group_id
if
self
.
unsupported_methods
:
result
[
'
unsupported_methods
'
]
=
self
.
unsupported_methods
if
self
.
unsupported_notifications
:
result
[
'
unsupported_notifications
'
]
=
self
.
unsupported_notifications
if
self
.
unsupported_attributes
:
result
[
'
unsupported_attributes
'
]
=
self
.
unsupported_attributes
return
result
def
_get_attributes
(
self
,
_attributes
=
None
):
...
...
@@ -279,7 +290,7 @@ class Device(object):
result
.
update
({
attr
.
name
:
attr
.
value
})
return
result
def
send_notification
(
self
,
notification
,
body
=
{}):
def
send_notification
(
self
,
notification
,
body
=
{}):
"""
queue an notification, this is just a method helper
"""
if
self
.
engine
:
self
.
engine
.
send_notification
(
self
,
notification
,
body
)
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