diff --git a/controller/tea_poor/lib/WaterPump/IWaterPump.h b/controller/tea_poor/lib/WaterPump/IWaterPump.h index 4377980..eef9ee9 100644 --- a/controller/tea_poor/lib/WaterPump/IWaterPump.h +++ b/controller/tea_poor/lib/WaterPump/IWaterPump.h @@ -1,6 +1,8 @@ #ifndef IWATERPUMP_H #define IWATERPUMP_H +#include + class IWaterPump { public: virtual ~IWaterPump() {} @@ -12,4 +14,6 @@ class IWaterPump { virtual bool isRunning() const = 0; }; +// define shared pointer alias +using IWaterPumpPtr = std::shared_ptr; #endif \ No newline at end of file diff --git a/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.cpp b/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.cpp index 888b4bf..eb9ae02 100644 --- a/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.cpp +++ b/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.cpp @@ -1,17 +1,12 @@ #include "WaterPumpScheduler.h" -WaterPumpScheduler::WaterPumpScheduler(IWaterPump* waterPump, unsigned long forceStopIntervalMs) : +WaterPumpScheduler::WaterPumpScheduler(IWaterPumpPtr waterPump, unsigned long forceStopIntervalMs) : _waterPump(waterPump), _forceStopIntervalMs(forceStopIntervalMs) { } -WaterPumpScheduler::~WaterPumpScheduler() { - // TODO: find better way to manage memory - // for now it's not a big deal, because Arduino will never stop - // and tests are manage memory by themselves - // delete _waterPump; -} +WaterPumpScheduler::~WaterPumpScheduler() {} void WaterPumpScheduler::setup() { _waterPump->setup(); diff --git a/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.h b/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.h index 6abb21f..84a6145 100644 --- a/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.h +++ b/controller/tea_poor/lib/WaterPump/WaterPumpScheduler.h @@ -8,13 +8,13 @@ // It is also ensuring that water pump is stopped if not needed class WaterPumpScheduler { private: - IWaterPump* _waterPump; + IWaterPumpPtr _waterPump; unsigned long _stopTime = 0; // each X milliseconds will force stop water pump unsigned long _forceStopIntervalMs; public: - WaterPumpScheduler(IWaterPump* waterPump, unsigned long forceStopIntervalMs); - WaterPumpScheduler(IWaterPump* waterPump) : WaterPumpScheduler(waterPump, 1000) {} + WaterPumpScheduler(IWaterPumpPtr waterPump, unsigned long forceStopIntervalMs); + WaterPumpScheduler(IWaterPumpPtr waterPump) : WaterPumpScheduler(waterPump, 1000) {} ~WaterPumpScheduler(); void setup(); diff --git a/controller/tea_poor/src/main.cpp b/controller/tea_poor/src/main.cpp index 82747dd..0a25e02 100644 --- a/controller/tea_poor/src/main.cpp +++ b/controller/tea_poor/src/main.cpp @@ -1,11 +1,12 @@ #include +#include #include #include #include // Setting up water pump WaterPumpScheduler waterPump( - new WaterPumpController(12, 9, 3) + std::make_shared(12, 9, 3) ); // Just for safety reasons, we don't want to pour tea for too long // Their is no reason to make it configurable and add unnecessary complexity @@ -59,9 +60,8 @@ void pour_tea(Request &req, Response &res) { char milliseconds[64]; req.query("milliseconds", milliseconds, 64); if (!isValidIntNumber(milliseconds, WATER_PUMP_SAFE_THRESHOLD)) { - res.println("Please specify amount of milliseconds in query parameter; pour_tea?milliseconds=10 e.g."); - res.print("Maximal allowed time is: "); - res.println(WATER_PUMP_SAFE_THRESHOLD); + // send error message as JSON + res.println("{ \"error\": \"invalid milliseconds value\" }"); return; } // start pouring tea diff --git a/controller/tea_poor/test/test_native/WaterPumpScheduler_test.cpp b/controller/tea_poor/test/test_native/WaterPumpScheduler_test.cpp index 8e2cd96..2b3b40f 100644 --- a/controller/tea_poor/test/test_native/WaterPumpScheduler_test.cpp +++ b/controller/tea_poor/test/test_native/WaterPumpScheduler_test.cpp @@ -21,8 +21,8 @@ class FakeWaterPump : public IWaterPump { TEST(WaterPumpScheduler, test_pump_stops_after_given_time) { // random time between 1 and 10 seconds const unsigned long runTimeMs = 1000 + (rand() % 10) * 1000; - FakeWaterPump fakeWaterPump; - WaterPumpScheduler waterPumpScheduler(&fakeWaterPump); + IWaterPumpPtr fakeWaterPump = std::make_shared(); + WaterPumpScheduler waterPumpScheduler(fakeWaterPump); waterPumpScheduler.setup(); // start water pump unsigned long currentTimeMs = 0; @@ -34,32 +34,32 @@ TEST(WaterPumpScheduler, test_pump_stops_after_given_time) { while (currentTimeMs < runTimeMs) { waterPumpScheduler.tick(currentTimeMs); - ASSERT_TRUE(fakeWaterPump.isRunning()); + ASSERT_TRUE(fakeWaterPump->isRunning()); currentTimeMs += 100; } // pump should be stopped after given time waterPumpScheduler.tick(runTimeMs + 1); - ASSERT_FALSE(fakeWaterPump.isRunning()); + ASSERT_FALSE(fakeWaterPump->isRunning()); } // test that pump is periodically forced to stop after given time TEST(WaterPumpScheduler, test_pump_is_periodically_forced_to_stop_after_given_time) { - FakeWaterPump fakeWaterPump; - WaterPumpScheduler waterPumpScheduler(&fakeWaterPump, 1000); // force stop each 1 second + IWaterPumpPtr fakeWaterPump = std::make_shared(); + WaterPumpScheduler waterPumpScheduler(fakeWaterPump, 1000); // force stop each 1 second waterPumpScheduler.setup(); // start water pump unsigned long currentTimeMs = 0; waterPumpScheduler.start(1, currentTimeMs); currentTimeMs += 1; waterPumpScheduler.tick(currentTimeMs); - ASSERT_FALSE(fakeWaterPump.isRunning()); // pump should be stopped after given time + ASSERT_FALSE(fakeWaterPump->isRunning()); // pump should be stopped after given time for(int i = 0; i < 10; i++) { // emulate that pump was started again - fakeWaterPump.start(); + fakeWaterPump->start(); currentTimeMs += 1000; waterPumpScheduler.tick(currentTimeMs); - ASSERT_FALSE(fakeWaterPump.isRunning()); // pump should be stopped + ASSERT_FALSE(fakeWaterPump->isRunning()); // pump should be stopped } }