X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/107a6a8d0b9eae3c4375a685e49dcf90bea69335..0907aa4037b12b6b88ee24495d4577a064d4f8db:/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp index 5088ee9d..8f8e8db5 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp @@ -23,8 +23,7 @@ #include "stg/tariffs.h" #include "stg/users.h" -#include "stg/common.h" -#include "stg/resetable.h" +#include "stg/optional.h" #include // snprintf #include @@ -52,28 +51,36 @@ std::string AOS2String(const A & array, size_t size, const F C::* field, F multi { if (!res.empty()) res += "/"; - res += x2str((array[i].*field) * multiplier); + res += std::to_string((array[i].*field) * multiplier); } return res; } +template +bool str2res(const std::string& source, STG::Optional& dest, T divisor) +{ + T value = 0; + if (str2x(source, value)) + return false; + dest = value / divisor; + return true; +} + template -bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE C::* field, F divisor) +bool String2AOS(const std::string & source, A & array, size_t size, STG::Optional C::* field, F divisor) { size_t index = 0; std::string::size_type from = 0; std::string::size_type pos = 0; while (index < size && (pos = source.find('/', from)) != std::string::npos) { - if (str2x(source.substr(from, pos - from), (array[index].*field).data())) + if (!str2res(source.substr(from, pos - from), array[index].*field, divisor)) return false; - (array[index].*field).data() /= divisor; from = pos + 1; ++index; } - if (str2x(source.substr(from), (array[index].*field).data())) + if (str2res(source.substr(from), array[index].*field, divisor)) return false; - (array[index].*field).data() /= divisor; return true; } @@ -81,36 +88,38 @@ bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE void GET_TARIFFS::CreateAnswer() { - m_answer = GetOpenTag(); + m_answer = ""; - std::list dataList; + std::vector dataList; m_tariffs.GetTariffsData(&dataList); - std::list::const_iterator it = dataList.begin(); + auto it = dataList.begin(); for (; it != dataList.end(); ++it) { m_answer += "tariffConf.name + "\">"; for (size_t i = 0; i < DIR_NUM; i++) - m_answer += "dirPrice[i].hDay) + ":" + x2str(it->dirPrice[i].mDay) + "-" + - x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>"; - - m_answer += "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayA, pt_mega) + "\"/>" + - "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayB, pt_mega) + "\"/>" + - "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightA, pt_mega) + "\"/>" + - "dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightB, pt_mega) + "\"/>" + - "dirPrice, DIR_NUM, &DIRPRICE_DATA::threshold, 1) + "\"/>" + - "dirPrice, DIR_NUM, &DIRPRICE_DATA::singlePrice, 1) + "\"/>" + - "dirPrice, DIR_NUM, &DIRPRICE_DATA::noDiscount, 1) + "\"/>" + - "tariffConf.fee) + "\"/>" + - "tariffConf.passiveCost) + "\"/>" + - "tariffConf.free) + "\"/>" + - "tariffConf.traffType) + "\"/>" + - "tariffConf.period) + "\"/>" + + m_answer += "dirPrice[i].hDay) + ":" + std::to_string(it->dirPrice[i].mDay) + "-" + + std::to_string(it->dirPrice[i].hNight) + ":" + std::to_string(it->dirPrice[i].mNight) + "\"/>"; + + m_answer += "dirPrice, DIR_NUM, &DirPriceData::priceDayA, pt_mega) + "\"/>" + + "dirPrice, DIR_NUM, &DirPriceData::priceDayB, pt_mega) + "\"/>" + + "dirPrice, DIR_NUM, &DirPriceData::priceNightA, pt_mega) + "\"/>" + + "dirPrice, DIR_NUM, &DirPriceData::priceNightB, pt_mega) + "\"/>" + + "dirPrice, DIR_NUM, &DirPriceData::threshold, 1) + "\"/>" + + "dirPrice, DIR_NUM, &DirPriceData::singlePrice, 1) + "\"/>" + + "dirPrice, DIR_NUM, &DirPriceData::noDiscount, 1) + "\"/>" + + "tariffConf.fee) + "\"/>" + + "tariffConf.passiveCost) + "\"/>" + + "tariffConf.free) + "\"/>" + + "tariffConf.traffType) + "\"/>" + + "tariffConf.period) + "\"/>" + + "tariffConf.changePolicy) + "\"/>" + + "tariffConf.changePolicyTimeout) + "\"/>" + ""; } - m_answer += GetCloseTag(); + m_answer += ""; } int ADD_TARIFF::Start(void *, const char * el, const char ** attr) @@ -163,7 +172,11 @@ int CHG_TARIFF::Start(void *, const char * el, const char ** attr) { if (strcasecmp(el, m_tag.c_str()) == 0) { - td.tariffConf.name = attr[1]; + const auto tariff = m_tariffs.FindByName(attr[1]); + if (tariff != NULL) + td = tariff->GetTariffData(); + else + return -1; return 0; } } @@ -171,7 +184,7 @@ int CHG_TARIFF::Start(void *, const char * el, const char ** attr) { if (strcasecmp(el, "PriceDayA") == 0) { - if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayA, pt_mega)) + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DirPriceDataOpt::priceDayA, pt_mega)) return -1; // TODO: log it else return 0; @@ -179,7 +192,7 @@ int CHG_TARIFF::Start(void *, const char * el, const char ** attr) if (strcasecmp(el, "PriceDayB") == 0) { - if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayB, pt_mega)) + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DirPriceDataOpt::priceDayB, pt_mega)) return -1; // TODO: log it else return 0; @@ -187,7 +200,7 @@ int CHG_TARIFF::Start(void *, const char * el, const char ** attr) if (strcasecmp(el, "PriceNightA") == 0) { - if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightA, pt_mega)) + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DirPriceDataOpt::priceNightA, pt_mega)) return -1; // TODO: log it else return 0; @@ -195,7 +208,7 @@ int CHG_TARIFF::Start(void *, const char * el, const char ** attr) if (strcasecmp(el, "PriceNightB") == 0) { - if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightB, pt_mega)) + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DirPriceDataOpt::priceNightB, pt_mega)) return -1; // TODO: log it else return 0; @@ -203,7 +216,7 @@ int CHG_TARIFF::Start(void *, const char * el, const char ** attr) if (strcasecmp(el, "Threshold") == 0) { - if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::threshold, 1)) + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DirPriceDataOpt::threshold, 1)) return -1; // TODO: log it else return 0; @@ -211,7 +224,7 @@ int CHG_TARIFF::Start(void *, const char * el, const char ** attr) if (strcasecmp(el, "SinglePrice") == 0) { - if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::singlePrice, 1)) + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DirPriceDataOpt::singlePrice, 1)) return -1; // TODO: log it else return 0; @@ -219,7 +232,7 @@ int CHG_TARIFF::Start(void *, const char * el, const char ** attr) if (strcasecmp(el, "NoDiscount") == 0) { - if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::noDiscount, 1)) + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DirPriceDataOpt::noDiscount, 1)) return -1; // TODO: log it else return 0; @@ -272,13 +285,27 @@ int CHG_TARIFF::Start(void *, const char * el, const char ** attr) if (strcasecmp(el, "TraffType") == 0) { - td.tariffConf.traffType = TARIFF::StringToTraffType(attr[1]); + td.tariffConf.traffType = Tariff::parseTraffType(attr[1]); return 0; } if (strcasecmp(el, "Period") == 0) { - td.tariffConf.period = TARIFF::StringToPeriod(attr[1]); + td.tariffConf.period = Tariff::parsePeriod(attr[1]); + return 0; + } + + if (strcasecmp(el, "ChangePolicy") == 0) + { + td.tariffConf.changePolicy = Tariff::parseChangePolicy(attr[1]); + return 0; + } + + if (strcasecmp(el, "ChangePolicyTimeout") == 0) + { + int64_t policyTime = 0; + if (str2x(attr[1], policyTime) == 0) + td.tariffConf.changePolicyTimeout = (time_t)policyTime; return 0; } } @@ -289,7 +316,7 @@ void CHG_TARIFF::CreateAnswer() { if (!td.tariffConf.name.data().empty()) { - TARIFF_DATA tariffData = td.GetData(); + auto tariffData = td.get({}); if (m_tariffs.Chg(tariffData, &m_currAdmin) == 0) m_answer = "<" + m_tag + " Result=\"ok\"/>"; else