Skip to content

Commit 76f08c6

Browse files
author
sajith
committed
Improved
1 parent 9bcfe66 commit 76f08c6

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

plant-care-better/main/src/main.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,36 @@
33
//
44

55
#include <iostream>
6+
#include <mutex>
7+
#include <thread>
68

7-
int main()
9+
#include "sensors/MoistureSensor.h"
10+
#include "sensors/SunlightSensor.h"
11+
#include "caretaker/AloeCaretaker.h"
12+
#include "caretaker/CactusCaretaker.h"
13+
14+
void run()
815
{
16+
std::mutex sensorMtx;
17+
auto sunlightSensor = sensors::SunlightSensor(std::chrono::seconds(2), sensorMtx);
18+
auto moistureSensor = sensors::MoistureSensor(std::chrono::seconds(2), sensorMtx);
19+
20+
caretaker::AloeCaretaker aloeCaretaker;
21+
moistureSensor.subscribe(aloeCaretaker);
22+
sunlightSensor.subscribe(aloeCaretaker);
23+
24+
caretaker::CactusCaretaker cactusCaretaker;
25+
moistureSensor.subscribe(cactusCaretaker);
926

27+
std::thread sunlightSensorThread(sunlightSensor);
28+
std::thread moistureSensorThread(moistureSensor);
29+
30+
sunlightSensorThread.join();
31+
moistureSensorThread.join();
32+
}
33+
34+
int main()
35+
{
36+
run();
1037
return 0;
1138
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ namespace sensors
1616
class MoistureSensor
1717
{
1818
const std::chrono::seconds sleepTime;
19-
std::mutex mtx;
19+
std::mutex &mtx;
2020
std::set<interfaces::WaterDevice *> devices;
2121

2222
const int min = 0;
2323
const int max = 10;
2424
const int threshold = 3;
2525

2626
public:
27-
MoistureSensor(const std::chrono::seconds, std::mutex);
27+
MoistureSensor(const std::chrono::seconds, std::mutex&);
2828
void subscribe(interfaces::WaterDevice &);
29-
void operator()();
29+
[[noreturn]] void operator()();
3030

3131
private:
3232
bool isAirTooDry();

plant-care-better/sensors/src/MoistureSensor.cpp

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

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

0 commit comments

Comments
 (0)