Skip to content

Commit

Permalink
Merge pull request #48 from tomvictor/feature/decorator
Browse files Browse the repository at this point in the history
Feature/decorator
  • Loading branch information
tomvictor authored Sep 16, 2023
2 parents b8fec21 + f385ae1 commit 4234316
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "iotcore"
version = "0.2.1"
version = "0.3.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ async def lifespan(app: FastAPI):
app = FastAPI(lifespan=lifespan)


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


def mqtt_callback(data):
Expand All @@ -88,10 +88,13 @@ def sub():

@app.get("/pub")
def pub():
iot.publish("iot", "test")
iot.publish("temperature", "{'temp': 18}")
return {"response": "published"}


@app.get("/")
def home():
return {"Hello": "World"}

```

Expand Down
11 changes: 7 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ async def lifespan(app: FastAPI):
app = FastAPI(lifespan=lifespan)


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


def mqtt_callback(data):
Expand All @@ -88,10 +88,13 @@ def sub():

@app.get("/pub")
def pub():
iot.publish("iot", "test")
iot.publish("temperature", "{'temp': 18}")
return {"response": "published"}


@app.get("/")
def home():
return {"Hello": "World"}

```

Expand Down
43 changes: 43 additions & 0 deletions examples/decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class IoTCore:
def __init__(self):
self.subs = {} # Dictionary to store topic-callback function pairs

def accept(self, topic):
def decorator(func):
self.subs[topic] = func # Store the topic and callback function in the dictionary

def wrapper(request):
func(request)

return wrapper

return decorator

def temp(self):
print("do something")


iot = IoTCore()


@iot.accept(topic="request/temperature/data")
def temperature_data_handler(request):
print("Handling temperature data request:", request)


# Simulate a temperature data request
request_data = {
'topic': 'request/temperature/data',
'data': {
'temperature': 25.5
}
}


def main():
temperature_data_handler(request_data)
iot.temp()


if __name__ == "__main__":
main()
12 changes: 8 additions & 4 deletions examples/fastapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ async def lifespan(app: FastAPI):
app = FastAPI(lifespan=lifespan)


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


def mqtt_callback(data):
Expand All @@ -31,6 +31,10 @@ def sub():

@app.get("/pub")
def pub():
iot.publish("iot", "test")
iot.publish("temperature", "{'temp': 18}")
return {"response": "published"}


@app.get("/")
def home():
return {"Hello": "World"}
11 changes: 11 additions & 0 deletions iotcore/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ def iot_core_callback(self, topic, data):
subscription.callback(data)
except KeyError:
print(f"invalid topic : {topic}")

def accept(self, topic):
def decorator(func):
self.subscribe(topic, func)

def wrapper(request):
func(request)

return wrapper

return decorator
14 changes: 14 additions & 0 deletions iotcore/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

class Request(object):

def __int__(self, topic, data):
self._topic = topic
self._data = data

def raw(self):
return self._data

def data(self):
data_array = bytes(self._data)
data_string = data_array.decode('utf-8')
return data_string

0 comments on commit 4234316

Please sign in to comment.