Skip to content

Commit 7d71a07

Browse files
author
sajith
committed
Improved
1 parent 19e9a54 commit 7d71a07

File tree

6 files changed

+235
-7
lines changed

6 files changed

+235
-7
lines changed

plant-care/main/main.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
//
22
// Created by sajith on 3/25/21.
33
//
4-
#include <iostream>
4+
#include <thread>
5+
#include <mutex>
56

6-
using namespace std;
7+
#include "caretaker/PlantCaretaker.h"
8+
#include "sensors/MoistureSensor.h"
9+
#include "sensors/SunlightSensor.h"
10+
11+
void run()
12+
{
13+
std::mutex sensorMutex;
14+
auto sunlightSensor = sensors::SunlightSensor(std::chrono::seconds(2), sensorMutex);
15+
auto moistureSensor = sensors::MoistureSensor(std::chrono::seconds(3), sensorMutex);
16+
17+
caretaker::PlantCareTaker plantCareTaker1;
18+
moistureSensor.subscribe(plantCareTaker1);
19+
sunlightSensor.subcribe(plantCareTaker1);
20+
21+
caretaker::PlantCareTaker plantCareTaker2;
22+
moistureSensor.subscribe(plantCareTaker2);
23+
sunlightSensor.subcribe(plantCareTaker2);
24+
25+
std::thread sunlightSensorThread(sunlightSensor);
26+
std::thread moistureSensorThread(moistureSensor);
27+
28+
sunlightSensorThread.join();
29+
moistureSensorThread.join();
30+
}
731

832
int main()
933
{
10-
cout << "Hello Plant care\n";
34+
35+
run();
1136
return 0;
1237

1338
}

plant-care/sensors/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
project(sensors)
22

3+
include_directories(${PROJECT_SOURCE_DIR}/include)
4+
35
set(SOURCE
46
${PROJECT_SOURCE_DIR}/include/sensors/SunlightSensor.h
57
${PROJECT_SOURCE_DIR}/include/sensors/MoistureSensor.h
68
${PROJECT_SOURCE_DIR}/src/SunlightSensor.cpp
79
${PROJECT_SOURCE_DIR}/src/MoistureSensor.cpp)
810

911

10-
add_library(${PROJECT_NAME} STATIC ${SOURCE})
12+
add_library(${PROJECT_NAME} STATIC ${SOURCE})
13+
14+
target_link_libraries(${PROJECT_NAME} caretaker)
15+
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)

plant-care/sensors/include/sensors/MoistureSensor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include "caretaker/PlantCaretaker.h"
1313

14-
namespace sensor
14+
namespace sensors
1515
{
1616
class MoistureSensor
1717
{
@@ -26,13 +26,13 @@ namespace sensor
2626
public:
2727
MoistureSensor(const std::chrono::seconds, std::mutex &mtx);
2828
void subscribe(caretaker::PlantCareTaker &);
29-
operator()();
29+
void operator()();
3030

3131
private:
3232
bool isAirTooDry();
3333
bool isSoilTooDry();
3434
int getAirMoisture();
35-
int getAirSoilMoisture();
35+
int getSoilMoisture();
3636
};
3737
}
3838

plant-care/sensors/include/sensors/SunlightSensor.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,42 @@
55
#ifndef SOLID_SUNLIGHTSENSOR_H
66
#define SOLID_SUNLIGHTSENSOR_H
77

8+
#include <chrono>
9+
#include <mutex>
10+
#include <optional>
11+
#include <set>
12+
13+
#include "caretaker/PlantCaretaker.h"
14+
15+
namespace sensors
16+
{
17+
class SunlightSensor
18+
{
19+
using TimePoint = decltype(std::chrono::system_clock::now());
20+
21+
const std::chrono::seconds sleepTime;
22+
std::mutex &mtx;
23+
std::set<caretaker::PlantCareTaker *> caretakers;
24+
25+
std::optional<TimePoint> sunlightOnFrom;
26+
std::optional<TimePoint> sunlightOffFrom;
27+
28+
const int threshold = 2;
29+
bool sensorOn = true;
30+
31+
public:
32+
SunlightSensor(const std::chrono::seconds, std::mutex &);
33+
void subcribe(caretaker::PlantCareTaker &);
34+
void operator()();
35+
36+
private:
37+
38+
void updateState(const bool);
39+
bool isTooMuchSunlight(const bool);
40+
bool isTooLittleSunlight(const bool);
41+
bool isSunlight() const;
42+
43+
};
44+
}// namespace sensors
45+
846
#endif //SOLID_SUNLIGHTSENSOR_H

plant-care/sensors/src/MoistureSensor.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,69 @@
22
// Created by sajith on 3/25/21.
33
//
44

