File tree Expand file tree Collapse file tree 6 files changed +60
-28
lines changed
Expand file tree Collapse file tree 6 files changed +60
-28
lines changed Original file line number Diff line number Diff line change 11[package ]
22name = " iotcore"
3- version = " 0.2.0 "
3+ version = " 0.2.1 "
44edition = " 2021"
55
66# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Original file line number Diff line number Diff line change @@ -100,10 +100,25 @@ def pub():
100100
101101Then add iotcore to the django apps as below in the settings.py file of your project
102102``` python
103- INSTALLED_APPS = [
104- " Other Apps here" ,
105- " iotcore.djangoiot"
106- ]
103+ from django.http import JsonResponse
104+ from iotcore import IotCore
105+
106+ iot = IotCore()
107+ iot.background_loop_forever()
108+
109+
110+ def mqtt_callback (data ):
111+ print (f " Django >: { data} " )
112+
113+
114+ def subscribe (request ):
115+ iot.subscribe(" iot" , mqtt_callback)
116+ return JsonResponse({" response" : " subscribed" })
117+
118+
119+ def publish (request ):
120+ iot.publish(" iot" , " demo" )
121+ return JsonResponse({" response" : " published" })
107122```
108123
109124Now Connect to mqtt broker on localhost
Original file line number Diff line number Diff line change @@ -100,10 +100,25 @@ def pub():
100100
101101Then add iotcore to the django apps as below in the settings.py file of your project
102102``` python
103- INSTALLED_APPS = [
104- " Other Apps here" ,
105- " iotcore.djangoiot"
106- ]
103+ from django.http import JsonResponse
104+ from iotcore import IotCore
105+
106+ iot = IotCore()
107+ iot.background_loop_forever()
108+
109+
110+ def mqtt_callback (data ):
111+ print (f " Django >: { data} " )
112+
113+
114+ def subscribe (request ):
115+ iot.subscribe(" iot" , mqtt_callback)
116+ return JsonResponse({" response" : " subscribed" })
117+
118+
119+ def publish (request ):
120+ iot.publish(" iot" , " demo" )
121+ return JsonResponse({" response" : " published" })
107122```
108123
109124Now Connect to mqtt broker on localhost
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", # Broker only
4140 "iot"
4241]
4342
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
109
11- def ready (self ):
12- server_status = os .environ .get ("MQTT_SERVER_RUNNING" , None )
13- if server_status is None :
14- print ("Starting MQTT server!" )
15- os .environ ["MQTT_SERVER_RUNNING" ] = "true"
16- self . iotcore_instance = IotCore ()
17- self . iotcore_instance .background_loop_forever ()
18- else :
19- print ("Server already running!" )
10+ # def ready(self):
11+ # server_status = os.environ.get("MQTT_SERVER_RUNNING", None)
12+ # if server_status is None:
13+ # print("Starting MQTT server!")
14+ # os.environ["MQTT_SERVER_RUNNING"] = "true"
15+ # iot = IotCore()
16+ # iot .background_loop_forever()
17+ # else:
18+ # print("Server already running!")
Original file line number Diff line number Diff line change @@ -24,13 +24,24 @@ pub struct IotCoreRs {
2424}
2525
2626
27+ fn port_available ( port : u16 ) -> bool {
28+ match TcpListener :: bind ( ( "127.0.0.1" , port) ) {
29+ Ok ( _) => { true }
30+ Err ( _) => {
31+ println ! ( "Port not available" ) ;
32+ false
33+ }
34+ }
35+ }
36+
2737#[ pymethods]
2838impl IotCoreRs {
2939 #[ new]
3040 fn new ( host : & str , port : u16 , callback : PyObject ) -> Self {
3141 let ( host, port) = {
3242 if host == "localhost" {
33- start_mqtt_broker ( ) ;
43+ let port_status = port_available ( 1883 ) ;
44+ if port_status { start_mqtt_broker ( ) } ;
3445 ( "localhost" , 1883 )
3546 } else {
3647 ( host, port)
@@ -73,13 +84,6 @@ impl IotCoreRs {
7384 Self { client, callback, tx, rx : rx_arc }
7485 }
7586
76- fn is_port_available ( & mut self , port : u16 ) -> bool {
77- match TcpListener :: bind ( ( "127.0.0.1" , port) ) {
78- Ok ( _) => { true }
79- Err ( _) => { false }
80- }
81- }
82-
8387 fn publish ( & mut self , topic : & str , data : & str ) -> PyResult < ( ) > {
8488 let topic_to_be_subscribed = topic. to_owned ( ) ;
8589 self . client
You can’t perform that action at this time.
0 commit comments