Skip to content

Commit

Permalink
Renaming Timestamp->TimeOfDay
Browse files Browse the repository at this point in the history
  • Loading branch information
lains committed May 8, 2024
1 parent 277df87 commit 4f1fb8c
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 255 deletions.
16 changes: 8 additions & 8 deletions inc/domain/PowerHistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include "FixedSizeRingBuffer.h"
#include "TicProcessingContext.h"
#include "TicFrameParser.h" // For TicEvaluatedPower
#include "Timestamp.h"
#include "TimeOfDay.h"

struct PowerHistoryEntry {
PowerHistoryEntry();
PowerHistoryEntry(const TicEvaluatedPower& power, const Timestamp& timestamp);
PowerHistoryEntry(const TicEvaluatedPower& power, const TimeOfDay& timestamp);

#ifndef __UNIT_TEST__
private:
Expand All @@ -27,11 +27,11 @@ struct PowerHistoryEntry {
* @param power A new power measurement sample to take into account
* @param timestamp The timestamp for the new @p power
*/
void averageWithPowerSample(const TicEvaluatedPower& power, const Timestamp& timestamp);
void averageWithPowerSample(const TicEvaluatedPower& power, const TimeOfDay& timestamp);

/* Attributes */
TicEvaluatedPower power; /*!< A power (in multiples or fractions of W... see scale below) */
Timestamp timestamp; /*!< The timestamp for the @p power entry */
TimeOfDay timestamp; /*!< The timestamp for the @p power entry */
unsigned int nbSamples; /*!< The number of samples that have been averaged to produce the value in @p power */
unsigned int scale; /*!< A divider for @p power. If scale=1000, then power is represented in mW */
};
Expand Down Expand Up @@ -71,7 +71,7 @@ struct PowerHistory {
* @param timestamp The timestamp associated with the @p power
* @param frameSequenceNb The TIC frame sequence number
*/
void onNewPowerData(const TicEvaluatedPower& power, const Timestamp& timestamp, unsigned int frameSequenceNb);
void onNewPowerData(const TicEvaluatedPower& power, const TimeOfDay& timestamp, unsigned int frameSequenceNb);

/**
* @brief Check if two timestamps are part of the same internal time resolution (and will thus be averaged to be stored in the same period history entry)
Expand All @@ -80,7 +80,7 @@ struct PowerHistory {
* @param second The second timestamp
* @return true If @p first and @p second end up in the same averaging period, and should thus be averaged to create one signel history entry
*/
bool timestampsAreInSamePeriodSample(const Timestamp& first, const Timestamp& second);
bool timestampsAreInSamePeriodSample(const TimeOfDay& first, const TimeOfDay& second);

/**
* @brief Get the averaging period value (in seconds)
Expand All @@ -105,7 +105,7 @@ struct PowerHistory {
* @param frameSequenceNb The TIC frame sequence number
* @param context A context as provided by TIC::DatasetExtractor, used to retrieve the wrapped TicFrameParser instance
*/
static void unWrapOnNewPowerData(const TicEvaluatedPower& power, const Timestamp& timestamp, unsigned int frameSequenceNb, void* context);
static void unWrapOnNewPowerData(const TicEvaluatedPower& power, const TimeOfDay& timestamp, unsigned int frameSequenceNb, void* context);

/**
* @brief Get the Last Values object
Expand All @@ -121,5 +121,5 @@ struct PowerHistory {
FixedSizeRingBuffer<PowerHistoryEntry, 1024> data; /*!< The last n instantaneous power measurements */
AveragingMode averagingPeriod; /*!< Which sampling period do we record (we will perform an average on all samples within the period) */
TicProcessingContext* ticContext; /*!< An optional context structure instance that we should refresh on new power data reception */
Timestamp lastPowerTimestamp; /*!< The timestamp of the last received power measurement */
TimeOfDay lastPowerTimeOfDay; /*!< The timestamp of the last received power measurement */
};
6 changes: 3 additions & 3 deletions inc/domain/TicFrameParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "TIC/DatasetExtractor.h"
#include "TIC/DatasetView.h"
#include "Timestamp.h"
#include "TimeOfDay.h"
#include "FixedSizeRingBuffer.h"

/* Forward declarations */
Expand Down Expand Up @@ -57,7 +57,7 @@ class TicMeasurements {

/* Attributes */
unsigned int fromFrameNb; /*!< The TIC frame ID from which the enclosed data has been extracted */
Timestamp timestamp; /*!< An optional timestamp for the TIC frame */
TimeOfDay timestamp; /*!< An optional timestamp for the TIC frame */
unsigned int instVoltage; /*!< The instantaneous (ie within the last TIC frame) RMS voltage, in Volts */
unsigned int instAbsCurrent; /*!< The instantaneous (ie within the last TIC frame) absolute current, in Amps */
unsigned int maxSubscribedPower; /*!< The maximum allowed withdrawned power (subscribed), in Watts */
Expand All @@ -67,7 +67,7 @@ class TicMeasurements {
class TicFrameParser {
public:
/* Types */
typedef void(*FOnNewPowerData)(const TicEvaluatedPower& power, const Timestamp& timestamp, unsigned int frameId, void* context); /*!< The prototype of callbacks invoked on new power data */
typedef void(*FOnNewPowerData)(const TicEvaluatedPower& power, const TimeOfDay& timestamp, unsigned int frameId, void* context); /*!< The prototype of callbacks invoked on new power data */

/* Methods */
/**
Expand Down
26 changes: 13 additions & 13 deletions inc/domain/Timestamp.h → inc/domain/TimeOfDay.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

#include "TIC/DatasetView.h" // For TIC::Horodate

class Timestamp {
class TimeOfDay {
private:
static constexpr unsigned int lastDayPerMonth[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public:
/* Methods */
Timestamp();
TimeOfDay();

/**
* @brief Construct from hour, minute, seconds and optionally milliseconds
*/
Timestamp(unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond = static_cast<unsigned int>(-1));
TimeOfDay(unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond = static_cast<unsigned int>(-1));

/**
* @brief Construct from month, day, hour, minute, seconds and optionally milliseconds
*/
Timestamp(unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond = static_cast<unsigned int>(-1));
TimeOfDay(unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond = static_cast<unsigned int>(-1));

/**
* @brief Construct from a TIC::Horodate
*/
Timestamp(const TIC::Horodate& from);
TimeOfDay(const TIC::Horodate& from);

/**
* @brief Make the current horodate go forward a given seconds in time
Expand All @@ -46,7 +46,7 @@ class Timestamp {
/**
* @brief Comparison of two timestamps
*
* @param other The other Timestamp to compare with
* @param other The other TimeOfDay to compare with
* @return int -1 is we are earlier than @other, 1 if we are later than @other and 0 if both are equal
*
* @note If one timestamp is invalid, it is considered as the origin of time, thus earlier (-1) than any valid timestamps
Expand All @@ -55,15 +55,15 @@ class Timestamp {
* If one date is unknown, it is considered as the origin of time, thus earlier (-1) than any valid timestamps
* @note We only compare milliseconds if available on both objects
*/
int timeStampCmp(const Timestamp& other) const;
int timeStampCmp(const TimeOfDay& other) const;

public:
bool operator==(const Timestamp& other) const;
bool operator!=(const Timestamp& other) const;
bool operator<(const Timestamp& other) const;
bool operator>(const Timestamp& other) const;
bool operator<=(const Timestamp& other) const;
bool operator>=(const Timestamp& other) const;
bool operator==(const TimeOfDay& other) const;
bool operator!=(const TimeOfDay& other) const;
bool operator<(const TimeOfDay& other) const;
bool operator>(const TimeOfDay& other) const;
bool operator<=(const TimeOfDay& other) const;
bool operator>=(const TimeOfDay& other) const;

#ifdef __TIC_LIB_USE_STD_STRING__
std::string toString() const;
Expand Down
26 changes: 13 additions & 13 deletions src/domain/PowerHistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ PowerHistoryEntry::PowerHistoryEntry() :
{
}

PowerHistoryEntry::PowerHistoryEntry(const TicEvaluatedPower& power, const Timestamp& timestamp) :
PowerHistoryEntry::PowerHistoryEntry(const TicEvaluatedPower& power, const TimeOfDay& timestamp) :
power(power),
timestamp(timestamp),
nbSamples(1)
Expand All @@ -34,7 +34,7 @@ signed int PowerHistoryEntry::truncateSignedLongToSignedInt(const signed long in
}
}

void PowerHistoryEntry::averageWithPowerSample(const TicEvaluatedPower& power, const Timestamp& timestamp) {
void PowerHistoryEntry::averageWithPowerSample(const TicEvaluatedPower& power, const TimeOfDay& timestamp) {
PowerHistoryEntry result;

/* If any of the two provided data instances are invalid, discard it and directly return the second one */
Expand Down Expand Up @@ -86,7 +86,7 @@ PowerHistory::PowerHistory(AveragingMode averagingPeriod, TicProcessingContext*
data(),
averagingPeriod(averagingPeriod),
ticContext(context),
lastPowerTimestamp()
lastPowerTimeOfDay()
{
}

Expand All @@ -95,7 +95,7 @@ void PowerHistory::setContext(TicProcessingContext* context) {
}


void PowerHistory::onNewPowerData(const TicEvaluatedPower& power, const Timestamp& timestamp, unsigned int frameSequenceNb) {
void PowerHistory::onNewPowerData(const TicEvaluatedPower& power, const TimeOfDay& timestamp, unsigned int frameSequenceNb) {
if (!power.isValid || !timestamp.isValid) {
return;
}
Expand All @@ -106,22 +106,22 @@ void PowerHistory::onNewPowerData(const TicEvaluatedPower& power, const Timestam
}

if (timestamp.isValid) {
if (this->timestampsAreInSamePeriodSample(timestamp, this->lastPowerTimestamp)) {
if (this->timestampsAreInSamePeriodSample(timestamp, this->lastPowerTimeOfDay)) {
PowerHistoryEntry* lastEntry = this->data.getPtrToLast();
if (lastEntry != nullptr) {
lastEntry->averageWithPowerSample(power, timestamp);
this->lastPowerTimestamp = timestamp;
this->lastPowerTimeOfDay = timestamp;
return;
}
/* If lastEntry is not valid, create a new entry by falling-through the following code */
}
/* If lastEntry is not valid, create a new entry by falling-through the following code */
}
if (false && this->lastPowerTimestamp.isValid) { /* If we already store some historical data, make sure last timestamp and new timestamp are consecutive (in periods), or otherwise pad */
Timestamp forwardTimestamp = this->lastPowerTimestamp;
if (false && this->lastPowerTimeOfDay.isValid) { /* If we already store some historical data, make sure last timestamp and new timestamp are consecutive (in periods), or otherwise pad */
TimeOfDay forwardTimeOfDay = this->lastPowerTimeOfDay;
for (unsigned int padding = 0; padding < this->data.getCapacity(); padding++) {
forwardTimestamp.addSecondsWrapDay(this->getAveragingPeriodInSeconds()); /* Check what would happen if timestamp was in the next averaging period */
if (this->timestampsAreInSamePeriodSample(timestamp, forwardTimestamp))
forwardTimeOfDay.addSecondsWrapDay(this->getAveragingPeriodInSeconds()); /* Check what would happen if timestamp was in the next averaging period */
if (this->timestampsAreInSamePeriodSample(timestamp, forwardTimeOfDay))
break;
/* If that timestamp slot does not match the timestamp passed as argument, we have a hole in our history entries, pad it */
this->data.push(PowerHistoryEntry()); /* Push an invalid power history entry to pad the history */
Expand All @@ -130,10 +130,10 @@ void PowerHistory::onNewPowerData(const TicEvaluatedPower& power, const Timestam
/* Warning: these lines are not reached when the new power is in the same average period as a previously valid entry (see the above return statement) */
/* If code needs to be executed systematically, put it at the top of this function, not here... */
this->data.push(PowerHistoryEntry(power, timestamp)); /* First sample in this period */
this->lastPowerTimestamp = timestamp;
this->lastPowerTimeOfDay = timestamp;
}

bool PowerHistory::timestampsAreInSamePeriodSample(const Timestamp& first, const Timestamp& second) {
bool PowerHistory::timestampsAreInSamePeriodSample(const TimeOfDay& first, const TimeOfDay& second) {
if (first.month != second.month ||
first.day != second.day ||
first.hour != second.hour)
Expand Down Expand Up @@ -188,7 +188,7 @@ unsigned int PowerHistory::getPowerRecordsPerHour() const {
return (60 * 60 / averagingPeriodInSeconds);
}

void PowerHistory::unWrapOnNewPowerData(const TicEvaluatedPower& power, const Timestamp& timestamp, unsigned int frameSequenceNb, void* context) {
void PowerHistory::unWrapOnNewPowerData(const TicEvaluatedPower& power, const TimeOfDay& timestamp, unsigned int frameSequenceNb, void* context) {
if (context == nullptr)
return; /* Failsafe, discard if no context */
PowerHistory* powerHistoryInstance = static_cast<PowerHistory*>(context);
Expand Down
6 changes: 3 additions & 3 deletions src/domain/TicFrameParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ TicMeasurements::TicMeasurements(unsigned int fromFrameNb) :

void TicMeasurements::reset() {
this->fromFrameNb = static_cast<unsigned int>(-1);
this->timestamp = Timestamp();
this->timestamp = TimeOfDay();
this->instVoltage = static_cast<unsigned int>(-1);
this->instAbsCurrent = static_cast<unsigned int>(-1);
this->instPower = TicEvaluatedPower();
Expand Down Expand Up @@ -205,7 +205,7 @@ void TicFrameParser::onNewMeasurementAvailable() {

void TicFrameParser::onNewDate(const TIC::Horodate& horodate) {
this->onNewMeasurementAvailable();
this->lastFrameMeasurements.timestamp = Timestamp(horodate);
this->lastFrameMeasurements.timestamp = TimeOfDay(horodate);
}

void TicFrameParser::guessFrameArrivalTime() {
Expand All @@ -223,7 +223,7 @@ void TicFrameParser::guessFrameArrivalTime() {
emulatedHorodateRemainder = emulatedHorodateRemainder / 60; /* Now count total remainder as hours */
unsigned int emulatedHour = emulatedHorodateRemainder % 24;
/* Note: we discard days and month for now */
this->lastFrameMeasurements.timestamp = Timestamp(emulatedHour, emulatedMinute, emulatedSecond);
this->lastFrameMeasurements.timestamp = TimeOfDay(emulatedHour, emulatedMinute, emulatedSecond);
#ifdef EMBEDDED_DEBUG_CONSOLE
Stm32DebugOutput::get().send("Injecting timestamp in historical frame: ");
Stm32DebugOutput::get().send(static_cast<unsigned int>(this->lastFrameMeasurements.timestamp.hour));
Expand Down
40 changes: 20 additions & 20 deletions src/domain/Timestamp.cpp → src/domain/TimeOfDay.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "Timestamp.h"
#include "TimeOfDay.h"

constexpr unsigned int Timestamp::lastDayPerMonth[];
constexpr unsigned int TimeOfDay::lastDayPerMonth[];

Timestamp::Timestamp():
TimeOfDay::TimeOfDay():
isValid(false),
estimatedTime(true),
month(static_cast<unsigned int>(-1)),
Expand All @@ -14,8 +14,8 @@ Timestamp::Timestamp():
millisecond(static_cast<unsigned int>(-1)),
knownMilliseconds(false) { }

Timestamp::Timestamp(unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond) :
Timestamp() {
TimeOfDay::TimeOfDay(unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond) :
TimeOfDay() {
if (hour >= 24 || minute >= 60 || second >= 60) {
return;
}
Expand All @@ -35,14 +35,14 @@ Timestamp::Timestamp(unsigned int hour, unsigned int minute, unsigned int second
this->isValid = true;
}

Timestamp::Timestamp(unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond) :
Timestamp(hour, minute, second, millisecond) {
TimeOfDay::TimeOfDay(unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second, unsigned int millisecond) :
TimeOfDay(hour, minute, second, millisecond) {
this->isValid = false; /* Even if timestamp construction has been delegated above, not all args have been checked yet */
if (month == 0 || month > 12) {
/* Error case */
return;
}
if (day == 0 || day > Timestamp::lastDayPerMonth[month-1]) {
if (day == 0 || day > TimeOfDay::lastDayPerMonth[month-1]) {
/* Invalid day */
return;
}
Expand All @@ -52,13 +52,13 @@ Timestamp::Timestamp(unsigned int month, unsigned int day, unsigned int hour, un
this->knownDate = true;
}

Timestamp::Timestamp(const TIC::Horodate& from) :
Timestamp(from.month, from.day, from.hour, from.minute, from.second) {
TimeOfDay::TimeOfDay(const TIC::Horodate& from) :
TimeOfDay(from.month, from.day, from.hour, from.minute, from.second) {
this->isValid = from.isValid;
this->estimatedTime = false;
}

unsigned int Timestamp::addSecondsWrapDay(unsigned int seconds) {
unsigned int TimeOfDay::addSecondsWrapDay(unsigned int seconds) {
this->second += seconds; /* Forward in time */
if (this->second < 60)
return 0;
Expand All @@ -75,13 +75,13 @@ unsigned int Timestamp::addSecondsWrapDay(unsigned int seconds) {
return dayAdd; /* Day wrap occured, return the number of days ahead */
}

unsigned int Timestamp::toSeconds() const {
unsigned int TimeOfDay::toSeconds() const {
if (!this->isValid)
return static_cast<unsigned int>(-1);
return this->second + this->minute*60 + this->hour*3600;
}

int Timestamp::timeStampCmp(const Timestamp& other) const {
int TimeOfDay::timeStampCmp(const TimeOfDay& other) const {
if (!this->isValid && !other.isValid) {
return 0;
}
Expand Down Expand Up @@ -121,35 +121,35 @@ int Timestamp::timeStampCmp(const Timestamp& other) const {
return 0;
}

bool Timestamp::operator==(const Timestamp& other) const {
bool TimeOfDay::operator==(const TimeOfDay& other) const {
if (!this->isValid || !other.isValid)
return false;

return (this->timeStampCmp(other) == 0);
}

bool Timestamp::operator!=(const Timestamp& other) const {
bool TimeOfDay::operator!=(const TimeOfDay& other) const {
return !(*this == other);
}

bool Timestamp::operator>(const Timestamp& other) const {
bool TimeOfDay::operator>(const TimeOfDay& other) const {
return (this->timeStampCmp(other) > 0);
}

bool Timestamp::operator<(const Timestamp& other) const {
bool TimeOfDay::operator<(const TimeOfDay& other) const {
return (this->timeStampCmp(other) < 0);
}

bool Timestamp::operator>=(const Timestamp& other) const {
bool TimeOfDay::operator>=(const TimeOfDay& other) const {
return (this->timeStampCmp(other) >= 0);
}

bool Timestamp::operator<=(const Timestamp& other) const {
bool TimeOfDay::operator<=(const TimeOfDay& other) const {
return (this->timeStampCmp(other) <= 0);
}

#ifdef __TIC_LIB_USE_STD_STRING__
std::string Timestamp::toString() const {
std::string TimeOfDay::toString() const {
if (!this->isValid) {
return "Invalid timestamp";
}
Expand Down
Loading

0 comments on commit 4f1fb8c

Please sign in to comment.