-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
very big update. Reorganized the code, added a command processor, tests.
- Loading branch information
1 parent
75a7629
commit 267322c
Showing
21 changed files
with
336 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Arduino environment | ||
#ifndef ARDUINO_ENVIRONMENT_H | ||
#define ARDUINO_ENVIRONMENT_H | ||
|
||
#include <IEnvironment.h> | ||
#include <Arduino.h> | ||
|
||
class ArduinoEnvironment : public IEnvironment { | ||
public: | ||
unsigned long time() const override { | ||
return millis(); | ||
} | ||
}; | ||
|
||
#endif // ARDUINO_ENVIRONMENT_H |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
58 changes: 58 additions & 0 deletions
58
controller/tea_poor/lib/CommandProcessor/CommandProcessor.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "CommandProcessor.h" | ||
#include <cstring> | ||
#include <sstream> | ||
|
||
bool isValidIntNumber(const char *str, const int maxValue, const int minValue=0) { | ||
const int len = strlen(str); | ||
if (len < 1) return false; | ||
// check that string contains only digits | ||
// first character can be '-' for negative numbers | ||
if ((str[0] != '-') && !isdigit(str[0])) return false; | ||
for (int i = 1; i < len; i++) { | ||
if (!isdigit(str[i])) return false; | ||
} | ||
// check that number is in range | ||
const int value = atoi(str); | ||
if (value < minValue) return false; | ||
if (maxValue <= value) return false; | ||
return true; | ||
} | ||
|
||
std::string CommandProcessor::status() { | ||
std::stringstream response; | ||
response << "{"; | ||
// send water threshold | ||
response << "\"water threshold\": " << _waterPumpSafeThreshold << ", "; | ||
// send water pump status | ||
const auto waterPumpStatus = _waterPump->status(); | ||
const auto now = _env->time(); | ||
const auto timeLeft = waterPumpStatus.isRunning ? waterPumpStatus.stopTime - now : 0; | ||
response | ||
<< "\"pump\": {" | ||
<< " \"running\": " << (waterPumpStatus.isRunning ? "true, " : "false, ") | ||
<< " \"time left\": " << timeLeft | ||
<< "}"; | ||
// end of water pump status | ||
/////////////////////////////////// | ||
// send remote control status | ||
// response << "\"remote control\": " << remoteControl.asJSONString(); | ||
// end of JSON | ||
response << "}"; | ||
|
||
return response.str(); | ||
} | ||
|
||
std::string CommandProcessor::pour_tea(const char *milliseconds) { | ||
if (!isValidIntNumber(milliseconds, _waterPumpSafeThreshold)) { | ||
// send error message as JSON | ||
return std::string("{ \"error\": \"invalid milliseconds value\" }"); | ||
} | ||
// start pouring tea | ||
_waterPump->start( atoi(milliseconds), _env->time() ); | ||
return status(); | ||
} | ||
|
||
std::string CommandProcessor::stop() { | ||
_waterPump->stop(); | ||
return status(); | ||
} |
30 changes: 30 additions & 0 deletions
30
controller/tea_poor/lib/CommandProcessor/CommandProcessor.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// CommandProcessor class definition | ||
#ifndef COMMANDPROCESSOR_H | ||
#define COMMANDPROCESSOR_H | ||
|
||
#include <string> | ||
#include <IWaterPumpSchedulerAPI.h> | ||
#include <IEnvironment.h> | ||
|
||
// This class is used to process incoming commands | ||
class CommandProcessor { | ||
public: | ||
CommandProcessor( | ||
int waterPumpSafeThreshold, | ||
const IEnvironmentPtr env, | ||
const IWaterPumpSchedulerAPIPtr waterPump | ||
) : | ||
_waterPumpSafeThreshold(waterPumpSafeThreshold), | ||
_env(env), | ||
_waterPump(waterPump) | ||
{} | ||
|
||
std::string status(); | ||
std::string pour_tea(const char *milliseconds); | ||
std::string stop(); | ||
private: | ||
const int _waterPumpSafeThreshold; | ||
const IEnvironmentPtr _env; | ||
const IWaterPumpSchedulerAPIPtr _waterPump; | ||
}; | ||
#endif // COMMANDPROCESSOR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#ifndef IENVIRONMENT_H | ||
#define IENVIRONMENT_H | ||
|
||
#include <memory> | ||
|
||
class IEnvironment { | ||
public: | ||
virtual unsigned long time() const = 0; | ||
virtual ~IEnvironment() {} | ||
}; | ||
|
||
typedef std::shared_ptr<IEnvironment> IEnvironmentPtr; | ||
#endif // IENVIRONMENT_H |
File renamed without changes.
30 changes: 30 additions & 0 deletions
30
controller/tea_poor/lib/interfaces/IWaterPumpSchedulerAPI.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// IWaterPumpSchedulerAPI interface | ||
#ifndef IWATERPUMPSCHEDULERAPI_H | ||
#define IWATERPUMPSCHEDULERAPI_H | ||
|
||
#include <memory> | ||
// pump status | ||
struct WaterPumpStatus { | ||
public: | ||
bool isRunning; | ||
unsigned long stopTime; | ||
// copy constructor | ||
WaterPumpStatus(const WaterPumpStatus &other) { | ||
isRunning = other.isRunning; | ||
stopTime = other.stopTime; | ||
} | ||
WaterPumpStatus(bool isRunning, unsigned long stopTime) : isRunning(isRunning), stopTime(stopTime) {} | ||
// default constructor | ||
WaterPumpStatus() : isRunning(false), stopTime(0) {} | ||
}; | ||
|
||
class IWaterPumpSchedulerAPI { | ||
public: | ||
virtual ~IWaterPumpSchedulerAPI() {} | ||
virtual void stop() = 0; | ||
virtual void start(unsigned long runTimeMs, unsigned long currentTimeMs) = 0; | ||
virtual WaterPumpStatus status() = 0; | ||
}; | ||
|
||
using IWaterPumpSchedulerAPIPtr = std::shared_ptr<IWaterPumpSchedulerAPI>; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <gtest/gtest.h> | ||
|
||
// include tests | ||
#include "tests/WaterPumpScheduler_test.h" | ||
#include "tests/CommandProcessor_test.h" | ||
|
||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
int result = RUN_ALL_TESTS(); // Intentionally ignoring the return value | ||
(void)result; // Silence unused variable warning | ||
// Always return zero-code and allow PlatformIO to parse results | ||
return 0; | ||
} |
Oops, something went wrong.