HomeWaterLeaksDetection
ALeakDetectable.cpp
Go to the documentation of this file.
1 #include "ALeakDetectable.h"
2 
4  Type type,
5  LeakDetectionConfig_t normalConfig,
6  LeakDetectionConfig_t alarmConfig) {
7 
8  // store all the params given through the constructor
9  this->normalConfig = normalConfig;
10  this->alarmConfig = alarmConfig;
13  this->type = type;
14  this->pulseCounter = pulseCounter;
15 
16  // the normal settings are used by default
18 
19  // initialize default values
22  stateOfHomeAlarm = 0;
23  oldBypassValue = 0;
24 }
25 
27  return alarmConfig;
28 }
29 
31  return normalConfig;
32 }
33 
35  // update the normal settings
37  #ifdef EMAIL_NOTIFICATION
38  sendEmailAboutSettings(EmailSender::APPLIED_NEW_SETTING, normalConfig, newNormalConfig, false);
39  #endif
42  }
43  // update the alarm settings
45  #ifdef EMAIL_NOTIFICATION
46  sendEmailAboutSettings(EmailSender::APPLIED_NEW_SETTING, alarmConfig, newAlarmConfig, true);
47  #endif
50  }
51  // set the current settings according to
52  // the state of the home alarm
53  if (stateOfHomeAlarm)
55  else config = &normalConfig;
56 }
57 
59  if (this->normalConfig != newNormalConfig) { // test if the settings are not the same
60  this->newNormalConfig = newNormalConfig; // store the new settings
61  requestUpdateNormalConfig = 1; // set the flag so it can be applied when the algorithm resets
62 
63  // if e-mail notifications are enabled send
64  // an e-mail to the user
65  #ifdef EMAIL_NOTIFICATION
66  sendEmailAboutSettings(EmailSender::CHANGED_SETTINGS, normalConfig, newNormalConfig, false);
67  #endif
68  }
69 }
70 
72  if (this->alarmConfig != newAlarmConfig) { // test if the settings are not the same
73  this->newAlarmConfig = newAlarmConfig; // store the new settings
74  requestUpdateAlarmConfig = 1; // set the flag so it can be applied when the algorithm resets
75 
76  // if e-mail notifications are enabled send
77  // an e-mail to the user
78  #ifdef EMAIL_NOTIFICATION
79  sendEmailAboutSettings(EmailSender::CHANGED_SETTINGS, alarmConfig, newAlarmConfig, true);
80  #endif
81  }
82 }
83 
84 String ALeakDetectable::getType() const {
85  switch (type) {
86  case High: return "High";
87  case Low: return "Low";
88  case Total: return "Total";
89  }
90  return "unknown";
91 }
92 
94  stateOfHomeAlarm = state;
95 }
96 
97 #ifdef EMAIL_NOTIFICATION
98  void ALeakDetectable::sendEmailAboutSettings(EmailSender::Type emailType, LeakDetectionConfig_t &oldSettings, LeakDetectionConfig_t &newSettings, bool alarm) const {
99  String data = "";
100  if (type == HIGH || type == Total) {
101  data += "current settings:\r\n";
102  data += "limit [l]: " + String(PULSE_TO_LITER(oldSettings.limitPulseAction)) + " | ";
103  data += "reset: " + FORMAT_TIME(oldSettings.limitResetTime) + "\r\n";
104  data += "new settings:\r\n";
105  data += "limit [l]: " + String(PULSE_TO_LITER(newSettings.limitPulseAction)) + " | ";
106  data += "reset: " + FORMAT_TIME(newSettings.limitResetTime) + "\r\n";
107  }
108  else if (type == Low){
109  data += "current settings:\r\n";
110  data += "limit: " + FORMAT_TIME(oldSettings.limitActionTime) + " | ";
111  data += "reset: " + FORMAT_TIME(oldSettings.limitResetTime) + "\r\n";
112  data += "new settings:\r\n";
113  data += "limit: " + FORMAT_TIME(newSettings.limitActionTime) + " | ";
114  data += "reset: " + FORMAT_TIME(newSettings.limitResetTime) + "\r\n";
115  }
116  String subject = getType();
117  subject += " leak detection settings ";
118  subject += emailType == EmailSender::CHANGED_SETTINGS ? "changed " : "put in place ";
119  subject += alarm ? "(ALARM)" : "(NORMAL)";
120 
121  EmailSender::getInstance()->sendEmail(emailType, subject, data);
122  }
123 #endif
EmailSender::CHANGED_SETTINGS
@ CHANGED_SETTINGS
when the user changes settings
Definition: EmailSender.h:51
ALeakDetectable::alarmConfig
LeakDetectionConfig_t alarmConfig
alarm configuration for when there's nobody in the house
Definition: ALeakDetectable.h:67
ALeakDetectable::Total
@ Total
total water leak detection algorithm
Definition: ALeakDetectable.h:60
ALeakDetectable::requestUpdateAlarmConfig
int requestUpdateAlarmConfig
flag if the new alarms config should be applied
Definition: ALeakDetectable.h:81
ALeakDetectable::changeStateOfHomeAlarm
void changeStateOfHomeAlarm(bool state)
Changes the state of the home alarm.
Definition: ALeakDetectable.cpp:93
ALeakDetectable::type
Type type
the type of the water leak detection algorithm
Definition: ALeakDetectable.h:76
FORMAT_TIME
#define FORMAT_TIME(time)
Converts a time in milliseconds into a formatted string.
Definition: DateTime.h:141
ALeakDetectable::getType
String getType() const
Returns the type of the water leak detection algorithm.
Definition: ALeakDetectable.cpp:84
pulseCounter
PulseCounter pulseCounter(SENSOR_PIN)
ALeakDetectable::Low
@ Low
low water leak detection algorithm
Definition: ALeakDetectable.h:58
LeakDetectionConfig_t
Definition: LeakDetectionConfig.h:17
EmailSender::Type
Type
different types of e-mail that can be sent to the user
Definition: EmailSender.h:44
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.h
ALeakDetectable::config
LeakDetectionConfig_t * config
pointer to the current configuration
Definition: ALeakDetectable.h:68
ALeakDetectable::updateAlarmConfig
void updateAlarmConfig(LeakDetectionConfig_t newAlarmConfig)
Updates the alarm settings.
Definition: ALeakDetectable.cpp:71
ALeakDetectable::High
@ High
high water leak detection algorithm
Definition: ALeakDetectable.h:59
ALeakDetectable::updateNormalConfig
void updateNormalConfig(LeakDetectionConfig_t newNormalConfig)
Updates the normal settings.
Definition: ALeakDetectable.cpp:58
ALeakDetectable::applyNewConfig
void applyNewConfig()
Applies the new config configuration.
Definition: ALeakDetectable.cpp:34
LeakDetectionConfig_t::limitActionTime
time_t limitActionTime
limit action time (leak has been detected)
Definition: LeakDetectionConfig.h:19
ALeakDetectable::stateOfHomeAlarm
int stateOfHomeAlarm
the current state of the home alarm
Definition: ALeakDetectable.h:72
ALeakDetectable::newNormalConfig
LeakDetectionConfig_t newNormalConfig
new parameters of the normal config when the user changes them
Definition: ALeakDetectable.h:78
PULSE_TO_LITER
#define PULSE_TO_LITER(p)
Converts pulses to liters.
Definition: LimitsDefinition.h:17
ALeakDetectable::getAlarmConfig
LeakDetectionConfig_t getAlarmConfig()
Returns the current alarm config.
Definition: ALeakDetectable.cpp:26
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
EmailSender::APPLIED_NEW_SETTING
@ APPLIED_NEW_SETTING
when the new settings are applied
Definition: EmailSender.h:52
ALeakDetectable::newAlarmConfig
LeakDetectionConfig_t newAlarmConfig
new parameters of the alarm config when the user changes them
Definition: ALeakDetectable.h:79
PulseCounter
Definition: PulseCounter.h:29
ALeakDetectable::oldBypassValue
int oldBypassValue
old bypass value (used for sending e-mails)
Definition: ALeakDetectable.h:70
ALeakDetectable::getNormalConfig
LeakDetectionConfig_t getNormalConfig()
Returns the current normal config.
Definition: ALeakDetectable.cpp:30
ALeakDetectable::pulseCounter
PulseCounter * pulseCounter
instance of a pulse counter (input of the system)
Definition: ALeakDetectable.h:65
ALeakDetectable::requestUpdateNormalConfig
int requestUpdateNormalConfig
flag if the new normal config should be applied
Definition: ALeakDetectable.h:80
ALeakDetectable::normalConfig
LeakDetectionConfig_t normalConfig
normal configuarion for when the house is occupied
Definition: ALeakDetectable.h:66
ALeakDetectable::ALeakDetectable
ALeakDetectable(PulseCounter *pulseCounter, Type type, LeakDetectionConfig_t normalConfig, LeakDetectionConfig_t alarmConfig)
Constructor of the class.
Definition: ALeakDetectable.cpp:3
EmailSender::getInstance
static EmailSender * getInstance()
Returns the instance of the class.
Definition: EmailSender.cpp:87