236 lines
8.9 KiB
C++
236 lines
8.9 KiB
C++
#pragma once
|
|
#include "defines.hpp"
|
|
#include "swordMain.hpp"
|
|
|
|
class MqttChannel {
|
|
public:
|
|
static constexpr array<const char*, 3> Modes = {"Undefined", "Maintanance", "Production"};
|
|
|
|
MqttChannel(string name, string version, SwordConfig cfg, DeviceIoState& ioStat, SeqLogger& log)
|
|
: Version(version),
|
|
Config(cfg),
|
|
IoStat(ioStat),
|
|
Logger(log),
|
|
cfgTopic(cfg.StationPath + "cfg"),
|
|
setTopic(cfg.StationPath + "set"),
|
|
verTopic(cfg.StationPath + "ver"),
|
|
eventTopic(cfg.StationPath + "event"),
|
|
statTopic(cfg.StationPath + "stat"),
|
|
resetTopic(cfg.StationPath + "reset"),
|
|
|
|
serviceCommands({
|
|
// cfg-topic
|
|
{"openLock", ServiceCommands_e::SetLockOpen},
|
|
{"closeLock", ServiceCommands_e::SetLockClose},
|
|
{"mute", ServiceCommands_e::SetDisableSound},
|
|
{"unmute", ServiceCommands_e::SetEnableSound},
|
|
{"playSound:Promo", ServiceCommands_e::PlaySoundPromo},
|
|
{"playSound:Deny", ServiceCommands_e::PlaySoundDeny},
|
|
{"playSound:Chosen", ServiceCommands_e::PlaySoundChosen},
|
|
{"setMaintanance", ServiceCommands_e::SetMaintanence},
|
|
}),
|
|
setCommands({
|
|
// cfg-topic
|
|
{"volume", ServiceCommands_e::SetConfigVolume},
|
|
{"promoDelay", ServiceCommands_e::SetConfigPromoDelay},
|
|
}),
|
|
gameCommands({
|
|
// set-topic
|
|
{"gameStarted", GameModeCommands_e::GameStarted},
|
|
{"gameFinished", GameModeCommands_e::GameFinished},
|
|
{"stayLocked", GameModeCommands_e::GameNextTryLocked},
|
|
{"releaseLock", GameModeCommands_e::GameNextTryRelease},
|
|
{"abortTry", GameModeCommands_e::GameAbortTry},
|
|
{"forceAbort", GameModeCommands_e::GameAbortTry},
|
|
}),
|
|
subTopics{{setTopic, cfgTopic, resetTopic}},
|
|
client(name, cfg.BrockerUrl, subTopics,
|
|
{cfg.MqttUser, cfg.MqttPassword},
|
|
MqttClient::LastWill{eventTopic, "offline", 1, false}) {
|
|
fmt::println("MQTT: Starting task:");
|
|
mqttCall = [this](string topic, string payload) {
|
|
fmt::print("MQTT: Got T:'{}' M:'{}'\n", topic, payload);
|
|
|
|
if (topic == resetTopic ||
|
|
topic == cfgTopic) {
|
|
auto mq = std::make_unique<MqttQe>(topic, payload);
|
|
MasterQueue.enqueue(std::move(mq));
|
|
return;
|
|
}
|
|
|
|
auto mq = std::make_unique<MqttQe>(topic, payload);
|
|
MqttQueue.enqueue(std::move(mq));
|
|
};
|
|
|
|
fmt::println("MQTT: service: successfully connected ...");
|
|
client.registerCallback(mqttCall);
|
|
|
|
client.send(verTopic, fmt::format("{}", version));
|
|
client.send(eventTopic, "online");
|
|
}
|
|
|
|
std::unique_ptr<MqttQe> tryDeque() {
|
|
auto serMsg = MqttQueue.dequeue();
|
|
return serMsg;
|
|
}
|
|
|
|
std::unique_ptr<MqttQe> tryDequeueFor(ITime::Duration timeout) {
|
|
auto serMsg = MqttQueue.tryDequeueFor(timeout);
|
|
return serMsg;
|
|
}
|
|
|
|
void flushInput() {
|
|
Thread::sleep(3s);
|
|
int cnt{};
|
|
while (true) {
|
|
if (!MqttQueue.tryDequeueFor(10ms)) {
|
|
break;
|
|
}
|
|
cnt++;
|
|
}
|
|
while (true) {
|
|
if (!MasterQueue.tryDequeueFor(10ms)) {
|
|
break;
|
|
}
|
|
cnt++;
|
|
}
|
|
fmt::println("Input queue flushed, {} Messages discarted", cnt);
|
|
}
|
|
|
|
static void runMasterService(Thread::Context& ctx, MqttChannel& instance, function<void()> resetCall) {
|
|
while (true) {
|
|
auto serMsg = instance.MasterQueue.tryDequeueFor(500ms);
|
|
// only resetTopic and cfgTopic arrive here
|
|
instance.sendStatusMessage();
|
|
if (!serMsg) {
|
|
continue;
|
|
}
|
|
|
|
if (serMsg->Topic == instance.resetTopic) {
|
|
return;
|
|
}
|
|
if (serMsg->Payload == "setMaintanance") {
|
|
std::string m{Modes[static_cast<int>(instance.IoStat.Mode)]};
|
|
instance.client.send(instance.verTopic, fmt::format("{}", instance.Version));
|
|
continue;
|
|
}
|
|
|
|
auto payload = serMsg->Payload;
|
|
if (payload == "mute") {
|
|
instance.IoStat.Mute = true;
|
|
}
|
|
if (payload == "unmute") {
|
|
instance.IoStat.Mute = false;
|
|
}
|
|
|
|
instance.Logger.sendEvent("Ctrl", payload);
|
|
if (auto pos = payload.find(":"); pos != std::string::npos) {
|
|
auto val = payload.substr(pos + 1);
|
|
if (auto iVal = tryParseInt(val); iVal.has_value()) {
|
|
auto val = iVal.value();
|
|
if (payload.starts_with("volume:")) {
|
|
if (val >= 20 && val <= 100) {
|
|
instance.IoStat.Volume = val;
|
|
MediaPlayer::setVolume(val);
|
|
} else {
|
|
instance.Logger.sendEvent("Vol", "discarded volume");
|
|
}
|
|
}
|
|
if (payload.starts_with("promoDelay:")) {
|
|
if (val >= 30 && val <= 300) {
|
|
instance.IoStat.PromoDelay = std::chrono::seconds(val);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (instance.serviceCommands.contains(payload)) {
|
|
auto code = instance.serviceCommands.at(payload);
|
|
switch (code) {
|
|
case ServiceCommands_e::SetLockOpen: {
|
|
if (instance.IoStat.GameRunning) {
|
|
break;
|
|
}
|
|
instance.IoStat.SwordLatch.moveIn();
|
|
} break;
|
|
case ServiceCommands_e::SetLockClose: {
|
|
if (instance.IoStat.GameRunning) {
|
|
break;
|
|
}
|
|
instance.IoStat.SwordLatch.moveOut();
|
|
} break;
|
|
case ServiceCommands_e::PlaySoundPromo: {
|
|
MediaPlayer::playWavFileAsync(instance.Config.SoundPromo);
|
|
} break;
|
|
case ServiceCommands_e::PlaySoundDeny: {
|
|
MediaPlayer::playWavFileAsync(instance.Config.getNextDeny());
|
|
} break;
|
|
case ServiceCommands_e::PlaySoundChosen: {
|
|
MediaPlayer::playWavFileAsync(instance.Config.getNextAllow());
|
|
} break;
|
|
}
|
|
}
|
|
}
|
|
resetCall();
|
|
}
|
|
|
|
void sendStatusMessage() {
|
|
try {
|
|
auto& s = IoStat;
|
|
client.send(statTopic, fmt::format("M:{},snd:{},sS:{},sE:{},sP:{},lE:{},v:{},pD:{}",
|
|
(int)IoStat.Mode,
|
|
!s.Mute,
|
|
s.SwordEntry.get(),
|
|
s.SwordEnd.get(),
|
|
s.SwordPulled.get(),
|
|
s.SwordLatch.isLocked(),
|
|
s.Volume,
|
|
s.PromoDelay));
|
|
} catch (const exception& exc) {
|
|
fmt::println(stderr, "\nERROR: Unable to connect to Setup bridge: {}", exc.what());
|
|
}
|
|
}
|
|
void sendEvent(std::string event) {
|
|
try {
|
|
auto& s = IoStat;
|
|
fmt::println("Sending Event: {}", event);
|
|
client.send(eventTopic, fmt::format("{}", event));
|
|
} catch (const exception& exc) {
|
|
fmt::println(stderr, "\nERROR: Unable to connect to Setup bridge: {}", exc.what());
|
|
}
|
|
}
|
|
|
|
const std::map<string, GameModeCommands_e>& getGameCommands() { return gameCommands; }
|
|
const std::map<string, ServiceCommands_e>& getSerCommands() { return serviceCommands; }
|
|
|
|
private:
|
|
std::string Version;
|
|
SwordConfig Config;
|
|
Queue<MqttQe> MasterQueue;
|
|
Queue<MqttQe> MqttQueue;
|
|
DeviceIoState& IoStat;
|
|
SeqLogger& Logger;
|
|
const string runTopic;
|
|
const string setTopic;
|
|
const string cfgTopic;
|
|
const string verTopic;
|
|
const string eventTopic;
|
|
const string statTopic;
|
|
const string resetTopic;
|
|
|
|
const std::map<string, GameModeCommands_e> gameCommands;
|
|
const std::map<string, ServiceCommands_e> serviceCommands;
|
|
const std::map<string, ServiceCommands_e> setCommands;
|
|
vector<string> subTopics;
|
|
MqttClient client;
|
|
std::function<void(string, string)> mqttCall;
|
|
|
|
static std::optional<int> tryParseInt(std::string val) {
|
|
try {
|
|
int iVal = std::stoi(val);
|
|
return {iVal};
|
|
} catch (...) {
|
|
return {};
|
|
}
|
|
}
|
|
}; |