Skip to content
This repository was archived by the owner on Nov 14, 2025. It is now read-only.

Commit 08f9c8f

Browse files
committed
urlencode query in subscription payloads
1 parent aa5d2a5 commit 08f9c8f

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,33 @@ client.count("ParkingSpot", q='refParkingSite=="urn:ngsi-ld:OffStreetParking:por
251251
client.close()
252252
```
253253

254+
### Let's go further
255+
256+
1. Develop a NGSI-LD Agent
257+
258+
- Collect incoming data from parking IoT *(ground sensors, cameras)* and the parking system API
259+
- Clean data, process data and convert to NGSI-LD entities
260+
- Create or update entities into the NGSI-LD broker
261+
262+
2. Subscribe to events
263+
264+
- Create a subscription to be informed when parking occupation exceeds 90%
265+
- The software that listens to these highly-occupied parking entities can also be a NGSI-LD Agent
266+
267+
<br>Example : programatically subscribe to events
268+
269+
```python
270+
from ngsildclient import SubscriptionBuilder
271+
272+
subscr = SubscriptionBuilder("https://parkingsystem.example.com:8000/subscription/high-occupancy")
273+
.description("Notify me of high occupancy on parking porto-23889")
274+
.select_type("OffStreetParking")
275+
.watch(["occupancy"])
276+
.query('occupancy>0.9;controlledAsset=="urn:ngsi-ld:OffStreetParking:porto-ParkingLot-23889"')
277+
.build()
278+
client.subscriptions.create(subscr)
279+
```
280+
254281
## Where to get it
255282

256283
The source code is currently hosted on GitHub at :

src/ngsildclient/api/helper/subscription.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
# Author: Fabien BATTELLO <fabien.battello@orange.com> et al.
1111

1212
from dataclasses import dataclass
13-
from ...model.constants import CORE_CONTEXT
13+
14+
import ngsildclient.utils.url as url
15+
from ngsildclient.model.constants import CORE_CONTEXT
1416

1517

1618
@dataclass
@@ -126,7 +128,7 @@ def watch(self, value: list[str]):
126128
def query(self, value: str):
127129
if not isinstance(value, str):
128130
raise ValueError("query shall be a string")
129-
self._subscr.query = value
131+
self._subscr.query = url.escape(value)
130132
return self
131133

132134
def notif(self, value: list[str]):

tests/test_helper_subscription.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_build_subscription():
2020
.description("Notify me of low feedstock on Farm:001")
2121
.select_type("FillingLevelSensor")
2222
.watch(["filling"])
23-
.query("filling>0.4;filling<0.6;controlledAsset==urn:ngsi-ld:Building:farm001")
23+
.query('filling>0.4;filling<0.6;controlledAsset=="urn:ngsi-ld:Building:farm001"')
2424
.notif(["filling", "controlledAsset"])
2525
)
2626
subscription = builder.build()
@@ -29,7 +29,7 @@ def test_build_subscription():
2929
"description": "Notify me of low feedstock on Farm:001",
3030
"entities": [{"type": "FillingLevelSensor"}],
3131
"watchedAttributes": ["filling"],
32-
"q": "filling>0.4;filling<0.6;controlledAsset==urn:ngsi-ld:Building:farm001",
32+
"q": "filling%3E0.4%3Bfilling%3C0.6%3BcontrolledAsset%3D%3D%22urn%3Angsi-ld%3ABuilding%3Afarm001%22",
3333
"isActive": True,
3434
"notification": {
3535
"attributes": ["filling", "controlledAsset"],

0 commit comments

Comments
 (0)