-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from tomvictor/feature/decorator
Feature/decorator
- Loading branch information
Showing
8 changed files
with
92 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |