X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/8c6fa3fbaccc22127280bf77a48fab5a3ee0716e..46b0747592074017ff0ea4b33d4a7194235886e5:/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp diff --git a/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp b/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp new file mode 100644 index 00000000..9c752674 --- /dev/null +++ b/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp @@ -0,0 +1,327 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + * Author : Maxim Mamontov + */ + +#include "parser_tariffs.h" + +#include "stg/tariffs.h" +#include "stg/users.h" +#include "stg/resetable.h" + +#include // snprintf +#include + +using STG::PARSER::GET_TARIFFS; +using STG::PARSER::ADD_TARIFF; +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 +{ + +const double pt_mega = 1024 * 1024; + +template +std::string AOS2String(const A & array, size_t size, const F C::* field, F multiplier) +{ + std::string res; + for (size_t i = 0; i < size; ++i) + { + if (!res.empty()) + res += "/"; + res += x2str((array[i].*field) * multiplier); + } + return res; +} + +template +bool str2res(const std::string& source, RESETABLE& 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) +{ + 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 (!str2res(source.substr(from, pos - from), array[index].*field, divisor)) + return false; + from = pos + 1; + ++index; + } + if (str2res(source.substr(from), array[index].*field, divisor)) + return false; + return true; +} + +} + +void GET_TARIFFS::CreateAnswer() +{ + m_answer = ""; + + std::list dataList; + m_tariffs.GetTariffsData(&dataList); + std::list::const_iterator 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) + "\"/>" + + "tariffConf.changePolicy) + "\"/>" + + "tariffConf.changePolicyTimeout) + "\"/>" + + ""; + } + + m_answer += ""; +} + +int ADD_TARIFF::Start(void *, const char * el, const char ** attr) +{ + if (strcasecmp(el, m_tag.c_str()) != 0) + return -1; + + if (attr[1] == NULL) + return -1; + + tariff = attr[1]; + return 0; +} + +void ADD_TARIFF::CreateAnswer() +{ + if (m_tariffs.Add(tariff, &m_currAdmin) == 0) + m_answer = "<" + m_tag + " Result=\"Ok\"/>"; + else + m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>"; +} + +int DEL_TARIFF::Start(void *, const char * el, const char ** attr) +{ + if (strcasecmp(el, m_tag.c_str()) != 0) + return -1; + + if (attr[1] == NULL) + return -1; + + tariff = attr[1]; + return 0; +} + +void DEL_TARIFF::CreateAnswer() +{ + if (m_users.TariffInUse(tariff)) + 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 + m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>"; +} + +int CHG_TARIFF::Start(void *, const char * el, const char ** attr) +{ + m_depth++; + + if (m_depth == 1) + { + if (strcasecmp(el, m_tag.c_str()) == 0) + { + const TARIFF * tariff = m_tariffs.FindByName(attr[1]); + if (tariff != NULL) + td = tariff->GetTariffData(); + else + return -1; + return 0; + } + } + else + { + if (strcasecmp(el, "PriceDayA") == 0) + { + if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::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)) + 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)) + 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)) + 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)) + 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)) + 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)) + return -1; // TODO: log it + else + return 0; + } + + for (int j = 0; j < DIR_NUM; j++) + { + char st[50]; + snprintf(st, 50, "Time%d", j); + if (strcasecmp(el, st) == 0) + { + int h1 = 0; + int m1 = 0; + int h2 = 0; + int m2 = 0; + if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0) + { + td.dirPrice[j].hDay = h1; + td.dirPrice[j].mDay = m1; + td.dirPrice[j].hNight = h2; + td.dirPrice[j].mNight = m2; + } + return 0; + } + } + + if (strcasecmp(el, "Fee") == 0) + { + double fee; + if (strtodouble2(attr[1], fee) == 0) + td.tariffConf.fee = fee; + return 0; + } + + if (strcasecmp(el, "PassiveCost") == 0) + { + double pc; + if (strtodouble2(attr[1], pc) == 0) + td.tariffConf.passiveCost = pc; + return 0; + } + + if (strcasecmp(el, "Free") == 0) + { + double free; + if (strtodouble2(attr[1], free) == 0) + td.tariffConf.free = free; + return 0; + } + + if (strcasecmp(el, "TraffType") == 0) + { + td.tariffConf.traffType = TARIFF::StringToTraffType(attr[1]); + return 0; + } + + if (strcasecmp(el, "Period") == 0) + { + td.tariffConf.period = TARIFF::StringToPeriod(attr[1]); + return 0; + } + + if (strcasecmp(el, "ChangePolicy") == 0) + { + td.tariffConf.changePolicy = TARIFF::StringToChangePolicy(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; + } + } + return -1; +} + +void CHG_TARIFF::CreateAnswer() +{ + if (!td.tariffConf.name.data().empty()) + { + TARIFF_DATA tariffData = td.GetData(); + if (m_tariffs.Chg(tariffData, &m_currAdmin) == 0) + m_answer = "<" + m_tag + " Result=\"ok\"/>"; + else + m_answer = "<" + m_tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>"; + } + else + m_answer = "<" + m_tag + " Result=\"Change tariff error!\"/>"; +}