diff --git a/.vscode/launch.json b/.vscode/launch.json index b65837b..2e80f76 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,11 +11,14 @@ "request": "launch", "program": "${workspaceFolder}/build/src/sword/SwordMain", "args": [ - "promo", //"test", + //"test", + // "promo", + "game", "${workspaceFolder}/assets", "-n", "SwordDebug", "-b", "'mqtts://excaliburs-erben.de:8883'", - "-t", "'staging/outdoor/king-arthur/sword/'", + // "-t", "'staging/outdoor/king-arthur/sword/'", + "-t", "'outdoor/king-arthur/sword/'", "-p", "'awesomeexodusoutdooradventures'", "-u", "'exodususer'", "-l", "'log.excaliburs-erben.de'", diff --git a/src/sword/mqttChannel.hpp b/src/sword/mqttChannel.hpp index fb4b15b..268dd77 100644 --- a/src/sword/mqttChannel.hpp +++ b/src/sword/mqttChannel.hpp @@ -10,6 +10,7 @@ class MqttChannel { Logger(log), runTopic(cfg.StationPath + "run"), cfgTopic(cfg.StationPath + "cfg"), + setTopic(cfg.StationPath + "set"), verTopic(cfg.StationPath + "ver"), eventTopic(cfg.StationPath + "event"), statTopic(cfg.StationPath + "stat"), @@ -34,11 +35,13 @@ class MqttChannel { {"volume", ServiceCommands_e::SetConfigVolume}, {"promoDelay", ServiceCommands_e::SetConfigPromoDelay}, }), - subTopics{{runTopic, cfgTopic, resetTopic}}, + subTopics{{runTopic, setTopic, cfgTopic, resetTopic}}, client(name, cfg.BrockerUrl, subTopics, {cfg.MqttUser, cfg.MqttPassword}) { 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(topic, payload); @@ -86,7 +89,8 @@ class MqttChannel { static void runMasterService(Thread::Context& ctx, MqttChannel& instance, function resetCall, CurrMon& mon) { while (true) { - auto serMsg = instance.MasterQueue.dequeue(); + auto serMsg = instance.MasterQueue.tryDequeueFor(500ms); + instance.sendStatusMessage(); if (!serMsg) { continue; } @@ -147,10 +151,10 @@ class MqttChannel { MediaPlayer::playWavFile(instance.Config.SoundPromo); } break; case ServiceCommands_e::PlaySoundDeny: { - MediaPlayer::playWavFile(instance.Config.SoundDeny); + MediaPlayer::playWavFile(instance.Config.getNextDeny()); } break; case ServiceCommands_e::PlaySoundChosen: { - MediaPlayer::playWavFile(instance.Config.SoundAllow); + MediaPlayer::playWavFile(instance.Config.getNextAllow()); } break; } } @@ -158,11 +162,11 @@ class MqttChannel { resetCall(); } - void sendStatusMessage(OpMode mode) { + void sendStatusMessage() { try { auto& s = IoStat; - client.send(statTopic, fmt::format("M:{}, snd:{}, sS:{},sE:{},sP:{},lE:{},v:{},pD:{}", - (int)mode, + client.send(statTopic, fmt::format("M:{},snd:{},sS:{},sE:{},sP:{},lE:{},v:{},pD:{}", + (int)IoStat.Mode, !s.Mute, s.SwordEntry.get(), s.SwordEnd.get(), @@ -177,6 +181,7 @@ class MqttChannel { 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()); @@ -193,6 +198,7 @@ class MqttChannel { DeviceIoState& IoStat; SeqLogger& Logger; const string runTopic; + const string setTopic; const string cfgTopic; const string verTopic; const string eventTopic; diff --git a/src/sword/swordMain.cpp b/src/sword/swordMain.cpp index 661160b..270febd 100644 --- a/src/sword/swordMain.cpp +++ b/src/sword/swordMain.cpp @@ -12,7 +12,6 @@ const string Version = "V 0.9.0"; unique_ptr SwordCfg; unique_ptr IoStat; -OpMode Mode = OpMode::Maintanance; void processPromo(MqttChannel& mqtt, SeqLogger& log); void processProduction(MqttChannel& mqtt, SeqLogger& log); @@ -42,30 +41,15 @@ int main(int argc, char* argv[]) { SeqLogger log(logUrl, logApiKey, appName); SwordCfg = make_unique(assetsBasePath, brocker, topic, user, password); - - fmt::println("{}", app.get_description()); - if (modeArg == "promo") { - fmt::println("starting with promo"); - Mode = OpMode::PromoMode; - } else if (modeArg == "game") { - fmt::println("starting with promo"); - Mode = OpMode::GamingMode; - } else { - fmt::println("unknown {}", modeArg); - } - constexpr array modes = {"Undefined", "Maintanance", "PromoMode", "Production"}; - log.sendBootUp(Version, modes[static_cast(Mode)]); - - auto itrHandler = [&]() { - TriggeredInput itr(26, "PItr"); - while (true) { - cout << "Await next interrupt\n"; - if (itr.await()) { - cout << "Interrupt Event\n"; - } + { + fmt::println("Win files found: {}", SwordCfg->getSoundsAllow().size()); + for (const auto& file : SwordCfg->getSoundsAllow()) { + fmt::println(" {}", file); } - }; - thread interruptThread(itrHandler); + fmt::println("Lose files found: {}", SwordCfg->getSoundsDeny().size()); + for (const auto& file : SwordCfg->getSoundsDeny()) + fmt::println(" {}", file); + } InputPort sbPort(SwordEntryPin, "PinSEntry"); InputPort sePort(SwordEndPin, "PInSEnd"); @@ -88,6 +72,19 @@ int main(int argc, char* argv[]) { IoStat = make_unique(SwordEntry, SwordEnd, SwordPulled, SwordLatch); + fmt::println("{}", app.get_description()); + if (modeArg == "promo") { + fmt::println("starting with promo"); + IoStat->Mode = OpMode::PromoMode; + } else if (modeArg == "game") { + fmt::println("starting with promo"); + IoStat->Mode = OpMode::GamingMode; + } else { + fmt::println("unknown {}", modeArg); + } + constexpr array modes = {"Undefined", "Maintanance", "PromoMode", "Production"}; + log.sendBootUp(Version, modes[static_cast(IoStat->Mode)]); + MqttChannel mqtt(appName, Version, *SwordCfg, *IoStat, log); auto resetCall = [&]() { log.sendEvent("Reset", ""); @@ -99,13 +96,12 @@ int main(int argc, char* argv[]) { SwordLatch.moveOut(); fmt::println("Running App"); while (true) { - switch (Mode) { + switch (IoStat->Mode) { case OpMode::Maintanance: { fmt::println("Running Maintanance"); // txQueue.emplace(msg->code, 0); Thread::sleep(500ms); IoStat->tick(); - mqtt.sendStatusMessage(Mode); // auto e = make_unique(msg->code, 0); // txQueue.enqueue(move(e)); } break; @@ -122,7 +118,6 @@ int main(int argc, char* argv[]) { break; } } - interruptThread.join(); return EXIT_SUCCESS; } @@ -134,7 +129,6 @@ enum MuteState { void processPromo(MqttChannel& mqtt, SeqLogger& log) { auto muterS = MuteState::WaitForUnmute; Tick tmu(1min); - Tick tn(1s); Tick tsf(30min); Waiter dP; auto& serCmds = mqtt.getSerCommands(); @@ -158,9 +152,6 @@ void processPromo(MqttChannel& mqtt, SeqLogger& log) { } } } - if (tn.tick()) { - mqtt.sendStatusMessage(Mode); - } if (tmu.tick()) { auto now = std::chrono::system_clock::now(); std::time_t time = std::chrono::system_clock::to_time_t(now); @@ -202,7 +193,8 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { auto& serCmds = mqtt.getSerCommands(); auto& gameCmds = mqtt.getGameCommands(); while (true) { - auto msg = mqtt.tryDeque(); + auto msg = mqtt.tryDequeueFor(500ms); + IoStat->tick(); if (!msg) { continue; } @@ -212,11 +204,12 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { case GameModeCommands_e::GameStarted: { fmt::println("Got GameStarted"); log.sendEvent("Req", "GameStarted"); - } + } break; + case GameModeCommands_e::GameFinished: { fmt::println("Got GameFinished"); log.sendEvent("Req", "GameFinished"); - } + } break; case GameModeCommands_e::GameNextTryLocked: { fmt::println("Got NextTryLock"); @@ -224,12 +217,12 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { while (true) { IoStat->SwordPulled.sample(); if (IoStat->SwordPulled.isPosEdge()) { - mqtt.sendStatusMessage(Mode); // say: You are not the one - MediaPlayer::playWavFile(SwordCfg->SoundDeny); - Thread::sleep(15s); + MediaPlayer::playWavFile(SwordCfg->getNextDeny()); + Thread::sleep(5s); log.sendEvent("Req", "Pulled"); mqtt.sendEvent("pull"); + break; } } } break; @@ -240,36 +233,38 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { IoStat->SwordLatch.moveIn(); while (true) { IoStat->SwordEnd.sample(); - if (IoStat->SwordEnd.isNegEdge()) { - mqtt.sendStatusMessage(Mode); + if (IoStat->SwordEnd.getState() == pal::InputBiDebounced::State::Passive) { + fmt::println("pulling sword"); break; } + Thread::sleep(10ms); } while (true) { IoStat->SwordEntry.sample(); - if (IoStat->SwordEntry.isNegEdge()) { - mqtt.sendStatusMessage(Mode); + if (IoStat->SwordEntry.getState() == pal::InputBiDebounced::State::Passive) { + fmt::println("sword pulled"); break; } + Thread::sleep(10ms); } mqtt.sendEvent("pulled-out"); log.sendEvent("Req", "Pulled Out"); // say: You ARE the one - MediaPlayer::playWavFile(SwordCfg->SoundAllow); - Thread::sleep(15s); + MediaPlayer::playWavFile(SwordCfg->getNextAllow()); + Thread::sleep(5s); mqtt.sendEvent("ceremonyDone"); while (true) { - IoStat->SwordEnd.sample(); - if (IoStat->SwordEnd.isNegEdge()) { - mqtt.sendStatusMessage(Mode); + IoStat->SwordEntry.sample(); + if (IoStat->SwordEntry.getState() == pal::InputBiDebounced::State::Active) { + fmt::println("returning sword"); break; } } while (true) { - IoStat->SwordEntry.sample(); - if (IoStat->SwordEntry.isNegEdge()) { - mqtt.sendStatusMessage(Mode); + IoStat->SwordEnd.sample(); + if (IoStat->SwordEnd.getState() == pal::InputBiDebounced::State::Active) { + fmt::println("sword returned"); break; } } @@ -279,3 +274,5 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { } } } + +// todo send state in background \ No newline at end of file diff --git a/src/sword/swordMain.hpp b/src/sword/swordMain.hpp index 6a9cecf..fb1fdbb 100644 --- a/src/sword/swordMain.hpp +++ b/src/sword/swordMain.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -29,6 +30,7 @@ #include "queue.hpp" #include "textUtils.hpp" #include "thread.hpp" +#include "defines.hpp" class LinMot { public: @@ -69,7 +71,7 @@ class CurrMon { float sampleCurrent() { auto val = sampe() - _refVal; - val = val / 0.264; + val = val / 0.264; fmt::println("{:.2} A", val); return val; } @@ -103,6 +105,7 @@ class DeviceIoState { SwordPulled.sample(); } + OpMode Mode{OpMode::Maintanance}; pal::InputTransAware& SwordEntry; pal::InputTransAware& SwordEnd; pal::InputTransAware& SwordPulled; @@ -125,7 +128,25 @@ struct SwordConfig { BrockerUrl(brocker), StationPath(topic), MqttUser(user), - MqttPassword(passwd) {} + MqttPassword(passwd) { + using namespace std::filesystem; + + try { + for (const auto& entry : directory_iterator(base)) { + if (!entry.is_regular_file()) { + continue; + } + const std::string filename = entry.path().filename().string(); + if (filename.find("Excalibur_LOSE") != std::string::npos) { + SoundsDeny.push_back(entry.path().string()); + } else if (filename.find("Excalibur_WIN_") != std::string::npos) { + SoundsAllow.push_back(entry.path().string()); + } + } + } catch (const filesystem_error& e) { + std::cerr << "Filesystem error: " << e.what() << '\n'; + } + } const std::string AssetBase; @@ -134,10 +155,44 @@ struct SwordConfig { const std::string SoundDeny; const std::string SoundAllow; + const std::vector& getSoundsAllow() { return SoundsAllow; } + const std::vector& getSoundsDeny() { return SoundsDeny; } + + const std::string getNextAllow() { + if (SoundsAllow.size() < 1) { + return ""; + } + + auto ret = SoundsAllow[idxAllow]; + idxAllow++; + if (idxAllow >= SoundsAllow.size()) { + idxAllow = 0; + } + return ret; + } + const std::string getNextDeny() { + if (SoundsDeny.size() < 1) { + return ""; + } + + auto ret = SoundsDeny[idxDeny]; + idxDeny++; + if (idxDeny >= SoundsDeny.size()) { + idxDeny = 0; + } + return ret; + } + const std::string BrockerUrl; const std::string StationPath; const std::string MqttUser; const std::string MqttPassword; + + private: + std::vector SoundsAllow; + std::vector SoundsDeny; + int idxAllow{}; + int idxDeny{}; }; class MediaPlayer {