HomeWaterLeaksDetection
LeaksController Class Reference

#include <LeaksController.h>

Public Member Functions

void addLeakDetection (ALeakDetectable::Type type, ALeakDetectable *leak)
 Adds another water leak detection algorithm to the collection. More...
 
int getNumberOfLeakDetections () const
 Return the number of water leak detection algorithm. More...
 
void changeSettings (ALeakDetectable::Type type, unsigned long detected, unsigned long reset, bool alarm)
 Changes settings of a water leak detection algorithm. More...
 
void setPulseCounter (PulseCounter *pulseCounter)
 Sets an instance of PulseCounter. More...
 
void update () override
 Updates the class. More...
 
String getFormatOfSettingsToSave ()
 Returns all settings. More...
 
const String getRow (int row) const override
 Returns the content of the row given as a parameter. More...
 
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 appropriate value according to the id given as a parameter. More...
 
void setDailyConsumptionCounter (Consumption *dailyConsumptionCounter)
 Sets an instance of Consumption (daily water consumption) More...
 
void setMonthlyConsumptionCounter (Consumption *monthlyConsumptionCounter)
 Sets an instance of Consumption (monthly water consumption) More...
 

Static Public Member Functions

static LeaksControllergetInstance ()
 Returns the instance of the class. More...
 

Private Member Functions

 LeaksController ()
 Constructor of the class. More...
 
 LeaksController (LeaksController const &)
 Copy constructor of the class. More...
 
LeaksControlleroperator= (LeaksController const &)
 Assignment operator of the class. More...
 

Private Attributes

std::map< ALeakDetectable::Type, ALeakDetectable * > leaks
 collection (map) of water leak detection algorithms More...
 
Button manualResetButton = RESET_PIN
 instance of Button (the reset buttton) More...
 
int valveState
 state of the main valve More...
 
int stateOfHomeAlarm
 state of the home alarm More...
 
int manualValveClose
 state of the "manual close" button More...
 
PulseCounterpulseCounter = NULL
 instance of PulseCounter (output from the sensor) More...
 
ConsumptiondailyConsumptionCounter = NULL
 daily water consumption More...
 
ConsumptionmonthlyConsumptionCounter = NULL
 monthly water consumption More...
 
std::map< int, String(*)(const LeaksController &leaksController)> htmlData
 A map of different values (keys) and their associated functions which returns the appropriate values. More...
 

Static Private Attributes

static LeaksControllerinstance = NULL
 the instance of the class More...
 

Friends

String HTML_valve (const LeaksController &leaksController)
 Associated function for returing the current state of the main valve. More...
 
String HTML_alarm (const LeaksController &leaksController)
 Associated function for returing the current state of the home alarm. More...
 
String HTML_manualClose (const LeaksController &leaksController)
 Associated function for returing the current state of the "manual close" button. More...
 
String HTML_dailyConsumption (const LeaksController &leaksController)
 Associated function for returing the daily water consumption in liters. More...
 
String HTML_monthlyConsumption (const LeaksController &leaksController)
 Associated function for returing the monthly water consumption in liters. More...
 

Additional Inherited Members

- Static Public Attributes inherited from HTMLDataSource
static const String UNDEFINED_DATA = String("UNDEFINED")
 string "UNDEFINED" More...
 

Detailed Description

Author
silhavyj A17B0362P

This class sepresents a controller for the water leak detection part of the system. It takes care of reading the input values, such as the state of the home alarm, as well as upddating all the algorithms.

Definition at line 57 of file LeaksController.h.

Constructor & Destructor Documentation

◆ LeaksController() [1/2]

LeaksController::LeaksController ( )
private

Constructor of the class.

Definition at line 13 of file LeaksController.cpp.

13  {
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 }

References HTML_alarm, HTML_dailyConsumption, HTML_manualClose, HTML_monthlyConsumption, HTML_valve, htmlData, stateOfHomeAlarm, and valveState.

Referenced by getInstance().

◆ LeaksController() [2/2]

LeaksController::LeaksController ( LeaksController const &  )
inlineprivate

Copy constructor of the class.

Definition at line 85 of file LeaksController.h.

85 {};

Member Function Documentation

◆ addLeakDetection()

void LeaksController::addLeakDetection ( ALeakDetectable::Type  type,
ALeakDetectable leak 
)

Adds another water leak detection algorithm to the collection.

This method is used in main.cpp during initialization

Parameters
typeof the water leak detection algorithm
leakpointer to an instance of ALeakDetectable (water leak detection algorithm)

Definition at line 33 of file LeaksController.cpp.

33  {
34  leaks[type] = leak;
35 }

References leaks.

Referenced by setup().

◆ changeSettings()

void LeaksController::changeSettings ( ALeakDetectable::Type  type,
unsigned long  detected,
unsigned long  reset,
bool  alarm 
)

