Skip to content

Commit

Permalink
Update user authentication logic
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenWizard2015 committed Dec 30, 2023
1 parent 7761f46 commit 7046cf4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
4 changes: 4 additions & 0 deletions controller/tea_poor/lib/WaterPump/IWaterPump.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef IWATERPUMP_H
#define IWATERPUMP_H

#include <memory>

class IWaterPump {
public:
virtual ~IWaterPump() {}
Expand All @@ -12,4 +14,6 @@ class IWaterPump {
virtual bool isRunning() const = 0;
};

// define shared pointer alias
using IWaterPumpPtr = std::shared_ptr<IWaterPump>;
#endif
9 changes: 2 additions & 7 deletions controller/tea_poor/lib/WaterPump/WaterPumpScheduler.cpp
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
6 changes: 3 additions & 3 deletions controller/tea_poor/lib/WaterPump/WaterPumpScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions controller/tea_poor/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <Arduino.h>
#include <memory>
#include <WaterPumpController.h>
#include <WaterPumpScheduler.h>
#include <RemoteControl.h>

// Setting up water pump
WaterPumpScheduler waterPump(
new WaterPumpController(12, 9, 3)
std::make_shared<WaterPumpController>(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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions controller/tea_poor/test/test_native/WaterPumpScheduler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FakeWaterPump>();
WaterPumpScheduler waterPumpScheduler(fakeWaterPump);
waterPumpScheduler.setup();
// start water pump
unsigned long currentTimeMs = 0;
Expand All @@ -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<FakeWaterPump>();
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
}
}

Expand Down

0 comments on commit 7046cf4

Please sign in to comment.