#include "stg/tariffs.h"
#include "stg/users.h"
-#include "stg/common.h"
-#include "stg/resetable.h"
+#include "stg/optional.h"
#include <cstdio> // snprintf
#include <cstring>
using STG::PARSER::DEL_TARIFF;
using STG::PARSER::CHG_TARIFF;
+const char * GET_TARIFFS::tag = "GetTariffs";
+const char * ADD_TARIFF::tag = "AddTariff";
+const char * DEL_TARIFF::tag = "DelTariff";
+const char * CHG_TARIFF::tag = "SetTariff";
+
namespace
{
{
if (!res.empty())
res += "/";
- res += x2str((array[i].*field) * multiplier);
+ res += std::to_string((array[i].*field) * multiplier);
}
return res;
}
+template <typename T>
+bool str2res(const std::string& source, STG::Optional<T>& dest, T divisor)
+{
+ T value = 0;
+ if (str2x(source, value))
+ return false;
+ dest = value / divisor;
+ return true;
+}
+
template <typename A, typename C, typename F>
-bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE<F> C::* field, F divisor)
+bool String2AOS(const std::string & source, A & array, size_t size, STG::Optional<F> 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;
}
void GET_TARIFFS::CreateAnswer()
{
- answer = GetOpenTag();
+ m_answer = "<Tariffs>";
- std::list<TARIFF_DATA> dataList;
+ std::vector<TariffData> dataList;
m_tariffs.GetTariffsData(&dataList);
- std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
+ auto it = dataList.begin();
for (; it != dataList.end(); ++it)
{
- answer += "<tariff name=\"" + it->tariffConf.name + "\">";
+ m_answer += "<tariff name=\"" + it->tariffConf.name + "\">";
for (size_t i = 0; i < DIR_NUM; i++)
- answer += "<Time" + x2str(i) + " value=\"" +
- x2str(it->dirPrice[i].hDay) + ":" + x2str(it->dirPrice[i].mDay) + "-" +
- x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>";
-
- answer += "<PriceDayA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayA, pt_mega) + "\"/>" +
- "<PriceDayB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayB, pt_mega) + "\"/>" +
- "<PriceNightA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightA, pt_mega) + "\"/>" +
- "<PriceNightB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightB, pt_mega) + "\"/>" +
- "<Threshold value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::threshold, 1) + "\"/>" +
- "<SinglePrice value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::singlePrice, 1) + "\"/>" +
- "<NoDiscount value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::noDiscount, 1) + "\"/>" +
- "<Fee value=\"" + x2str(it->tariffConf.fee) + "\"/>" +
- "<PassiveCost value=\"" + x2str(it->tariffConf.passiveCost) + "\"/>" +
- "<Free value=\"" + x2str(it->tariffConf.free) + "\"/>" +
- "<TraffType value=\"" + TARIFF::TraffTypeToString(it->tariffConf.traffType) + "\"/>" +
- "<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>" +
+ m_answer += "<Time" + std::to_string(i) + " value=\"" +
+ std::to_string(it->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 += "<PriceDayA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DirPriceData::priceDayA, pt_mega) + "\"/>" +
+ "<PriceDayB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DirPriceData::priceDayB, pt_mega) + "\"/>" +
+ "<PriceNightA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DirPriceData::priceNightA, pt_mega) + "\"/>" +
+ "<PriceNightB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DirPriceData::priceNightB, pt_mega) + "\"/>" +
+ "<Threshold value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DirPriceData::threshold, 1) + "\"/>" +
+ "<SinglePrice value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DirPriceData::singlePrice, 1) + "\"/>" +
+ "<NoDiscount value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DirPriceData::noDiscount, 1) + "\"/>" +
+ "<Fee value=\"" + std::to_string(it->tariffConf.fee) + "\"/>" +
+ "<PassiveCost value=\"" + std::to_string(it->tariffConf.passiveCost) + "\"/>" +
+ "<Free value=\"" + std::to_string(it->tariffConf.free) + "\"/>" +
+ "<TraffType value=\"" + Tariff::toString(it->tariffConf.traffType) + "\"/>" +
+ "<Period value=\"" + Tariff::toString(it->tariffConf.period) + "\"/>" +
+ "<ChangePolicy value=\"" + Tariff::toString(it->tariffConf.changePolicy) + "\"/>" +
+ "<ChangePolicyTimeout value=\"" + std::to_string(it->tariffConf.changePolicyTimeout) + "\"/>" +
"</tariff>";
}
- answer += GetCloseTag();
+ m_answer += "</Tariffs>";
}
int ADD_TARIFF::Start(void *, const char * el, const char ** attr)
{
- if (strcasecmp(el, tag.c_str()) != 0)
+ if (strcasecmp(el, m_tag.c_str()) != 0)
return -1;
if (attr[1] == NULL)
void ADD_TARIFF::CreateAnswer()
{
- if (m_tariffs.Add(tariff, &currAdmin) == 0)
- answer = "<" + tag + " Result=\"Ok\"/>";
+ if (m_tariffs.Add(tariff, &m_currAdmin) == 0)
+ m_answer = "<" + m_tag + " Result=\"Ok\"/>";
else
- answer = "<" + tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
+ m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
}
int DEL_TARIFF::Start(void *, const char * el, const char ** attr)
{
- if (strcasecmp(el, tag.c_str()) != 0)
+ if (strcasecmp(el, m_tag.c_str()) != 0)
return -1;
if (attr[1] == NULL)
void DEL_TARIFF::CreateAnswer()
{
if (m_users.TariffInUse(tariff))
- answer = "<" + tag + " Result=\"Error. Tariff \'" + tariff + "\' cannot be deleted, it is in use.\"/>";
- else if (m_tariffs.Del(tariff, &currAdmin) == 0)
- answer = "<" + tag + " Result=\"Ok\"/>";
+ m_answer = "<" + m_tag + " Result=\"Error. Tariff \'" + tariff + "\' cannot be deleted, it is in use.\"/>";
+ else if (m_tariffs.Del(tariff, &m_currAdmin) == 0)
+ m_answer = "<" + m_tag + " Result=\"Ok\"/>";
else
- answer = "<" + tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
+ m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
}
int CHG_TARIFF::Start(void *, const char * el, const char ** attr)
{
- depth++;
+ m_depth++;
- if (depth == 1)
+ if (m_depth == 1)
{
- if (strcasecmp(el, tag.c_str()) == 0)
+ 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;
}
}
{
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;
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;
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;
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;
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;
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;
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;
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;
}
- }
- return -1;
-}
-int CHG_TARIFF::End(void *, const char * el)
-{
- if (depth == 1)
- {
- if (strcasecmp(el, tag.c_str()) != 0)
- return -1;
- CreateAnswer();
- }
+ if (strcasecmp(el, "ChangePolicy") == 0)
+ {
+ td.tariffConf.changePolicy = Tariff::parseChangePolicy(attr[1]);
+ return 0;
+ }
- --depth;
- return 0;
+ if (strcasecmp(el, "ChangePolicyTimeout") == 0)
+ {
+ int64_t policyTime = 0;
+ if (str2x(attr[1], policyTime) == 0)
+ td.tariffConf.changePolicyTimeout = policyTime;
+ return 0;
+ }
+ }
+ return -1;
}
void CHG_TARIFF::CreateAnswer()
{
if (!td.tariffConf.name.data().empty())
{
- TARIFF_DATA tariffData = td.GetData();
- if (m_tariffs.Chg(tariffData, &currAdmin) == 0)
- answer = "<" + tag + " Result=\"ok\"/>";
+ auto tariffData = td.get({});
+ if (m_tariffs.Chg(tariffData, &m_currAdmin) == 0)
+ m_answer = "<" + m_tag + " Result=\"ok\"/>";
else
- answer = "<" + tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>";
+ m_answer = "<" + m_tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>";
}
else
- answer = "<" + tag + " Result=\"Change tariff error!\"/>";
+ m_answer = "<" + m_tag + " Result=\"Change tariff error!\"/>";
}