Skip to content

Commit f385ae1

Browse files
committed
add accept decorator for subscription
1 parent 450138a commit f385ae1

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "iotcore"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ async def lifespan(app: FastAPI):
7171
app = FastAPI(lifespan=lifespan)
7272

7373

74-
@app.get("/")
75-
def home():
76-
return {"Hello": "World"}
74+
@iot.accept(topic="temperature")
75+
def temperature_data(request):
76+
print(f"Temperature data : {request}")
7777

7878

7979
def mqtt_callback(data):
@@ -88,10 +88,13 @@ def sub():
8888

8989
@app.get("/pub")
9090
def pub():
91-
iot.publish("iot", "test")
91+
iot.publish("temperature", "{'temp': 18}")
9292
return {"response": "published"}
9393

9494

95+
@app.get("/")
96+
def home():
97+
return {"Hello": "World"}
9598

9699
```
97100

docs/index.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ async def lifespan(app: FastAPI):
7171
app = FastAPI(lifespan=lifespan)
7272

7373

74-
@app.get("/")
75-
def home():
76-
return {"Hello": "World"}
74+
@iot.accept(topic="temperature")
75+
def temperature_data(request):
76+
print(f"Temperature data : {request}")
7777

7878

7979
def mqtt_callback(data):
@@ -88,10 +88,13 @@ def sub():
8888

8989
@app.get("/pub")
9090
def pub():
91-
iot.publish("iot", "test")
91+
iot.publish("temperature", "{'temp': 18}")
9292
return {"response": "published"}
9393

9494

95+
@app.get("/")
96+
def home():
97+
return {"Hello": "World"}
9598

9699
```
97100

examples/fastapi/main.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ async def lifespan(app: FastAPI):
1414
app = FastAPI(lifespan=lifespan)
1515

1616

17-
@app.get("/")
18-
def home():
19-
return {"Hello": "World"}
17+
@iot.accept(topic="temperature")
18+
def temperature_data(request):
19+
print(f"Temperature data : {request}")
2020

2121

2222
def mqtt_callback(data):
@@ -31,10 +31,10 @@ def sub():
3131

3232
@app.get("/pub")
3333
def pub():
34-
iot.publish("iot", "test")
34+
iot.publish("temperature", "{'temp': 18}")
3535
return {"response": "published"}
3636

3737

38-
@iot.accept(topic="request/temperature/data")
39-
def temperature_data(request):
40-
print(request)
38+
@app.get("/")
39+
def home():
40+
return {"Hello": "World"}

iotcore/mqtt.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ def iot_core_callback(self, topic, data):
4646

4747
def accept(self, topic):
4848
def decorator(func):
49-
self.subscribed_topics[hash(topic)] = func # Store the topic and callback function in the dictionary
49+
self.subscribe(topic, func)
5050

5151
def wrapper(request):
5252
func(request)
5353

5454
return wrapper
5555

5656
return decorator
57-

0 commit comments

Comments
 (0)