diff --git a/frontend/src/__apps/painel/main.js b/frontend/src/__apps/painel/main.js index dfa0651ce..df2e4879a 100644 --- a/frontend/src/__apps/painel/main.js +++ b/frontend/src/__apps/painel/main.js @@ -242,24 +242,8 @@ const v = new Vue({ // eslint-disable-line this.started = setInterval(() => { this.clockRunning(crono) }, 100) - }, - call_data: function call_data () { - socket.send('Calling Data...') - }, - pollData () { - /* this.fetchData() */ - this.polling = setInterval(() => { - // console.info('Fetching data from backend') - this.call_data() - this.fetchData() - /* console.log(this.teste) */ - }, 1000) } }, - beforeDestroy () { - console.info('Destroying polling.') - clearInterval(this.polling) - }, created () { socket.onopen = function (e) { console.log('Connection established') @@ -269,14 +253,12 @@ const v = new Vue({ // eslint-disable-line socket.onmessage = function (e) { _this.teste = JSON.parse(e.data) + _this.fetchData() console.log('Data Received...') } socket.onclose = function (e) { console.error('Ws fechou!') } - - console.info('Start polling data...') - this.pollData() } }) diff --git a/sapl/celery.py b/sapl/celery.py index af9d36f2f..495747130 100644 --- a/sapl/celery.py +++ b/sapl/celery.py @@ -8,10 +8,10 @@ app = Celery('sapl') app.config_from_object('django.conf:settings', namespace='CELERY') app.conf.beat_schedule = { - 'get_cronometro_inst': { - 'task': 'sapl.painel.tasks.get_cronometro', - 'schedule': 1.0 + 'get_dados_inst': { + 'task': 'sapl.painel.tasks.get_dados_painel_celery', + 'schedule': 0.2 } } -app.autodiscover_tasks() +app.autodiscover_tasks() \ No newline at end of file diff --git a/sapl/painel/consumers.py b/sapl/painel/consumers.py index ff546241c..0970b319b 100644 --- a/sapl/painel/consumers.py +++ b/sapl/painel/consumers.py @@ -8,18 +8,14 @@ from sapl.painel import views class PainelConsumer(AsyncJsonWebsocketConsumer): async def connect (self): print('Conectado') + await self.channel_layer.group_add('painel', self.channel_name) await self.accept() - async def disconnect(self, close_code): - print('Desconectado:', close_code) - pass + async def disconnect(self, code): + await self.channel_layer.group_discard('painel', self.channel_name) - async def receive(self, text_data): - await self.send_data(786) - - async def send_data(self, id): - response = views.get_dados_painel(id) - - await self.send_json(response) + async def send_data(self, event): + new_data = event['message'] + await self.send(json.dumps(new_data)) \ No newline at end of file diff --git a/sapl/painel/tasks.py b/sapl/painel/tasks.py index 0d2a56a1c..2a0571135 100644 --- a/sapl/painel/tasks.py +++ b/sapl/painel/tasks.py @@ -1,27 +1,14 @@ -import json -import requests -import urllib - from celery import shared_task +from urllib import request from channels.layers import get_channel_layer from asgiref.sync import async_to_sync +from sapl.painel import views + channel_layer = get_channel_layer() @shared_task -def get_cronometro(): - url_dados = 'http://localhost:8000/painel/786/dados' - - - login_data = {'username': 'sapl', 'password': 'sapl'} - with requests.Session() as session: - session.get('http://localhost:8000/login/') - csrftoken = session.cookies['csrftoken'] - login_data['csrfmiddlewaretoken'] = csrftoken - post = session.post('http://localhost:8000/login/', data=login_data) - if post.ok: print('conexao realizada com sucesso') - r = session.get('http://localhost:8000/painel/786/dados') - json_data = r.json() - +def get_dados_painel_celery(): + json_data = views.get_dados_painel(786) async_to_sync(channel_layer.group_send)('painel', {'type':'send_data', 'message': json_data}) diff --git a/sapl/painel/views.py b/sapl/painel/views.py index 3a3d11d07..4f05ebdbe 100644 --- a/sapl/painel/views.py +++ b/sapl/painel/views.py @@ -348,7 +348,7 @@ def cronometro_painel(request): return HttpResponse({}) -def get_cronometro_status(name): +def get_cronometro_status(request, name): logger = logging.getLogger(__name__) username = request.user.username try: