HomeWaterLeaksDetection
DateTime.h
Go to the documentation of this file.
1 #ifndef UNTITLED_DATETIME_H
2 #define UNTITLED_DATETIME_H
3 
4 #include "Wire.h"
5 #include "DS3231.h"
6 #include "Arduino.h"
7 
8 #include "Setup.h"
9 
10 // if the webserver is enabled,
11 // include these files as well
12 #ifdef WEB_SERVER
13 # include <map>
14 # include <HTMLDataSource.h>
15 #endif
16 
20 typedef unsigned long time_t;
21 
35 #define DELTA_TIME(time1, time2) \
36 ((time_t)((time1) - (time2)))
37 
38 //==============TIME CONSTANTS DEFINITIONS==============
39 
41 #define MILLIS_PER_SEC 1000UL
42 #define SECS_PER_MIN 60UL
44 #define MINS_PER_HOUR 60UL
46 #define HOURS_PER_DAY 24UL
48 
50 #define MILLIS_PER_MIN (MILLIS_PER_SEC * SECS_PER_MIN)
51 #define MILLIS_PER_HOD (MILLIS_PER_MIN * MINS_PER_HOUR)
53 #define MILLIS_PER_DAY (MILLIS_PER_HOD * HOURS_PER_DAY)
55 
56 //====================TIME CONVERSION===================
57 
61 #define SEC_TO_MILLIS(s) ((time_t)(s) * MILLIS_PER_SEC)
62 
66 #define MIN_TO_MILLIS(m) ((time_t)(m) * MILLIS_PER_MIN)
67 
71 #define HOD_TO_MILLIS(h) ((time_t)(h) * MILLIS_PER_HOD)
72 
76 #define DAY_TO_MILLIS(d) ((time_t)(d) * MILLIS_PER_DAY)
77 
78 //====================TIME FORMATTING===================
79 
85 #define MILLIS(time) ((time) % MILLIS_PER_SEC)
86 
92 #define SECS(time) (((time) / MILLIS_PER_SEC) % SECS_PER_MIN)
93 
99 #define MINS(time) (((time) / MILLIS_PER_MIN) % MINS_PER_HOUR)
100 
106 #define HOURS(time) (((time) / MILLIS_PER_HOD) % HOURS_PER_DAY)
107 
113 #define DAYS(time) ((time) / MILLIS_PER_DAY)
114 
122 #define FORMAT_2_DIGITS(x) \
123 ((x) >= 10 ? String(x) : ("0" + String(x)))
124 
133 #define FORMAT_3_DIGITS(x) \
134 ((x) >= 100 ? String(x) : ( \
135 (x) >= 10 ? ("0" + String(x)) : ("00" + String(x))))
136 
141 #define FORMAT_TIME(time) \
142 (String(DAYS(time)) + ":" + \
143  String(FORMAT_2_DIGITS(HOURS(time))) + ":" + \
144  String(FORMAT_2_DIGITS(MINS(time))) + ":" + \
145  String(FORMAT_2_DIGITS(SECS(time))) + ":" + \
146  String(FORMAT_3_DIGITS(MILLIS(time))) \
147 )
148 
149 //=================HTMLDataSource class=================
150 
158 class DateTime
159 #ifdef WEB_SERVER
160  : public HTMLDataSource
161 #endif
162 {
163 //====================private data======================
164 private:
165  static DateTime* instance;
166  DS3231 rtc;
167  String startDate;
168  String startTime;
169 
170 //==================private methods=====================
171 private:
173  DateTime();
174 
177 
179  DateTime(DateTime const&) {};
180 
182  DateTime& operator=(DateTime const&) { return *this; }
183 
184 //==================public methods======================
185 public:
192  static DateTime* getInstance();
193 
196  String getDateTimeStr() const;
197 
200  String getDateStr() const;
201 
204  String getTimeStr() const;
205 
208  RTCDateTime getDateTime() const;
209 
210 //====================web server========================
211 #ifdef WEB_SERVER
212 private:
215  std::map<int, String (*)(const DateTime& dateTime)> htmlData;
216 
217 public:
225  const String getHTMLData(const int id) const;
226 
227 private:
231  friend String HTML_startDate(const DateTime& dateTime);
232 
236  friend String HTML_startTime(const DateTime& dateTime);
237 #endif
238 };
239 
240 #endif
DateTime::instance
static DateTime * instance
the instance of the class (Singleton)
Definition: DateTime.h:165
DateTime::DateTime
DateTime(DateTime const &)
Copy constructor of the class.
Definition: DateTime.h:179
WEB_SERVER
#define WEB_SERVER
enable the webserver
Definition: Setup.h:13
DateTime::getDateStr
String getDateStr() const
Returns the current day in a string format.
Definition: DateTime.cpp:39
HTMLDataSource.h
Setup.h
DateTime::getHTMLData
const String getHTMLData(const int id) const
Since this class is registered as a source of data for the HTML content, it needs return the appropri...
DateTime::HTML_startDate
friend String HTML_startDate(const DateTime &dateTime)
Associated function for returing the start day of the device.
DateTime::~DateTime
~DateTime()
Destructor of the class.
DateTime::getInstance
static DateTime * getInstance()
Returns the instance of the class.
Definition: DateTime.cpp:26
DateTime::startTime
String startTime
start time of the system
Definition: DateTime.h:168
time_t
unsigned long time_t
Referring to the data type unsigned long as time_t.
Definition: DateTime.h:20
DateTime::DateTime
DateTime()
Constructor of the class.
Definition: DateTime.cpp:10
DateTime::htmlData
std::map< int, String(*)(const DateTime &dateTime)> htmlData
A map of different values (keys) and their associated functions which returns the appropriate values.
Definition: DateTime.h:215
DateTime::operator=
DateTime & operator=(DateTime const &)
Assignment operator of the class.
Definition: DateTime.h:182
DateTime::HTML_startTime
friend String HTML_startTime(const DateTime &dateTime)
Associated function for returing the start time of the device.
DateTime::rtc
DS3231 rtc
real-time module
Definition: DateTime.h:166
DateTime::getDateTimeStr
String getDateTimeStr() const
Returns the current datatime in a string format.
Definition: DateTime.cpp:47
DateTime::startDate
String startDate
start day of the system
Definition: DateTime.h:167
DateTime
Definition: DateTime.h:162
HTMLDataSource
Definition: HTMLDataSource.h:15
DateTime::getDateTime
RTCDateTime getDateTime() const
Returns the current datatime.
Definition: DateTime.cpp:51
DateTime::getTimeStr
String getTimeStr() const
Returns the current time in a string format.
Definition: DateTime.cpp:32