Older/Server/AlarmClockServer.cpp
2023-07-21 16:17:01 +08:00

93 lines
3.5 KiB
C++

#include "AlarmClockServer.h"
#include "ServiceManager.h"
#include <boost/json/object.hpp>
#include <boost/json/serialize.hpp>
AlarmClockServer::AlarmClockServer(boost::asio::io_context &context)
: m_publisher(context, ZeroMQ::SocketType::Pub), m_timer(context) {
auto manager = Amass::Singleton<ServiceManager>::instance();
manager->registerTopic(SetAlarmClock, [this](uint8_t hour, uint8_t minute) { setAlarmTime(hour, minute); });
manager->registerTopic(TextToSpeech, [this](const std::string &text) { textToSpeech(text); });
manager->registerTopic(CurrentDatetime, [this]() { currentDatatime(); });
manager->registerTopic(PlayRandomMusic, [this]() { playRandomMusic(); });
manager->registerTopic(StopPlayMusic, [this]() { stopPlayMusic(); });
}
void AlarmClockServer::startPublishHeartBeat() {
m_timer.expires_after(std::chrono::seconds(25));
m_timer.async_wait([this](const boost::system::error_code &error) {
if (error) {
LOG(error) << error.message();
return;
}
boost::json::object obj;
obj["type"] = HeartBeat;
ZeroMQ::Message message(boost::json::serialize(obj));
auto size = m_publisher.send(std::move(message), ZeroMQ::SendFlags::Dontwait);
startPublishHeartBeat();
});
}
void AlarmClockServer::listen(const std::string_view &host, const std::string_view &port) {
std::ostringstream oss;
oss << "tcp://" << host << ":" << port;
boost::system::error_code error;
auto address = oss.str();
m_publisher.bind(address, error);
if (error) {
LOG(error) << error.message();
return;
}
startPublishHeartBeat();
LOG(info) << "AlarmClockServer bind address: " << address;
}
void AlarmClockServer::setAlarmTime(uint8_t hour, uint8_t minute) {
boost::json::object obj;
obj["type"] = SetAlarmClock;
obj["hour"] = hour;
obj["minute"] = minute;
auto body = boost::json::serialize(obj);
ZeroMQ::Message message(body);
auto size = m_publisher.send(std::move(message), ZeroMQ::SendFlags::Dontwait);
LOG(info) << "send to client: " << body << ", size: " << size;
}
void AlarmClockServer::textToSpeech(const std::string_view &text) {
boost::json::object obj;
obj["type"] = TextToSpeech.topic;
obj["text"] = text.data();
auto body = boost::json::serialize(obj);
ZeroMQ::Message message(body);
auto size = m_publisher.send(std::move(message), ZeroMQ::SendFlags::Dontwait);
LOG(info) << "text:[" << text << "].\nsend to client: " << body << ", size: " << size;
}
void AlarmClockServer::currentDatatime() {
boost::json::object obj;
obj["type"] = CurrentDatetime;
auto body = boost::json::serialize(obj);
ZeroMQ::Message message(body);
auto size = m_publisher.send(std::move(message), ZeroMQ::SendFlags::Dontwait);
LOG(info) << "send to client: " << body << ", size: " << size;
}
void AlarmClockServer::playRandomMusic() {
boost::json::object obj;
obj["type"] = PlayRandomMusic;
auto body = boost::json::serialize(obj);
ZeroMQ::Message message(body);
auto size = m_publisher.send(std::move(message), ZeroMQ::SendFlags::Dontwait);
LOG(info) << "send to client: " << body << ", size: " << size;
}
void AlarmClockServer::stopPlayMusic() {
boost::json::object obj;
obj["type"] = StopPlayMusic.topic;
auto body = boost::json::serialize(obj);
ZeroMQ::Message message(body);
auto size = m_publisher.send(std::move(message), ZeroMQ::SendFlags::Dontwait);
LOG(info) << "send to client: " << body << ", size: " << size;
}