-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add current_song_controller, to do reporting logic once, and send fin…
…al string to servicecs
- Loading branch information
Showing
8 changed files
with
148 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <current_song_controller.h> | ||
|
||
#include <iostream> | ||
#include <boost/bind.hpp> | ||
#include "nlohmann/json.hpp" | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace wavplayeralsa | ||
{ | ||
|
||
CurrentSongController::CurrentSongController(boost::asio::io_service &io_service, MqttApi *mqtt_service, WebSocketsApi *ws_service) | ||
: throttle_timer_(io_service) | ||
{ | ||
mqtt_service_ = mqtt_service; | ||
ws_service_ = ws_service; | ||
|
||
json j; | ||
j["song_is_playing"] = false; | ||
UpdateLastStatusMsg(j); | ||
} | ||
|
||
void CurrentSongController::NewSongStatus(const std::string &file_id, uint64_t start_time_millis_since_epoch, double speed) | ||
{ | ||
json j; | ||
j["song_is_playing"] = true; | ||
j["file_id"] = file_id; | ||
j["start_time_millis_since_epoch"] = start_time_millis_since_epoch; | ||
j["speed"] = speed; | ||
UpdateLastStatusMsg(j); | ||
} | ||
|
||
void CurrentSongController::NoSongPlayingStatus(const std::string &file_id) | ||
{ | ||
json j; | ||
j["song_is_playing"] = false; | ||
j["stopped_file_id"] = file_id; | ||
UpdateLastStatusMsg(j); | ||
} | ||
|
||
void CurrentSongController::UpdateLastStatusMsg(const json &msgJson) | ||
{ | ||
const std::string msg_json_str = msgJson.dump(); | ||
|
||
if(msg_json_str == last_status_msg_) { | ||
return; | ||
} | ||
|
||
last_status_msg_ = msg_json_str; | ||
|
||
if(!throttle_timer_set_) { | ||
throttle_timer_.expires_from_now(boost::posix_time::milliseconds(THROTTLE_WAIT_TIME_MS)); | ||
throttle_timer_.async_wait(boost::bind(&CurrentSongController::ReportCurrentSongToServices, this, _1)); | ||
throttle_timer_set_ = true; | ||
} | ||
} | ||
|
||
void CurrentSongController::ReportCurrentSongToServices(const boost::system::error_code& error) | ||
{ | ||
throttle_timer_set_ = false; | ||
if(error) | ||
return; | ||
|
||
mqtt_service_->ReportCurrentSong(last_status_msg_); | ||
ws_service_->ReportCurrentSong(last_status_msg_); | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef CURRENT_SONG_CONTROLLER_H_ | ||
#define CURRENT_SONG_CONTROLLER_H_ | ||
|
||
#include <string> | ||
#include <boost/asio/deadline_timer.hpp> | ||
#include "nlohmann/json_fwd.hpp" | ||
|
||
#include <player_events_ifc.h> | ||
|
||
#include <mqtt_api.h> | ||
#include <web_sockets_api.h> | ||
|
||
using json = nlohmann::json; | ||
|
||
namespace wavplayeralsa { | ||
|
||
class CurrentSongController : public PlayerEventsIfc | ||
{ | ||
|
||
public: | ||
CurrentSongController(boost::asio::io_service &io_service, MqttApi *mqtt_service, WebSocketsApi *ws_service); | ||
|
||
public: | ||
void NewSongStatus(const std::string &file_id, uint64_t start_time_millis_since_epoch, double speed); | ||
void NoSongPlayingStatus(const std::string &file_id); | ||
|
||
private: | ||
void UpdateLastStatusMsg(const json &msgJson); | ||
void ReportCurrentSongToServices(const boost::system::error_code& error); | ||
|
||
private: | ||
MqttApi *mqtt_service_; | ||
WebSocketsApi *ws_service_; | ||
|
||
private: | ||
std::string last_status_msg_; | ||
|
||
private: | ||
// throttling issues: | ||
const int THROTTLE_WAIT_TIME_MS = 50; | ||
boost::asio::deadline_timer throttle_timer_; | ||
bool throttle_timer_set_ = false; | ||
|
||
}; | ||
} | ||
|
||
#endif // CURRENT_SONG_CONTROLLER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.