Initial demo for Promo-Operation
This commit is contained in:
1 parent
55b7909717
commit
eb1c84cf05
25 files changed
+24615
-2
No files matched your search
@@ -0,0 +1,207 @@
|
||||
#pragma once
|
||||
#include "swordMain.hpp"
|
||||
#include "defines.hpp"
|
||||
|
||||
class MqttChannel {
|
||||
public:
|
||||
MqttChannel(string version, SwordConfig cfg, DeviceIoState& ioStat, SeqLogger& log)
|
||||
: Config(cfg),
|
||||
IoStat(ioStat),
|
||||
Logger(log),
|
||||
runTopic(cfg.getStationPath() + "run"),
|
||||
cfgTopic(cfg.getStationPath() + "cfg"),
|
||||
verTopic(cfg.getStationPath() + "ver"),
|
||||
eventTopic(cfg.getStationPath() + "event"),
|
||||
statTopic(cfg.getStationPath() + "stat"),
|
||||
resetTopic(cfg.getStationPath() + "reset"),
|
||||
|
||||
gameCommands({
|
||||
{"gameStarted", GameModeCommands_e::GameStarted},
|
||||
{"gameFinished", GameModeCommands_e::GameFinished},
|
||||
{"stayLocked", GameModeCommands_e::GameNextTryLocked},
|
||||
{"releaseLock", GameModeCommands_e::GameNextTryRelease},
|
||||
}),
|
||||
serviceCommands({
|
||||
{"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},
|
||||
}),
|
||||
setCommands({
|
||||
{"volume", ServiceCommands_e::SetConfigVolume},
|
||||
{"promoDelay", ServiceCommands_e::SetConfigPromoDelay},
|
||||
}),
|
||||
subTopics{{runTopic, cfgTopic, resetTopic}},
|
||||
client("Sword",
|
||||
cfg.getBrockerUrl(),
|
||||
subTopics,
|
||||
{cfg.getMqttUser(), cfg.getMqttPassword()}) {
|
||||
fmt::println("MQTT: Starting task:");
|
||||
mqttCall = [this](string topic, string 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));
|
||||
}
|
||||
|
||||
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.dequeue();
|
||||
if (!serMsg) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (serMsg->Topic == instance.resetTopic) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
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: {
|
||||
instance.IoStat.SwordLatch.moveIn();
|
||||
} break;
|
||||
case ServiceCommands_e::SetLockClose: {
|
||||
instance.IoStat.SwordLatch.moveOut();
|
||||
} break;
|
||||
case ServiceCommands_e::PlaySoundPromo: {
|
||||
MediaPlayer::playWavFile(instance.Config.getSoundPromo());
|
||||
} break;
|
||||
case ServiceCommands_e::PlaySoundDeny: {
|
||||
MediaPlayer::playWavFile(instance.Config.getSoundDeny());
|
||||
} break;
|
||||
case ServiceCommands_e::PlaySoundChosen: {
|
||||
MediaPlayer::playWavFile(instance.Config.getSoundAllow());
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
resetCall();
|
||||
}
|
||||
|
||||
void sendStatusMessage(OpMode mode) {
|
||||
try {
|
||||
auto& s = IoStat;
|
||||
client.send(statTopic, fmt::format("M:{}, snd:{}, sS:{},sE:{},sP:{},lE:{},v:{},pD:{}",
|
||||
(int)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;
|
||||
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:
|
||||
SwordConfig Config;
|
||||
Queue<MqttQe> MasterQueue;
|
||||
Queue<MqttQe> MqttQueue;
|
||||
DeviceIoState& IoStat;
|
||||
SeqLogger& Logger;
|
||||
const string runTopic;
|
||||
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 {};
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user