5+
#include <iostream>
6+
#include <random>
7+
#include <thread>
8+
9+
10+
#include "sensors/MoistureSensor.h"
11+
12+
namespace sensors
13+
{
14+
MoistureSensor::MoistureSensor(const std::chrono::seconds sleeptime, std::mutex &mtx) : sleepTime{sleeptime},
15+
mtx{mtx} {}
16+
17+
void MoistureSensor::subscribe(caretaker::PlantCareTaker &c)
18+
{
19+
careTakers.insert(&c);
20+
}
21+
22+
void MoistureSensor::operator()()
23+
{
24+
for (;;)
25+
{
26+
std::unique_lock<std::mutex> lock(mtx);
27+
if (isAirTooDry())
28+
{
29+
for (auto p: careTakers)
30+
{
31+
p->sprinkleWater();
32+
}
33+
}
34+
35+
if (isSoilTooDry())
36+
{
37+
for (auto p: careTakers)
38+
{
39+
p->pourWater();
40+
}
41+
}
42+
43+
lock.unlock();
44+
std::this_thread::sleep_for(sleepTime);
45+
}
46+
}
47+
48+
49+
bool MoistureSensor::isAirTooDry()
50+
{
51+
return getAirMoisture() < threshold;
52+
}
53+
54+
bool MoistureSensor::isSoilTooDry()
55+
{
56+
return getSoilMoisture() < threshold;
57+
}
58+
59+
int MoistureSensor::getAirMoisture()
60+
{
61+
static std::mt19937 generator;
62+
return std::uniform_int_distribution<int>(min, max)(generator);
63+
}
64+
65+
int MoistureSensor::getSoilMoisture()
66+
{
67+
static std::mt19937 generator;
68+
return std::uniform_int_distribution<int>(min, max)(generator);
69+
}
70+
}

plant-care/sensors/src/SunlightSensor.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,97 @@
22
// Created by sajith on 3/25/21.
33
//
44

5+
#include <iostream>
6+
#include <random>
7+
#include <thread>
8+
9+
#include "sensors/SunlightSensor.h"
10+
11+
namespace sensors
12+
{
13+
SunlightSensor::SunlightSensor(const std::chrono::seconds sleepTime, std::mutex &mtx) : sleepTime{sleepTime},
14+
mtx{mtx} {}
15+
16+
void SunlightSensor::subcribe(caretaker::PlantCareTaker &c)
17+
{
18+
caretakers.insert(&c);
19+
}
20+
21+
void SunlightSensor::operator()()
22+
{
23+
for (;;)
24+
{
25+
std::unique_lock<std::mutex> lock(mtx);
26+
const auto sunlight = isSunlight();
27+
updateState(sunlight);
28+
29+
std::cout << "Sun shines: " << std::boolalpha << sunlight << "\n";
30+
31+
if (isTooMuchSunlight(sunlight))
32+
{
33+
for (auto p : caretakers)
34+
p->closeWindowBlinds();
35+
sensorOn = false;
36+
}
37+
else if (isTooLittleSunlight(sunlight))
38+
{
39+
for (auto p : caretakers)
40+
p->openWindowBlinds();
41+
sensorOn = true;
42+
}
43+
44+
lock.unlock();
45+
std::this_thread::sleep_for(sleepTime);
46+
}
47+
}
48+
49+
void SunlightSensor::updateState(const bool sunlight)
50+
{
51+
const auto currentTime = std::chrono::system_clock::now();
52+
if (sunlight and sensorOn)
53+
{
54+
sunlightOnFrom = sunlightOnFrom ? *sunlightOnFrom : currentTime;
55+
sunlightOffFrom = std::nullopt;
56+
}
57+
else if (not sunlight)
58+
{
59+
sunlightOffFrom = sunlightOffFrom ? *sunlightOffFrom : currentTime;
60+
sunlightOnFrom = std::nullopt;
61+
}
62+
}
63+
64+
bool SunlightSensor::isTooMuchSunlight(const bool sunlight)
65+
{
66+
if (sunlight)
67+
{
68+
const auto timeNow = std::chrono::system_clock::now();
69+
std::chrono::duration<double> elapsedSeconds = timeNow - (*sunlightOnFrom);
70+
return elapsedSeconds.count() > threshold;
71+
}
72+
return false;
73+
}
74+
75+
bool SunlightSensor::isTooLittleSunlight(const bool sunlight)
76+
{
77+
if (not sunlight)
78+
{
79+
const auto timeNow = std::chrono::system_clock::now();
80+
std::chrono::duration<double> elapsedSeconds = timeNow - (*sunlightOffFrom);
81+
return elapsedSeconds.count() > threshold;
82+
}
83+
return false;
84+
}
85+
86+
bool SunlightSensor::isSunlight() const
87+
{
88+
/* We simulate changes of sunlight by generating random numbers that represent
89+
* a probability of change in sunlight: if the sun shines or not.
90+
*/
91+
static bool sunShines = false;
92+
static std::mt19937 generator;
93+
auto prob = std::uniform_int_distribution<int>(1, 100)(generator);
94+
if (prob >= 80)
95+
sunShines = !sunShines;
96+
return sunShines;
97+
}
98+
}

0 commit comments

Comments
 (0)