Skip to content

Commit

Permalink
Merge pull request #45 from tomvictor/feature/djangofix
Browse files Browse the repository at this point in the history
add port guard
  • Loading branch information
tomvictor authored Sep 15, 2023
2 parents 67ecc01 + 0bccaf5 commit a064733
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
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.2.0"
version = "0.2.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
23 changes: 19 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 19 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion examples/django/develop/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# "iotcore.djangoiot", # Broker only
"iot"
]

Expand Down
19 changes: 9 additions & 10 deletions iotcore/djangoiot/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
20 changes: 12 additions & 8 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a064733

Please sign in to comment.