Skip to content

Commit 4234316

Browse files
authored
Merge pull request #48 from tomvictor/feature/decorator
Feature/decorator
2 parents b8fec21 + f385ae1 commit 4234316

File tree

8 files changed

+92
-14
lines changed

8 files changed

+92
-14
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/decorator.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class IoTCore:
2+
def __init__(self):
3+
self.subs = {} # Dictionary to store topic-callback function pairs
4+
5+
def accept(self, topic):
6+
def decorator(func):
7+
self.subs[topic] = func # Store the topic and callback function in the dictionary
8+
9+
def wrapper(request):
10+
func(request)
11+
12+
return wrapper
13+
14+
return decorator
15+
16+
def temp(self):
17+
print("do something")
18+
19+
20+
iot = IoTCore()
21+
22+
23+
@iot.accept(topic="request/temperature/data")
24+
def temperature_data_handler(request):
25+
print("Handling temperature data request:", request)
26+
27+
28+
# Simulate a temperature data request
29+
request_data = {
30+
'topic': 'request/temperature/data',
31+
'data': {
32+
'temperature': 25.5
33+
}
34+
}
35+
36+
37+
def main():
38+
temperature_data_handler(request_data)
39+
iot.temp()
40+
41+
42+
if __name__ == "__main__":
43+
main()

examples/fastapi/main.py

Lines changed: 8 additions & 4 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,6 +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

37+
38+
@app.get("/")
39+
def home():
40+
return {"Hello": "World"}

iotcore/mqtt.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,14 @@ def iot_core_callback(self, topic, data):
4343
subscription.callback(data)
4444
except KeyError:
4545
print(f"invalid topic : {topic}")
46+
47+
def accept(self, topic):
48+
def decorator(func):
49+
self.subscribe(topic, func)
50+
51+
def wrapper(request):
52+
func(request)
53+
54+
return wrapper
55+
56+
return decorator

iotcore/request.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
class Request(object):
3+
4+
def __int__(self, topic, data):
5+
self._topic = topic
6+
self._data = data
7+
8+
def raw(self):
9+
return self._data
10+
11+
def data(self):
12+
data_array = bytes(self._data)
13+
data_string = data_array.decode('utf-8')
14+
return data_string

0 commit comments

Comments
 (0)