Important Update:

- DateTimeCore:
  - Added new functions Get/SetDateTime() for combined date & time get/set
  - Added LocalTime param to ReadDate/Time() and BuildDate/TimeStr()
    - Allow input time to interpretted as ether UTC or local time
- DeviceCore:
  - Added SourceRef to HandleCommand()
This commit is contained in:
Charl Wentzel
2019-06-19 11:18:34 +02:00
parent 794b7e5486
commit 207caa696d
5 changed files with 129 additions and 58 deletions

View File

@@ -20,24 +20,24 @@ static char ReturnStr[30];
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Set current time on real-time clock
bool SetTime( unsigned char Hours, unsigned char Minutes, unsigned char Seconds ) bool SetTime( unsigned char Hours, unsigned char Minutes, unsigned char Seconds )
{ {
struct tm NewTime; struct tm NewTime;
struct timeval tv; struct timeval tv;
struct timezone tz; struct timezone tz;
// Get current date and time // Get current Epoch (UTC)
gettimeofday( &tv, &tz); gettimeofday( &tv, &tz);
// Change to new time // Convert to local time
localtime_r( &tv.tv_sec, &NewTime ); localtime_r( &tv.tv_sec, &NewTime );
// Set new time (keep date as is)
NewTime.tm_hour = Hours; NewTime.tm_hour = Hours;
NewTime.tm_min = Minutes; NewTime.tm_min = Minutes;
NewTime.tm_sec = Seconds; NewTime.tm_sec = Seconds;
// Convert back // Convert back from local time to epoch time
tv.tv_sec = mktime( &NewTime ); tv.tv_sec = mktime( &NewTime );
tv.tv_usec = 0; tv.tv_usec = 0;
@@ -47,43 +47,24 @@ bool SetTime( unsigned char Hours, unsigned char Minutes, unsigned char Seconds
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Get current time from real-time clock
bool GetTime( unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds )
{
struct tm CurrentTime;
time_t UTC;
// Get current date and time
time( &UTC );
localtime_r( &UTC, &CurrentTime );
// Extract time
Hours = CurrentTime.tm_hour;
Minutes = CurrentTime.tm_min;
Seconds = CurrentTime.tm_sec;
return true;
}
//---------------------------------------------------------------------------
// Set current date on real-time clock
bool SetDate( unsigned char Day, unsigned char Month, unsigned Year ) bool SetDate( unsigned char Day, unsigned char Month, unsigned Year )
{ {
struct tm NewDate; struct tm NewDate;
struct timeval tv; struct timeval tv;
struct timezone tz; struct timezone tz;
// Get current date and time // Get current Epoch (UTC)
gettimeofday( &tv, &tz ); gettimeofday( &tv, &tz );
// Change to new time // Convert to local time
localtime_r( &tv.tv_sec, &NewDate ); localtime_r( &tv.tv_sec, &NewDate );
// Set new date (keep time as is)
NewDate.tm_year = Year - 1900; NewDate.tm_year = Year - 1900;
NewDate.tm_mon = Month - 1; NewDate.tm_mon = Month - 1;
NewDate.tm_mday = Day; NewDate.tm_mday = Day;
// Convert back // Convert back from local time to epoch time
tv.tv_sec = mktime( &NewDate ); tv.tv_sec = mktime( &NewDate );
tv.tv_usec = 0; tv.tv_usec = 0;
@@ -94,17 +75,70 @@ bool SetDate( unsigned char Day, unsigned char Month, unsigned Year )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Get current data from real-time clock bool SetDateTime( unsigned char Day, unsigned char Month, unsigned Year,
unsigned char Hours, unsigned char Minutes, unsigned char Seconds )
{
struct tm NewTime;
struct timeval tv;
struct timezone tz;
// Get current Epoch (UTC)
gettimeofday( &tv, &tz);
// Convert to local time
localtime_r( &tv.tv_sec, &NewTime );
// Set new date & time
NewTime.tm_year = Year - 1900;
NewTime.tm_mon = Month - 1;
NewTime.tm_mday = Day;
NewTime.tm_hour = Hours;
NewTime.tm_min = Minutes;
NewTime.tm_sec = Seconds;
// Convert back from local time to epoch time
tv.tv_sec = mktime( &NewTime );
tv.tv_usec = 0;
// Set date and new time
settimeofday( &tv, &tz);
return true;
}
//---------------------------------------------------------------------------
bool GetTime( unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds )
{
struct tm CurrentTime;
time_t UTC;
// Get current UTC date and time
time( &UTC );
// Convert to local time
localtime_r( &UTC, &CurrentTime );
// Extract time only
Hours = CurrentTime.tm_hour;
Minutes = CurrentTime.tm_min;
Seconds = CurrentTime.tm_sec;
return true;
}
//---------------------------------------------------------------------------
bool GetDate( unsigned char &Day, unsigned char &Month, unsigned &Year ) bool GetDate( unsigned char &Day, unsigned char &Month, unsigned &Year )
{ {
struct tm CurrentDate; struct tm CurrentDate;
time_t UTC; time_t UTC;
// Get current date and time // Get current UTC date and time
time( &UTC ); time( &UTC );
// Convert to local time
localtime_r( &UTC, &CurrentDate ); localtime_r( &UTC, &CurrentDate );
// Extract date // Extract date only
Day = CurrentDate.tm_mday; Day = CurrentDate.tm_mday;
Month = CurrentDate.tm_mon + 1; Month = CurrentDate.tm_mon + 1;
Year = CurrentDate.tm_year + 1900; Year = CurrentDate.tm_year + 1900;
@@ -113,7 +147,31 @@ bool GetDate( unsigned char &Day, unsigned char &Month, unsigned &Year )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Get the current date in a string bool GetDateTime( unsigned char &Day, unsigned char &Month, unsigned &Year,
unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds )
{
struct tm CurrentTime;
time_t UTC;
// Get current UTC date and time
time( &UTC );
// Convert to local time
localtime_r( &UTC, &CurrentTime );
// Extract Date & time
Day = CurrentTime.tm_mday;
Month = CurrentTime.tm_mon + 1;
Year = CurrentTime.tm_year + 1900;
Hours = CurrentTime.tm_hour;
Minutes = CurrentTime.tm_min;
Seconds = CurrentTime.tm_sec;
return true;
}
//---------------------------------------------------------------------------
char const * GetDateStr( const char * DateSeparator ) char const * GetDateStr( const char * DateSeparator )
{ {
unsigned char Day; unsigned char Day;
@@ -172,12 +230,15 @@ char const * GetDateTimeStr( const char * DateSeparator, const char * TimeSepar
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Get current time from real-time clock // Get current time from real-time clock
bool ReadTime( const time_t EpochTime, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ) bool ReadTime( const time_t EpochTime, bool LocalTime, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds )
{ {
struct tm CurrentTime; struct tm CurrentTime;
// Get current date and time // Get current date and time
localtime_r( &EpochTime, &CurrentTime ); if (LocalTime)
localtime_r( &EpochTime, &CurrentTime );
else
gmtime_r( &EpochTime, &CurrentTime );
// Extract time // Extract time
Hours = CurrentTime.tm_hour; Hours = CurrentTime.tm_hour;
@@ -188,12 +249,15 @@ bool ReadTime( const time_t EpochTime, unsigned char &Hours, unsigned char &Minu
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool ReadDate( const time_t EpochTime, unsigned char &Day, unsigned char &Month, unsigned &Year ) bool ReadDate( const time_t EpochTime, bool LocalTime, unsigned char &Day, unsigned char &Month, unsigned &Year )
{ {
struct tm CurrentDate; struct tm CurrentDate;
// Get current date and time // Get current date and time
localtime_r( &EpochTime, &CurrentDate ); if (LocalTime)
localtime_r( &EpochTime, &CurrentDate );
else
gmtime_r( &EpochTime, &CurrentDate );
// Extract date // Extract date
Day = CurrentDate.tm_mday; Day = CurrentDate.tm_mday;
@@ -204,13 +268,16 @@ bool ReadDate( const time_t EpochTime, unsigned char &Day, unsigned char &Month,
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool ReadDateTime( const time_t EpochTime, unsigned char &Day, unsigned char &Month, unsigned &Year, bool ReadDateTime( const time_t EpochTime, bool LocalTime, unsigned char &Day, unsigned char &Month, unsigned &Year,
unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ) unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds )
{ {
struct tm CurrentTime; struct tm CurrentTime;
// Get current date and time // Get current date and time
localtime_r( &EpochTime, &CurrentTime ); if (LocalTime)
localtime_r( &EpochTime, &CurrentTime );
else
gmtime_r( &EpochTime, &CurrentTime );
// Extract time // Extract time
Hours = CurrentTime.tm_hour; Hours = CurrentTime.tm_hour;
@@ -247,7 +314,7 @@ bool ReadTimeStr( const char *DateTimeStr, unsigned char &Hours, unsigned char
Seconds = (TempSeconds != -1)? TempSeconds : (Maxtime)? 59 : 0; Seconds = (TempSeconds != -1)? TempSeconds : (Maxtime)? 59 : 0;
// Check if success // Check if success
return ((ItemsAssigned == 6)? true : false); return ((ItemsAssigned >= 3)? true : false);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -271,7 +338,7 @@ bool ReadDateStr( const char *DateTimeStr, unsigned char &Day, unsigned char &M
Year = TempYear; Year = TempYear;
// Check if success // Check if success
return ((ItemsAssigned == 6)? true : false); return ((ItemsAssigned >= 3)? true : false);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -300,19 +367,19 @@ bool ReadDateTimeStr( const char *DateTimeStr, unsigned char &Day, unsigned cha
Seconds = (TempSeconds != -1)? TempSeconds : (Maxtime)? 59 : 0; Seconds = (TempSeconds != -1)? TempSeconds : (Maxtime)? 59 : 0;
// Check if success // Check if success
return ((ItemsAssigned == 6)? true : false); return ((ItemsAssigned >= 3)? true : false);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Get the current date in a string // Get the current date in a string
char const * BuildDateStr( const time_t EpochTime, const char * DateSeparator ) char const * BuildDateStr( const time_t EpochTime, bool LocalTime, const char * DateSeparator )
{ {
unsigned char Day; unsigned char Day;
unsigned char Month; unsigned char Month;
unsigned int Year; unsigned int Year;
// Get Date // Get Date
ReadDate( EpochTime, Day, Month, Year ); ReadDate( EpochTime, LocalTime, Day, Month, Year );
// Build String // Build String
sprintf( ReturnStr, "%04d%s%02d%s%02d", sprintf( ReturnStr, "%04d%s%02d%s%02d",
@@ -323,14 +390,14 @@ char const * BuildDateStr( const time_t EpochTime, const char * DateSeparator )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
char const * BuildTimeStr( const time_t EpochTime, const char * TimeSeparator ) char const * BuildTimeStr( const time_t EpochTime, bool LocalTime, const char * TimeSeparator )
{ {
unsigned char Hours; unsigned char Hours;
unsigned char Minutes; unsigned char Minutes;
unsigned char Seconds; unsigned char Seconds;
// Get Date & Time // Get Date & Time
ReadTime( EpochTime, Hours, Minutes, Seconds ); ReadTime( EpochTime, LocalTime, Hours, Minutes, Seconds );
// Build String // Build String
sprintf( ReturnStr, "%02d%s%02d%s%02d", sprintf( ReturnStr, "%02d%s%02d%s%02d",
@@ -340,7 +407,7 @@ char const * BuildTimeStr( const time_t EpochTime, const char * TimeSeparator )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
char const * BuildDateTimeStr( const time_t EpochTime, const char * DateSeparator, const char * TimeSeparator ) char const * BuildDateTimeStr( const time_t EpochTime, bool LocalTime, const char * DateSeparator, const char * TimeSeparator )
{ {
unsigned char Day; unsigned char Day;
unsigned char Month; unsigned char Month;
@@ -350,7 +417,7 @@ char const * BuildDateTimeStr( const time_t EpochTime, const char * DateSeparato
unsigned char Seconds; unsigned char Seconds;
// Get Date & Time // Get Date & Time
ReadDateTime( EpochTime, Day, Month, Year, Hours, Minutes, Seconds ); ReadDateTime( EpochTime, LocalTime, Day, Month, Year, Hours, Minutes, Seconds );
// Build String // Build String
sprintf( ReturnStr, "%04d%s%02d%s%02d %02d%s%02d%s%02d", sprintf( ReturnStr, "%04d%s%02d%s%02d %02d%s%02d%s%02d",

View File

@@ -17,11 +17,15 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Get and set System Date and Time // Get and set System Date and Time
bool SetTime( unsigned char Hours, unsigned char Minutes, unsigned char Seconds );
bool GetTime( unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ); bool GetTime( unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds );
bool SetDate( unsigned char Day, unsigned char Month, unsigned Year );
bool GetDate( unsigned char &Day, unsigned char &Month, unsigned &Year ); bool GetDate( unsigned char &Day, unsigned char &Month, unsigned &Year );
bool GetDateTime( unsigned char &Day, unsigned char &Month, unsigned &Year,
unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds );
bool SetTime( unsigned char Hours, unsigned char Minutes, unsigned char Seconds );
bool SetDate( unsigned char Day, unsigned char Month, unsigned Year );
bool SetDateTime( unsigned char Day, unsigned char Month, unsigned Year,
unsigned char Hours, unsigned char Minutes, unsigned char Seconds );
char const * GetDateStr( const char * DateSeparator = "/" ); char const * GetDateStr( const char * DateSeparator = "/" );
char const * GetTimeStr( const char * TimeSeparator = ":" ); char const * GetTimeStr( const char * TimeSeparator = ":" );
@@ -29,9 +33,10 @@ char const * GetDateTimeStr( const char * DateSeparator = "/", const char * Tim
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool ReadTime( const time_t EpochTime, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ); // Build or interpret time data
bool ReadDate( const time_t EpochTime, unsigned char &Day, unsigned char &Month, unsigned &Year ); bool ReadTime( const time_t EpochTime, bool LocalTime, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds );
bool ReadDateTime( const time_t EpochTime, unsigned char &Day, unsigned char &Month, unsigned &Year, bool ReadDate( const time_t EpochTime, bool LocalTime, unsigned char &Day, unsigned char &Month, unsigned &Year );
bool ReadDateTime( const time_t EpochTime, bool LocalTime, unsigned char &Day, unsigned char &Month, unsigned &Year,
unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ); unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds );
bool ReadTimeStr( const char *DateTimeStr, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime ); bool ReadTimeStr( const char *DateTimeStr, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime );
@@ -39,9 +44,9 @@ bool ReadDateStr( const char *DateTimeStr, unsigned char &Day, unsigned char
bool ReadDateTimeStr( const char *DateTimeStr, unsigned char &Day, unsigned char &Month, unsigned &Year, bool ReadDateTimeStr( const char *DateTimeStr, unsigned char &Day, unsigned char &Month, unsigned &Year,
unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime ); unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime );
char const * BuildDateStr( const time_t EpochTime, const char * DateSeparator = "/" ); char const * BuildDateStr( const time_t EpochTime, bool LocalTime, const char * DateSeparator = "/" );
char const * BuildTimeStr( const time_t EpochTime, const char * TimeSeparator = ":" ); char const * BuildTimeStr( const time_t EpochTime, bool LocalTime, const char * TimeSeparator = ":" );
char const * BuildDateTimeStr( const time_t EpochTime, const char * DateSeparator = "/", const char * TimeSeparator = ":" ); char const * BuildDateTimeStr( const time_t EpochTime, bool LocalTime, const char * DateSeparator = "/", const char * TimeSeparator = ":" );
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@@ -1534,7 +1534,7 @@ bool CDeviceCore::GetCmdParam( const char * Start, char * Param, char ** NextPar
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
int CDeviceCore::HandleCommand( const char *ChannelName, const char * Data, const int MaxLen ) int CDeviceCore::HandleCommand( const char *ChannelName, const char * SourceRef, const char * Data, const int MaxLen )
{ {
int Len; int Len;
char Value[50]; char Value[50];

View File

@@ -387,7 +387,7 @@ public:
// Handle Interface Commands // Handle Interface Commands
bool GetCmdParam( const char * Start, char * Param, char ** NextParam ); bool GetCmdParam( const char * Start, char * Param, char ** NextParam );
int HandleCommand( const char *ChannelName, const char * Data, const int MaxLen ); int HandleCommand( const char *ChannelName, const char * SourceRef, const char * Data, const int MaxLen );
// Command Text Interfaces // Command Text Interfaces
bool SetValue( TDeviceParam * Param, const char * Value, const int Len, bool Force ); bool SetValue( TDeviceParam * Param, const char * Value, const int Len, bool Force );

View File

@@ -43,7 +43,6 @@ CFunctionCore::CFunctionCore( const char * pName, const char * pType ) : Type( p
CFunctionCore::~CFunctionCore() CFunctionCore::~CFunctionCore()
{ {
TChannel * NextChannel = NULL; TChannel * NextChannel = NULL;
TChannelLink * NextLinkedChannel = NULL;
// Destroy Channels // Destroy Channels
while (FirstChannel) while (FirstChannel)