From 3fdfa20f054e22bed1da3b1b85e6dc7c3de59d1d Mon Sep 17 00:00:00 2001 From: Jonathan Baecker Date: Wed, 29 Apr 2020 10:30:17 +0200 Subject: [PATCH] send text messages to playout --- ffplayout/api/utils.py | 43 ++++++++++++++++++++++++++++ ffplayout/api/views.py | 12 +++++++- ffplayout/ffplayout/settings.py | 6 ++++ ffplayout/ffplayout/urls.py | 1 + ffplayout/frontend/pages/message.vue | 20 ++++++++++--- 5 files changed, 77 insertions(+), 5 deletions(-) diff --git a/ffplayout/api/utils.py b/ffplayout/api/utils.py index 5d55bd1e..737c2a03 100644 --- a/ffplayout/api/utils.py +++ b/ffplayout/api/utils.py @@ -6,6 +6,8 @@ from time import sleep import psutil import yaml +import zmq +from django.conf import settings from pymediainfo import MediaInfo from api.models import GuiSettings @@ -56,6 +58,47 @@ def read_log(type): return log.read().strip() +def send_message(data): + config = read_yaml() + address, port = config['text']['bind_address'].split(':') + + context = zmq.Context(1) + client = context.socket(zmq.REQ) + client.connect('tcp://{}:{}'.format(address, port)) + + poll = zmq.Poller() + poll.register(client, zmq.POLLIN) + + request = '' + reply_msg = '' + + for key, value in data.items(): + request += "{}='{}':".format(key, value) + + request = "{} reinit {}".format(settings.DRAW_TEXT_NODE, request.rstrip(':')) + + client.send_string(request) + + socks = dict(poll.poll(settings.REQUEST_TIMEOUT)) + + if socks.get(client) == zmq.POLLIN: + reply = client.recv() + + if reply and reply.decode() == '0 Success': + reply_msg = reply.decode() + else: + reply_msg = reply.decode() + else: + reply_msg = 'No response from server' + + client.setsockopt(zmq.LINGER, 0) + client.close() + poll.unregister(client) + + context.term() + return {'Success': reply_msg} + + def sizeof_fmt(num, suffix='B'): for unit in ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi']: if abs(num) < 1024.0: diff --git a/ffplayout/api/views.py b/ffplayout/api/views.py index 9cde384f..cb3a5bf2 100644 --- a/ffplayout/api/views.py +++ b/ffplayout/api/views.py @@ -13,7 +13,7 @@ from rest_framework.response import Response from rest_framework.views import APIView from .utils import (PlayoutService, SystemStats, get_media_path, read_json, - read_yaml, write_json, write_yaml, read_log) + read_yaml, write_json, write_yaml, read_log, send_message) class CurrentUserView(APIView): @@ -58,6 +58,16 @@ class MessengerViewSet(viewsets.ModelViewSet): filterset_class = MessengerFilter +class MessegeSender(APIView): + + def post(self, request, *args, **kwargs): + if 'data' in request.data: + response = send_message(request.data['data']) + return Response({"success": True, 'status': response}) + + return Response({"success": False}) + + class Config(APIView): """ read and write config from ffplayout engine diff --git a/ffplayout/ffplayout/settings.py b/ffplayout/ffplayout/settings.py index 1019a5c6..bbaaca67 100644 --- a/ffplayout/ffplayout/settings.py +++ b/ffplayout/ffplayout/settings.py @@ -148,3 +148,9 @@ USE_TZ = True # https://docs.djangoproject.com/en/3.0/howto/static-files/ STATIC_URL = '/static/' + +# ffmpeg filter node, needs to be edit only when the filter chain changes +DRAW_TEXT_NODE = 'Parsed_drawtext_2' + +# zmq settings +REQUEST_TIMEOUT = 1000 diff --git a/ffplayout/ffplayout/urls.py b/ffplayout/ffplayout/urls.py index fa7540c2..7c48bd0d 100644 --- a/ffplayout/ffplayout/urls.py +++ b/ffplayout/ffplayout/urls.py @@ -39,6 +39,7 @@ urlpatterns = [ path('api/log/', views.LogReader.as_view()), path('api/current/user/', views.CurrentUserView.as_view()), path('api/media/', views.Media.as_view()), + path('api/send/', views.MessegeSender.as_view()), re_path(r'^api/media/upload/(?P[^/]+)$', views.FileUpload.as_view()), path('api/media/op/', views.FileOperations.as_view()), path('api-auth/', include( diff --git a/ffplayout/frontend/pages/message.vue b/ffplayout/frontend/pages/message.vue index c988107b..d8feda56 100644 --- a/ffplayout/frontend/pages/message.vue +++ b/ffplayout/frontend/pages/message.vue @@ -189,10 +189,10 @@ - + Sending success... - + Sending failed... @@ -403,7 +403,8 @@ export default { this.getPreset('') }, - submitMessage () { + async submitMessage () { + await this.$store.dispatch('auth/inspectToken') function aToHex (num) { return '0x' + Math.round(num * 255).toString(16) } @@ -420,7 +421,18 @@ export default { boxcolor: this.form.boxColor + '@' + aToHex(this.form.boxAlpha), boxborderw: this.form.border } - console.log(obj) + + const response = await this.$axios.post( + 'api/send/', + { data: obj }, + { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } + ) + + if (response.data && response.data.status.Success && response.data.status.Success === '0 Success') { + this.success = true + } else { + this.failed = true + } } } }