diff --git a/doc/setup.md b/doc/setup.md index c4c86ed..23d356a 100644 --- a/doc/setup.md +++ b/doc/setup.md @@ -29,4 +29,18 @@ sudo crontab -e ``` ```txt 0 3 * * * /sbin/reboot -``` \ No newline at end of file +0 23 * * * /sbin/reboot +0 6 * * * /sbin/reboot +*/5 * * * * ping -c 4 8.8.8.8 > /dev/null 2>&1 +``` + +## PowerSavings + +```sh +sudo nano /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf +``` + +```ini +[connection] +wifi.powersave = 2 +``` diff --git a/platform b/platform index b998471..387735f 160000 --- a/platform +++ b/platform @@ -1 +1 @@ -Subproject commit b998471c3fd7949121aaf78552a70d1bcbb3221d +Subproject commit 387735f701e4ea682438247ff0ea435fbda294f2 diff --git a/src/sword/defines.hpp b/src/sword/defines.hpp index 8bf30e2..842540f 100644 --- a/src/sword/defines.hpp +++ b/src/sword/defines.hpp @@ -23,7 +23,8 @@ enum GameModeCommands_e { GameFinished, // The Game is finished (resume to Promo mode) GameNextTryLocked, // Lock sword on next try GameNextTryRelease, // Release sword on next try - GameAbortTry // Abort this try + GameAbortTry, // Abort this try + GameForceAbort // Abort current state }; enum OutgoingCommands_e { diff --git a/src/sword/mqttChannel.hpp b/src/sword/mqttChannel.hpp index cd1baec..26e175c 100644 --- a/src/sword/mqttChannel.hpp +++ b/src/sword/mqttChannel.hpp @@ -41,10 +41,12 @@ class MqttChannel { {"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}) { + {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); @@ -64,6 +66,7 @@ class MqttChannel { client.registerCallback(mqttCall); client.send(verTopic, fmt::format("{}", version)); + client.send(eventTopic, "online"); } std::unique_ptr tryDeque() { diff --git a/src/sword/swordMain.cpp b/src/sword/swordMain.cpp index 4f95c6e..b1c0c8d 100644 --- a/src/sword/swordMain.cpp +++ b/src/sword/swordMain.cpp @@ -8,7 +8,7 @@ using namespace std::literals::string_view_literals; using namespace std::literals::chrono_literals; using namespace std; -const string Version = "V 0.9.3"; +const string Version = "V 0.10.0"; unique_ptr SwordCfg; unique_ptr IoStat; @@ -185,6 +185,23 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { auto& gameCmds = mqtt.getGameCommands(); static Tick tSend(500ms); + auto sendLogIfAborted = [&log, &mqtt](bool abort) { + if (abort) { + fmt::println("..abort"); + log.sendEvent("Req", "Aborted"); + mqtt.sendEvent("aborted"); + } + }; + + auto testAbortCondition = [](MqttChannel& mqtt, string cond) -> bool { + auto abortMsg = mqtt.tryDequeueFor(10ms); + return (abortMsg && abortMsg->Payload == cond); + }; + + auto isDone = [](decltype(std::chrono::steady_clock::now()) until) -> bool { + return std::chrono::steady_clock::now() > until; + }; + while (true) { auto msg = mqtt.tryDequeueFor(10ms); IoStat->tick(); @@ -208,13 +225,14 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { fmt::println("Got GameStarted"); log.sendEvent("Req", "GameStarted"); IoStat->GameRunning = true; - // system("pinctrl set 6 ip pd"); + // todo abort all running tests } break; case GameModeCommands_e::GameFinished: { fmt::println("Got GameFinished"); log.sendEvent("Req", "GameFinished"); IoStat->GameRunning = false; + // todo abort all running tests } break; case GameModeCommands_e::GameNextTryLocked: { @@ -231,9 +249,9 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { mqtt.sendEvent("pull-denied"); break; } - auto abortMsg = mqtt.tryDequeueFor(10ms); - abort = abortMsg && abortMsg->Payload == "abortTry"; + abort = testAbortCondition(mqtt, "abortTry"); } + sendLogIfAborted(abort); } break; case GameModeCommands_e::GameNextTryRelease: { @@ -253,8 +271,7 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { } break; } - auto abortMsg = mqtt.tryDequeueFor(10ms); - abort = abortMsg && abortMsg->Payload == "abortTry"; + abort = testAbortCondition(mqtt, "abortTry"); } while (!abort) { IoStat->SwordEntry.sample(); @@ -262,16 +279,12 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { fmt::println("..sword pulled"); break; } - auto abortMsg = mqtt.tryDequeueFor(10ms); - abort = abortMsg && abortMsg->Payload == "abortTry"; - } - if (abort) { - fmt::println("..abort"); - log.sendEvent("Req", "Aborted Start"); - break; + abort = testAbortCondition(mqtt, "abortTry"); } + sendLogIfAborted(abort); + if (abort) break; - IoStat->SwordEnd.sample(); // still stuck? + IoStat->SwordEnd.sample(); // still stuck? bool stuckSensor = (!IoStat->SwordEntry.isActive()) && (IoStat->SwordEnd.isActive()); mqtt.sendEvent("pulled-out"); @@ -287,8 +300,7 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { fmt::println("..returning sword"); break; } - auto abortMsg = mqtt.tryDequeueFor(10ms); - abort = abortMsg && abortMsg->Payload == "abortTry"; + abort = testAbortCondition(mqtt, "forceAbort"); } if (stuckSensor) { fmt::println("..extra wait due to stuck sensor"); @@ -300,14 +312,11 @@ void processProduction(MqttChannel& mqtt, SeqLogger& log) { fmt::println("..sword returned"); break; } - auto abortMsg = mqtt.tryDequeueFor(10ms); - abort = abortMsg && abortMsg->Payload == "abortTry"; - } - if (abort) { - fmt::println("..abortEnd"); - log.sendEvent("Req", "Aborted End"); - break; + abort = testAbortCondition(mqtt, "forceAbort"); } + sendLogIfAborted(abort); + if (abort) break; + log.sendEvent("Game", "Put Back"); mqtt.sendEvent("put-in"); IoStat->SwordLatch.moveOut(); // Lock