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  QStringList tokens = message.split(" ", QString::SkipEmptyParts);
26 
27  if (tokens[0] == "WS_EVENT_ENABLE") // Only send events if asked
28  {
29  m_sendEvents = true;
30  LOG(VB_HTTP, LOG_NOTICE, "WebSocketMythEvent: Enabled");
31  }
32  else if (tokens[0] == "WS_EVENT_DISABLE")
33  {
34  m_sendEvents = false;
35  LOG(VB_HTTP, LOG_NOTICE, "WebSocketMythEvent: Disabled");
36  }
37  else if (tokens[0] == "WS_EVENT_SET_FILTER")
38  {
39  m_filters.clear();
40 
41  if (tokens.length() == 1)
42  return true;
43 
44  m_filters = tokens.mid(1);
45 
46  QString filterString = m_filters.join(", ");
47  LOG(VB_HTTP, LOG_NOTICE, QString("WebSocketMythEvent: Updated filters (%1)").arg(filterString));
48  }
49 
50  return false;
51 }
52 
54 {
55  if (event->type() == MythEvent::MythEventMessage)
56  {
57  if (!m_sendEvents)
58  return;
59 
60  MythEvent *me = static_cast<MythEvent *>(event);
61  QString message = me->Message();
62 
63  if (message.startsWith("SYSTEM_EVENT"))
64  message.remove(0, 13); // Strip SYSTEM_EVENT from the frontend, it's not useful
65 
66  QStringList tokens = message.split(" ", QString::SkipEmptyParts);
67 
68  if (tokens.isEmpty())
69  return;
70 
71  // If no-one is listening for this event, then ignore it
72  if (!m_filters.contains("ALL") && !m_filters.contains(tokens[0]))
73  return;
74 
75  SendTextMessage(message);
76  }
77 }
static Type MythEventMessage
Definition: mythevent.h:66
void removeListener(QObject *listener)
Remove a listener to the observable.
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
bool HandleTextFrame(const WebSocketFrame &frame) override
void addListener(QObject *listener)
Add a listener to the observable.
void SendTextMessage(const QString &)
QByteArray m_payload
Definition: websocket.h:114
This class is used as a container for messages.
Definition: mythevent.h:16
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void customEvent(QEvent *) override
const QString & Message() const
Definition: mythevent.h:58
A representation of a single WebSocket frame.
Definition: websocket.h:75