HomeWaterLeaksDetection
LeaksController.cpp
Go to the documentation of this file.
1 #include "LeaksController.h"
2 
3 #ifdef WEB_SERVER
4  String HTML_valve(const LeaksController& leaksController);
5  String HTML_alarm(const LeaksController& leaksController);
6  String HTML_manualClose(const LeaksController& leaksController);
7  String HTML_dailyConsumption(const LeaksController& leaksController);
8  String HTML_monthlyConsumption(const LeaksController& leaksController);
9 #endif
10 
12 
14  valveState = LOW;
15  stateOfHomeAlarm = LOW;
16 
17  #ifdef WEB_SERVER
18  // functions providing data to the webserver
19  htmlData[13] = &HTML_valve;
20  htmlData[14] = &HTML_alarm;
24  #endif
25 }
26 
28  if (instance == NULL)
30  return instance;
31 }
32 
34  leaks[type] = leak;
35 }
36 
38  return leaks.size();
39 }
40 
41 #ifdef WEB_SERVER
42  void LeaksController::setDailyConsumptionCounter(Consumption *dailyConsumptionCounter) {
43  this->dailyConsumptionCounter = dailyConsumptionCounter;
44  }
45 
46  void LeaksController::setMonthlyConsumptionCounter(Consumption *monthlyConsumptionCounter) {
47  this->monthlyConsumptionCounter = monthlyConsumptionCounter;
48  }
49 #endif
50 
51 void LeaksController::changeSettings(ALeakDetectable::Type type, unsigned long detected, unsigned long reset, bool alarm) {
52  // tests if that kind of water leak
53  // detection algorithm exists
54  auto it = leaks.find(type);
55  if (it == leaks.end())
56  return;
57 
58  // get the current settings (makes a copy
59  // of the original one)
60  ALeakDetectable *leak = it->second;
61  LeakDetectionConfig_t config = alarm ? leak->getAlarmConfig() : leak->getNormalConfig();
62 
63  // high-water leak detection algorithm
64  if (type == ALeakDetectable::High) {
65  config.limitPulseAction = detected; // limit
66  config.limitResetTime = reset; // reset
67  }
68  // low-water leak detection algorithm
69  else if (type == ALeakDetectable::Low) {
70  config.limitActionTime = detected; // limit
71  config.limitResetTime = reset; // reset
72  }
73  // total-water leak detection algorithm
74  else if (type == ALeakDetectable::Total) {
75  config.limitPulseAction = detected; // limit
76  config.limitResetTime = reset; // reset
77  }
78  // Update the settings
79  if (alarm)
80  leak->updateAlarmConfig(config);
81  else leak->updateNormalConfig(config);
82 }
83 
85  return leaks[ALeakDetectable::High]->getFormatOfSettingsToSave() + "\n" +
86  leaks[ALeakDetectable::Low]->getFormatOfSettingsToSave() + "\n" +
87  leaks[ALeakDetectable::Total]->getFormatOfSettingsToSave();
88 }
89 
91  this->pulseCounter = pulseCounter;
92 }
93 
95  pulseCounter->update(); // update the pulse counter
96  manualValveClose = digitalRead(VALVE_MANUAL_CLOSE_PIN); // manual close reset button
97  int stateOfHomeAlarmTmp = digitalRead(HOME_ALARM_PIN); // read the state of the home alarm
98 
99  // if the state of the home alarm changed,
100  // send an e-mail to the user
101  #ifdef EMAIL_NOTIFICATION
102  if (stateOfHomeAlarmTmp != stateOfHomeAlarm) {
103  String state = stateOfHomeAlarmTmp ? "ACTIVE" : "NOT ACTIVE";
106  "Home alarm",
107  "Home alarm is now " + state + "\r\n"
108  "time: " + DateTime::getInstance()->getDateTimeStr()
109  );
110  }
111  #endif
112  stateOfHomeAlarm = stateOfHomeAlarmTmp;
113 
114  #ifdef WEB_SERVER
115  // update the daily and month water consumptions
118  #endif
119 
120  // check if any of the algorithms has detected a water leak
121  int isAnyLeakActive = 0;
122  for (const auto leak : leaks) {
123  // if the state of the home alarm changed
124  // notify all the water leak detection algorithms
125  #ifdef HOME_ALARM_SETTINGS
126  leak.second->changeStateOfHomeAlarm(stateOfHomeAlarm);
127  #endif
128 
129  leak.second->update();
130  if (leak.second->isActive())
131  isAnyLeakActive = 1;
132  }
133 
134  // check out if the state of the main valve should be changed
135  if ((isAnyLeakActive || manualValveClose) && valveState == LOW) {
136  valveState = HIGH;
137  } else if (!isAnyLeakActive && !manualValveClose && valveState == HIGH)
138  valveState = LOW;
139 
140  // check if the reset button is being presset
142  // reset all the algorithms
143  for (const auto leak : leaks)
144  leak.second->reset();
145 
146  // if the valve has not been closed
147  // manually by the user, open it up again
148  if (!manualValveClose)
149  valveState = LOW;
150 
151  // send a notification e-mail to the user
152  #ifdef EMAIL_NOTIFICATION
155  "Reset of the device",
156  "All the detection algorithms have been reset");
157  #endif
158  }
159 
160  digitalWrite(VALVE_PIN, !valveState); // send a signal to the main valve (0=close, 1=open)
161  digitalWrite(VALVE_LED_PIN, valveState); // send a signal to the LED of the main valve
162 
163  #ifdef EMAIL_NOTIFICATION
164  // if the state of the main valve's changed
165  // send an e-mail to the user
166  if (valveState != oldValveState) {
167  String state, msg;
168  if (valveState) {
169  state = "CLOSED";
170  msg = "open to closed";
171  } else {
172  state = "OPEN";
173  msg = "closed to open";
174  }
177  "Valve is now " + state,
178  "The state of the valve has been changed from " + msg);
179  }
180  #endif
181 }
182 
183 #ifdef DEBUG
184  const String LeaksController::getLogID() const { return "[LEAKS_CONTROLLER]"; }
185 
186  const String LeaksController::getLogDescription() const {
187  return "[" +
188  String("VALVE_STATE=") + String(valveState) + " | " +
189  String("HOME ALARM=") + String(stateOfHomeAlarm) + " | " +
190  String("MANUAL_CLOSE=") + String(manualValveClose) +
191  "]";
192  }
193 #endif
194 
195 #ifdef LCD_DISPLAY
196  const String LeaksController::getRow(int row) const {
197  switch(row) {
198  case 0:
199  // 1st row on the LCD
200  return String("Overview (1)");
201  case 1:
202  // 2nd row on the LCD
203  return String("VALVE STATE=") + String(valveState);
204  break;
205  case 2:
206  // 3rd row on the LCD
207  return String("HOME ALARM=") + String(stateOfHomeAlarm);
208  break;
209  case 3:
210  // 4th row on the LCD
211  return String("MANUAL CLOSE=") + String(manualValveClose);
212  break;
213  }
214  return "UNTITLED"; // for any other row return "UNTITLED"
215  }
216 #endif
217 
218 #ifdef WEB_SERVER
219  const String LeaksController::getHTMLData(const int id) const {
220  auto fce = htmlData.find(id);
221  if (fce == htmlData.end())
223  return fce->second(*this);
224  }
225 
226  String HTML_valve(const LeaksController& leaksController) {
227  if (leaksController.valveState == HIGH)
228  return String("OFF");
229  return String("ON");
230  }
231 
232  String HTML_alarm(const LeaksController& leaksController) {
233  if (leaksController.stateOfHomeAlarm)
234  return String("ON");
235  return String("OFF");
236  }
237 
238  String HTML_manualClose(const LeaksController& leaksController) {
239  if (leaksController.manualValveClose)
240  return String("ON");
241  return String("OFF");
242  }
243 
244  String HTML_dailyConsumption(const LeaksController& leaksController) {
245  int count = leaksController.dailyConsumptionCounter->getConsumptionCount();
246  return String(PULSE_TO_LITER(count));
247  }
248 
249  String HTML_monthlyConsumption(const LeaksController& leaksController) {
250  int count = leaksController.monthlyConsumptionCounter->getConsumptionCount();
251  return String(PULSE_TO_LITER(count));
252  }
253 #endif
ALeakDetectable::Total
@ Total
total water leak detection algorithm
Definition: ALeakDetectable.h:60
LeaksController::HTML_monthlyConsumption
friend String HTML_monthlyConsumption(const LeaksController &leaksController)
Associated function for returing the monthly water consumption in liters.
LeaksController::addLeakDetection
void addLeakDetection(ALeakDetectable::Type type, ALeakDetectable *leak)
Adds another water leak detection algorithm to the collection.
Definition: LeaksController.cpp:33
LeaksController::leaks
std::map< ALeakDetectable::Type, ALeakDetectable * > leaks
collection (map) of water leak detection algorithms
Definition: LeaksController.h:71
LeaksController::valveState
int valveState
state of the main valve
Definition: LeaksController.h:74
LeaksController::monthlyConsumptionCounter
Consumption * monthlyConsumptionCounter
monthly water consumption
Definition: LeaksController.h:186
HOME_ALARM_PIN
#define HOME_ALARM_PIN
home alarm
Definition: Pins.h:22
pulseCounter
PulseCounter pulseCounter(SENSOR_PIN)
Button::isPressed
int isPressed()
Tests if the button is being pressed.
Definition: Button.cpp:10
ALeakDetectable::Low
@ Low
low water leak detection algorithm
Definition: ALeakDetectable.h:58
LeaksController::LeaksController
LeaksController()
Constructor of the class.
Definition: LeaksController.cpp:13
LeaksController::manualValveClose
int manualValveClose
state of the "manual close" button
Definition: LeaksController.h:76
LeakDetectionConfig_t
Definition: LeakDetectionConfig.h:17
LeaksController::getNumberOfLeakDetections
int getNumberOfLeakDetections() const
Return the number of water leak detection algorithm.
Definition: LeaksController.cpp:37
LeakDetectionConfig_t::limitResetTime
time_t limitResetTime
limit reset time e.g. 5 mins, 24h, ....
Definition: LeakDetectionConfig.h:18
LeaksController::HTML_valve
friend String HTML_valve(const LeaksController &leaksController)
Associated function for returing the current state of the main valve.
LeakDetectionConfig_t::limitPulseAction
int limitPulseAction
limit action in pulses
Definition: LeakDetectionConfig.h:20
LeaksController::setMonthlyConsumptionCounter
void setMonthlyConsumptionCounter(Consumption *monthlyConsumptionCounter)
Sets an instance of Consumption (monthly water consumption)
LeaksController::stateOfHomeAlarm
int stateOfHomeAlarm
state of the home alarm
Definition: LeaksController.h:75
PulseCounter::update
void update()
Updates the class.
Definition: PulseCounter.cpp:25
Consumption::getConsumptionCount
int getConsumptionCount() const
Returns the current number of pulses detected so far within the monitoring period.
Definition: Consumption.cpp:44
ALeakDetectable
Definition: ALeakDetectable.h:52
ALeakDetectable::updateAlarmConfig
void updateAlarmConfig(LeakDetectionConfig_t newAlarmConfig)
Updates the alarm settings.
Definition: ALeakDetectable.cpp:71
LeaksController::htmlData
std::map< int, String(*)(const LeaksController &leaksController)> htmlData
A map of different values (keys) and their associated functions which returns the appropriate values.
Definition: LeaksController.h:190
ALeakDetectable::High
@ High
high water leak detection algorithm
Definition: ALeakDetectable.h:59
VALVE_MANUAL_CLOSE_PIN
#define VALVE_MANUAL_CLOSE_PIN
"manual close" button
Definition: Pins.h:18
LeaksController.h
LeaksController::HTML_manualClose
friend String HTML_manualClose(const LeaksController &leaksController)
Associated function for returing the current state of the "manual close" button.
Consumption::update
void update()
Updates the variables.
Definition: Consumption.cpp:11
ALeakDetectable::updateNormalConfig
void updateNormalConfig(LeakDetectionConfig_t newNormalConfig)
Updates the normal settings.
Definition: ALeakDetectable.cpp:58
DateTime::getInstance
static DateTime * getInstance()
Returns the instance of the class.
Definition: DateTime.cpp:26
HTMLDataSource::UNDEFINED_DATA
static const String UNDEFINED_DATA
string "UNDEFINED"
Definition: HTMLDataSource.h:17
Consumption
Definition: Consumption.h:13
LeakDetectionConfig_t::limitActionTime
time_t limitActionTime
limit action time (leak has been detected)
Definition: LeakDetectionConfig.h:19
VALVE_PIN
#define VALVE_PIN
main valve
Definition: Pins.h:14
VALVE_LED_PIN
#define VALVE_LED_PIN
main valve LED
Definition: Pins.h:15
LeaksController::setDailyConsumptionCounter
void setDailyConsumptionCounter(Consumption *dailyConsumptionCounter)
Sets an instance of Consumption (daily water consumption)
PULSE_TO_LITER
#define PULSE_TO_LITER(p)
Converts pulses to liters.
Definition: LimitsDefinition.h:17
LeaksController::instance
static LeaksController * instance
the instance of the class
Definition: LeaksController.h:70
LeaksController::update
void update() override
Updates the class.
Definition: LeaksController.cpp:94
ALeakDetectable::getAlarmConfig
LeakDetectionConfig_t getAlarmConfig()
Returns the current alarm config.
Definition: ALeakDetectable.cpp:26
LeaksController::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...
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
LeaksController::setPulseCounter
void setPulseCounter(PulseCounter *pulseCounter)
Sets an instance of PulseCounter.
Definition: LeaksController.cpp:90
EmailSender::VALVE_STATE
@ VALVE_STATE
when the state of the valve has changed
Definition: EmailSender.h:48
EmailSender::RESET
@ RESET
when the device resets
Definition: EmailSender.h:49
LeaksController::HTML_alarm
friend String HTML_alarm(const LeaksController &leaksController)
Associated function for returing the current state of the home alarm.
PulseCounter
Definition: PulseCounter.h:29
LeaksController::dailyConsumptionCounter
Consumption * dailyConsumptionCounter
daily water consumption
Definition: LeaksController.h:185
LeaksController::manualResetButton
Button manualResetButton
instance of Button (the reset buttton)
Definition: LeaksController.h:72
LeaksController::getFormatOfSettingsToSave
String getFormatOfSettingsToSave()
Returns all settings.
Definition: LeaksController.cpp:84
LeaksController
Definition: LeaksController.h:67
LeaksController::getRow
const String getRow(int row) const override
Returns the content of the row given as a parameter.
LeaksController::HTML_dailyConsumption
friend String HTML_dailyConsumption(const LeaksController &leaksController)
Associated function for returing the daily water consumption in liters.
ALeakDetectable::getNormalConfig
LeakDetectionConfig_t getNormalConfig()
Returns the current normal config.
Definition: ALeakDetectable.cpp:30
LeaksController::getInstance
static LeaksController * getInstance()
Returns the instance of the class.
Definition: LeaksController.cpp:27
EmailSender::ALARM
@ ALARM
when the state of the home alarm changes
Definition: EmailSender.h:50
LeaksController::changeSettings
void changeSettings(ALeakDetectable::Type type, unsigned long detected, unsigned long reset, bool alarm)
Changes settings of a water leak detection algorithm.
Definition: LeaksController.cpp:51
LeaksController::pulseCounter
PulseCounter * pulseCounter
instance of PulseCounter (output from the sensor)
Definition: LeaksController.h:77
EmailSender::getInstance
static EmailSender * getInstance()
Returns the instance of the class.
Definition: EmailSender.cpp:87