ParseWeekDay():

Parsing "Monday" - "Sunday" section i a tariff-file.

	Each day has a memebr named "week_day_default".

	It contains the valid default tariff settings for a usual day (i.e. no
	holiday etc.)
	It also contains the carry-over-settings for the usual day.

	In case there is some holiday on this day (for instance on "Monday"),
	the the date of the holiday replaces the name "week_day_default".
	Otherwise the structure is the same as for a usual day.
This commit is contained in:
Gerhard Hoffmann 2024-09-02 16:49:21 +02:00
parent 431664d8b9
commit 58d8f16681

View File

@ -46,6 +46,286 @@ MemberType Configuration::IdentifyJsonMember(const char* member_name)
else return MemberType::UnknownType;
}
ATBWeekDay parseWeekDay(Configuration &cfg,
rapidjson::GenericMemberIterator<false,
rapidjson::UTF8<char>,
rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>> k,
QString const &innerObjName,
Qt::DayOfWeek weekDay,
QString const &weekDayName) {
int max_price = 0;
int min_price = 0;
int min_time = 0;
int max_time = 0;
int duration = 0;
ATBWeekDay WeekDay;
QTime start, end, parking_time_limit, about_to_exceed_limit;
ATBTariffCarryOverSettings::ParkingTimeLimitChecker parkTimeLimitChecker;
if (innerObjName == QString("week_day_default")) {
if (k->value.IsObject()) {
auto obj = k->value.GetObject();
for (auto m = obj.MemberBegin(); m != obj.MemberEnd(); ++m) {
QString const &name = m->name.GetString();
if (name == "week_day_type") {
//
} else
if (name == "tariff_settings") {
if (m->value.IsObject()) {
auto obj = m->value.GetObject();
for (auto n = obj.MemberBegin(); n != obj.MemberEnd(); ++n) {
QString const &name = QString::fromStdString(n->name.GetString());
if (name == "min_time") {
if (n->value.IsInt()) { min_time = n->value.GetInt(); }
} else if (name == "max_time") {
if (n->value.IsInt()) { max_time = n->value.GetInt(); }
} else if (name == "min_price") {
if (n->value.IsInt()) { min_price = n->value.GetInt(); }
} else if (name == "max_price") {
if (n->value.IsInt()) { max_price = n->value.GetInt(); }
}
}
}
} else
if (name == "carry_over_settings") {
if (m->value.IsObject()) {
auto obj = m->value.GetObject();
for (auto n = obj.MemberBegin(); n != obj.MemberEnd(); ++n) {
QString const &name = QString::fromStdString(n->name.GetString());
if (name == "duration") {
if (n->value.IsInt()) {
duration = n->value.GetInt();
}
} else
if (name == "start") {
if (n->value.IsString()) {
start = QTime::fromString(QString::fromStdString(n->value.GetString()), Qt::ISODate);
}
} else
if (name == "end") {
if (n->value.IsString()) {
end = QTime::fromString(QString::fromStdString(n->value.GetString()), Qt::ISODate);
}
} else
if (name == "parking_time_limit") {
if (n->value.IsObject()) {
auto o = n->value.GetObject();
for (auto l = o.MemberBegin(); l != o.MemberEnd(); ++l) {
if (l->name.IsString()) {
QString const &member = QString::fromStdString(l->name.GetString());
if (member == "default") {
if (l->value.IsArray()) {
auto limits = l->value.GetArray();
if (limits.Size() >= 2) {
about_to_exceed_limit = QTime::fromString(QString::fromStdString(limits[0].GetString()), Qt::ISODate);
parking_time_limit = QTime::fromString(QString::fromStdString(limits[1].GetString()), Qt::ISODate);
}
parkTimeLimitChecker = [&cfg, weekDay, weekDayName](ATBTariffCarryOverSettings const& cs,
QDateTime const &endTime,
int paymentOptionIndex) {
if (endTime.date().dayOfWeek() == (int)weekDay) {
if (endTime.time() > cs.m_parking_time_limit) {
qCritical() << __func__ << ":" << __LINE__
<< QString("ERROR time limit for end-time violated: [%1, %2 end-time=%3], but time-limit=%4")
.arg(weekDayName)
.arg(endTime.date().toString(Qt::ISODate))
.arg(endTime.time().toString(Qt::ISODate))
.arg(cs.m_parking_time_limit.toString(Qt::ISODate));
return true;
}
if (endTime.time() >= cs.m_about_to_exceed_parking_time_limit) {
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps = 1;
} else {
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps =
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps_saved;
}
}
return false;
};
}
} else
if (member == "prev_day_holiday?") {
if (l->value.IsArray()) {
auto limits = l->value.GetArray();
if (limits.Size() >= 2) {
about_to_exceed_limit = QTime::fromString(QString::fromStdString(limits[0].GetString()), Qt::ISODate);
parking_time_limit = QTime::fromString(QString::fromStdString(limits[1].GetString()), Qt::ISODate);
}
parkTimeLimitChecker = [&cfg, weekDayName](ATBTariffCarryOverSettings const& cs,
QDateTime const &endTime,
int paymentOptionIndex) {
if (previousDayHoliday(cfg, endTime)) {
if (endTime.time() > cs.m_parking_time_limit) {
qCritical() << __func__ << ":" << __LINE__
<< QString("ERROR time limit for end-time violated: [%1, %2, end-time=%3], but time-limit=%4")
.arg(weekDayName)
.arg(endTime.date().toString(Qt::ISODate))
.arg(endTime.time().toString(Qt::ISODate))
.arg(cs.m_parking_time_limit.toString(Qt::ISODate));
return true;
}
if (endTime.time() >= cs.m_about_to_exceed_parking_time_limit) {
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps = 1;
} else {
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps =
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps_saved;
}
}
return false;
};
}
}
}
}
}
}
}
}
}
}
ATBTariffSettings ts(max_price, min_price, max_time, min_time);
ATBTariffCarryOverSettings cs(duration, start, end, parking_time_limit, about_to_exceed_limit,
parkTimeLimitChecker);
WeekDay = ATBWeekDay(weekDay, weekDayName, ATBWeekDay::USUAL_WEEKDAY, QDate(), ts, cs);
} // if (k->value.IsObject()) {
} else {
QDate date(QDate::fromString(innerObjName, Qt::ISODate));
if (date.isValid()) {
if (k->value.IsObject()) {
auto obj = k->value.GetObject();
for (auto m = obj.MemberBegin(); m != obj.MemberEnd(); ++m) {
QString const &name = m->name.GetString();
if (name == "week_day_type") {
// TODO
} else
if (name == "tariff_settings") {
if (m->value.IsObject()) {
auto obj = m->value.GetObject();
for (auto n = obj.MemberBegin(); n != obj.MemberEnd(); ++n) {
QString const &name = QString::fromStdString(n->name.GetString());
if (name == "min_time") {
if (n->value.IsInt()) { min_time = n->value.GetInt(); }
} else if (name == "max_time") {
if (n->value.IsInt()) { max_time = n->value.GetInt(); }
} else if (name == "min_price") {
if (n->value.IsInt()) { min_price = n->value.GetInt(); }
} else if (name == "max_price") {
if (n->value.IsInt()) { max_price = n->value.GetInt(); }
}
}
}
} else
if (name == "carry_over_settings") {
if (m->value.IsObject()) {
auto obj = m->value.GetObject();
for (auto n = obj.MemberBegin(); n != obj.MemberEnd(); ++n) {
QString const &name = QString::fromStdString(n->name.GetString());
if (name == "duration") {
if (n->value.IsInt()) {
duration = n->value.GetInt();
}
} else
if (name == "start") {
if (n->value.IsString()) {
start = QTime::fromString(QString::fromStdString(n->value.GetString()), Qt::ISODate);
}
} else
if (name == "end") {
if (n->value.IsString()) {
end = QTime::fromString(QString::fromStdString(n->value.GetString()), Qt::ISODate);
}
} else
if (name == "parking_time_limit") {
if (n->value.IsObject()) {
auto o = n->value.GetObject();
for (auto l = o.MemberBegin(); l != o.MemberEnd(); ++l) {
if (l->name.IsString()) {
QString const &member = QString::fromStdString(l->name.GetString());
if (member == "default") {
if (l->value.IsArray()) {
auto limits = l->value.GetArray();
if (limits.Size() >= 2) {
about_to_exceed_limit = QTime::fromString(QString::fromStdString(limits[0].GetString()), Qt::ISODate);
parking_time_limit = QTime::fromString(QString::fromStdString(limits[1].GetString()), Qt::ISODate);
}
}
parkTimeLimitChecker = [&cfg, weekDay](ATBTariffCarryOverSettings const& cs,
QDateTime const &endTime,
int paymentOptionIndex) {
if (endTime.date().dayOfWeek() == (int)weekDay) {
if (endTime.time() > cs.m_parking_time_limit) {
qCritical() << __func__ << ":" << __LINE__
<< QString("ERROR time limit for end-time violated: end-time=%1, but time-limit=%2")
.arg(endTime.time().toString(Qt::ISODate))
.arg(cs.m_parking_time_limit.toString(Qt::ISODate));
return true;
}
if (endTime.time() >= cs.m_about_to_exceed_parking_time_limit) {
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps = 1;
} else {
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps =
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps_saved;
}
}
return false;
};
} else
if (member == "prev_day_holiday?") {
if (l->value.IsArray()) {
auto limits = l->value.GetArray();
if (limits.Size() >= 2) {
about_to_exceed_limit = QTime::fromString(QString::fromStdString(limits[0].GetString()), Qt::ISODate);
parking_time_limit = QTime::fromString(QString::fromStdString(limits[1].GetString()), Qt::ISODate);
}
}
parkTimeLimitChecker = [&cfg](ATBTariffCarryOverSettings const& cs,
QDateTime const &endTime,
int paymentOptionIndex) {
if (previousDayHoliday(cfg, endTime)) {
if (endTime.time() > cs.m_parking_time_limit) {
qCritical() << __func__ << ":" << __LINE__
<< QString("ERROR time limit for end-time violated: end-time=%1, but time-limit=%2")
.arg(endTime.time().toString(Qt::ISODate))
.arg(cs.m_parking_time_limit.toString(Qt::ISODate));
return true;
}
if (endTime.time() >= cs.m_about_to_exceed_parking_time_limit) {
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps = 1;
} else {
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps =
cfg.getPaymentOptions(paymentOptionIndex).pop_plus_steps_saved;
}
}
return false;
};
}
}
}
}
}
}
}
}
}
ATBTariffSettings ts(max_price, min_price, max_time, min_time);
ATBTariffCarryOverSettings cs(duration, start, end, parking_time_limit,
about_to_exceed_limit, parkTimeLimitChecker);
WeekDay = ATBWeekDay(weekDay, weekDayName, ATBWeekDay::HOLIDAY, date, ts, cs);
} else {// if (k->value.IsObject()) {
qCritical() << __func__ << ":" << __LINE__ << "ERROR: no object";
}
} else {
qCritical() << __func__ << __LINE__
<< "ERROR: invalid date for" << weekDayName << "," << innerObjName;
}
}
return WeekDay;
}
/// <inheritdoc/>
bool Configuration::ParseJson(Configuration* cfg, const char* json)
{