From 4069b94f6b55faa9adedce36c7f40d6de5de16d9 Mon Sep 17 00:00:00 2001 From: AlGouvea Date: Thu, 23 Sep 2021 12:27:57 -0300 Subject: [PATCH] =?UTF-8?q?Reccep=C3=A7=C3=A3o=20de=20dados=20no=20VueJS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/__apps/painel/main.js | 12 ++++----- sapl/painel/consumers.py | 43 +++++++++++++++++++----------- sapl/painel/tasks.py | 2 +- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/frontend/src/__apps/painel/main.js b/frontend/src/__apps/painel/main.js index 52e18fc96..6d9c6ef72 100644 --- a/frontend/src/__apps/painel/main.js +++ b/frontend/src/__apps/painel/main.js @@ -250,14 +250,14 @@ const v = new Vue({ // eslint-disable-line }, 100) }, foi: function foi () { - socket.send('teste') + socket.send('Teste de entrada') }, pollData () { this.fetchData() this.polling = setInterval(() => { // console.info('Fetching data from backend') - this.fetchData() - }, 100) + this.foi() + }, 30000) } }, beforeDestroy () { @@ -269,11 +269,9 @@ const v = new Vue({ // eslint-disable-line console.log('Connection established') } - const _this = this - socket.onmessage = function (e) { - _this.teste = e.data - console.log(_this.teste) + this.teste = JSON.parse(e.data) + console.log(this.teste.sessao_plenaria) } socket.onclose = function (e) { diff --git a/sapl/painel/consumers.py b/sapl/painel/consumers.py index 7153bdb09..91a437dfa 100644 --- a/sapl/painel/consumers.py +++ b/sapl/painel/consumers.py @@ -1,29 +1,42 @@ import json import requests +from asgiref.sync import async_to_sync from channels.generic.websocket import AsyncJsonWebsocketConsumer - +from sapl.sessao.models import (ExpedienteMateria, OradorExpediente, OrdemDia, + PresencaOrdemDia, RegistroVotacao, + SessaoPlenaria, SessaoPlenariaPresenca, + VotoParlamentar, RegistroLeitura) class PainelConsumer(AsyncJsonWebsocketConsumer): async def connect (self): + print('Conectado') await self.accept() async def disconnect(self, close_code): + print('Disconectado:', close_code) pass - async def receive(self, text_data): - print('Received Message') - - print('Enviando...') - url_dados = 'http://localhost:8000/painel/786/dados' + async def join_group(self): + print('Group') + await self.channel_layer.group_add('painel', self.channel_name) - #response = requests.get(url_dados) - #print(response) - - await self.send_json({ - 'message': 'teste' - }) - - async def send(self, event): - print('Entrou') + async def receive(self, text_data): + print('Received Message:' + text_data) + + await self.send_data(786) + + async def send_data(self, id): + sessao = SessaoPlenaria.objects.get(id=id) + + response = { + 'sessao_plenaria': str(sessao), + 'sessao_plenaria_data': sessao.data_inicio.strftime('%d/%m/%Y'), + 'sessao_plenaria_hora_inicio': sessao.hora_inicio, + 'sessao_solene': sessao.tipo.nome == "Solene", + 'sessao_finalizada': sessao.finalizada, + 'tema_solene': sessao.tema_solene, + 'status_painel': sessao.painel_aberto, + } + await self.send_json(response) \ No newline at end of file diff --git a/sapl/painel/tasks.py b/sapl/painel/tasks.py index befd8442c..0d2a56a1c 100644 --- a/sapl/painel/tasks.py +++ b/sapl/painel/tasks.py @@ -24,4 +24,4 @@ def get_cronometro(): r = session.get('http://localhost:8000/painel/786/dados') json_data = r.json() - async_to_sync(channel_layer.group_send)('message', {'type':'send_message', 'text': json_data}) + async_to_sync(channel_layer.group_send)('painel', {'type':'send_data', 'message': json_data})