File tree 3 files changed +93
-4
lines changed
3 files changed +93
-4
lines changed Original file line number Diff line number Diff line change 3
3
//
4
4
5
5
#include < iostream>
6
+ #include < mutex>
7
+ #include < thread>
6
8
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 ()
8
15
{
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);
9
26
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 ();
10
37
return 0 ;
11
38
}
Original file line number Diff line number Diff line change @@ -16,17 +16,17 @@ namespace sensors
16
16
class MoistureSensor
17
17
{
18
18
const std::chrono::seconds sleepTime;
19
- std::mutex mtx;
19
+ std::mutex & mtx;
20
20
std::set<interfaces::WaterDevice *> devices;
21
21
22
22
const int min = 0 ;
23
23
const int max = 10 ;
24
24
const int threshold = 3 ;
25
25
26
26
public:
27
- MoistureSensor (const std::chrono::seconds, std::mutex);
27
+ MoistureSensor (const std::chrono::seconds, std::mutex& );
28
28
void subscribe (interfaces::WaterDevice &);
29
- void operator ()();
29
+ [[noreturn]] void operator ()();
30
30
31
31
private:
32
32
bool isAirTooDry ();
Original file line number Diff line number Diff line change 2
2
// Created by sajith on 4/20/21.
3
3
//
4
4
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
+
You can’t perform that action at this time.
0 commit comments