MythTV  master
mythwebsocketevent.cpp
Go to the documentation of this file.
2 #include "mythcorecontext.h"
3 #include "mythlogging.h"
4 
6 {
7  setObjectName("MythWebSocketEvent");
9 }
10 
12 {
14 }
15 
17 {
18  // TODO: Payloads potentially has multiple messages to process
19  // We only handle the first one here.
21  (*p_message).append(*(Payloads.front()));
22  return HandleTextMessage(p_message);
23 }
24 
26 {
27  const QString& message = *Text;
28 
29  if (message.isEmpty())
30  return false;
31 
32  QStringList tokens = message.split(" ", Qt::SkipEmptyParts);
33  if (tokens[0] == "WS_EVENT_ENABLE") // Only send events if asked
34  {
35  m_sendEvents = true;
36  LOG(VB_HTTP, LOG_NOTICE, "WebSocketMythEvent: Enabled");
37  }
38  else if (tokens[0] == "WS_EVENT_DISABLE")
39  {
40  m_sendEvents = false;
41  LOG(VB_HTTP, LOG_NOTICE, "WebSocketMythEvent: Disabled");
42  }
43  else if (tokens[0] == "WS_EVENT_SET_FILTER")
44  {
45  m_filters.clear();
46 
47  if (tokens.length() == 1)
48  return true;
49 
50  m_filters = tokens.mid(1);
51 
52  QString filterString = m_filters.join(", ");
53  LOG(VB_HTTP, LOG_NOTICE, QString("WebSocketMythEvent: Updated filters (%1)").arg(filterString));
54  }
55 
56  return false;
57 }
58 
60 {
61  if (event->type() == MythEvent::kMythEventMessage)
62  {
63  if (!m_sendEvents)
64  return;
65 
66  auto *me = dynamic_cast<MythEvent *>(event);
67  if (me == nullptr)
68  return;
69  QString message = me->Message();
70 
71  if (message.startsWith("SYSTEM_EVENT"))
72  message.remove(0, 13); // Strip SYSTEM_EVENT from the frontend, it's not useful
73 
74  QStringList tokens = message.split(" ", Qt::SkipEmptyParts);
75  if (tokens.isEmpty())
76  return;
77 
78  // If no-one is listening for this event, then ignore it
79  if (!m_filters.contains("ALL") && !m_filters.contains(tokens[0]))
80  return;
81 
82  emit SendTextMessage(message);
83  }
84 }
StringPayload
std::shared_ptr< MythSharedString > StringPayload
Definition: mythhttpcommon.h:56
MythEvent::kMythEventMessage
static const Type kMythEventMessage
Definition: mythevent.h:79
MythWebSocketEvent::m_filters
QStringList m_filters
Definition: mythwebsocketevent.h:25
MythSharedString::CreateString
static StringPayload CreateString()
Definition: mythhttpcommon.cpp:37
MythWebSocketEvent::customEvent
void customEvent(QEvent *) override
Definition: mythwebsocketevent.cpp:59
MythEvent
This class is used as a container for messages.
Definition: mythevent.h:16
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythWebSocketEvent::HandleRawTextMessage
bool HandleRawTextMessage(const DataPayloads &Payloads)
Definition: mythwebsocketevent.cpp:16
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
MythWebSocketEvent::MythWebSocketEvent
MythWebSocketEvent()
Definition: mythwebsocketevent.cpp:5
mythlogging.h
MythWebSocketEvent::SendTextMessage
void SendTextMessage(const QString &)
MythWebSocketEvent::HandleTextMessage
bool HandleTextMessage(const StringPayload &Text)
Definition: mythwebsocketevent.cpp:25
MythWebSocketEvent::~MythWebSocketEvent
~MythWebSocketEvent() override
Definition: mythwebsocketevent.cpp:11
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
mythcorecontext.h
mythwebsocketevent.h
MythObservable::removeListener
void removeListener(QObject *listener)
Remove a listener to the observable.
Definition: mythobservable.cpp:55
MythWebSocketEvent::m_sendEvents
bool m_sendEvents
Definition: mythwebsocketevent.h:26
DataPayloads
std::vector< DataPayload > DataPayloads
Definition: mythhttpcommon.h:37