Skip to content

Commit

Permalink
Refactor EventThread before adding opiton to publish power
Browse files Browse the repository at this point in the history
  • Loading branch information
ghormann committed Mar 24, 2024
1 parent 6075c6f commit 3f385f4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 46 deletions.
100 changes: 56 additions & 44 deletions src/Events.cpp
Expand Up @@ -37,6 +37,41 @@ static std::list<EventHandler*> EVENT_HANDLERS;
static std::map<std::string, std::function<void(const std::string& topic, const std::string& payload)>> EVENT_CALLBACKS;
static std::thread* EVENT_PUBLISH_THREAD = nullptr;
static volatile bool EVENT_PUBLISH_THREAD_RUN = true;
static std::vector<EventNotifier*> eventNotifiers;

class PublishPlaylistStatus : public EventNotifier {
public:
PublishPlaylistStatus(int freq) :
EventNotifier(freq) {}
~PublishPlaylistStatus() {}

void notify() {
Json::Value json = Player::INSTANCE.GetMqttStatusJSON();
std::stringstream buffer;
buffer << json << std::endl;
LogDebug(VB_CONTROL, "Notify PublishPlaylist\n");
Events::Publish("playlist_details", buffer.str());
}
};

extern void GetCurrentFPPDStatus(Json::Value& result);

class PublishFPPDStatus : public EventNotifier {
public:
PublishFPPDStatus(int freq) :
EventNotifier(freq) {}
~PublishFPPDStatus() {}

void notify() {
Json::Value json;
GetCurrentFPPDStatus(json);

std::stringstream buffer;
buffer << json << std::endl;
LogDebug(VB_CONTROL, "Notify FPPDStatus\n");
Events::Publish("fppd_status", buffer.str());
}
};

class EventWarningListener : public WarningListener {
public:
Expand Down Expand Up @@ -73,7 +108,15 @@ void Events::Ready() {

int playlistFrequency = getSettingInt("MQTTFrequency");
int statusFrequency = getSettingInt("MQTTStatusFrequency");
if (EVENT_PUBLISH_THREAD == nullptr && (playlistFrequency > 0) || (statusFrequency > 0)) {
if (playlistFrequency > 0) {
eventNotifiers.push_back(new PublishPlaylistStatus(playlistFrequency));
}

if (statusFrequency > 0) {
eventNotifiers.push_back(new PublishFPPDStatus(statusFrequency));
}

if (EVENT_PUBLISH_THREAD == nullptr && eventNotifiers.size() > 0) {
EVENT_PUBLISH_THREAD = new std::thread();
// create background Publish Thread
EVENT_PUBLISH_THREAD = new std::thread(Events::RunPublishThread);
Expand Down Expand Up @@ -150,19 +193,10 @@ void Events::RunPublishThread() {
SetThreadName("FPP-Events");
sleep(3); // Give everything time to start up

int playlistFrequency = getSettingInt("MQTTFrequency");
int statusFrequency = getSettingInt("MQTTStatusFrequency");
if (playlistFrequency < 0) {
playlistFrequency = 0;
}
if (statusFrequency < 0) {
statusFrequency = 0;
}

LogInfo(VB_CONTROL, "Starting Publish Thread with Playlist Frequency: %d and Status Frequency %d\n", playlistFrequency, statusFrequency);
if ((playlistFrequency == 0) && (statusFrequency == 0)) {
LogInfo(VB_CONTROL, "Starting Publish Thread\n");
if (eventNotifiers.size() == 0) {
// kill thread
LogInfo(VB_CONTROL, "Stopping Publish Thread as frequency is zero.\nc");
LogInfo(VB_CONTROL, "Stopping Publish Thread as frequency is zero.\n");
return;
}

Expand All @@ -172,37 +206,15 @@ void Events::RunPublishThread() {
time_t lastPlaylist = std::time(0);
while (EVENT_PUBLISH_THREAD_RUN) {
time_t now = std::time(0);
int statusDiff = statusFrequency == 0 ? INT_MIN : (int)(now - lastStatus);
int playlistDiff = playlistFrequency == 0 ? INT_MIN : (int)(now - lastPlaylist);
if (playlistDiff >= playlistFrequency) {
Events::PublishPlaylistStatus();
lastPlaylist = now;
playlistDiff = 0;
}
if (statusDiff >= statusFrequency) {
Events::PublishFPPDStatus();
lastStatus = now;
statusDiff = 0;
}
int playlistSleep = playlistFrequency == 0 ? INT_MAX : (playlistFrequency - playlistDiff);
int statusSleep = statusFrequency == 0 ? INT_MAX : (statusFrequency - statusDiff);
int sleep_dur = INT_MAX;

sleep(std::min(playlistSleep, statusSleep));
for (auto it = eventNotifiers.begin(); it != eventNotifiers.end(); it++) {
if ((*it)->next_time < now) {
(*it)->notify();
(*it)->next_time = (now + (*it)->frequency);
}
sleep_dur = std::min(sleep_dur, (int)((*it)->next_time - now));
}
sleep(sleep_dur);
}
}
void Events::PublishPlaylistStatus() {
Json::Value json = Player::INSTANCE.GetMqttStatusJSON();

std::stringstream buffer;
buffer << json << std::endl;
Publish("playlist_details", buffer.str());
}
extern void GetCurrentFPPDStatus(Json::Value& result);
void Events::PublishFPPDStatus() {
Json::Value json;
GetCurrentFPPDStatus(json);

std::stringstream buffer;
buffer << json << std::endl;
Publish("fppd_status", buffer.str());
}
12 changes: 10 additions & 2 deletions src/Events.h
Expand Up @@ -12,6 +12,7 @@
*/
#include <functional>
#include <string>
#include <time.h>

#define MQTT_READY_TOPIC_NAME "ready"

Expand Down Expand Up @@ -49,6 +50,13 @@ class Events {

private:
static void RunPublishThread();
static void PublishPlaylistStatus();
static void PublishFPPDStatus();
};

class EventNotifier {
public:
int frequency;
time_t next_time = std::time(0);
virtual void notify() = 0;
EventNotifier(int freq) { this->frequency = freq; }
~EventNotifier() {}
};

0 comments on commit 3f385f4

Please sign in to comment.