HomeWaterLeaksDetection
HighLeakDetection.cpp
Go to the documentation of this file.
1 #include "HighLeakDetection.h"
2 
3 #ifdef WEB_SERVER
4  String HTML_bypass(const HighLeakDetection& highLeakDetection);
5  String HTML_detected(const HighLeakDetection& highLeakDetection);
6  String HTML_activePercentage(const HighLeakDetection& highLeakDetection);
7  String HTML_resetPercentage(const HighLeakDetection& highLeakDetection);
8  String HTML_detectedText(const HighLeakDetection& highLeakDetection);
9  String HTML_activePercentageColor(const HighLeakDetection& highLeakDetection);
10  String HTML_detectedTime(const HighLeakDetection& highLeakDetection);
11  String HTML_detectionLimit(const HighLeakDetection& highLeakDetection);
12  String HTML_resetLimitDays(const HighLeakDetection& highLeakDetection);
13  String HTML_resetLimitHours(const HighLeakDetection& highLeakDetection);
14  String HTML_resetLimitMins(const HighLeakDetection& highLeakDetection);
15  String HTML_resetLimitSecs(const HighLeakDetection& highLeakDetection);
16  String HTML_detectionLimitAlarm(const HighLeakDetection& highLeakDetection);
17  String HTML_resetLimitDaysAlarm(const HighLeakDetection& highLeakDetection);
18  String HTML_resetLimitHoursAlarm(const HighLeakDetection& highLeakDetection);
19  String HTML_resetLimitMinsAlarm(const HighLeakDetection& highLeakDetection);
20  String HTML_resetLimitSecsAlarm(const HighLeakDetection& highLeakDetection);
21 #endif
22 
24  Type type,
25  LeakDetectionConfig_t normalConfig,
26  LeakDetectionConfig_t alarmConfig)
27  : ALeakDetectable(pulseCounter,type, normalConfig, alarmConfig) {
28  #ifdef WEB_SERVER
29  // functions providing data to the webserver
30  htmlData[0] = &HTML_bypass;
31  htmlData[1] = &HTML_detected;
37 
43 
49  #endif
50  reset(); // reset the algorithm
51 }
52 
54  active = LOW;
55  bypass = LOW;
56  detected = 0;
58  pulseCount = 0;
59  timeOfDetection = "";
60  ALeakDetectable::applyNewConfig(); // check if new settings should be applied
61 }
62 
64  return String(newNormalConfig.limitResetTime) + ";" +
65  String(newNormalConfig.limitPulseAction) + ";" +
66  String(newAlarmConfig.limitResetTime) + ";" +
67  String(newAlarmConfig.limitPulseAction) + ";";
68 }
69 
71  time_t currentTime = millis();
72  time_t lastPulseTime = pulseCounter->getLastPulseTime();
73  time_t deltaTime = DELTA_TIME(currentTime, lastPulseTime);
74 
75  if (deltaTime >= config->limitResetTime && !detected) {
76  pulseCount = 0;
78  }
79 }
80 
83 
84  if (detected) {
85  // store the reset % when a leak was detected
86  // if it has not been done yet
87  if (timeOfDetection == "") {
88  timeOfDetection = DateTime::getInstance()->getDateTimeStr(); // date time of detection
90 
91  // send e-mail to the user
92  #ifdef EMAIL_NOTIFICATION
93  sendEmailLeakDetected();
94  #endif
95  }
96  active = !bypass; // if the bypass is off, close the main valve
97  }
98  // indication LED
99  digitalWrite(HIGH_ILOCK_LED_PIN, detected);
100 }
101 
102 #ifdef EMAIL_NOTIFICATION
103  void HighLeakDetection::sendEmailLeakDetected() {
106  "High leak detected!",
107  "High leak was just detected\n\r"
108  "time of detection: " + timeOfDetection + "; "
109  "limit for leak to be detected: " +
112  }
113 #endif
114 
116  // read the bypass
117  bypass = digitalRead(config->bypassPin);
118 
119  // if the state of the bypass has changed
120  // send an e-mail to the user
121  #ifdef EMAIL_NOTIFICATION
122  if (oldBypassValue != bypass) {
124  String state = bypass ? "on" : "off";
127  getType() + " leak detection BYPASS",
128  "bypass is now " + state
129  );
130  }
131  #endif
132 
133  // if a pulse has been detected and a leak
134  // has not been detected, increment the counter
135  if (pulseCounter->isActive() && detected == LOW)
136  pulseCount++;
137 
138  testActiveLeak(); // test if a leak has occurred
139  testResetLeak(); // test if the algorithm should be reset
140 }
141 
143  float percentDetected = pulseCount / (float)config->limitPulseAction;
144  percentDetected *= 100;
145  return min(100, percentDetected);
146 }
147 
149  if (active == HIGH)
150  return 0;
151 
152  time_t currentTime = millis();
153  time_t lastPulseTime = pulseCounter->getLastPulseTime();
154  time_t deltaTime = DELTA_TIME(currentTime, lastPulseTime);
155 
156  float percentReset = deltaTime / (float)config->limitResetTime;
157  percentReset *= 100;
158  return min(100, percentReset);
159 }
160 
161 #ifdef DEBUG
162  const String HighLeakDetection::getLogID() const { return "[HIGH LEAK]"; }
163 
164  const String HighLeakDetection::getLogDescription() const {
165  return "[" +
166  String("BYPASS=") + String(bypass) + " | " +
167  String("ACTIVE=") + String(active) + " | " +
168  String("ACTIVE=") + String(getPercentLeakDetected()) + "% | " +
169  String("RESET=") + String(getPercentLeakDetectionReset()) + "% | " +
170  String("CONFIG=") + ((config == &alarmConfig) ? String("ALARM") : String("NORMAL")) + " | " +
171  String("PULSE_COUNT=") + String(pulseCount) +
172  "]";
173  }
174 #endif
175 
176 #ifdef LCD_DISPLAY
177  const String HighLeakDetection::getRow(int row) const {
178  switch(row) {
179  case 0:
180  // 1st row on the LCD
181  return getType() + " water leak";
182  break;
183  case 1:
184  // 2nd row on the LCD
185  return String("BYPASS=") + String(bypass) + String(" | ") + String("ACTIVE=") + String(active);
186  break;
187  case 2:
188  // 3rd row on the LCD
189  return String("ACTIVE=") + String(getPercentLeakDetected()) + "%";
190  break;
191  case 3:
192  // 4th row on the LCD
193  return String("RESET=") + String(getPercentLeakDetectionReset()) + "%";
194  break;
195  break;
196  }
197  return "UNTITLED"; // for any other row return "UNTITLED"
198  }
199 #endif
200 
201 
202 #ifdef WEB_SERVER
203  const String HighLeakDetection::getHTMLData(const int id) const {
204  auto fce = htmlData.find(id);
205  if (fce == htmlData.end())
207  return fce->second(*this);
208  }
209 
210  String HTML_bypass(const HighLeakDetection& highLeakDetection) {
211  if (highLeakDetection.bypass == LOW)
212  return String("OFF");
213  return String("ON");
214  }
215 
216  String HTML_detectedText(const HighLeakDetection& highLeakDetection) {
218  return String("Detected");
219  return String("Undetected");
220  }
221 
222  String HTML_detected(const HighLeakDetection& highLeakDetection) {
224  return String("ON");
225  return String("OFF");
226  }
227 
228  String HTML_activePercentage(const HighLeakDetection& highLeakDetection) {
229  return String((int)highLeakDetection.getPercentLeakDetected());
230  }
231 
232  String HTML_resetPercentage(const HighLeakDetection& highLeakDetection) {
234  return String(highLeakDetection.resetWhenDetected);
235  return String((int)highLeakDetection.getPercentLeakDetectionReset());
236  }
237 
238  String HTML_activePercentageColor(const HighLeakDetection& highLeakDetection) {
239  int percentage = (int)highLeakDetection.getPercentLeakDetected();
240  if (percentage < 60) return "rgba(44, 62, 80, 0.301)";
241  if (percentage >= 60 && percentage < 75) return "#ffdc52";
242  if (percentage >= 75 && percentage < 85) return "#fca758";
243  return "#fc5858";
244  }
245 
246  String HTML_detectedTime(const HighLeakDetection& highLeakDetection) {
248  }
249 
250  String HTML_detectionLimit(const HighLeakDetection& highLeakDetection) {
252  }
253 
254  String HTML_resetLimitDays(const HighLeakDetection& highLeakDetection) {
256  }
257 
258  String HTML_resetLimitHours(const HighLeakDetection& highLeakDetection) {
260  }
261 
262  String HTML_resetLimitMins(const HighLeakDetection& highLeakDetection) {
264  }
265  String HTML_resetLimitSecs(const HighLeakDetection& highLeakDetection) {
267  }
268 
269  String HTML_detectionLimitAlarm(const HighLeakDetection& highLeakDetection) {
271  }
272 
273  String HTML_resetLimitDaysAlarm(const HighLeakDetection& highLeakDetection) {
275  }
276  String HTML_resetLimitHoursAlarm(const HighLeakDetection& highLeakDetection) {
278  }
279 
280  String HTML_resetLimitMinsAlarm(const HighLeakDetection& highLeakDetection) {
282  }
283 
284  String HTML_resetLimitSecsAlarm(const HighLeakDetection& highLeakDetection) {
286  }
287 #endif
ALeakDetectable::alarmConfig
LeakDetectionConfig_t alarmConfig
alarm configuration for when there's nobody in the house
Definition: ALeakDetectable.h:67
ALeakDetectable::timeOfDetection
String timeOfDetection
time when the leak has been detected
Definition: ALeakDetectable.h:75
EmailSender::BYPASS
@ BYPASS
when a bypass changes
Definition: EmailSender.h:53
highLeakDetection
HighLeakDetection * highLeakDetection
Definition: main.cpp:51
ALeakDetectable::getType
String getType() const
Returns the type of the water leak detection algorithm.
Definition: ALeakDetectable.cpp:84
pulseCounter
PulseCounter pulseCounter(SENSOR_PIN)
ALeakDetectable::bypass
int bypass
bypass pin
Definition: ALeakDetectable.h:69
PulseCounter::getLastPulseTime
time_t getLastPulseTime() const
Return the time when the last pulse was detected.
Definition: PulseCounter.cpp:44
HighLeakDetection::htmlData
std::map< int, String(*)(const HighLeakDetection &highLeakDetection)> htmlData
A map of different values (keys) and their associated functions which returns the appropriate values.
Definition: HighLeakDetection.h:114
HighLeakDetection::HighLeakDetection
HighLeakDetection(PulseCounter *pulseCounter, Type type, LeakDetectionConfig_t normalConfig, LeakDetectionConfig_t alarmConfig)
Constructor of the class.
Definition: HighLeakDetection.cpp:23
HighLeakDetection::HTML_resetLimitSecsAlarm
friend String HTML_resetLimitSecsAlarm(const HighLeakDetection &highLeakDetection)
Associated function for returing the reset time (secs) in the alarm configuration.
LeakDetectionConfig_t
Definition: LeakDetectionConfig.h:17
LeakDetectionConfig_t::limitResetTime
time_t limitResetTime
limit reset time e.g. 5 mins, 24h, ....
Definition: LeakDetectionConfig.h:18
LeakDetectionConfig_t::limitPulseAction
int limitPulseAction
limit action in pulses
Definition: LeakDetectionConfig.h:20
ALeakDetectable::resetWhenDetected
int resetWhenDetected
the reset value (%) when a leak was detected
Definition: ALeakDetectable.h:74
HIGH_ILOCK_LED_PIN
#define HIGH_ILOCK_LED_PIN
high-water leak LED (detected = on)
Definition: Pins.h:26
HighLeakDetection::getPercentLeakDetectionReset
float getPercentLeakDetectionReset() const
Returns percentage information about how close a high-water leak is from being detected.
Definition: HighLeakDetection.cpp:148
HighLeakDetection::getFormatOfSettingsToSave
String getFormatOfSettingsToSave() override
Returns the current settings of the algorithm.
Definition: HighLeakDetection.cpp:63
ALeakDetectable::config
LeakDetectionConfig_t * config
pointer to the current configuration
Definition: ALeakDetectable.h:68
HighLeakDetection
Definition: HighLeakDetection.h:23
ALeakDetectable::detected
bool detected
indication if a leak has been detected
Definition: ALeakDetectable.h:73
ALeakDetectable
Definition: ALeakDetectable.h:52
HighLeakDetection::HTML_activePercentage
friend String HTML_activePercentage(const HighLeakDetection &highLeakDetection)
Associated function for returing the [%] information about how close a leak is from being detected.
DAYS
#define DAYS(time)
Calculates the number of days when formatting the time given in milliseconds.
Definition: DateTime.h:113
HighLeakDetection::reset
void reset() override
Resets the algorithm.
Definition: HighLeakDetection.cpp:53
LeakDetectionConfig_t::bypassPin
int bypassPin
number of the bypass pin
Definition: LeakDetectionConfig.h:21
HighLeakDetection::HTML_detectedTime
friend String HTML_detectedTime(const HighLeakDetection &highLeakDetection)
Associated function for returing the detection time of a high-water leak.
HighLeakDetection::testResetLeak
void testResetLeak() override
Tests if the high-water the algorithm should be reset based on inactivity of pulse on the input pin.
Definition: HighLeakDetection.cpp:70
HighLeakDetection::HTML_resetLimitMins
friend String HTML_resetLimitMins(const HighLeakDetection &highLeakDetection)
Associated function for returing the reset time (mins) in the normal configuration.
HighLeakDetection::getRow
const String getRow(int row) const override
Returns the content of the row given as a parameter.
ALeakDetectable::applyNewConfig
void applyNewConfig()
Applies the new config configuration.
Definition: ALeakDetectable.cpp:34
HighLeakDetection::pulseCount
int pulseCount
number of pulses detected so far
Definition: HighLeakDetection.h:26
DateTime::getInstance
static DateTime * getInstance()
Returns the instance of the class.
Definition: DateTime.cpp:26
EmailSender::LEAK_DETECTED
@ LEAK_DETECTED
when a leak has been detected
Definition: EmailSender.h:47
HTMLDataSource::UNDEFINED_DATA
static const String UNDEFINED_DATA
string "UNDEFINED"
Definition: HTMLDataSource.h:17
HighLeakDetection.h
HighLeakDetection::HTML_bypass
friend String HTML_bypass(const HighLeakDetection &highLeakDetection)
Associated function for returing information about the state of the bypass.
HighLeakDetection::HTML_resetPercentage
friend String HTML_resetPercentage(const HighLeakDetection &highLeakDetection)
Associated function for returing the [%] information about how close the algorithm is from being rese...
ALeakDetectable::newNormalConfig
LeakDetectionConfig_t newNormalConfig
new parameters of the normal config when the user changes them
Definition: ALeakDetectable.h:78
HighLeakDetection::HTML_detectionLimitAlarm
friend String HTML_detectionLimitAlarm(const HighLeakDetection &highLeakDetection)
Associated function for returing the limit (detection) in terms of the alarm configuration.
time_t
unsigned long time_t
Referring to the data type unsigned long as time_t.
Definition: DateTime.h:20
PULSE_TO_LITER
#define PULSE_TO_LITER(p)
Converts pulses to liters.
Definition: LimitsDefinition.h:17
HighLeakDetection::HTML_resetLimitHoursAlarm
friend String HTML_resetLimitHoursAlarm(const HighLeakDetection &highLeakDetection)
Associated function for returing the reset time (hours) in the alarm configuration.
ALeakDetectable::Type
Type
The type of the water leak detection algorithm.
Definition: ALeakDetectable.h:57
EmailSender::sendEmail
byte sendEmail(String subject, String data)
Sends an e-mail off to the smtp2go server.
Definition: EmailSender.cpp:93
HighLeakDetection::HTML_detectionLimit
friend String HTML_detectionLimit(const HighLeakDetection &highLeakDetection)
Associated function for returing the limit (detection) in terms of the normal configuration.
HighLeakDetection::HTML_resetLimitMinsAlarm
friend String HTML_resetLimitMinsAlarm(const HighLeakDetection &highLeakDetection)
Associated function for returing the reset time (mins) in the alarm configuration.
DateTime::getDateTimeStr
String getDateTimeStr() const
Returns the current datatime in a string format.
Definition: DateTime.cpp:47
SECS
#define SECS(time)
Calculates the number of seconds when formatting the time given in milliseconds.
Definition: DateTime.h:92
ALeakDetectable::active
int active
indication if a leak has been detected + bypass is off
Definition: ALeakDetectable.h:71
ALeakDetectable::newAlarmConfig
LeakDetectionConfig_t newAlarmConfig
new parameters of the alarm config when the user changes them
Definition: ALeakDetectable.h:79
HighLeakDetection::getHTMLData
const String getHTMLData(const int id) const override
Since this class is registered as a source of data for the HTML content, it needs return the appropri...
PulseCounter
Definition: PulseCounter.h:29
PulseCounter::isActive
int isActive() const
Returns information if a pulse has been detected.
Definition: PulseCounter.cpp:40
HighLeakDetection::HTML_resetLimitDaysAlarm
friend String HTML_resetLimitDaysAlarm(const HighLeakDetection &highLeakDetection)
Associated function for returing the reset time (days) in the alarm configuration.
HighLeakDetection::HTML_activePercentageColor
friend String HTML_activePercentageColor(const HighLeakDetection &highLeakDetection)
Associated function for returing a color according to [%] information about how close a leak is from ...
HighLeakDetection::HTML_detectedText
friend String HTML_detectedText(const HighLeakDetection &highLeakDetection)
Associated function for returing information about a high-water leak being detected (2)
ALeakDetectable::oldBypassValue
int oldBypassValue
old bypass value (used for sending e-mails)
Definition: ALeakDetectable.h:70
HighLeakDetection::testActiveLeak
void testActiveLeak() override
Tests if a high-water leak has occurred, and if so, it will set the appropriate variables and flags.
Definition: HighLeakDetection.cpp:81
HighLeakDetection::HTML_resetLimitDays
friend String HTML_resetLimitDays(const HighLeakDetection &highLeakDetection)
Associated function for returing the reset time (days) in the normal configuration.
DELTA_TIME
#define DELTA_TIME(time1, time2)
Calculates the time difference between two times given as parameters.
Definition: DateTime.h:35
ALeakDetectable::pulseCounter
PulseCounter * pulseCounter
instance of a pulse counter (input of the system)
Definition: ALeakDetectable.h:65
HighLeakDetection::getPercentLeakDetected
float getPercentLeakDetected() const
Returns percentage information about how close the algorithm is from being reset.
Definition: HighLeakDetection.cpp:142
HighLeakDetection::HTML_resetLimitSecs
friend String HTML_resetLimitSecs(const HighLeakDetection &highLeakDetection)
Associated function for returing the reset time (secs) in the normal configuration.
MINS
#define MINS(time)
Calculates the number of minutes when formatting the time given in milliseconds.
Definition: DateTime.h:99
HighLeakDetection::update
void update() override
Updates the algorithm.
Definition: HighLeakDetection.cpp:115
HOURS
#define HOURS(time)
Calculates the number of hours when formatting the time given in milliseconds.
Definition: DateTime.h:106
HighLeakDetection::HTML_detected
friend String HTML_detected(const HighLeakDetection &highLeakDetection)
Associated function for returing information about a high-water leak being detected (1)
ALeakDetectable::normalConfig
LeakDetectionConfig_t normalConfig
normal configuarion for when the house is occupied
Definition: ALeakDetectable.h:66
EmailSender::getInstance
static EmailSender * getInstance()
Returns the instance of the class.
Definition: EmailSender.cpp:87
HighLeakDetection::HTML_resetLimitHours
friend String HTML_resetLimitHours(const HighLeakDetection &highLeakDetection)
Associated function for returing the reset time (hours) in the normal configuration.