This commit is contained in:
Jakob Gegeniger committed 2026-10-13 22:10:06 +02:00
1 parent e7393389b8
commit c1e8f2a7a4
4 files changed
+119 -58

No files matched your search

+5 -2
View File
@@ -11,11 +11,14 @@
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/build/src/sword/SwordMain", "program": "${workspaceFolder}/build/src/sword/SwordMain",
"args": [ "args": [
"promo", //"test", //"test",
// "promo",
"game",
"${workspaceFolder}/assets", "${workspaceFolder}/assets",
"-n", "SwordDebug", "-n", "SwordDebug",
"-b", "'mqtts://excaliburs-erben.de:8883'", "-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'", "-p", "'awesomeexodusoutdooradventures'",
"-u", "'exodususer'", "-u", "'exodususer'",
"-l", "'log.excaliburs-erben.de'", "-l", "'log.excaliburs-erben.de'",
+13 -7
View File
@@ -10,6 +10,7 @@ class MqttChannel {
Logger(log), Logger(log),
runTopic(cfg.StationPath + "run"), runTopic(cfg.StationPath + "run"),
cfgTopic(cfg.StationPath + "cfg"), cfgTopic(cfg.StationPath + "cfg"),
setTopic(cfg.StationPath + "set"),
verTopic(cfg.StationPath + "ver"), verTopic(cfg.StationPath + "ver"),
eventTopic(cfg.StationPath + "event"), eventTopic(cfg.StationPath + "event"),
statTopic(cfg.StationPath + "stat"), statTopic(cfg.StationPath + "stat"),
@@ -34,11 +35,13 @@ class MqttChannel {
{"volume", ServiceCommands_e::SetConfigVolume}, {"volume", ServiceCommands_e::SetConfigVolume},
{"promoDelay", ServiceCommands_e::SetConfigPromoDelay}, {"promoDelay", ServiceCommands_e::SetConfigPromoDelay},
}), }),
subTopics{{runTopic, cfgTopic, resetTopic}}, subTopics{{runTopic, setTopic, cfgTopic, resetTopic}},
client(name, cfg.BrockerUrl, subTopics, client(name, cfg.BrockerUrl, subTopics,
{cfg.MqttUser, cfg.MqttPassword}) { {cfg.MqttUser, cfg.MqttPassword}) {
fmt::println("MQTT: Starting task:"); fmt::println("MQTT: Starting task:");
mqttCall = [this](string topic, string payload) { mqttCall = [this](string topic, string payload) {
fmt::print("MQTT: Got T:'{}' M:'{}'\n", topic, payload);
if (topic == resetTopic || if (topic == resetTopic ||
topic == cfgTopic) { topic == cfgTopic) {
auto mq = std::make_unique<MqttQe>(topic, payload); auto mq = std::make_unique<MqttQe>(topic, payload);
@@ -86,7 +89,8 @@ class MqttChannel {
static void runMasterService(Thread::Context& ctx, MqttChannel& instance, function<void()> resetCall, CurrMon& mon) { static void runMasterService(Thread::Context& ctx, MqttChannel& instance, function<void()> resetCall, CurrMon& mon) {
while (true) { while (true) {
auto serMsg = instance.MasterQueue.dequeue(); auto serMsg = instance.MasterQueue.tryDequeueFor(500ms);
instance.sendStatusMessage();
if (!serMsg) { if (!serMsg) {
continue; continue;
} }
@@ -147,10 +151,10 @@ class MqttChannel {
MediaPlayer::playWavFile(instance.Config.SoundPromo); MediaPlayer::playWavFile(instance.Config.SoundPromo);
} break; } break;
case ServiceCommands_e::PlaySoundDeny: { case ServiceCommands_e::PlaySoundDeny: {
MediaPlayer::playWavFile(instance.Config.SoundDeny); MediaPlayer::playWavFile(instance.Config.getNextDeny());
} break; } break;
case ServiceCommands_e::PlaySoundChosen: { case ServiceCommands_e::PlaySoundChosen: {
MediaPlayer::playWavFile(instance.Config.SoundAllow); MediaPlayer::playWavFile(instance.Config.getNextAllow());
} break; } break;
} }
} }
@@ -158,11 +162,11 @@ class MqttChannel {
resetCall(); resetCall();
} }
void sendStatusMessage(OpMode mode) { void sendStatusMessage() {
try { try {
auto& s = IoStat; auto& s = IoStat;
client.send(statTopic, fmt::format("M:{}, snd:{}, sS:{},sE:{},sP:{},lE:{},v:{},pD:{}", client.send(statTopic, fmt::format("M:{},snd:{},sS:{},sE:{},sP:{},lE:{},v:{},pD:{}",
(int)mode, (int)IoStat.Mode,
!s.Mute, !s.Mute,
s.SwordEntry.get(), s.SwordEntry.get(),
s.SwordEnd.get(), s.SwordEnd.get(),
@@ -177,6 +181,7 @@ class MqttChannel {
void sendEvent(std::string event) { void sendEvent(std::string event) {
try { try {
auto& s = IoStat; auto& s = IoStat;
fmt::println("Sending Event: {}",event);
client.send(eventTopic, fmt::format("{}", event)); client.send(eventTopic, fmt::format("{}", event));
} catch (const exception& exc) { } catch (const exception& exc) {
fmt::println(stderr, "\nERROR: Unable to connect to Setup bridge: {}", exc.what()); fmt::println(stderr, "\nERROR: Unable to connect to Setup bridge: {}", exc.what());
@@ -193,6 +198,7 @@ class MqttChannel {
DeviceIoState& IoStat; DeviceIoState& IoStat;
SeqLogger& Logger; SeqLogger& Logger;
const string runTopic; const string runTopic;
const string setTopic;
const string cfgTopic; const string cfgTopic;
const string verTopic; const string verTopic;
const string eventTopic; const string eventTopic;
+45 -48
View File
@@ -12,7 +12,6 @@ const string Version = "V 0.9.0";
unique_ptr<SwordConfig> SwordCfg; unique_ptr<SwordConfig> SwordCfg;
unique_ptr<DeviceIoState> IoStat; unique_ptr<DeviceIoState> IoStat;
OpMode Mode = OpMode::Maintanance;
void processPromo(MqttChannel& mqtt, SeqLogger& log); void processPromo(MqttChannel& mqtt, SeqLogger& log);
void processProduction(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); SeqLogger log(logUrl, logApiKey, appName);
SwordCfg = make_unique<SwordConfig>(assetsBasePath, brocker, topic, user, password); SwordCfg = make_unique<SwordConfig>(assetsBasePath, brocker, topic, user, password);
{
fmt::println("{}", app.get_description()); fmt::println("Win files found: {}", SwordCfg->getSoundsAllow().size());
if (modeArg == "promo") { for (const auto& file : SwordCfg->getSoundsAllow()) {
fmt::println("starting with promo"); fmt::println(" {}", file);
Mode = OpMode::PromoMode;
} else if (modeArg == "game") {
fmt::println("starting with promo");
Mode = OpMode::GamingMode;
} else {
fmt::println("unknown {}", modeArg);
} }
constexpr array<const char*, 4> modes = {"Undefined", "Maintanance", "PromoMode", "Production"}; fmt::println("Lose files found: {}", SwordCfg->getSoundsDeny().size());
log.sendBootUp(Version, modes[static_cast<int>(Mode)]); for (const auto& file : SwordCfg->getSoundsDeny())
fmt::println(" {}", file);
auto itrHandler = [&]() {
TriggeredInput itr(26, "PItr");
while (true) {
cout << "Await next interrupt\n";
if (itr.await()) {
cout << "Interrupt Event\n";
} }
}
};
thread interruptThread(itrHandler);
InputPort sbPort(SwordEntryPin, "PinSEntry"); InputPort sbPort(SwordEntryPin, "PinSEntry");
InputPort sePort(SwordEndPin, "PInSEnd"); InputPort sePort(SwordEndPin, "PInSEnd");
@@ -88,6 +72,19 @@ int main(int argc, char* argv[]) {
IoStat = make_unique<DeviceIoState>(SwordEntry, SwordEnd, SwordPulled, SwordLatch); IoStat = make_unique<DeviceIoState>(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<const char*, 4> modes = {"Undefined", "Maintanance", "PromoMode", "Production"};
log.sendBootUp(Version, modes[static_cast<int>(IoStat->Mode)]);
MqttChannel mqtt(appName, Version, *SwordCfg, *IoStat, log); MqttChannel mqtt(appName, Version, *SwordCfg, *IoStat, log);
auto resetCall = [&]() { auto resetCall = [&]() {
log.sendEvent("Reset", ""); log.sendEvent("Reset", "");
@@ -99,13 +96,12 @@ int main(int argc, char* argv[]) {
SwordLatch.moveOut(); SwordLatch.moveOut();
fmt::println("Running App"); fmt::println("Running App");
while (true) { while (true) {
switch (Mode) { switch (IoStat->Mode) {
case OpMode::Maintanance: { case OpMode::Maintanance: {
fmt::println("Running Maintanance"); fmt::println("Running Maintanance");
// txQueue.emplace<ResponseTxMsg>(msg->code, 0); // txQueue.emplace<ResponseTxMsg>(msg->code, 0);
Thread::sleep(500ms); Thread::sleep(500ms);
IoStat->tick(); IoStat->tick();
mqtt.sendStatusMessage(Mode);
// auto e = make_unique<ResponseTxMsg>(msg->code, 0); // auto e = make_unique<ResponseTxMsg>(msg->code, 0);
// txQueue.enqueue(move(e)); // txQueue.enqueue(move(e));
} break; } break;
@@ -122,7 +118,6 @@ int main(int argc, char* argv[]) {
break; break;
} }
} }
interruptThread.join();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@@ -134,7 +129,6 @@ enum MuteState {
void processPromo(MqttChannel& mqtt, SeqLogger& log) { void processPromo(MqttChannel& mqtt, SeqLogger& log) {
auto muterS = MuteState::WaitForUnmute; auto muterS = MuteState::WaitForUnmute;
Tick tmu(1min); Tick tmu(1min);
Tick tn(1s);
Tick tsf(30min); Tick tsf(30min);
Waiter dP; Waiter dP;
auto& serCmds = mqtt.getSerCommands(); auto& serCmds = mqtt.getSerCommands();
@@ -158,9 +152,6 @@ void processPromo(MqttChannel& mqtt, SeqLogger& log) {
} }
} }
} }
if (tn.tick()) {
mqtt.sendStatusMessage(Mode);
}
if (tmu.tick()) { if (tmu.tick()) {
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
std::time_t time = std::chrono::system_clock::to_time_t(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& serCmds = mqtt.getSerCommands();
auto& gameCmds = mqtt.getGameCommands(); auto& gameCmds = mqtt.getGameCommands();
while (true) { while (true) {
auto msg = mqtt.tryDeque(); auto msg = mqtt.tryDequeueFor(500ms);
IoStat->tick();
if (!msg) { if (!msg) {
continue; continue;
} }
@@ -212,11 +204,12 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) {
case GameModeCommands_e::GameStarted: { case GameModeCommands_e::GameStarted: {
fmt::println("Got GameStarted"); fmt::println("Got GameStarted");
log.sendEvent("Req", "GameStarted"); log.sendEvent("Req", "GameStarted");
} } break;
case GameModeCommands_e::GameFinished: { case GameModeCommands_e::GameFinished: {
fmt::println("Got GameFinished"); fmt::println("Got GameFinished");
log.sendEvent("Req", "GameFinished"); log.sendEvent("Req", "GameFinished");
} } break;
case GameModeCommands_e::GameNextTryLocked: { case GameModeCommands_e::GameNextTryLocked: {
fmt::println("Got NextTryLock"); fmt::println("Got NextTryLock");
@@ -224,12 +217,12 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) {
while (true) { while (true) {
IoStat->SwordPulled.sample(); IoStat->SwordPulled.sample();
if (IoStat->SwordPulled.isPosEdge()) { if (IoStat->SwordPulled.isPosEdge()) {
mqtt.sendStatusMessage(Mode);
// say: You are not the one // say: You are not the one
MediaPlayer::playWavFile(SwordCfg->SoundDeny); MediaPlayer::playWavFile(SwordCfg->getNextDeny());
Thread::sleep(15s); Thread::sleep(5s);
log.sendEvent("Req", "Pulled"); log.sendEvent("Req", "Pulled");
mqtt.sendEvent("pull"); mqtt.sendEvent("pull");
break;
} }
} }
} break; } break;
@@ -240,36 +233,38 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) {
IoStat->SwordLatch.moveIn(); IoStat->SwordLatch.moveIn();
while (true) { while (true) {
IoStat->SwordEnd.sample(); IoStat->SwordEnd.sample();
if (IoStat->SwordEnd.isNegEdge()) { if (IoStat->SwordEnd.getState() == pal::InputBiDebounced::State::Passive) {
mqtt.sendStatusMessage(Mode); fmt::println("pulling sword");
break; break;
} }
Thread::sleep(10ms);
} }
while (true) { while (true) {
IoStat->SwordEntry.sample(); IoStat->SwordEntry.sample();
if (IoStat->SwordEntry.isNegEdge()) { if (IoStat->SwordEntry.getState() == pal::InputBiDebounced::State::Passive) {
mqtt.sendStatusMessage(Mode); fmt::println("sword pulled");
break; break;
} }
Thread::sleep(10ms);
} }
mqtt.sendEvent("pulled-out"); mqtt.sendEvent("pulled-out");
log.sendEvent("Req", "Pulled Out"); log.sendEvent("Req", "Pulled Out");
// say: You ARE the one // say: You ARE the one
MediaPlayer::playWavFile(SwordCfg->SoundAllow); MediaPlayer::playWavFile(SwordCfg->getNextAllow());
Thread::sleep(15s); Thread::sleep(5s);
mqtt.sendEvent("ceremonyDone"); mqtt.sendEvent("ceremonyDone");
while (true) { while (true) {
IoStat->SwordEnd.sample(); IoStat->SwordEntry.sample();
if (IoStat->SwordEnd.isNegEdge()) { if (IoStat->SwordEntry.getState() == pal::InputBiDebounced::State::Active) {
mqtt.sendStatusMessage(Mode); fmt::println("returning sword");
break; break;
} }
} }
while (true) { while (true) {
IoStat->SwordEntry.sample(); IoStat->SwordEnd.sample();
if (IoStat->SwordEntry.isNegEdge()) { if (IoStat->SwordEnd.getState() == pal::InputBiDebounced::State::Active) {
mqtt.sendStatusMessage(Mode); fmt::println("sword returned");
break; break;
} }
} }
@@ -279,3 +274,5 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) {
} }
} }
} }
// todo send state in background
+56 -1
View File
@@ -12,6 +12,7 @@
#include <cstdint> #include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <filesystem>
#include <functional> #include <functional>
#include <map> #include <map>
#include <memory> #include <memory>
@@ -29,6 +30,7 @@
#include "queue.hpp" #include "queue.hpp"
#include "textUtils.hpp" #include "textUtils.hpp"
#include "thread.hpp" #include "thread.hpp"
#include "defines.hpp"
class LinMot { class LinMot {
public: public:
@@ -103,6 +105,7 @@ class DeviceIoState {
SwordPulled.sample(); SwordPulled.sample();
} }
OpMode Mode{OpMode::Maintanance};
pal::InputTransAware& SwordEntry; pal::InputTransAware& SwordEntry;
pal::InputTransAware& SwordEnd; pal::InputTransAware& SwordEnd;
pal::InputTransAware& SwordPulled; pal::InputTransAware& SwordPulled;
@@ -125,7 +128,25 @@ struct SwordConfig {
BrockerUrl(brocker), BrockerUrl(brocker),
StationPath(topic), StationPath(topic),
MqttUser(user), 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; const std::string AssetBase;
@@ -134,10 +155,44 @@ struct SwordConfig {
const std::string SoundDeny; const std::string SoundDeny;
const std::string SoundAllow; const std::string SoundAllow;
const std::vector<std::string>& getSoundsAllow() { return SoundsAllow; }
const std::vector<std::string>& 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 BrockerUrl;
const std::string StationPath; const std::string StationPath;
const std::string MqttUser; const std::string MqttUser;
const std::string MqttPassword; const std::string MqttPassword;
private:
std::vector<std::string> SoundsAllow;
std::vector<std::string> SoundsDeny;
int idxAllow{};
int idxDeny{};
}; };
class MediaPlayer { class MediaPlayer {