Changes settings of a water leak detection algorithm.

Parameters
typeof the water leak detection algorithm
detectedlimit (action) of a leak being detected
resetreset time value of the algorithm
alarmtype of the settings (normal/alarm)

Definition at line 51 of file LeaksController.cpp.

51  {
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 }

References ALeakDetectable::getAlarmConfig(), ALeakDetectable::getNormalConfig(), ALeakDetectable::High, leaks, LeakDetectionConfig_t::limitActionTime, LeakDetectionConfig_t::limitPulseAction, LeakDetectionConfig_t::limitResetTime, ALeakDetectable::Low, ALeakDetectable::Total, ALeakDetectable::updateAlarmConfig(), and ALeakDetectable::updateNormalConfig().

Referenced by WebServer::processHTTPRequestSettings().

◆ getFormatOfSettingsToSave()

String LeaksController::getFormatOfSettingsToSave ( )

Returns all settings.

This method is used when storing data on the SD card. It returns the settings of all water leak detection algorithms.

Returns
settings in a string format

Definition at line 84 of file LeaksController.cpp.

84  {
85  return leaks[ALeakDetectable::High]->getFormatOfSettingsToSave() + "\n" +
86  leaks[ALeakDetectable::Low]->getFormatOfSettingsToSave() + "\n" +
87  leaks[ALeakDetectable::Total]->getFormatOfSettingsToSave();
88 }

References ALeakDetectable::High, leaks, ALeakDetectable::Low, and ALeakDetectable::Total.

Referenced by WebServer::saveSettings().

◆ getHTMLData()

const String LeaksController::getHTMLData ( const int  id) const
overridevirtual

Since this class is registered as a source of data for the HTML content, it needs return the appropriate value according to the id given as a parameter.

Parameters
id- the id of the piece of data
Returns
If the class holds this piece of information, it will return it. Otherwise, "UNDEFINED" will be returned.

Implements HTMLDataSource.

◆ getInstance()

LeaksController * LeaksController::getInstance ( )
static

Returns the instance of the class.

If the instance has not been created, it will create it and then return it.

Returns
the instance of the class

Definition at line 27 of file LeaksController.cpp.

27  {
28  if (instance == NULL)
30  return instance;
31 }

References instance, and LeaksController().

Referenced by setup().

◆ getNumberOfLeakDetections()

int LeaksController::getNumberOfLeakDetections ( ) const

Return the number of water leak detection algorithm.

This is used when parsin an HTTP request so we know how many values there are supposed to be (settings).

Returns
number of water leak detection algorithm in the collection

Definition at line 37 of file LeaksController.cpp.

37  {
38  return leaks.size();
39 }

References leaks.

Referenced by WebServer::processHTTPRequest().

◆ getRow()

const String LeaksController::getRow ( int  row) const
overridevirtual

Returns the content of the row given as a parameter.

This class represents one page on the LCD display. Each of the 4 rows has different content identified by the number of the row.

Parameters
rownumber of the row
Returns
content to be displayed on that row of the LCD

Implements IDisplayable.

◆ operator=()

LeaksController& LeaksController::operator= ( LeaksController const &  )
inlineprivate

Assignment operator of the class.

Definition at line 88 of file LeaksController.h.

88 { return *this; }

◆ setDailyConsumptionCounter()

void LeaksController::setDailyConsumptionCounter ( Consumption dailyConsumptionCounter)

Sets an instance of Consumption (daily water consumption)

Parameters
dailyConsumptionCounterpointer to an instance of Consumption

Referenced by setup().

◆ setMonthlyConsumptionCounter()

void LeaksController::setMonthlyConsumptionCounter ( Consumption monthlyConsumptionCounter)

Sets an instance of Consumption (monthly water consumption)

Parameters
monthlyConsumptionCounterpointer to an instance of Consumption

Referenced by setup().

◆ setPulseCounter()

void LeaksController::setPulseCounter ( PulseCounter pulseCounter)

Sets an instance of PulseCounter.

This method is used in main.cpp during initialization

Parameters
pulseCounterpointer to an instance of PulseCounter

Definition at line 90 of file LeaksController.cpp.

90  {
91  this->pulseCounter = pulseCounter;
92 }

References pulseCounter.

Referenced by setup().

◆ update()

void LeaksController::update ( )
overridevirtual

Updates the class.

It updates step by step all the variables and instance it holds. This process includes reading the state of the input pins, updating all water leak detection algorithms, and if necessary, closing the the main valve.

Implements IControllable.

Definition at line 94 of file LeaksController.cpp.

94  {
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 }

