HomeWaterLeaksDetection
TotalLeakDetection.cpp
Go to the documentation of this file.
1 #include "TotalLeakDetection.h"
2 
3 #ifdef WEB_SERVER
4  String HTML_bypass(const TotalLeakDetection& totalLeakDetection);
5  String HTML_detected(const TotalLeakDetection& totalLeakDetection);
6  String HTML_activePercentage(const TotalLeakDetection& totalLeakDetection);
7  String HTML_resetPercentage(const TotalLeakDetection& totalLeakDetection);
8  String HTML_detectedText(const TotalLeakDetection& totalLeakDetection);
9  String HTML_activePercentageColor(const TotalLeakDetection& totalLeakDetection);
10  String HTML_detectedTime(const TotalLeakDetection& totalLeakDetection);
11  String HTML_detectionLimit(const TotalLeakDetection& totalLeakDetection);
12  String HTML_resetLimitDays(const TotalLeakDetection& totalLeakDetection);
13  String HTML_resetLimitHours(const TotalLeakDetection& totalLeakDetection);
14  String HTML_resetLimitMins(const TotalLeakDetection& totalLeakDetection);
15  String HTML_resetLimitSecs(const TotalLeakDetection& totalLeakDetection);
16  String HTML_detectionLimitAlarm(const TotalLeakDetection& totalLeakDetection);
17  String HTML_resetLimitDaysAlarm(const TotalLeakDetection& totalLeakDetection);
18  String HTML_resetLimitHoursAlarm(const TotalLeakDetection& totalLeakDetection);
19  String HTML_resetLimitMinsAlarm(const TotalLeakDetection& totalLeakDetection);
20  String HTML_resetLimitSecsAlarm(const TotalLeakDetection& totalLeakDetection);
21 #endif
22 
24  Type type,
25  LeakDetectionConfig_t normalConfig,
26  LeakDetectionConfig_t alarmConfig)
27  : HighLeakDetection(pulseCounter, type, normalConfig, alarmConfig) {
28 #ifdef WEB_SERVER
29  // functions providing data to the webserver
30  htmlData[8] = &HTML_bypass;
31  htmlData[9] = &HTML_detected;
37 
43 
49 #endif
50  reset(); // reset the algorithm
51 }
52 
55  startDayPulseTime = millis(); // start of the 24hrs
56 }
57 
59  time_t currentTime = millis();
60  time_t deltaTime = DELTA_TIME(currentTime, startDayPulseTime);
61 
62  if (deltaTime >= config->limitResetTime && !detected) {
63  pulseCount = 0;
64  startDayPulseTime = currentTime;
65  ALeakDetectable::applyNewConfig(); // check if new settings should be applied
66  }
67 }
68 
71 
72  if (detected) {
73  // store the reset % when a leak was detected
74  // if it has not been done yet
75  if (timeOfDetection == "") {
78 
79  // send e-mail to the user
80  #ifdef EMAIL_NOTIFICATION
81  sendEmailLeakDetected();
82  #endif
83  }
84  active = !bypass; // if the bypass is off, close the main valve
85  }
86  // indication LED
87  digitalWrite(TOTAL_ILOCK_LED_PIN, detected);
88 }
89 
90 #ifdef EMAIL_NOTIFICATION
91  void TotalLeakDetection::sendEmailLeakDetected() {
94  "Total leak detected!",
95  "Total leak was just detected\n\r"
96  "time of detection: " + timeOfDetection + "; "
97  "limit for leak to be detected: " +
100  }
101 #endif
102 
104  if (this->active == HIGH) return 0;
105 
106  time_t currentTime = millis();
107  time_t deltaTime = DELTA_TIME(currentTime, startDayPulseTime);
108 
109  float percentDetected = deltaTime / (float)config->limitResetTime;
110  percentDetected *= 100;
111  return min(100, percentDetected);
112 }
113 
114 #ifdef DEBUG
115  const String TotalLeakDetection::getLogID() const {
116  return "[TOTAL LEAK]";
117  }
118 #endif
119 
120 #ifdef LCD_DISPLAY
121  const String TotalLeakDetection::getRow(int row) const {
122  switch(row) {
123  case 0:
124  // 1st row on the LCD
125  return getType() + " water leak";
126  break;
127  case 1:
128  // 2nd row on the LCD
129  return String("BYPASS=") + String(bypass) + String(" | ") + String("ACTIVE=") + String(active);
130  break;
131  case 2:
132  // 3rd row on the LCD
133  return String("ACTIVE=") + String(getPercentLeakDetected()) + "%";
134  break;
135  case 3:
136  // 4th row on the LCD
137  return String("RESET=") + String(getPercentLeakDetectionReset()) + "%";
138  break;
139  break;
140  }
141  return "UNTITLED"; // for any other row return "UNTITLED"
142  }
143 #endif
144 
145 #ifdef WEB_SERVER
146  const String TotalLeakDetection::getHTMLData(const int id) const {
147  auto fce = htmlData.find(id);
148  if (fce == htmlData.end())
150  return fce->second(*this);
151  }
152 
153  String HTML_detectedText(const TotalLeakDetection& totalLeakDetection) {
154  if (totalLeakDetection.active == LOW)
155  return String("Undetected");
156  return String("Detected");
157  }
158 
159  String HTML_bypass(const TotalLeakDetection& totalLeakDetection) {
160  if (totalLeakDetection.bypass == LOW)
161  return String("OFF");
162  return String("ON");
163  }
164 
165  String HTML_detected(const TotalLeakDetection& totalLeakDetection) {
167  return String("ON");
168  return String("OFF");
169  }
170 
171  String HTML_activePercentage(const TotalLeakDetection& totalLeakDetection) {
172  return String((int)totalLeakDetection.getPercentLeakDetected());
173  }
174 
175  String HTML_resetPercentage(const TotalLeakDetection& totalLeakDetection) {
177  return String(totalLeakDetection.resetWhenDetected);
178  return String((int)totalLeakDetection.getPercentLeakDetectionReset());
179  }
180 
181  String HTML_activePercentageColor(const TotalLeakDetection& totalLeakDetection) {
182  int percentage = (int)totalLeakDetection.getPercentLeakDetected();
183  if (percentage < 60) return "rgba(44, 62, 80, 0.301)";
184  if (percentage >= 60 && percentage < 75) return "#ffdc52";
185  if (percentage >= 75 && percentage < 85) return "#fca758";
186  return "#fc5858";
187  }
188 
189  String HTML_detectedTime(const TotalLeakDetection& totalLeakDetection) {
191  }
192 
193  String HTML_detectionLimit(const TotalLeakDetection& totalLeakDetection) {
195  }
196 
197  String HTML_resetLimitDays(const TotalLeakDetection& totalLeakDetection) {
199  }
200 
201  String HTML_resetLimitHours(const TotalLeakDetection& totalLeakDetection) {
203  }
204 
205  String HTML_resetLimitMins(const TotalLeakDetection& totalLeakDetection) {
207  }
208 
209  String HTML_resetLimitSecs(const TotalLeakDetection& totalLeakDetection) {
211  }
212 
213  String HTML_detectionLimitAlarm(const TotalLeakDetection& totalLeakDetection) {
215  }
216 
217  String HTML_resetLimitDaysAlarm(const TotalLeakDetection& totalLeakDetection) {
219  }
220 
221  String HTML_resetLimitHoursAlarm(const TotalLeakDetection& totalLeakDetection) {
223  }
224 
225  String HTML_resetLimitMinsAlarm(const TotalLeakDetection& totalLeakDetection) {
227  }
228 
229  String HTML_resetLimitSecsAlarm(const TotalLeakDetection& totalLeakDetection) {
231  }
232 #endif
ALeakDetectable::alarmConfig
LeakDetectionConfig_t alarmConfig
alarm configuration for when there's nobody in the house
Definition: ALeakDetectable.h:67
TotalLeakDetection::getRow
const String getRow(int row) const override
Returns the content of the row given as a parameter.
ALeakDetectable::timeOfDetection
String timeOfDetection
time when the leak has been detected
Definition: ALeakDetectable.h:75
TotalLeakDetection::TotalLeakDetection
TotalLeakDetection(PulseCounter *pulseCounter, Type type, LeakDetectionConfig_t normalConfig, LeakDetectionConfig_t alarmConfig)
Constructor of the class.
Definition: TotalLeakDetection.cpp:23
TotalLeakDetection::HTML_resetLimitMinsAlarm
friend String HTML_resetLimitMinsAlarm(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the reset time (mins) in the alarm configuration.
ALeakDetectable::getType
String getType() const
Returns the type of the water leak detection algorithm.
Definition: ALeakDetectable.cpp:84
pulseCounter
PulseCounter pulseCounter(SENSOR_PIN)
TotalLeakDetection::HTML_detectedTime
friend String HTML_detectedTime(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the detection time of a total-water leak.
ALeakDetectable::bypass
int bypass
bypass pin
Definition: ALeakDetectable.h:69
TotalLeakDetection::HTML_detectionLimitAlarm
friend String HTML_detectionLimitAlarm(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the limit (detection) in terms of the alarm configuration.
TotalLeakDetection::HTML_resetLimitSecs
friend String HTML_resetLimitSecs(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the reset time (secs) in the normal configuration.
LeakDetectionConfig_t
Definition: LeakDetectionConfig.h:17
TotalLeakDetection::HTML_resetLimitHoursAlarm
friend String HTML_resetLimitHoursAlarm(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the reset time (hours) in the alarm configuration.
LeakDetectionConfig_t::limitResetTime
time_t limitResetTime
limit reset time e.g. 5 mins, 24h, ....
Definition: LeakDetectionConfig.h:18
TotalLeakDetection::HTML_detected
friend String HTML_detected(const TotalLeakDetection &totalLeakDetection)
Associated function for returing information about a total-water leak being detected (1)
LeakDetectionConfig_t::limitPulseAction
int limitPulseAction
limit action in pulses
Definition: LeakDetectionConfig.h:20
TotalLeakDetection::HTML_activePercentage
friend String HTML_activePercentage(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the [%] information about how close a leak is from being detected.
TOTAL_ILOCK_LED_PIN
#define TOTAL_ILOCK_LED_PIN
total-water leak bypass button
Definition: Pins.h:34
ALeakDetectable::resetWhenDetected
int resetWhenDetected
the reset value (%) when a leak was detected
Definition: ALeakDetectable.h:74
TotalLeakDetection
Definition: TotalLeakDetection.h:15
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
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
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
TotalLeakDetection::HTML_resetLimitDaysAlarm
friend String HTML_resetLimitDaysAlarm(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the reset time (days) in the alarm configuration.
TotalLeakDetection.h
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
totalLeakDetection
TotalLeakDetection * totalLeakDetection
Definition: main.cpp:53
TotalLeakDetection::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...
TotalLeakDetection::HTML_detectedText
friend String HTML_detectedText(const TotalLeakDetection &totalLeakDetection)
Associated function for returing information about a total-water leak being detected (2)
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
TotalLeakDetection::startDayPulseTime
time_t startDayPulseTime
the start of the monoring period
Definition: TotalLeakDetection.h:19
TotalLeakDetection::HTML_detectionLimit
friend String HTML_detectionLimit(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the limit (detection) in terms of the normal configuration.
TotalLeakDetection::HTML_resetLimitMins
friend String HTML_resetLimitMins(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the reset time (mins) in the normal configuration.
TotalLeakDetection::getPercentLeakDetectionReset
float getPercentLeakDetectionReset() const
Returns percentage information about how close a total-water leak is from being detected.
Definition: TotalLeakDetection.cpp:103
TotalLeakDetection::HTML_resetLimitSecsAlarm
friend String HTML_resetLimitSecsAlarm(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the reset time (secs) 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
TotalLeakDetection::HTML_bypass
friend String HTML_bypass(const TotalLeakDetection &totalLeakDetection)
Associated function for returing information about the state of the bypass.
DateTime::getDateTimeStr
String getDateTimeStr() const
Returns the current datatime in a string format.
Definition: DateTime.cpp:47
TotalLeakDetection::HTML_resetLimitHours
friend String HTML_resetLimitHours(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the reset time (hours) in the normal configuration.
TotalLeakDetection::HTML_resetLimitDays
friend String HTML_resetLimitDays(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the reset time (days) in the normal configuration.
SECS
#define SECS(time)
Calculates the number of seconds when formatting the time given in milliseconds.
Definition: DateTime.h:92
TotalLeakDetection::reset
void reset() override
Resets the algorithm.
Definition: TotalLeakDetection.cpp:53
ALeakDetectable::active
int active
indication if a leak has been detected + bypass is off
Definition: ALeakDetectable.h:71
PulseCounter
Definition: PulseCounter.h:29
TotalLeakDetection::testActiveLeak
void testActiveLeak() override
Tests if the total-water the algorithm should be reset if the current monitoring period is over.
Definition: TotalLeakDetection.cpp:69
TotalLeakDetection::testResetLeak
void testResetLeak() override
Tests if a total-water leak has occurred, and if so, it will set the appropriate variables and flags.
Definition: TotalLeakDetection.cpp:58
TotalLeakDetection::HTML_activePercentageColor
friend String HTML_activePercentageColor(const TotalLeakDetection &totalLeakDetection)
Associated function for returing a color according to [%] information about how close a leak is from ...
DELTA_TIME
#define DELTA_TIME(time1, time2)
Calculates the time difference between two times given as parameters.
Definition: DateTime.h:35
HighLeakDetection::getPercentLeakDetected
float getPercentLeakDetected() const
Returns percentage information about how close the algorithm is from being reset.
Definition: HighLeakDetection.cpp:142
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
TotalLeakDetection::HTML_resetPercentage
friend String HTML_resetPercentage(const TotalLeakDetection &totalLeakDetection)
Associated function for returing the [%] information about how close the algorithm is from being rese...
ALeakDetectable::normalConfig
LeakDetectionConfig_t normalConfig
normal configuarion for when the house is occupied
Definition: ALeakDetectable.h:66
TotalLeakDetection::htmlData
std::map< int, String(*)(const TotalLeakDetection &highLeakDetection)> htmlData
A map of different values (keys) and their associated functions which returns the appropriate values.
Definition: TotalLeakDetection.h:83
EmailSender::getInstance
static EmailSender * getInstance()
Returns the instance of the class.
Definition: EmailSender.cpp:87