File tree Expand file tree Collapse file tree 10 files changed +42
-3
lines changed
Expand file tree Collapse file tree 10 files changed +42
-3
lines changed Original file line number Diff line number Diff line change 3737 "django.contrib.sessions" ,
3838 "django.contrib.messages" ,
3939 "django.contrib.staticfiles" ,
40- "iotcore.djangoiot"
40+ # "iotcore.djangoiot", # Broker only
41+ "iot"
4142]
4243
4344MIDDLEWARE = [
Original file line number Diff line number Diff line change 1616"""
1717from django .contrib import admin
1818from django .urls import path
19+ from iot import views
1920
2021urlpatterns = [
2122 path ("admin/" , admin .site .urls ),
23+ path ("sub" , views .subscribe ),
24+ path ("pub" , views .publish ),
2225]
Original file line number Diff line number Diff line change 1+ from django .contrib import admin
2+
3+ # Register your models here.
Original file line number Diff line number Diff line change 1+ from django .apps import AppConfig
2+
3+
4+ class IotConfig (AppConfig ):
5+ default_auto_field = "django.db.models.BigAutoField"
6+ name = "iot"
Original file line number Diff line number Diff line change 1+ from django .db import models
2+
3+ # Create your models here.
Original file line number Diff line number Diff line change 1+ from django .test import TestCase
2+
3+ # Create your tests here.
Original file line number Diff line number Diff line change 1+ from django .http import JsonResponse
2+ from iotcore import IotCore
3+
4+ iot = IotCore ()
5+ iot .background_loop_forever ()
6+
7+
8+ def mqtt_callback (data ):
9+ print (f"Django >: { data } " )
10+
11+
12+ def subscribe (request ):
13+ iot .subscribe ("iot" , mqtt_callback )
14+ return JsonResponse ({"response" : "subscribed" })
15+
16+
17+ def publish (request ):
18+ iot .publish ("iot" , "demo" )
19+ return JsonResponse ({"response" : "published" })
Original file line number Diff line number Diff line change 66class IotConfig (AppConfig ):
77 default_auto_field = "django.db.models.BigAutoField"
88 name = "iotcore.djangoiot"
9+ iotcore_instance = None
910
1011 def ready (self ):
1112 server_status = os .environ .get ("MQTT_SERVER_RUNNING" , None )
1213 if server_status is None :
1314 print ("Starting MQTT server!" )
1415 os .environ ["MQTT_SERVER_RUNNING" ] = "true"
15- iot = IotCore ()
16- iot .background_loop_forever ()
16+ self . iotcore_instance = IotCore ()
17+ self . iotcore_instance .background_loop_forever ()
1718 else :
1819 print ("Server already running!" )
You can’t perform that action at this time.
0 commit comments