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
49f2f7a7
Commit
49f2f7a7
authored
5 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Format
parent
f10cd3d7
No related branches found
No related tags found
1 merge request
!1
First try of type hints
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
devices/protocols/SensFloor/xaal/sensfloor/gw.py
+40
-41
40 additions, 41 deletions
devices/protocols/SensFloor/xaal/sensfloor/gw.py
with
40 additions
and
41 deletions
devices/protocols/SensFloor/xaal/sensfloor/gw.py
+
40
−
41
View file @
49f2f7a7
from
xaal.lib
import
tools
,
Device
,
AsyncEngine
from
xaal.lib
import
tools
from
xaal.schemas
import
devices
import
asyncio
import
socketio
import
atexit
import
logging
...
...
@@ -16,39 +15,38 @@ logger = logging.getLogger(PACKAGE_NAME)
class
GW
(
object
):
def
__init__
(
self
,
engine
):
def
__init__
(
self
,
engine
):
self
.
engine
=
engine
self
.
devices
=
{}
atexit
.
register
(
self
.
_exit
)
self
.
config
()
self
.
setup
()
def
config
(
self
):
cfg
=
tools
.
load_cfg
(
PACKAGE_NAME
)
if
not
cfg
:
cfg
=
tools
.
new_cfg
(
PACKAGE_NAME
)
cfg
[
'
config
'
][
'
url
'
]
=
'
http://floor1.enstb.org:8000/
'
cfg
=
tools
.
new_cfg
(
PACKAGE_NAME
)
cfg
[
'
config
'
][
'
url
'
]
=
'
http://floor1.enstb.org:8000/
'
cfg
[
'
devices
'
]
=
{}
logger
.
warn
(
"
Created an empty config file
"
)
logger
.
warn
ing
(
"
Created an empty config file
"
)
cfg
.
write
()
self
.
cfg
=
cfg
def
setup
(
self
):
# socketio config
self
.
sio
=
socketio
.
AsyncClient
(
engineio_logger
=
True
,
ssl_verify
=
False
)
self
.
sio
.
on
(
'
alarms-detected
'
,
self
.
on_alarm
)
# xaal gateway
self
.
sio
=
socketio
.
AsyncClient
(
engineio_logger
=
True
,
ssl_verify
=
False
)
self
.
sio
.
on
(
'
alarms-detected
'
,
self
.
on_alarm
)
# xaal gateway
addr
=
tools
.
get_uuid
(
self
.
cfg
[
'
config
'
][
'
addr
'
])
self
.
gw
=
devices
.
gateway
(
addr
)
self
.
engine
.
add_device
(
self
.
gw
)
for
k
in
self
.
cfg
[
'
devices
'
]:
cfg
=
self
.
cfg
[
'
devices
'
][
k
]
dev
=
self
.
add_device
(
k
,
cfg
[
'
type
'
],
tools
.
get_uuid
(
cfg
[
'
addr
'
]))
dev
=
self
.
add_device
(
k
,
cfg
[
'
type
'
],
tools
.
get_uuid
(
cfg
[
'
addr
'
]))
self
.
engine
.
add_device
(
dev
)
def
add_device
(
self
,
idx
,
al_type
,
addr
=
None
):
def
add_device
(
self
,
idx
,
al_type
,
addr
=
None
):
if
not
addr
:
addr
=
tools
.
get_random_uuid
()
dev
=
None
...
...
@@ -56,44 +54,42 @@ class GW(object):
dev
=
devices
.
falldetector
(
addr
)
if
al_type
==
'
presence
'
:
dev
=
devices
.
motion
(
addr
)
if
dev
==
None
:
return
if
dev
is
None
:
return
logger
.
debug
(
f
"
New device
{
addr
}
{
idx
}
"
)
dev
.
vendor_id
=
'
Future Shape
'
dev
.
product_id
=
'
SensFloor
'
dev
.
info
=
'
zone index: %s
'
%
idx
dev
.
product_id
=
'
SensFloor
'
dev
.
info
=
'
zone index: %s
'
%
idx
self
.
devices
.
update
({
idx
:
dev
})
self
.
devices
.
update
({
idx
:
dev
})
self
.
engine
.
add_device
(
dev
)
return
dev
def
get_device
(
self
,
idx
,
al_type
):
dev
=
self
.
devices
.
get
(
idx
,
None
)
def
get_device
(
self
,
idx
,
al_type
):
dev
=
self
.
devices
.
get
(
idx
,
None
)
return
dev
def
on_alarm
(
self
,
data
):
def
on_alarm
(
self
,
data
):
active
=
[]
for
k
in
data
:
#print(k)
#
print(k)
idx
=
str
(
k
[
'
index
'
])
al_type
=
k
[
'
type
'
]
state
=
k
[
'
state
'
]
dev
=
self
.
get_device
(
idx
,
al_type
)
if
dev
==
None
:
dev
=
self
.
add_device
(
idx
,
al_type
)
self
.
cfg
[
'
devices
'
][
str
(
idx
)]
=
{
'
addr
'
:
dev
.
address
,
'
type
'
:
al_type
}
if
dev
.
dev_type
==
'
motion.basic
'
:
#
state = k['state']
dev
=
self
.
get_device
(
idx
,
al_type
)
if
dev
is
None
:
dev
=
self
.
add_device
(
idx
,
al_type
)
self
.
cfg
[
'
devices
'
][
str
(
idx
)]
=
{
'
addr
'
:
dev
.
address
,
'
type
'
:
al_type
}
if
dev
.
dev_type
==
'
motion.basic
'
:
dev
.
attributes
[
'
presence
'
]
=
True
active
.
append
(
dev
)
#print(active)
#
print(active)
for
dev
in
self
.
devices
.
values
():
if
dev
in
active
:
continue
if
dev
.
dev_type
==
'
motion.basic
'
:
dev
.
attributes
[
'
presence
'
]
=
False
if
dev
in
active
:
continue
if
dev
.
dev_type
==
'
motion.basic
'
:
dev
.
attributes
[
'
presence
'
]
=
False
def
_exit
(
self
):
cfg
=
tools
.
load_cfg
(
PACKAGE_NAME
)
...
...
@@ -101,21 +97,24 @@ class GW(object):
logger
.
info
(
'
Saving configuration file
'
)
self
.
cfg
.
write
()
# async def xaal_task(self):
# await self.engine.run()
# async def xaal_task(self):
# await self.engine.run()
async
def
sio_task
(
self
):
url
=
self
.
cfg
[
'
config
'
][
'
url
'
]
await
self
.
sio
.
connect
(
url
)
#
transports='polling')
await
self
.
sio
.
connect
(
url
)
#
transports='polling')
await
self
.
sio
.
wait
()
def
stop
():
import
pdb
;
pdb
.
set_trace
()
import
pdb
pdb
.
set_trace
()
def
setup
(
eng
):
gw
=
GW
(
eng
)
eng
.
on_stop
(
stop
)
eng
.
new_task
(
gw
.
sio_task
())
return
True
\ No newline at end of file
return
True
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