Skip to content

Commit 7c0ccc3

Browse files
basic impl. of power control
1 parent 85c3bae commit 7c0ccc3

13 files changed

+46
-28
lines changed

controller/tea_poor/lib/Arduino/WaterPumpController.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ void WaterPumpController::setup() {
2020
stop();
2121
}
2222

23-
void WaterPumpController::start() {
23+
void WaterPumpController::start(int powerInPercents) {
24+
const int power = map(powerInPercents, 0, 100, 0, _maxPower);
2425
_isRunning = true;
2526
digitalWrite(_brakePin, LOW); // release breaks
26-
analogWrite(_powerPin, 255);
27+
analogWrite(_powerPin, power);
2728
}
2829

2930
void WaterPumpController::stop() {

controller/tea_poor/lib/Arduino/WaterPumpController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class WaterPumpController: public IWaterPump {
1414
virtual ~WaterPumpController() override;
1515

1616
virtual void setup() override;
17-
virtual void start() override;
17+
virtual void start(int powerInPercents) override;
1818
virtual void stop() override;
1919

2020
virtual bool isRunning() const override { return _isRunning; }

controller/tea_poor/lib/CommandProcessor/CommandProcessor.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,17 @@ std::string CommandProcessor::status() {
4444
return response.str();
4545
}
4646

47-
std::string CommandProcessor::pour_tea(const char *milliseconds) {
47+
std::string CommandProcessor::pour_tea(const char *milliseconds, const char *power) {
4848
if (!isValidIntNumber(milliseconds, _waterPumpSafeThreshold + 1)) {
4949
// send error message as JSON
5050
return std::string("{ \"error\": \"invalid milliseconds value\" }");
5151
}
52+
if (!isValidIntNumber(power, 101)) {
53+
// send error message as JSON
54+
return std::string("{ \"error\": \"invalid power value\" }");
55+
}
5256
// start pouring tea
53-
_waterPump->start( atoi(milliseconds), _env->time() );
57+
_waterPump->start( atoi(milliseconds), atoi(power), _env->time() );
5458
return status();
5559
}
5660

controller/tea_poor/lib/CommandProcessor/CommandProcessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class CommandProcessor {
2020
{}
2121

2222
std::string status();
23-
std::string pour_tea(const char *milliseconds);
23+
std::string pour_tea(const char *milliseconds, const char *power);
2424
std::string stop();
2525
private:
2626
const int _waterPumpSafeThreshold;

controller/tea_poor/lib/WaterPumpScheduler/WaterPumpScheduler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ void WaterPumpScheduler::setup() {
1212
_waterPump->setup();
1313
}
1414

15-
void WaterPumpScheduler::start(unsigned long runTimeMs, unsigned long currentTimeMs) {
15+
void WaterPumpScheduler::start(unsigned long runTimeMs, int power, unsigned long currentTimeMs) {
1616
_stopTime = currentTimeMs + runTimeMs;
17-
_waterPump->start();
17+
_waterPump->start(power);
1818
}
1919

2020
void WaterPumpScheduler::stop() {

controller/tea_poor/lib/WaterPumpScheduler/WaterPumpScheduler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class WaterPumpScheduler : public IWaterPumpSchedulerAPI {
2323
void tick(unsigned long currentTimeMs);
2424

2525
// Public API
26-
void start(unsigned long runTimeMs, unsigned long currentTimeMs) override;
26+
void start(unsigned long runTimeMs, int power, unsigned long currentTimeMs) override;
2727
void stop() override;
2828
WaterPumpStatus status() override;
2929
};

controller/tea_poor/lib/interfaces/IWaterPump.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class IWaterPump {
88
virtual ~IWaterPump() {}
99

1010
virtual void setup() = 0;
11-
virtual void start() = 0;
11+
virtual void start(int powerInPercents) = 0;
1212
virtual void stop() = 0;
1313

1414
virtual bool isRunning() const = 0;

controller/tea_poor/lib/interfaces/IWaterPumpSchedulerAPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class IWaterPumpSchedulerAPI {
2222
public:
2323
virtual ~IWaterPumpSchedulerAPI() {}
2424
virtual void stop() = 0;
25-
virtual void start(unsigned long runTimeMs, unsigned long currentTimeMs) = 0;
25+
virtual void start(unsigned long runTimeMs, int power, unsigned long currentTimeMs) = 0;
2626
virtual WaterPumpStatus status() = 0;
2727
};
2828

controller/tea_poor/src/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ RemoteControl remoteControl(
4646
app.get("/pour_tea", [](Request &req, Response &res) {
4747
char milliseconds[64];
4848
req.query("milliseconds", milliseconds, 64);
49+
50+
char power[64];
51+
req.query("powerLevel", power, 64);
4952

50-
const auto response = commandProcessor.pour_tea(milliseconds);
53+
const auto response = commandProcessor.pour_tea(milliseconds, power);
5154
withExtraHeaders(res);
5255
res.print(response.c_str());
5356
});

controller/tea_poor/test/test_native/tests/CommandProcessor_test.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "mocks/FakeWaterPumpSchedulerAPI.h"
44
#include "mocks/FakeEnvironment.h"
55

6+
const auto VALID_POWER = "100";
67
const auto INVALID_TIME_ERROR_MESSAGE = "{ \"error\": \"invalid milliseconds value\" }";
78
// test that pour_tea() method returns error message if milliseconds:
89
// - greater than threshold
@@ -11,20 +12,20 @@ const auto INVALID_TIME_ERROR_MESSAGE = "{ \"error\": \"invalid milliseconds val
1112
// - not a number
1213
TEST(CommandProcessor, pour_tea_invalid_milliseconds) {
1314
CommandProcessor commandProcessor(123, nullptr, nullptr);
14-
ASSERT_EQ(commandProcessor.pour_tea("1234"), INVALID_TIME_ERROR_MESSAGE);
15-
ASSERT_EQ(commandProcessor.pour_tea("-1"), INVALID_TIME_ERROR_MESSAGE);
16-
ASSERT_EQ(commandProcessor.pour_tea(""), INVALID_TIME_ERROR_MESSAGE);
17-
ASSERT_EQ(commandProcessor.pour_tea("abc"), INVALID_TIME_ERROR_MESSAGE);
15+
ASSERT_EQ(commandProcessor.pour_tea("1234", VALID_POWER), INVALID_TIME_ERROR_MESSAGE);
16+
ASSERT_EQ(commandProcessor.pour_tea("-1", VALID_POWER), INVALID_TIME_ERROR_MESSAGE);
17+
ASSERT_EQ(commandProcessor.pour_tea("", VALID_POWER), INVALID_TIME_ERROR_MESSAGE);
18+
ASSERT_EQ(commandProcessor.pour_tea("abc", VALID_POWER), INVALID_TIME_ERROR_MESSAGE);
1819
}
1920

2021
// for simplicity of the UI, we should accept as valid 0 and exactly threshold value
2122
TEST(CommandProcessor, pour_tea_valid_boundary_values) {
2223
auto env = std::make_shared<FakeEnvironment>();
2324
auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>();
2425
CommandProcessor commandProcessor(123, env, waterPump);
25-
26-
ASSERT_NE(commandProcessor.pour_tea("0"), INVALID_TIME_ERROR_MESSAGE);
27-
ASSERT_NE(commandProcessor.pour_tea("123"), INVALID_TIME_ERROR_MESSAGE);
26+
27+
ASSERT_NE(commandProcessor.pour_tea("0", VALID_POWER), INVALID_TIME_ERROR_MESSAGE);
28+
ASSERT_NE(commandProcessor.pour_tea("123", VALID_POWER), INVALID_TIME_ERROR_MESSAGE);
2829
}
2930

3031
// test that start pouring tea by calling pour_tea() method and its stops after T milliseconds
@@ -33,8 +34,8 @@ TEST(CommandProcessor, pour_tea) {
3334
env->time(2343);
3435
auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>();
3536
CommandProcessor commandProcessor(10000, env, waterPump);
36-
const auto response = commandProcessor.pour_tea("1234");
37-
ASSERT_EQ(waterPump->_log, "start(1234, 2343)\n");
37+
const auto response = commandProcessor.pour_tea("1234", "23");
38+
ASSERT_EQ(waterPump->_log, "start(1234, 23, 2343)\n");
3839
}
3940

4041
// test that stop() method stops pouring tea
@@ -69,7 +70,7 @@ TEST(CommandProcessor, status_running) {
6970
auto waterPump = std::make_shared<FakeWaterPumpSchedulerAPI>();
7071
CommandProcessor commandProcessor(12345, env, waterPump);
7172

72-
commandProcessor.pour_tea("1123");
73+
commandProcessor.pour_tea("1123", "100");
7374

7475
env->time(123);
7576
waterPump->_status.isRunning = true;

controller/tea_poor/test/test_native/tests/WaterPumpScheduler_test.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ TEST(WaterPumpScheduler, test_pump_stops_after_given_time) {
1111
waterPumpScheduler.setup();
1212
// start water pump
1313
unsigned long currentTimeMs = 0;
14-
waterPumpScheduler.start(runTimeMs, currentTimeMs);
14+
waterPumpScheduler.start(runTimeMs, 1, currentTimeMs);
1515
// check status
1616
auto status = waterPumpScheduler.status();
1717
ASSERT_TRUE(status.isRunning);
@@ -34,14 +34,14 @@ TEST(WaterPumpScheduler, test_pump_is_periodically_forced_to_stop_after_given_ti
3434
waterPumpScheduler.setup();
3535
// start water pump
3636
unsigned long currentTimeMs = 0;
37-
waterPumpScheduler.start(1, currentTimeMs);
37+
waterPumpScheduler.start(1, 1, currentTimeMs);
3838
currentTimeMs += 1;
3939
waterPumpScheduler.tick(currentTimeMs);
4040
ASSERT_FALSE(fakeWaterPump->isRunning()); // pump should be stopped after given time
4141

4242
for(int i = 0; i < 10; i++) {
4343
// emulate that pump was started again
44-
fakeWaterPump->start();
44+
fakeWaterPump->start(1);
4545
currentTimeMs += 1000;
4646
waterPumpScheduler.tick(currentTimeMs);
4747
ASSERT_FALSE(fakeWaterPump->isRunning()); // pump should be stopped

controller/tea_poor/test/test_native/tests/mocks/FakeWaterPump.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
class FakeWaterPump : public IWaterPump {
88
private:
99
bool _isRunning = false;
10+
int _powerInPercents = 0;
1011
public:
1112
void setup() override { _isRunning = false; }
12-
void start() override { _isRunning = true; }
1313
void stop() override { _isRunning = false; }
14+
void start(int powerInPercents) override {
15+
_isRunning = true;
16+
_powerInPercents = powerInPercents;
17+
}
1418

1519
bool isRunning() const override { return _isRunning; }
20+
int powerInPercents() const { return _powerInPercents; }
1621
};
1722

1823
#endif // FAKE_WATER_PUMP_H

controller/tea_poor/test/test_native/tests/mocks/FakeWaterPumpSchedulerAPI.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ class FakeWaterPumpSchedulerAPI : public IWaterPumpSchedulerAPI {
1111
_log += "stop()\n";
1212
}
1313

14-
void start(unsigned long runTimeMs, unsigned long currentTimeMs) override {
15-
_log += "start(" + std::to_string(runTimeMs) + ", " + std::to_string(currentTimeMs) + ")\n";
14+
void start(unsigned long runTimeMs, int power, unsigned long currentTimeMs) override {
15+
_log += "start(" +
16+
std::to_string(runTimeMs) + ", " +
17+
std::to_string(power) + ", " +
18+
std::to_string(currentTimeMs) +
19+
")\n";
1620
}
1721

1822
WaterPumpStatus status() override {

0 commit comments

Comments
 (0)