References EmailSender::ALARM, dailyConsumptionCounter, EmailSender::getInstance(), DateTime::getInstance(), HOME_ALARM_PIN, Button::isPressed(), leaks, manualResetButton, manualValveClose, monthlyConsumptionCounter, pulseCounter, EmailSender::RESET, EmailSender::sendEmail(), stateOfHomeAlarm, Consumption::update(), PulseCounter::update(), VALVE_LED_PIN, VALVE_MANUAL_CLOSE_PIN, VALVE_PIN, EmailSender::VALVE_STATE, and valveState.

Referenced by WebServer::update().

Friends And Related Function Documentation

◆ HTML_alarm

String HTML_alarm ( const LeaksController leaksController)
friend

Associated function for returing the current state of the home alarm.

Parameters
leaksController- the instance of the LeaksController class
Returns
"ON"/"OFF" according to the state of the home alarm

Referenced by LeaksController().

◆ HTML_dailyConsumption

String HTML_dailyConsumption ( const LeaksController leaksController)
friend

Associated function for returing the daily water consumption in liters.

Parameters
leaksController- the instance of the LeaksController class
Returns
daily water consumption in liters

Referenced by LeaksController().

◆ HTML_manualClose

String HTML_manualClose ( const LeaksController leaksController)
friend

Associated function for returing the current state of the "manual close" button.

Parameters
leaksController- the instance of the LeaksController class
Returns
"ON"/"OFF" according to the state of the "manual close" button

Referenced by LeaksController().

◆ HTML_monthlyConsumption

String HTML_monthlyConsumption ( const LeaksController leaksController)
friend

Associated function for returing the monthly water consumption in liters.

Parameters
leaksController- the instance of the LeaksController class
Returns
monthly water consumption in liters

Referenced by LeaksController().

◆ HTML_valve

String HTML_valve ( const LeaksController leaksController)
friend

Associated function for returing the current state of the main valve.

Parameters
leaksController- the instance of the LeaksController class
Returns
"ON"/"OFF" according to the state of the main valve

Referenced by LeaksController().

Member Data Documentation

◆ dailyConsumptionCounter

Consumption* LeaksController::dailyConsumptionCounter = NULL
private

daily water consumption

Definition at line 185 of file LeaksController.h.

Referenced by update().

◆ htmlData

std::map<int, String (*)(const LeaksController& leaksController)> LeaksController::htmlData
private

A map of different values (keys) and their associated functions which returns the appropriate values.

Definition at line 190 of file LeaksController.h.

Referenced by LeaksController().

◆ instance

LeaksController * LeaksController::instance = NULL
staticprivate

the instance of the class

Definition at line 70 of file LeaksController.h.

Referenced by getInstance().

◆ leaks

std::map<ALeakDetectable::Type, ALeakDetectable*> LeaksController::leaks
private

collection (map) of water leak detection algorithms

Definition at line 71 of file LeaksController.h.

Referenced by addLeakDetection(), changeSettings(), getFormatOfSettingsToSave(), getNumberOfLeakDetections(), and update().

◆ manualResetButton

Button LeaksController::manualResetButton = RESET_PIN
private

instance of Button (the reset buttton)

Definition at line 72 of file LeaksController.h.

Referenced by update().

◆ manualValveClose

int LeaksController::manualValveClose
private

state of the "manual close" button

Definition at line 76 of file LeaksController.h.

Referenced by update().

◆ monthlyConsumptionCounter

Consumption* LeaksController::monthlyConsumptionCounter = NULL
private

monthly water consumption

Definition at line 186 of file LeaksController.h.

Referenced by update().

◆ pulseCounter

PulseCounter* LeaksController::pulseCounter = NULL
private

instance of PulseCounter (output from the sensor)

Definition at line 77 of file LeaksController.h.

Referenced by setPulseCounter(), and update().

◆ stateOfHomeAlarm

int LeaksController::stateOfHomeAlarm
private

state of the home alarm

Definition at line 75 of file LeaksController.h.

Referenced by LeaksController(), and update().

◆ valveState

int LeaksController::valveState
private

state of the main valve

Definition at line 74 of file LeaksController.h.

Referenced by LeaksController(), and update().


The documentation for this class was generated from the following files:
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::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
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
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::stateOfHomeAlarm
int stateOfHomeAlarm
state of the home alarm
Definition: LeaksController.h:75
PulseCounter::update
void update()
Updates the class.
Definition: PulseCounter.cpp:25
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::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
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::instance
static LeaksController * instance
the instance of the class
Definition: LeaksController.h:70
ALeakDetectable::getAlarmConfig
LeakDetectionConfig_t getAlarmConfig()
Returns the current alarm config.
Definition: ALeakDetectable.cpp:26
EmailSender::sendEmail
byte sendEmail(String subject, String data)
Sends an e-mail off to the smtp2go server.
Definition: EmailSender.cpp:93
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.
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::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
EmailSender::ALARM
@ ALARM
when the state of the home alarm changes
Definition: EmailSender.h:50
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