3 #include "api_action.h"
7 #include "stg/servconf.h"
8 #include "stg/servconf_types.h"
9 #include "stg/tariff_conf.h"
10 #include "stg/os_int.h"
21 std::string Indent(size_t level, bool dash = false)
25 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
28 std::string PeriodToString(TARIFF::PERIOD period)
40 std::string TraffTypeToString(int traffType)
49 return "upload + download";
51 return "max(upload, download)";
56 std::string TimeToString(int h, int m)
58 std::ostringstream stream;
59 stream << (h < 10 ? "0" : "") << h << ":"
60 << (m < 10 ? "0" : "") << m;
64 void PrintDirPriceData(size_t dir, const DIRPRICE_DATA & data, size_t level)
66 std::string night = TimeToString(data.hNight, data.mNight);
67 std::string day = TimeToString(data.hDay, data.mDay);
68 std::cout << Indent(level, true) << "dir: " << dir << "\n"
69 << Indent(level) << "'" << night << "' - '" << day << "': " << data.priceDayA << "/" << data.priceDayB << "\n"
70 << Indent(level) << "'" << day << "' - '" << night << "': " << data.priceNightA << "/" << data.priceNightB << "\n"
71 << Indent(level) << "threshold: " << data.threshold << "\n"
72 << Indent(level) << "single price: " << (data.singlePrice ? "yes" : "no") << "\n"
73 << Indent(level) << "discount: " << (data.noDiscount ? "no" : "yes") << "\n"; // Attention!
76 void PrintTariffConf(const TARIFF_CONF & conf, size_t level)
78 std::cout << Indent(level, true) << "name: " << conf.name << "\n"
79 << Indent(level) << "fee: " << conf.fee << "\n"
80 << Indent(level) << "free mb: " << conf.free << "\n"
81 << Indent(level) << "passive cost: " << conf.passiveCost << "\n"
82 << Indent(level) << "traff type: " << TraffTypeToString(conf.traffType) << "\n"
83 << Indent(level) << "period: " << PeriodToString(conf.period) << "\n";
86 void PrintTariff(const STG::GET_TARIFF::INFO & info, size_t level = 0)
88 PrintTariffConf(info.tariffConf, level);
89 std::cout << Indent(level) << "dir prices:\n";
90 for (size_t i = 0; i < info.dirPrice.size(); ++i)
91 PrintDirPriceData(i, info.dirPrice[i], level + 1);
94 std::vector<SGCONF::API_ACTION::PARAM> GetTariffParams()
96 std::vector<SGCONF::API_ACTION::PARAM> params;
97 params.push_back(SGCONF::API_ACTION::PARAM("fee", "<fee>", "\t\ttariff fee"));
98 params.push_back(SGCONF::API_ACTION::PARAM("free", "<free mb>", "\tprepaid traff"));
99 params.push_back(SGCONF::API_ACTION::PARAM("passive-cost", "<cost>", "\tpassive cost"));
100 params.push_back(SGCONF::API_ACTION::PARAM("traff-type", "<type>", "\ttraff type (up, dow, up+down, max)"));
101 params.push_back(SGCONF::API_ACTION::PARAM("period", "<period>", "\ttarification period (daily, monthly)"));
102 params.push_back(SGCONF::API_ACTION::PARAM("times", "<hh:mm-hh:mm, ...>", "coma-separated day time-spans for each direction"));
103 params.push_back(SGCONF::API_ACTION::PARAM("day-prices", "<price/price, ...>", "coma-separated day prices for each direction"));
104 params.push_back(SGCONF::API_ACTION::PARAM("night-prices", "<price/price, ...>", "coma-separated day prices for each direction"));
105 params.push_back(SGCONF::API_ACTION::PARAM("thresholds", "<threshold, ...>", "coma-separated thresholds for each direction"));
109 void SimpleCallback(bool result,
110 const std::string & reason,
115 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
118 std::cout << "Success.\n";
121 void GetTariffsCallback(bool result,
122 const std::string & reason,
123 const std::vector<STG::GET_TARIFF::INFO> & info,
128 std::cerr << "Failed to get tariff list. Reason: '" << reason << "'." << std::endl;
131 std::cout << "Tariffs:\n";
132 for (size_t i = 0; i < info.size(); ++i)
133 PrintTariff(info[i], 1);
136 void GetTariffCallback(bool result,
137 const std::string & reason,
138 const std::vector<STG::GET_TARIFF::INFO> & info,
141 assert(data != NULL && "Expecting pointer to std::string with the tariff's name.");
142 const std::string & name = *static_cast<const std::string *>(data);
145 std::cerr << "Failed to get tariff. Reason: '" << reason << "'." << std::endl;
148 for (size_t i = 0; i < info.size(); ++i)
149 if (info[i].tariffConf.name == name)
150 PrintTariff(info[i]);
153 bool GetTariffsFunction(const SGCONF::CONFIG & config,
154 const std::string & /*arg*/,
155 const std::map<std::string, std::string> & /*options*/)
157 STG::SERVCONF proto(config.server.data(),
159 config.userName.data(),
160 config.userPass.data());
161 return proto.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok;
164 bool GetTariffFunction(const SGCONF::CONFIG & config,
165 const std::string & arg,
166 const std::map<std::string, std::string> & /*options*/)
168 STG::SERVCONF proto(config.server.data(),
170 config.userName.data(),
171 config.userPass.data());
172 // STG currently doesn't support <GetTariff name="..."/>.
173 // So get a list of tariffs and filter it. 'data' param holds a pointer to 'name'.
174 std::string name(arg);
175 return proto.GetTariffs(GetTariffCallback, &name) == STG::st_ok;
178 bool DelTariffFunction(const SGCONF::CONFIG & config,
179 const std::string & arg,
180 const std::map<std::string, std::string> & /*options*/)
182 STG::SERVCONF proto(config.server.data(),
184 config.userName.data(),
185 config.userPass.data());
186 return proto.DelTariff(arg, SimpleCallback, NULL) == STG::st_ok;
189 bool AddTariffFunction(const SGCONF::CONFIG & config,
190 const std::string & arg,
191 const std::map<std::string, std::string> & /*options*/)
194 std::cerr << "Unimplemented.\n";
198 bool ChgTariffFunction(const SGCONF::CONFIG & config,
199 const std::string & arg,
200 const std::map<std::string, std::string> & options)
203 std::cerr << "Unimplemented.\n";
207 } // namespace anonymous
209 void SGCONF::AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
211 std::vector<API_ACTION::PARAM> params(GetTariffParams());
212 blocks.Add("Tariff management options")
213 .Add("get-tariffs", SGCONF::MakeAPIAction(commands, GetTariffsFunction), "\tget tariff list")
214 .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", GetTariffFunction), "get tariff")
215 .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, AddTariffFunction), "add tariff")
216 .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", DelTariffFunction), "del tariff")
217 .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, ChgTariffFunction), "change tariff");