Skip to content

Commit 935da03

Browse files
Modifying the SubscribeDataobjectsReq to add defaults to parameters a… (#11)
* Modifying the SubscribeDataobjectsReq to add defaults to parameters and the LDM Factory class to simplify the instantiation * Fixing flake8 issues * Updating version
1 parent 425415b commit 935da03

File tree

3 files changed

+143
-77
lines changed

3 files changed

+143
-77
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "v2xflexstack"
7-
version = "0.10.4"
7+
version = "0.10.5"
88
authors = [
99
{ name = "Jordi Marias-i-Parella", email = "[email protected]" },
1010
{ name = "Daniel Ulied Guevara", email = "[email protected]" },
Lines changed: 137 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1+
from __future__ import annotations
2+
from collections.abc import Callable
13
import logging
24

3-
from .ldm_classes import Location
5+
from .ldm_classes import (
6+
Location,
7+
SubscribeDataobjectsReq,
8+
SubscribeDataObjectsResp,
9+
RequestDataObjectsResp,
10+
RegisterDataConsumerReq,
11+
RegisterDataConsumerResp,
12+
Filter,
13+
FilterStatement,
14+
ComparisonOperators,
15+
AccessPermission,
16+
)
417

518
from .ldm_maintenance_reactive import LDMMaintenanceReactive
619
from .ldm_maintenance_thread import LDMMaintenanceThread
@@ -16,79 +29,132 @@
1629
from .exceptions import LDMMaintenanceKeyError, LDMServiceKeyError, LDMDatabaseKeyError
1730

1831

19-
def ldm_factory(
20-
ldm_location: Location,
21-
ldm_maintenance_type: str = "Reactive",
22-
ldm_service_type: str = "Reactive",
23-
ldm_database_type: str = "Dictionary",
24-
) -> LDMFacility:
25-
"""
26-
Factory function to create a Local Dynamic Map Facility.
27-
28-
Parameters
29-
----------
30-
ldm_location : Location
31-
Location object to be used by the LDM Facility.
32-
ldm_maintenance_type : str, optional
33-
Type of LDM Maintenance to be used. Defaults to "Reactive".
34-
ldm_service_type : str, optional
35-
Type of LDM Service to be used. Defaults to "Reactive".
36-
ldm_database_type : str, optional
37-
Type of LDM Database to be used. Defaults to "Dictionary".
38-
"""
39-
40-
logs = logging.getLogger("local_dynamic_map")
41-
42-
if ldm_database_type == "Dictionary":
43-
ldm_database = DictionaryDataBase()
44-
logs.info('LDM Database "Dictionary" configured.')
45-
elif ldm_database_type == "TinyDB":
46-
ldm_database = TinyDB()
47-
logs.info('LDM Database "TindyDB" configured.')
48-
else:
49-
logs.error(
50-
'LDM Database must be either "Dictionary" or "TindyDB". %s is an invalid type for LDM Database.',
51-
ldm_database_type,
52-
)
53-
raise LDMDatabaseKeyError(
54-
f'LDM Database must be either "Dictionary" or "TindyDB". {ldm_database_type} is an invalid type.'
55-
)
32+
class LDMFactory:
33+
"""Factory class to create a Local Dynamic Map Facility."""
5634

57-
if ldm_maintenance_type == "Reactive":
58-
ldm_maintenance = LDMMaintenanceReactive(ldm_location, ldm_database)
59-
logs.info('LDM Maintenance "Reactive" configured.')
60-
elif ldm_maintenance_type == "Thread":
61-
ldm_maintenance = LDMMaintenanceThread(ldm_location, ldm_database, None)
62-
logs.info('LDM Maintenance "Thread" configured.')
63-
else:
64-
logs.error(
65-
'LDM Maintenance must be either "Reactive" or "Thread". %s is an invalid type for LDM Maintenance.',
66-
ldm_maintenance_type,
67-
)
68-
raise LDMMaintenanceKeyError(
69-
f'LDM Maintenance must be either "Reactive" or "Thread". {ldm_maintenance_type} is an invalid type.'
70-
)
35+
def __init__(self) -> None:
36+
self.ldm = None
37+
38+
def create_ldm(
39+
self,
40+
ldm_location: Location,
41+
ldm_maintenance_type: str = "Reactive",
42+
ldm_service_type: str = "Reactive",
43+
ldm_database_type: str = "Dictionary",
44+
) -> LDMFacility:
45+
"""
46+
Factory function to create a Local Dynamic Map Facility.
47+
48+
Parameters
49+
----------
50+
ldm_location : Location
51+
Location object to be used by the LDM Facility.
52+
ldm_maintenance_type : str, optional
53+
Type of LDM Maintenance to be used. Defaults to "Reactive".
54+
ldm_service_type : str, optional
55+
Type of LDM Service to be used. Defaults to "Reactive".
56+
ldm_database_type : str, optional
57+
Type of LDM Database to be used. Defaults to "Dictionary".
58+
"""
59+
60+
logs = logging.getLogger("local_dynamic_map")
61+
62+
if ldm_database_type == "Dictionary":
63+
ldm_database = DictionaryDataBase()
64+
logs.info('LDM Database "Dictionary" configured.')
65+
elif ldm_database_type == "TinyDB":
66+
ldm_database = TinyDB()
67+
logs.info('LDM Database "TinyDB" configured.')
68+
else:
69+
logs.error(
70+
'LDM Database must be either "Dictionary" or "TinyDB". %s is an invalid type for LDM Database.',
71+
ldm_database_type,
72+
)
73+
raise LDMDatabaseKeyError(
74+
f'LDM Database must be either "Dictionary" or "TinyDB". {ldm_database_type} is an invalid type.'
75+
)
7176

72-
if ldm_service_type == "Reactive":
73-
ldm_service = LDMServiceReactive(ldm_maintenance)
74-
logs.info('LDM Service "Reactive" configured.')
75-
elif ldm_service_type == "Thread":
76-
ldm_service = LDMServiceThreads(ldm_maintenance)
77-
logs.info('LDM Service "Thread" configured.')
78-
else:
79-
logs.error(
80-
'LDM Service must be either "Reactive" or "Thread". %s is an invalid type for LDM Service.',
77+
if ldm_maintenance_type == "Reactive":
78+
ldm_maintenance = LDMMaintenanceReactive(ldm_location, ldm_database)
79+
logs.info('LDM Maintenance "Reactive" configured.')
80+
elif ldm_maintenance_type == "Thread":
81+
ldm_maintenance = LDMMaintenanceThread(ldm_location, ldm_database, None)
82+
logs.info('LDM Maintenance "Thread" configured.')
83+
else:
84+
logs.error(
85+
'LDM Maintenance must be either "Reactive" or "Thread". %s is an invalid type for LDM Maintenance.',
86+
ldm_maintenance_type,
87+
)
88+
raise LDMMaintenanceKeyError(
89+
f'LDM Maintenance must be either "Reactive" or "Thread". {ldm_maintenance_type} is an invalid type.'
90+
)
91+
92+
if ldm_service_type == "Reactive":
93+
ldm_service = LDMServiceReactive(ldm_maintenance)
94+
logs.info('LDM Service "Reactive" configured.')
95+
elif ldm_service_type == "Thread":
96+
ldm_service = LDMServiceThreads(ldm_maintenance)
97+
logs.info('LDM Service "Thread" configured.')
98+
else:
99+
logs.error(
100+
'LDM Service must be either "Reactive" or "Thread". %s is an invalid type for LDM Service.',
101+
ldm_service_type,
102+
)
103+
raise LDMServiceKeyError(
104+
f'LDM Service must be either "Reactive" or "Thread". {ldm_service_type} is an invalid type for LDM Service.'
105+
)
106+
107+
ldm_facility = LDMFacility(ldm_maintenance, ldm_service)
108+
logs.info(
109+
'LDM Facility configured with: LDM Maintenance: "%s", LDM Service: "%s", LDM Database: "%s".',
110+
ldm_maintenance_type,
81111
ldm_service_type,
112+
ldm_database_type,
82113
)
83-
raise LDMServiceKeyError(
84-
f'LDM Service must be either "Reactive" or "Thread". {ldm_service_type} is an invalid type for LDM Service.'
114+
self.ldm = ldm_facility
115+
return ldm_facility
116+
117+
def subscribe_to_ldm(
118+
self,
119+
own_station_id: int,
120+
ldm_location: Location,
121+
callback_function: Callable[[RequestDataObjectsResp], None],
122+
) -> None:
123+
"""
124+
Method to subscribe to the LDM Facility.
125+
126+
Parameters
127+
----------
128+
subscription_request : SubscribeDataobjectsReq
129+
Subscription request object.
130+
callback_function : Callable
131+
Callback function to be called when new data is available.
132+
133+
Returns
134+
-------
135+
int
136+
Subscription ID.
137+
"""
138+
if self.ldm is None:
139+
raise Exception("LDM Facility not initialized. Please create an LDM Facility first.")
140+
141+
register_data_consumer_reponse: RegisterDataConsumerResp = self.ldm.if_ldm_4.register_data_consumer(
142+
RegisterDataConsumerReq(
143+
application_id=AccessPermission.CAM,
144+
access_permisions=(AccessPermission.CAM, AccessPermission.VAM),
145+
area_of_interest=ldm_location,
146+
)
85147
)
148+
if register_data_consumer_reponse.result == 2:
149+
raise Exception(f"Failed to register data consumer: {str(register_data_consumer_reponse)}")
86150

87-
ldm_facility = LDMFacility(ldm_maintenance, ldm_service)
88-
logs.info(
89-
'LDM Facility configured with: LDM Maintenance: "%s", LDM Service: "%s", LDM Database: "%s".',
90-
ldm_maintenance_type,
91-
ldm_service_type,
92-
ldm_database_type,
93-
)
94-
return ldm_facility
151+
subscribe_data_consumer_response: SubscribeDataObjectsResp = self.ldm.if_ldm_4.subscribe_data_consumer(
152+
SubscribeDataobjectsReq(
153+
application_id=AccessPermission.CAM,
154+
data_object_type=(AccessPermission.CAM, AccessPermission.VAM),
155+
filter=Filter(FilterStatement("header.stationId", ComparisonOperators.NOT_EQUAL, own_station_id)),
156+
),
157+
callback_function,
158+
)
159+
if subscribe_data_consumer_response.result != 0:
160+
raise Exception(f"Failed to subscribe to data objects: {str(subscribe_data_consumer_response.result)}")

src/flexstack/facilities/local_dynamic_map/ldm_classes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,11 +1530,11 @@ class SubscribeDataobjectsReq:
15301530
"""
15311531
application_id: int
15321532
data_object_type: tuple[int, ...]
1533-
priority: int
1534-
filter: Filter
1535-
notify_time: TimestampIts
1536-
multiplicity: int
1537-
order: tuple[OrderTupleValue, ...]
1533+
priority: int = None
1534+
filter: Filter = None
1535+
notify_time: TimestampIts = TimestampIts(1)
1536+
multiplicity: int = 1
1537+
order: tuple[OrderTupleValue, ...] = None
15381538

15391539

15401540
@dataclass(frozen=True)

0 commit comments

Comments
 (0)