diff --git a/Cargo.toml b/Cargo.toml index 556fd59..54f19d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "iotcore" -version = "0.2.0" +version = "0.2.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 77b51f6..a737860 100644 --- a/README.md +++ b/README.md @@ -100,10 +100,25 @@ def pub(): Then add iotcore to the django apps as below in the settings.py file of your project ```python -INSTALLED_APPS = [ - "Other Apps here", - "iotcore.djangoiot" -] +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"}) ``` Now Connect to mqtt broker on localhost diff --git a/docs/index.md b/docs/index.md index 77b51f6..a737860 100644 --- a/docs/index.md +++ b/docs/index.md @@ -100,10 +100,25 @@ def pub(): Then add iotcore to the django apps as below in the settings.py file of your project ```python -INSTALLED_APPS = [ - "Other Apps here", - "iotcore.djangoiot" -] +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"}) ``` Now Connect to mqtt broker on localhost diff --git a/examples/django/develop/settings.py b/examples/django/develop/settings.py index c291672..bd19f5a 100644 --- a/examples/django/develop/settings.py +++ b/examples/django/develop/settings.py @@ -37,7 +37,6 @@ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", - # "iotcore.djangoiot", # Broker only "iot" ] diff --git a/iotcore/djangoiot/apps.py b/iotcore/djangoiot/apps.py index b821053..481642d 100644 --- a/iotcore/djangoiot/apps.py +++ b/iotcore/djangoiot/apps.py @@ -6,14 +6,13 @@ 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" - self.iotcore_instance = IotCore() - self.iotcore_instance.background_loop_forever() - else: - print("Server already running!") + # 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() + # else: + # print("Server already running!") diff --git a/src/core.rs b/src/core.rs index 3a05eef..f75432b 100644 --- a/src/core.rs +++ b/src/core.rs @@ -24,13 +24,24 @@ pub struct IotCoreRs { } +fn port_available(port: u16) -> bool { + match TcpListener::bind(("127.0.0.1", port)) { + Ok(_) => { true } + Err(_) => { + println!("Port not available"); + false + } + } +} + #[pymethods] impl IotCoreRs { #[new] fn new(host: &str, port: u16, callback: PyObject) -> Self { let (host, port) = { if host == "localhost" { - start_mqtt_broker(); + let port_status = port_available(1883); + if port_status { start_mqtt_broker() }; ("localhost", 1883) } else { (host, port) @@ -73,13 +84,6 @@ impl IotCoreRs { Self { client, callback, tx, rx: rx_arc } } - fn is_port_available(&mut self, port: u16) -> bool { - match TcpListener::bind(("127.0.0.1", port)) { - Ok(_) => { true } - Err(_) => { false } - } - } - fn publish(&mut self, topic: &str, data: &str) -> PyResult<()> { let topic_to_be_subscribed = topic.to_owned(); self.client