MythTV  master
websocket_mythevent.cpp
Go to the documentation of this file.
1 
2 #include "websocket_mythevent.h"
3 #include "mythcorecontext.h"
4 #include "mythevent.h"
5 #include "mythlogging.h"
6 
8 {
9  setObjectName("WebSocketMythEvent");
11 }
12 
14 {
16 }
17 
19 {
20  QString message = QString(frame.m_payload);
21 
22  if (message.isEmpty())
23  return false;
24 
25 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
26  QStringList tokens = message.split(" ", QString::SkipEmptyParts);
27 #else
28  QStringList tokens = message.split(" ", Qt::SkipEmptyParts);
29 #endif
30 
31  if (tokens[0] == "WS_EVENT_ENABLE") // Only send events if asked
32  {
33  m_sendEvents = true;
34  LOG(VB_HTTP, LOG_NOTICE, "WebSocketMythEvent: Enabled");
35  }
36  else if (tokens[0] == "WS_EVENT_DISABLE")
37  {
38  m_sendEvents = false;
39  LOG(VB_HTTP, LOG_NOTICE, "WebSocketMythEvent: Disabled");
40  }
41  else if (tokens[0] == "WS_EVENT_SET_FILTER")
42  {
43  m_filters.clear();
44 
45  if (tokens.length() == 1)
46  return true;
47 
48  m_filters = tokens.mid(1);
49 
50  QString filterString = m_filters.join(", ");
51  LOG(VB_HTTP, LOG_NOTICE, QString("WebSocketMythEvent: Updated filters (%1)").arg(filterString));
52  }
53 
54  return false;
55 }
56 
58 {
59  if (event->type() == MythEvent::MythEventMessage)
60  {
61  if (!m_sendEvents)
62  return;
63 
64  auto *me = dynamic_cast<MythEvent *>(event);
65  if (me == nullptr)
66  return;
67  QString message = me->Message();
68 
69  if (message.startsWith("SYSTEM_EVENT"))
70  message.remove(0, 13); // Strip SYSTEM_EVENT from the frontend, it's not useful
71 
72 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
73  QStringList tokens = message.split(" ", QString::SkipEmptyParts);
74 #else
75  QStringList tokens = message.split(" ", Qt::SkipEmptyParts);
76 #endif
77 
78  if (tokens.isEmpty())
79  return;
80 
81  // If no-one is listening for this event, then ignore it
82  if (!m_filters.contains("ALL") && !m_filters.contains(tokens[0]))
83  return;
84 
85  SendTextMessage(message);
86  }
87 }
mythevent.h
MythEvent::MythEventMessage
static Type MythEventMessage
Definition: mythevent.h:73
WebSocketFrame
A representation of a single WebSocket frame.
Definition: websocket.h:75
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:16
WebSocketMythEvent::~WebSocketMythEvent
~WebSocketMythEvent() override
Definition: websocket_mythevent.cpp:13
arg
arg(title).arg(filename).arg(doDelete))
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythEvent::Message
const QString & Message() const
Definition: mythevent.h:65
MythObservable::addListener
void addListener(QObject *listener)
Add a listener to the observable.
Definition: mythobservable.cpp:38
mythlogging.h
WebSocketExtension::SendTextMessage
void SendTextMessage(const QString &)
WebSocketFrame::m_payload
QByteArray m_payload
Definition: websocket.h:114
WebSocketMythEvent::m_filters
QStringList m_filters
Definition: websocket_mythevent.h:26
WebSocketMythEvent::customEvent
void customEvent(QEvent *) override
Definition: websocket_mythevent.cpp:57
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
WebSocketMythEvent::WebSocketMythEvent
WebSocketMythEvent()
Definition: websocket_mythevent.cpp:7
WebSocketMythEvent::m_sendEvents
bool m_sendEvents
Definition: websocket_mythevent.h:27
mythcorecontext.h
websocket_mythevent.h
MythObservable::removeListener
void removeListener(QObject *listener)
Remove a listener to the observable.
Definition: mythobservable.cpp:55
WebSocketMythEvent::HandleTextFrame
bool HandleTextFrame(const WebSocketFrame &frame) override
Definition: websocket_mythevent.cpp:18