Repository navigation
Expand file tree
/
Copy pathSensor.cpp
More file actions
222 lines (210 loc) · 5.1 KB
/
Copy pathSensor.cpp
File metadata and controls
222 lines (210 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "Sensor.h"
#include <algorithm>
#include <ctime>
#include <ostream>
#include <stdlib.h>
Sensor::Sensor(int id, sensorStatus status,timeOfDay timeOfDay)
{
this->id = id;
this->status = status;
this->time = timeOfDay;
}
Sensor::~Sensor()
{
}
bool Sensor::isActive() const
{
return status == sensorStatus::Active;
}
void Sensor::attach(NoiseObserver* observ)
{
observers.push_back(observ);
}
void Sensor::detach(NoiseObserver* observ)
{
observers.erase(std::remove(observers.begin(), observers.end(), observ), observers.end());
}
void Sensor::Notify(const NoiseNorm& norm)
{
if(!isActive())return;
Metering m = takeMetering();
double limit = 0;
if(norm.isExceeded(m, place(), currentTimeOfDay(), limit))
{
++exceedances;
Metering alerted = m.withLimit(limit);
for(NoiseObserver* observ:observers)
{
observ->MaxNoiseDetected(alerted);
}
}
}
Metering Sensor::makeMetering(double db)
{
Metering m(id, db, std::time(nullptr));
history.push_back(m);
levels.push_back(db);
return m;
}
timeOfDay Sensor::currentTimeOfDay() const
{
std::time_t now = std::time(nullptr);
std::tm* local = std::localtime(&now);
if(local == nullptr) return time;
int hour = local->tm_hour;
return (hour >= 7 && hour < 22) ? timeOfDay::day : timeOfDay::night;
}
void Sensor::printInfo(std::ostream& out) const
{
out << "Sensor " << id << " [" << (status == sensorStatus::Active ? "active" : "inactive")
<< "], period: " << (currentTimeOfDay() == timeOfDay::day ? "day" : "night") << "\n";
}
void Sensor::printHistory(std::ostream& out) const
{
for(const Metering& m : history)
{
m.printTo(out);
}
}
void Sensor::printStatistics(std::ostream& out) const
{
out << "Sensor " << id << ": measurements " << levels.size();
if(!levels.empty())
{
double sum = 0;
double maxLevel = levels[0];
for(double level : levels)
{
sum += level;
if(level > maxLevel) maxLevel = level;
}
out << ", average " << sum / levels.size() << " dB, max " << maxLevel << " dB";
}
out << ", exceedances " << exceedances << "\n";
}
void sensorMap::addSensor(Sensor* addingSensor)
{
citySensors.push_back(addingSensor);
}
InSideSensor::InSideSensor(int id, sensorStatus status,timeOfDay timeOfDay , roomTypeForSensor roomType) : Sensor(id,status,timeOfDay)
{
this->roomType = roomType;
}
placeType InSideSensor::place() const
{
switch (roomType)
{
case roomTypeForSensor::livingRoom:
{
return placeType::livingRoom;
}
case roomTypeForSensor::officeRoom:
{
return placeType::officeRoom;
}
case roomTypeForSensor::industrialRoom:
{
return placeType::industrialRoom;
}
default:
{
return placeType::none;
}
}
}
Metering InSideSensor::takeMetering()
{
double db = rand() % 11 + 25;
if(roomType==roomTypeForSensor::industrialRoom)db+=15;
else if(roomType==roomTypeForSensor::livingRoom)db+=5;
else db+=10;
return makeMetering(db);
}
void InSideSensor::printInfo(std::ostream& out) const
{
Sensor::printInfo(out);
out << " room: ";
switch (roomType)
{
case roomTypeForSensor::livingRoom:
{
out << "living room";
break;
}
case roomTypeForSensor::officeRoom:
{
out << "office";
break;
}
case roomTypeForSensor::industrialRoom:
{
out << "industrial";
break;
}
}
out << "\n";
}
OutSideSensor::OutSideSensor(int id, sensorStatus status,timeOfDay timeOfDay, int windSpeed, bool isRaining) : Sensor(id, status,timeOfDay)
{
this->isRaining = isRaining;
this->windSpeed = windSpeed;
}
placeType OutSideSensor::place() const
{
return placeType::street;
}
Metering OutSideSensor::takeMetering()
{
double db = rand() % 11 + 35;
db += windSpeed * 0.3;
if(isRaining)db+=5;
return makeMetering(db);
}
void OutSideSensor::printInfo(std::ostream& out) const
{
Sensor::printInfo(out);
out << " wind: " << windSpeed << " m/s, raining: " << (isRaining ? "yes" : "no") << "\n";
}
sensorMap::sensorMap()
{
}
sensorMap::~sensorMap()
{
for (Sensor* sensorPtr : citySensors)
{
delete sensorPtr;
}
citySensors.clear();
}
void sensorMap::notifyAll(const NoiseNorm& norm)
{
for (Sensor* sensorPtr : citySensors)
{
sensorPtr->Notify(norm);
}
}
void sensorMap::printAll(std::ostream& out) const
{
for (Sensor* sensorPtr : citySensors)
{
sensorPtr->printInfo(out);
}
}
Sensor* SensorFactory::createSensor(sensorType type, int id, sensorStatus status, timeOfDay part,roomTypeForSensor room, int windSpeed, bool isRaining)
{
static bool seeded = false;
if(!seeded)
{
srand(static_cast<unsigned>(std::time(nullptr)));
seeded = true;
}
if (type == sensorType::inside)
{
return new InSideSensor(id, status, part, room);
}
if (type == sensorType::outside)
{
return new OutSideSensor(id, status, part, windSpeed, isRaining);
}
return nullptr;
}