Skip to content

Commit

Permalink
ajout endpoint sse Data
Browse files Browse the repository at this point in the history
  • Loading branch information
enzofrnt committed Jan 31, 2024
1 parent 67d6fd0 commit 40052d5
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 195 deletions.
3 changes: 2 additions & 1 deletion backend/API/app/signals/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .datasignals import data_post_save
from .datasignals import data_post_save
from .sensorsignals import sensor_post_save
17 changes: 16 additions & 1 deletion backend/API/app/signals/datasignals.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ def data_post_save(sender, instance, **kwargs):

fields_data.pop('sensor', None)

redis_sender(f'Data/{sensor.room}', fields_data)


if sensor.room is not None:
redis_sender(f'Data', {"room" : sensor.room, "data" : fields_data})
logger.debug("Data")

redis_sender(f'Data/{sensor.room}/', fields_data)
logger.debug(f"Data/{sensor.room}/")

if sensor.building is not None and sensor.room is not None :
redis_sender(f'Data/{sensor.building}/', {"room" : sensor.room, "data" : fields_data})
logger.debug(f"Data/{sensor.building}/")

if sensor.floor is not None and sensor.building is not None and sensor.room is not None:
redis_sender(f'Data/{sensor.building}/{sensor.floor}/', {"room" : sensor.room, "data" : fields_data})
logger.debug(f"Data/{sensor.building}/{sensor.floor}/")

logger.debug(f"Data sent to Redis on {sensor.room}")
14 changes: 14 additions & 0 deletions backend/API/app/signals/sensorsignals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import logging
import json

from django.db.models.signals import post_save
from django.dispatch import receiver
from app.models import Data, Sensor
from app.usecases.redis import redis_sender
from django.core import serializers

logger = logging.getLogger('API')

@receiver(post_save, sender=Sensor)
def sensor_post_save(sender, instance, **kwargs):
pass
14 changes: 13 additions & 1 deletion backend/API/app/usecases/redis/redislistener.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,27 @@ def redis_listener(stop_event):
logger.debug(f"Message redis reçu: {message_data}")

if 'Data' in message_data['type']:
logger.debug("Data received from Redis")
logger.debug(f"Data received from Redis on {message_data['type']}")
formatted_message = message_data['message']
send_event(message_data['type'], 'message', formatted_message)
logger.debug(f"Event sent on {message_data['type']}")

elif message_data['type'] == 'Data':
logger.debug("Data received from Redis NoRoom")
formatted_message = message_data['message']
logger.debug(f"Event sent on Data")
send_event('Data', 'message', formatted_message)

elif 'Sensor' in message_data['type']:
logger.debug("Sensor received from Redis")
formatted_message = message_data['message']
logger.debug(f"Event sent on Sensor")
send_event(message_data['type'], 'message', formatted_message)

elif message_data['type'] == 'Sensor':
logger.debug("Sensor received from Redis NoRoom")
formatted_message = message_data['message']
logger.debug(f"Event sent on Sensor")
send_event('Sensor', 'message', formatted_message)

listener.unsubscribe('sse')
Expand Down
6 changes: 3 additions & 3 deletions backend/API/gunicorn_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os

from project.loggerconfig import setup_logger, setup_gunicorn_loggers
from project.settings.prod_settings import DEFAULT_IP, DEFAULT_PORT, CERTFILE, KEYFILE
from project.settings.prod_settings import DEFAULT_IP, DEFAULT_PORT#, CERTFILE, KEYFILE
from app.processmqttlistenerstarter import start_process_mqtt_listener
from app.threadredislistenerstarter import start_thread_redis_listener
from django.core.wsgi import get_wsgi_application
Expand All @@ -11,8 +11,8 @@
bind = f"{DEFAULT_IP}:{DEFAULT_PORT}"
custom_logger = setup_logger()
workers = multiprocessing.cpu_count() * 2
certfile = CERTFILE
keyfile = KEYFILE
# certfile = CERTFILE
# keyfile = KEYFILE

def on_starting(server):
setup_gunicorn_loggers(custom_logger)
Expand Down
50 changes: 36 additions & 14 deletions backend/API/project/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,47 @@
path('Events/Sensor/', AuthMiddlewareStack(
URLRouter(django_eventstream.routing.urlpatterns)
), {'format-channels': ['Sensor']}),

