HomeWaterLeaksDetection
LowLeakDetection.cpp
Go to the documentation of this file.
1 #include "LowLeakDetection.h"
2 
3 #ifdef WEB_SERVER
4  String HTML_bypass(const LowLeakDetection& lowLeakDetection);
5  String HTML_detected(const LowLeakDetection& lowLeakDetection);
6  String HTML_activePercentage(const LowLeakDetection& lowLeakDetection);
7  String HTML_resetPercentage(const LowLeakDetection& lowLeakDetection);
8  String HTML_detectedText(const LowLeakDetection& lowLeakDetection);
9  String HTML_activePercentageColor(const LowLeakDetection& lowLeakDetection);
10  String HTML_detectedTime(const LowLeakDetection& lowLeakDetection);
11  String HTML_detectedLimitDays(const LowLeakDetection& lowLeakDetection);
12  String HTML_detectedLimitHours(const LowLeakDetection& lowLeakDetection);
13  String HTML_detectedLimitMins(const LowLeakDetection& lowLeakDetection);
14  String HTML_detectedLimitSecs(const LowLeakDetection& lowLeakDetection);
15  String HTML_resetLimitDays(const LowLeakDetection& lowLeakDetection);
16  String HTML_resetLimitHours(const LowLeakDetection& lowLeakDetection);
17  String HTML_resetLimitMins(const LowLeakDetection& lowLeakDetection);
18  String HTML_resetLimitSecs(const LowLeakDetection& lowLeakDetection);
19  String HTML_detectedLimitDaysAlarm(const LowLeakDetection& lowLeakDetection);
20  String HTML_detectedLimitHoursAlarm(const LowLeakDetection& lowLeakDetection);
21  String HTML_detectedLimitMinsAlarm(const LowLeakDetection& lowLeakDetection);
22  String HTML_detectedLimitSecsAlarm(const LowLeakDetection& lowLeakDetection);
23  String HTML_resetLimitDaysAlarm(const LowLeakDetection& lowLeakDetection);
24  String HTML_resetLimitHoursAlarm(const LowLeakDetection& lowLeakDetection);
25  String HTML_resetLimitMinsAlarm(const LowLeakDetection& lowLeakDetection);
26  String HTML_resetLimitSecsAlarm(const LowLeakDetection& lowLeakDetection);
27 #endif
28 
30  Type type,
31  LeakDetectionConfig_t normalConfig,
32  LeakDetectionConfig_t alarmConfig)
33  : ALeakDetectable(pulseCounter, type, normalConfig, alarmConfig) {
34 #ifdef WEB_SERVER
35  // functions providing data to the webserver
36  htmlData[4] = &HTML_bypass;
37  htmlData[5] = &HTML_detected;
43 
48 
53 
58 
63 #endif
64  reset(); // reset the algorithm
65 }
66 
68  bypass = LOW;
69  active = LOW;
70  flipFlop = LOW;
71  flipFlopSetTime = 0;
72  detected = 0;
74  timeOfDetection = "";
75  ALeakDetectable::applyNewConfig(); // check if new settings should be applied
76 }
77 
79  // read the bypass
80  bypass = digitalRead(config->bypassPin);
81 
82  // if the state of the bypass has changed
83  // send an e-mail to the user
84  #ifdef EMAIL_NOTIFICATION
85  if (oldBypassValue != bypass) {
87  String state = bypass ? "on" : "off";
90  getType() + " leak detection BYPASS",
91  "bypass is now " + state
92  );
93  }
94  #endif
95 
96  // if a pulse has been detected and the flipflop
97  // is down, set it up and store the time when it
98  // has been set up
99  if (pulseCounter->isActive() && flipFlop == LOW) {
100  flipFlop = HIGH;
102  }
103  testActiveLeak(); // test if a leak has occurred
104  testResetLeak(); // test if the algorithm should be reset
105 }
106 
108  return String(newNormalConfig.limitResetTime) + ";" +
109  String(newNormalConfig.limitActionTime) + ";" +
110  String(newAlarmConfig.limitResetTime) + ";" +
111  String(newAlarmConfig.limitActionTime) + ";";
112 }
113 
115  time_t currentTime = millis();
116  time_t lastPulseTime = pulseCounter->getLastPulseTime();
117  time_t deltaTime = DELTA_TIME(currentTime, lastPulseTime);
118 
119  if (deltaTime >= config->limitResetTime && !detected) {
120  flipFlop = LOW;
122  }
123 }
124 
126  time_t currentTime = millis();
127  time_t deltaTime = DELTA_TIME(currentTime, flipFlopSetTime);
128  detected = (flipFlop == HIGH && deltaTime >= config->limitActionTime);
129 
130  if (detected) {
131  // store the reset % when a leak was detected
132  // if it has not been done yet
133  if (timeOfDetection == "") {
134  timeOfDetection = DateTime::getInstance()->getDateTimeStr(); // date time of detection
135  resetWhenDetected = (int)getPercentLeakDetectionReset(); // reset %
136 
137  // send e-mail to the user
138  #ifdef EMAIL_NOTIFICATION
139  sendEmailLeakDetected();
140  #endif
141  }
142  active = !bypass; // if the bypass is off, close the main valve
143  }
144  // indication LED
145  digitalWrite(LOW_ILOCK_LED_PIN, detected);
146 }
147 
148 #ifdef EMAIL_NOTIFICATION
149  void LowLeakDetection::sendEmailLeakDetected() {
152  "Low leak detected!",
153  "Low leak was just detected\n\r"
154  "time of detection: " + timeOfDetection + "; "
155  "limit for leak to be detected: " +
157  FORMAT_TIME(alarmConfig.limitActionTime) + "l (alarm)");
158  }
159 #endif
160 
162  if (active == HIGH)
163  return 0;
164 
165  time_t currentTime = millis();
166  time_t lastPulseTime = pulseCounter->getLastPulseTime();
167  time_t deltaTime = DELTA_TIME(currentTime, lastPulseTime);
168 
169  float percentReset = deltaTime / (float)config->limitResetTime;
170  percentReset *= 100;
171  return min(100, percentReset);
172 }
173 
175  if (flipFlop == LOW)
176  return 0;
177 
178  time_t currentTime = millis();
179  time_t deltaTime = DELTA_TIME(currentTime, flipFlopSetTime);
180  float percentDetected = deltaTime / (float)config->limitActionTime;
181  percentDetected *= 100;
182  return min(100, percentDetected);
183 }
184 
185 #ifdef DEBUG
186  const String LowLeakDetection::getLogID() const { return "[LOW LEAK]"; }
187 
188  const String LowLeakDetection::getLogDescription() const {
189  return "[" +
190  String("BYPASS=") + String(bypass) + " | " +
191  String("ACTIVE=") + String(active) + " | " +
192  String("ACTIVE=") + String(getPercentLeakDetected()) + "% | " +
193  String("RESET=") + String(getPercentLeakDetectionReset()) + "% | " +
194  String("FLIP_FLOP=") + String(flipFlop) + " | " +
195  String("FLIP_FLOP_SET_TIME=") + FORMAT_TIME(flipFlopSetTime) + " | " +
196  String("CONFIG=") + ((config == &alarmConfig) ? String("ALARM") : String("NORMAL")) +
197  "]";
198  }
199 #endif
200 
201 #ifdef LCD_DISPLAY
202  const String LowLeakDetection::getRow(int row) const {
203  switch(row) {
204  case 0:
205  // 1st row on the LCD
206  return getType() + " water leak";
207  break;
208  case 1:
209  // 2nd row on the LCD
210  return String("BYPASS=") + String(bypass) + String(" | ") + String("ACTIVE=") + String(active);
211  break;
212  case 2:
213  // 3rd row on the LCD
214  return String("ACTIVE=") + String(getPercentLeakDetected()) + "%";
215  break;
216  case 3:
217  // 4th row on the LCD
218  return String("RESET=") + String(getPercentLeakDetectionReset()) + "%";
219  break;
220  break;
221  }
222  return "UNTITLED"; // for any other row return "UNTITLED"
223  }
224 #endif
225 
226 #ifdef WEB_SERVER
227  const String LowLeakDetection::getHTMLData(const int id) const {
228  auto fce = htmlData.find(id);
229  if (fce == htmlData.end())
231  return fce->second(*this);
232  }
233 
234  String HTML_detectedText(const LowLeakDetection& lowLeakDetection) {
235  if (lowLeakDetection.active == LOW)
236  return String("Undetected");
237  return String("Detected");
238  }
239 
240  String HTML_bypass(const LowLeakDetection& lowLeakDetection) {
241  if (lowLeakDetection.bypass == LOW)
242  return String("OFF");
243  return String("ON");
244  }
245 
246  String HTML_detected(const LowLeakDetection& lowLeakDetection) {
248  return String("ON");
249  return String("OFF");
250  }
251 
252  String HTML_activePercentage(const LowLeakDetection& lowLeakDetection) {
253  return String((int)lowLeakDetection.getPercentLeakDetected());
254  }
255 
256  String HTML_resetPercentage(const LowLeakDetection& lowLeakDetection) {
258  return String(lowLeakDetection.resetWhenDetected);
259  return String((int)lowLeakDetection.getPercentLeakDetectionReset());
260  }
261 
262  String HTML_activePercentageColor(const LowLeakDetection& lowLeakDetection) {
263  int percentage = (int)lowLeakDetection.getPercentLeakDetected();
264  if (percentage < 60) return "rgba(44, 62, 80, 0.301)";
265  if (percentage >= 60 && percentage < 75) return "#ffdc52";
266  if (percentage >= 75 && percentage < 85) return "#fca758";
267  return "#fc5858";
268  }
269 
270  String HTML_detectedTime(const LowLeakDetection& lowLeakDetection) {
272  }
273 
274  String HTML_detectedLimitDays(const LowLeakDetection& lowLeakDetection) {
276  }
277 
278  String HTML_detectedLimitHours(const LowLeakDetection& lowLeakDetection) {
280  }
281 
282  String HTML_detectedLimitMins(const LowLeakDetection& lowLeakDetection) {
284  }
285 
286  String HTML_detectedLimitSecs(const LowLeakDetection& lowLeakDetection) {
288  }
289 
290  String HTML_resetLimitDays(const LowLeakDetection& lowLeakDetection) {
292  }
293 
294  String HTML_resetLimitHours(const LowLeakDetection& lowLeakDetection) {
296  }
297 
298  String HTML_resetLimitMins(const LowLeakDetection& lowLeakDetection) {
300  }
301 
302  String HTML_resetLimitSecs(const LowLeakDetection& lowLeakDetection) {
304  }
305 
306  String HTML_detectedLimitDaysAlarm(const LowLeakDetection& lowLeakDetection) {
308  }
309 
310  String HTML_detectedLimitHoursAlarm(const LowLeakDetection& lowLeakDetection) {
312  }
313  String HTML_detectedLimitMinsAlarm(const LowLeakDetection& lowLeakDetection) {
315  }
316 
317  String HTML_detectedLimitSecsAlarm(const LowLeakDetection& lowLeakDetection) {
319  }
320 
321  String HTML_resetLimitDaysAlarm(const LowLeakDetection& lowLeakDetection) {
323  }
324 
325  String HTML_resetLimitHoursAlarm(const LowLeakDetection& lowLeakDetection) {
327  }
328 
329  String HTML_resetLimitMinsAlarm(const LowLeakDetection& lowLeakDetection) {
331  }
332 
333  String HTML_resetLimitSecsAlarm(const LowLeakDetection& lowLeakDetection) {
335  }
336 #endif
ALeakDetectable::alarmConfig
LeakDetectionConfig_t alarmConfig
alarm configuration for when there's nobody in the house
Definition: ALeakDetectable.h:67
LowLeakDetection::LowLeakDetection
LowLeakDetection(PulseCounter *pulseCounter, Type type, LeakDetectionConfig_t normalConfig, LeakDetectionConfig_t alarmConfig)
Constructor of the class.
Definition: LowLeakDetection.cpp:29
LowLeakDetection::flipFlop
int flipFlop
state of the flip-flop (1/0)
Definition: LowLeakDetection.h:24
LowLeakDetection::HTML_resetLimitDaysAlarm
friend String HTML_resetLimitDaysAlarm(const LowLeakDetection &lowLeakDetection)
Associated function for returing the reset time (days) in the alarm configuration.
ALeakDetectable::timeOfDetection
String timeOfDetection
time when the leak has been detected
Definition: ALeakDetectable.h:75
LowLeakDetection::getFormatOfSettingsToSave
String getFormatOfSettingsToSave() override
Returns the current settings of the algorithm.
Definition: LowLeakDetection.cpp:107
lowLeakDetection
LowLeakDetection * lowLeakDetection
Definition: main.cpp:52
LOW_ILOCK_LED_PIN
#define LOW_ILOCK_LED_PIN
low-water leak LED (detected = on)
Definition: Pins.h:30
LowLeakDetection::HTML_detectedLimitDays
friend String HTML_detectedLimitDays(const LowLeakDetection &lowLeakDetection)
Associated function for returing the limit action time (days) in terms of the normal configuration.
LowLeakDetection::htmlData
std::map< int, String(*)(const LowLeakDetection &highLeakDetection)> htmlData
A map of different values (keys) and their associated functions which returns the appropriate values.
Definition: LowLeakDetection.h:113
FORMAT_TIME
#define FORMAT_TIME(time)
Converts a time in milliseconds into a formatted string.
Definition: DateTime.h:141
EmailSender::BYPASS
@ BYPASS
when a bypass changes
Definition: EmailSender.h:53
LowLeakDetection::HTML_resetPercentage
friend String HTML_resetPercentage(const LowLeakDetection &lowLeakDetection)
Associated function for returing the [%] information about how close the algorithm is from being rese...
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
LowLeakDetection::reset
void reset() override
Resets the algorithm.
Definition: LowLeakDetection.cpp:67
LeakDetectionConfig_t
Definition: LeakDetectionConfig.h:17
LowLeakDetection::HTML_detectedLimitSecsAlarm
friend String HTML_detectedLimitSecsAlarm(const LowLeakDetection &lowLeakDetection)
Associated function for returing the limit action time (secs) in terms of the alarm configuration.
LowLeakDetection::flipFlopSetTime
time_t flipFlopSetTime
time when the flip-flop has been set to 1
Definition: LowLeakDetection.h:25
LeakDetectionConfig_t::limitResetTime
time_t limitResetTime
limit reset time e.g. 5 mins, 24h, ....
Definition: LeakDetectionConfig.h:18
LowLeakDetection::HTML_resetLimitSecsAlarm
friend String HTML_resetLimitSecsAlarm(const LowLeakDetection &lowLeakDetection)
Associated function for returing the reset time (secs) in the alarm configuration.
ALeakDetectable::resetWhenDetected
int resetWhenDetected
the reset value (%) when a leak was detected
Definition: ALeakDetectable.h:74
LowLeakDetection::HTML_resetLimitHours
friend String HTML_resetLimitHours(const LowLeakDetection &lowLeakDetection)
Associated function for returing the reset time (hours) in the normal configuration.
LowLeakDetection.h
ALeakDetectable::config
LeakDetectionConfig_t * config
pointer to the current configuration
Definition: ALeakDetectable.h:68
ALeakDetectable::detected
bool detected
indication if a leak has been detected
Definition: ALeakDetectable.h:73
ALeakDetectable
Definition: ALeakDetectable.h:52
LowLeakDetection::HTML_detectedLimitSecs
friend String HTML_detectedLimitSecs(const LowLeakDetection &lowLeakDetection)
Associated function for returing the limit action time (secs) in terms of the normal configuration.
LowLeakDetection::HTML_detectedLimitMinsAlarm
friend String HTML_detectedLimitMinsAlarm(const LowLeakDetection &lowLeakDetection)
Associated function for returing the limit action time (mins) in terms of the alarm configuration.
LowLeakDetection::HTML_resetLimitDays
friend String HTML_resetLimitDays(const LowLeakDetection &lowLeakDetection)
Associated function for returing the reset time (days) in the normal configuration.
LowLeakDetection::HTML_activePercentageColor
friend String HTML_activePercentageColor(const LowLeakDetection &lowLeakDetection)
Associated function for returing a color according to [%] information about how close a leak is from ...
LowLeakDetection::HTML_detected
friend String HTML_detected(const LowLeakDetection &lowLeakDetection)
Associated function for returing information about a low-water leak being detected (1)
DAYS
#define DAYS(time)
Calculates the number of days when formatting the time given in milliseconds.
Definition: DateTime.h:113
LowLeakDetection::HTML_resetLimitMins
friend String HTML_resetLimitMins(const LowLeakDetection &lowLeakDetection)
Associated function for returing the reset time (mins) in the normal configuration.
LeakDetectionConfig_t::bypassPin
int bypassPin
number of the bypass pin
Definition: LeakDetectionConfig.h:21
LowLeakDetection::getPercentLeakDetectionReset
float getPercentLeakDetectionReset() const
Returns percentage information about how close a low-water leak is from being detected.
Definition: LowLeakDetection.cpp:161
LowLeakDetection::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
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
LowLeakDetection::update
void update() override
Updates the algorithm.
Definition: LowLeakDetection.cpp:78
LowLeakDetection::HTML_detectedLimitHoursAlarm
friend String HTML_detectedLimitHoursAlarm(const LowLeakDetection &lowLeakDetection)
Associated function for returing the limit action time (hours) in terms of the alarm configuration.
LeakDetectionConfig_t::limitActionTime
time_t limitActionTime
limit action time (leak has been detected)
Definition: LeakDetectionConfig.h:19
ALeakDetectable::newNormalConfig
LeakDetectionConfig_t newNormalConfig
new parameters of the normal config when the user changes them
Definition: ALeakDetectable.h:78
time_t
unsigned long time_t
Referring to the data type unsigned long as time_t.
Definition: DateTime.h:20
LowLeakDetection::HTML_detectedLimitDaysAlarm
friend String HTML_detectedLimitDaysAlarm(const LowLeakDetection &lowLeakDetection)
Associated function for returing the limit action time (days) in terms of the alarm configuration.
LowLeakDetection::getPercentLeakDetected
float getPercentLeakDetected() const
Returns percentage information about how close the algorithm is from being reset.
Definition: LowLeakDetection.cpp:174
LowLeakDetection::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...
LowLeakDetection::HTML_resetLimitHoursAlarm
friend String HTML_resetLimitHoursAlarm(const LowLeakDetection &lowLeakDetection)
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
LowLeakDetection::HTML_bypass
friend String HTML_bypass(const LowLeakDetection &lowLeakDetection)
Associated function for returing information about the state of the bypass.
EmailSender::sendEmail
byte sendEmail(String subject, String data)
Sends an e-mail off to the smtp2go server.
Definition: EmailSender.cpp:93
LowLeakDetection::testActiveLeak
void testActiveLeak() override
Tests if a low-water leak has occurred, and if so, it will set the appropriate variables and flags.
Definition: LowLeakDetection.cpp:125
LowLeakDetection::HTML_resetLimitMinsAlarm
friend String HTML_resetLimitMinsAlarm(const LowLeakDetection &lowLeakDetection)
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
LowLeakDetection::HTML_detectedLimitHours
friend String HTML_detectedLimitHours(const LowLeakDetection &lowLeakDetection)
Associated function for returing the limit action time (hours) in terms of the normal configuration.
ALeakDetectable::newAlarmConfig
LeakDetectionConfig_t newAlarmConfig
new parameters of the alarm config when the user changes them
Definition: ALeakDetectable.h:79
LowLeakDetection::testResetLeak
void testResetLeak() override
Tests if the low-water the algorithm should be reset based on inactivity of pulse on the input pin.
Definition: LowLeakDetection.cpp:114
PulseCounter
Definition: PulseCounter.h:29
PulseCounter::isActive
int isActive() const
Returns information if a pulse has been detected.
Definition: PulseCounter.cpp:40
ALeakDetectable::oldBypassValue
int oldBypassValue
old bypass value (used for sending e-mails)
Definition: ALeakDetectable.h:70
LowLeakDetection::HTML_detectedTime
friend String HTML_detectedTime(const LowLeakDetection &lowLeakDetection)
Associated function for returing the detection time of a low-water leak.
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
LowLeakDetection::HTML_detectedText
friend String HTML_detectedText(const LowLeakDetection &lowLeakDetection)
Associated function for returing information about a low-water leak being detected (2)
MINS
#define MINS(time)
Calculates the number of minutes when formatting the time given in milliseconds.
Definition: DateTime.h:99
HOURS
#define HOURS(time)
Calculates the number of hours when formatting the time given in milliseconds.
Definition: DateTime.h:106
LowLeakDetection::HTML_detectedLimitMins
friend String HTML_detectedLimitMins(const LowLeakDetection &lowLeakDetection)
Associated function for returing the limit action time (mins) in terms of the normal configuration.
ALeakDetectable::normalConfig
LeakDetectionConfig_t normalConfig
normal configuarion for when the house is occupied
Definition: ALeakDetectable.h:66
LowLeakDetection
Definition: LowLeakDetection.h:21
LowLeakDetection::HTML_activePercentage
friend String HTML_activePercentage(const LowLeakDetection &lowLeakDetection)
Associated function for returing the [%] information about how close a leak is from being detected.
LowLeakDetection::HTML_resetLimitSecs
friend String HTML_resetLimitSecs(const LowLeakDetection &lowLeakDetection)
Associated function for returing the reset time (secs) in the normal configuration.
EmailSender::getInstance
static EmailSender * getInstance()
Returns the instance of the class.
Definition: EmailSender.cpp:87