Skip to content

Commit 6a29a96

Browse files
authored
Merge pull request #30 from tomvictor/feature/fastapi
add Fastapi support
2 parents d6e9dff + bf67b59 commit 6a29a96

File tree

12 files changed

+105
-30
lines changed

12 files changed

+105
-30
lines changed

README.md

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
# iotcore - MQTT and IoT capabilities to Django Rest Framework and FastAPI.
1+
# Iotcore - MQTT and IoT capabilities to Django Rest Framework and FastAPI.
22

3-
4-
Project under development. Apis can change often until stable.
3+
The project aims to give full support for mqtt based server and related apis. The internals of the mqtt server is
4+
written in golang. The python will not directly interact with the golang instead communicate using grpc(planned). The
5+
motive is to avoid the GIL limitation of python and bring all the fun features offered by golang.
56

67
## Planned Features
78

8-
* MQTT based IoT protocol
9-
* MQTT broker with websocket and tcp support (Written in golang)
10-
* Easy sensor data storage
11-
* APIs for IoT Device management and storage
12-
* Most of the IoT logics will be handled by inbuilt golang application. It will then communicate with the django using channels
13-
* and more coming soon
9+
* Full fledged MQTT server
10+
* with websocket and tcp support (Written in golang)
11+
* MQTT v5 support
12+
* and more coming soon
1413

1514
## Installation
1615

@@ -19,6 +18,52 @@ PyPI
1918
pip install iotcore
2019
```
2120

21+
# FastAPI setup
22+
23+
```python
24+
from fastapi import FastAPI
25+
from contextlib import asynccontextmanager
26+
from iotcore import IotCore
27+
28+
iot_core = IotCore()
29+
30+
31+
@asynccontextmanager
32+
async def lifespan(app: FastAPI):
33+
iot_core.run()
34+
yield
35+
36+
37+
app = FastAPI(lifespan=lifespan)
38+
39+
40+
@app.get("/")
41+
def read_root():
42+
return {"Hello": "World"}
43+
44+
```
45+
46+
Output
47+
48+
```shell
49+
cd examples/fastapi
50+
❯ uvicorn main:app
51+
INFO: Started server process [62593]
52+
INFO: Waiting for application startup.
53+
Starting Go process...
54+
INFO: Application startup complete.
55+
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
56+
11:21PM INF added hook hook=allow-all-auth
57+
11:21PM INF attached listener address=:1883 id=t1 protocol=tcp
58+
11:21PM INF attached listener address=:1882 id=ws1 protocol=ws
59+
11:21PM INF attached listener address=:8080 id=stats protocol=http
60+
11:21PM INF mochi mqtt starting version=2.3.0
61+
11:21PM INF mochi mqtt server started
62+
63+
```
64+
65+
# Django Setup
66+
2267
Then add iotcore to the django apps as below in the settings.py file of your project
2368
```python
2469
INSTALLED_APPS = [
@@ -27,17 +72,16 @@ INSTALLED_APPS = [
2772
]
2873
```
2974

30-
Now Connect to mqtt broker on localhost
31-
MQTT Port : 1883
32-
Websocket Port : 1882
33-
Stat Port: 8080
75+
Now Connect to mqtt broker on localhost
76+
MQTT Port : 1883
77+
Websocket Port : 1882
78+
Stat Port: 8080
3479

3580
# Run Example project
3681

3782
```shell
38-
cd example
3983
pip install -r requirements.txt
40-
python manage.py runserver
84+
python examples/manage.py runserver
4185
```
4286
Output
4387

@@ -58,6 +102,8 @@ Starting golang
58102

59103
```
60104

105+
For more details check the example folder
106+
61107
## Development
62108

63109
Use mage for development

config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"pypackage": "iotcore",
3-
"version": "0.0.10",
3+
"version": "0.0.11",
44
"gobinary": "goiotbackend"
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
django
2-
djangoiot
2+
iotcore
33
psutil

examples/fastapi/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from fastapi import FastAPI
2+
from contextlib import asynccontextmanager
3+
from iotcore import IotCore
4+
5+
iot_core = IotCore()
6+
7+
8+
@asynccontextmanager
9+
async def lifespan(app: FastAPI):
10+
iot_core.run()
11+
yield
12+
13+
14+
app = FastAPI(lifespan=lifespan)
15+
16+
17+
@app.get("/")
18+
def read_root():
19+
return {"Hello": "World"}

examples/fastapi/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
psutil
2+
iotcore
3+
fastapi

iotcore/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .core import IotCore
1+
from .core import IotCore

iotcore/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.0.10'
1+
version = '0.0.11'

iotcore/core.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import psutil
66

7-
from djangoiot import __version__
7+
from .__version__ import version
88

99

1010
class IotCore(object):
@@ -28,14 +28,21 @@ def run(self):
2828
print("Starting Go process...")
2929
subprocess.Popen([self.executable_path()])
3030

31+
def clean(self):
32+
# TODO: Clean the proces
33+
raise NotImplementedError("clean function is not implemented")
34+
35+
36+
37+
3138
def executable_path(self):
3239
current_file_path = os.path.abspath(__file__)
3340
return os.path.join(os.path.dirname(current_file_path), self.executable)
3441

3542
def get_executable_name(self):
3643
binary_map = {
37-
"Darwin": f"{self.binary_prefix}-mac-{__version__}",
38-
"Windows": f"{self.binary_prefix}-win-{__version__}.exe",
39-
"Linux": f"{self.binary_prefix}-linux-{__version__}",
44+
"Darwin": f"{self.binary_prefix}-mac-{version}",
45+
"Windows": f"{self.binary_prefix}-win-{version}.exe",
46+
"Linux": f"{self.binary_prefix}-linux-{version}",
4047
}
4148
return binary_map[platform.system()]

iotcore/djangoiot/apps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from django.apps import AppConfig
2-
from djangoiot.core import IotCore
2+
from iotcore.core import IotCore
33

44
iot_core = IotCore()
55

66

77
class IotConfig(AppConfig):
88
default_auto_field = "django.db.models.BigAutoField"
9-
name = "djangoiot"
9+
name = "iotcore.djangoiot"
1010

1111
def ready(self):
1212
iot_core.run()

magefile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func Build() error {
117117

118118
BuildGo()
119119

120-
versionFileContent := fmt.Sprintf("__version__ = '%s'\n", Config().Version)
120+
versionFileContent := fmt.Sprintf("version = '%s'\n", Config().Version)
121121

122122
file, err := os.OpenFile("iotcore/__version__.py", os.O_WRONLY|os.O_TRUNC, 0644)
123123
if err != nil {

0 commit comments

Comments
 (0)