path('Events/Data/', AuthMiddlewareStack(
URLRouter(django_eventstream.routing.urlpatterns)
), {'format-channels': ['Data']}),
re_path(r'^Events/Sensor/(?P<channel_name>\w+)/$',
AuthMiddlewareStack(
URLRouter(
django_eventstream.routing.urlpatterns
)
),
{'format-channels': ['Sensor/{channel_name}']}

re_path(r'^Events/Sensor/(?P<RoomOrBuilding>\w+)/$',
AuthMiddlewareStack(
URLRouter(
django_eventstream.routing.urlpatterns
)
),
{'format-channels': ['Sensor/{RoomOrBuilding}/']}
),

re_path(r'^Events/Sensor/(?P<Building>\w+)/(?P<Floor>\w+)/$',
AuthMiddlewareStack(
URLRouter(
django_eventstream.routing.urlpatterns
)
),
{'format-channels': ['Sensor/{RoomOrBuilding}/{Floor}/']}
),
re_path(r'^Events/Data/(?P<channel_name>\w+)/$',
AuthMiddlewareStack(
URLRouter(
django_eventstream.routing.urlpatterns
)
),
{'format-channels': ['Data/{channel_name}']}

re_path(r'^Events/Data/(?P<RoomOrBuilding>\w+)/$',
AuthMiddlewareStack(
URLRouter(
django_eventstream.routing.urlpatterns
)
),
{'format-channels': ['Data/{RoomOrBuilding}/']}
),

re_path(r'^Events/Data/(?P<Building>\w+)/(?P<Floor>\w+)/$',
AuthMiddlewareStack(
URLRouter(
django_eventstream.routing.urlpatterns
)
),
{'format-channels': ['Data/{Building}/{Floor}/']}
),

re_path(r'', get_asgi_application()),
]),
})
2 changes: 1 addition & 1 deletion backend/API/project/settings/dev_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

ALLOWED_HOSTS = ['localhost', 'localhost:8080', 'localhost:5173', "http://localhost:8080", "http://localhost:5173", "http://localhost:5500", "localhost:5500" ]

LOGGING_LEVEL = logging.DEBUG if DEBUG else logging.INFO
LOGGING_LEVEL = logging.DEBUG if DEBUG else logging.INFO
6 changes: 2 additions & 4 deletions backend/API/project/settings/prod_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,5 @@
)
}

GRIP_URL = 'http://localhost:5561'

CERTFILE = os.environ.get('CERTFILE')
KEYFILE = os.environ.get('KEYFILE')
# CERTFILE = os.environ.get('CERTFILE')
# KEYFILE = os.environ.get('KEYFILE')
4 changes: 1 addition & 3 deletions backend/Dockerfile.Dev
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ COPY ./requirements.txt /API/
RUN pip install --no-cache-dir -r requirements.txt

# Installer redis et suppirmer le cache
RUN apt-get update && apt-get install -y redis-server pushpin && apt-get clean
RUN apt-get update && apt-get install -y redis-server && apt-get clean

RUN rm -rf /var/lib/apt/lists/*

COPY ./pushpin.conf /etc/pushpin/pushpin.conf

# Execute pour debug si nécessaire
# CMD [ "tail", "-f", "/dev/null" ]

Expand Down
4 changes: 1 addition & 3 deletions backend/Dockerfile.Prod
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ RUN pip install --no-cache-dir -r requirements.txt
# Ajouter uvicorn et gunicorn
RUN pip install uvicorn gunicorn

RUN apt-get update && apt-get install -y openssl redis-server pushpin && apt-get clean && rm -rf /var/lib/apt/lists/*

COPY ./pushpin.conf /etc/pushpin/pushpin.conf
RUN apt-get update && apt-get install -y openssl redis-server && apt-get clean && rm -rf /var/lib/apt/lists/*

# Création d'un répertoire pour le certificat et la clé
RUN mkdir -p /certificates
Expand Down
3 changes: 0 additions & 3 deletions backend/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
# Démarrer Redis en arrière-plan
redis-server --daemonize yes

# Démarrer Pushpin en arrière-plan
pushpin &

# Attendre que la base de données soit prête
python manage.py wait_for_db

Expand Down
160 changes: 0 additions & 160 deletions backend/pushpin.conf

This file was deleted.

2 changes: 1 addition & 1 deletion deploiement-prod/.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ DJANGO_PORT=8000
TZ=Europe/Paris

# Env variable for API url
API_URL=https://localhost:8000
API_URL=http://localhost:8000

# Définition des paramètres de l'application Django à utiliser
DJANGO_SETTINGS_MODULE=project.settings.prod_settings
Expand Down

0 comments on commit 40052d5

Please sign in to comment.