Skip to content

Commit

Permalink
Merge pull request #44 from tomvictor/feature/django
Browse files Browse the repository at this point in the history
Feature/django
  • Loading branch information
tomvictor authored Sep 15, 2023
2 parents 4616d52 + 8980f5a commit 67ecc01
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 5 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.0.14"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
3 changes: 2 additions & 1 deletion examples/django/develop/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"iotcore.djangoiot"
# "iotcore.djangoiot", # Broker only
"iot"
]

MIDDLEWARE = [
Expand Down
3 changes: 3 additions & 0 deletions examples/django/develop/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"""
from django.contrib import admin
from django.urls import path
from iot import views

urlpatterns = [
path("admin/", admin.site.urls),
path("sub", views.subscribe),
path("pub", views.publish),
]
Empty file added examples/django/iot/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions examples/django/iot/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions examples/django/iot/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class IotConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "iot"
Empty file.
3 changes: 3 additions & 0 deletions examples/django/iot/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions examples/django/iot/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
19 changes: 19 additions & 0 deletions examples/django/iot/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.http import JsonResponse
from iotcore import IotCore

iot = IotCore()
iot.background_loop_forever()


def mqtt_callback(data):
print(f"Django >: {data}")


def subscribe(request):
iot.subscribe("iot", mqtt_callback)
return JsonResponse({"response": "subscribed"})


def publish(request):
iot.publish("iot", "demo")
return JsonResponse({"response": "published"})
5 changes: 3 additions & 2 deletions iotcore/djangoiot/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
class IotConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "iotcore.djangoiot"
iotcore_instance = None

def ready(self):
server_status = os.environ.get("MQTT_SERVER_RUNNING", None)
if server_status is None:
print("Starting MQTT server!")
os.environ["MQTT_SERVER_RUNNING"] = "true"
iot = IotCore()
iot.background_loop_forever()
self.iotcore_instance = IotCore()
self.iotcore_instance.background_loop_forever()
else:
print("Server already running!")

0 comments on commit 67ecc01

Please sign in to comment.