3 #include "api_action.h"
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/tariff_conf.h"
11 #include "stg/common.h"
12 #include "stg/os_int.h"
24 std::string Indent(size_t level, bool dash = false)
28 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
31 std::string PeriodToString(TARIFF::PERIOD period)
43 std::string TraffTypeToString(int traffType)
52 return "upload + download";
54 return "max(upload, download)";
59 void ConvPeriod(const std::string & value, RESETABLE<TARIFF::PERIOD> & res)
61 std::string lowered = ToLower(value);
62 if (lowered == "daily")
64 else if (lowered == "monthly")
67 throw SGCONF::ACTION::ERROR("Period should be 'daily' or 'monthly'. Got: '" + value + "'");
70 void ConvTraffType(const std::string & value, RESETABLE<int> & res)
72 std::string lowered = ToLower(value);
73 lowered.erase(std::remove(lowered.begin(), lowered.end(), ' '), lowered.end());
74 if (lowered == "upload")
76 else if (lowered == "download")
78 else if (lowered == "upload+download")
80 else if (lowered.substr(0, 3) == "max")
83 throw SGCONF::ACTION::ERROR("Traff type should be 'upload', 'download', 'upload + download' or 'max'. Got: '" + value + "'");
86 DIRPRICE_DATA_RES ConvTimeSpan(const std::string & value)
88 size_t dashPos = value.find_first_of('-');
89 if (dashPos == std::string::npos)
90 throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
91 size_t fromColon = value.find_first_of(':');
92 if (fromColon == std::string::npos || fromColon > dashPos)
93 throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
94 size_t toColon = value.find_first_of(':', dashPos);
95 if (toColon == std::string::npos)
96 throw SGCONF::ACTION::ERROR("Time span should be in format 'hh:mm-hh:mm'. Got: '" + value + "'");
97 DIRPRICE_DATA_RES res;
98 res.hDay = FromString<int>(value.substr(0, fromColon));
99 if (res.hDay.data() < 0 || res.hDay.data() > 23)
100 throw SGCONF::ACTION::ERROR("Invalid 'from' hours. Got: '" + value.substr(0, fromColon) + "'");
101 res.mDay = FromString<int>(value.substr(fromColon + 1, dashPos - fromColon - 1));
102 if (res.mDay.data() < 0 || res.mDay.data() > 59)
103 throw SGCONF::ACTION::ERROR("Invalid 'from' minutes. Got: '" + value.substr(fromColon + 1, dashPos - fromColon - 1) + "'");
104 res.hNight = FromString<int>(value.substr(dashPos + 1, toColon - dashPos - 1));
105 if (res.hNight.data() < 0 || res.hNight.data() > 23)
106 throw SGCONF::ACTION::ERROR("Invalid 'to' hours. Got: '" + value.substr(dashPos + 1, toColon - dashPos - 1) + "'");
107 res.mNight = FromString<int>(value.substr(toColon + 1, value.length() - toColon));
108 if (res.mNight.data() < 0 || res.mNight.data() > 59)
109 throw SGCONF::ACTION::ERROR("Invalid 'to' minutes. Got: '" + value.substr(toColon + 1, value.length() - toColon) + "'");
113 void Splice(std::vector<DIRPRICE_DATA_RES> & lhs, const std::vector<DIRPRICE_DATA_RES> & rhs)
115 for (size_t i = 0; i < lhs.size() && i < rhs.size(); ++i)
116 lhs[i].Splice(rhs[i]);
119 void ConvTimes(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
121 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
122 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvTimeSpan));
125 struct ConvPrice : public std::unary_function<std::string, DIRPRICE_DATA_RES>
127 typedef RESETABLE<double> (DIRPRICE_DATA_RES::* MemPtr);
128 ConvPrice(MemPtr before, MemPtr after)
129 : m_before(before), m_after(after)
132 DIRPRICE_DATA_RES operator()(const std::string & value)
134 DIRPRICE_DATA_RES res;
135 size_t slashPos = value.find_first_of('/');
136 if (slashPos == std::string::npos)
139 if (str2x(value, price) < 0)
140 throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value + "'");
141 (res.*m_before) = (res.*m_after) = price;
142 res.noDiscount = true;
147 if (str2x(value.substr(0, slashPos), price) < 0)
148 throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value.substr(0, slashPos) + "'");
149 (res.*m_before) = price;
150 if (str2x(value.substr(slashPos + 1, value.length() - slashPos), price) < 0)
151 throw SGCONF::ACTION::ERROR("Price should be a floating point number. Got: '" + value.substr(slashPos + 1, value.length() - slashPos) + "'");
152 (res.*m_after) = price;
153 res.noDiscount = false;
162 void ConvDayPrices(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
164 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
165 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvPrice(&DIRPRICE_DATA_RES::priceDayA, &DIRPRICE_DATA_RES::priceDayB)));
168 void ConvNightPrices(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
170 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
171 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvPrice(&DIRPRICE_DATA_RES::priceNightA, &DIRPRICE_DATA_RES::priceNightB)));
174 DIRPRICE_DATA_RES ConvThreshold(std::string value)
176 DIRPRICE_DATA_RES res;
177 double threshold = 0;
178 if (str2x(value, threshold) < 0)
179 throw SGCONF::ACTION::ERROR("Threshold should be a floating point value. Got: '" + value + "'");
180 res.threshold = threshold;
184 void ConvThresholds(std::string value, std::vector<DIRPRICE_DATA_RES> & res)
186 value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
187 Splice(res, Split<std::vector<DIRPRICE_DATA_RES> >(value, ',', ConvThreshold));
190 std::string TimeToString(int h, int m)
192 std::ostringstream stream;
193 stream << (h < 10 ? "0" : "") << h << ":"
194 << (m < 10 ? "0" : "") << m;
198 void PrintDirPriceData(size_t dir, const DIRPRICE_DATA & data, size_t level)
200 std::string night = TimeToString(data.hNight, data.mNight);
201 std::string day = TimeToString(data.hDay, data.mDay);
202 std::cout << Indent(level, true) << "dir: " << dir << "\n"
203 << Indent(level) << "'" << night << "' - '" << day << "': " << data.priceDayA << "/" << data.priceDayB << "\n"
204 << Indent(level) << "'" << day << "' - '" << night << "': " << data.priceNightA << "/" << data.priceNightB << "\n"
205 << Indent(level) << "threshold: " << data.threshold << "\n"
206 << Indent(level) << "single price: " << (data.singlePrice ? "yes" : "no") << "\n"
207 << Indent(level) << "discount: " << (data.noDiscount ? "no" : "yes") << "\n"; // Attention!
210 void PrintTariffConf(const TARIFF_CONF & conf, size_t level)
212 std::cout << Indent(level, true) << "name: " << conf.name << "\n"
213 << Indent(level) << "fee: " << conf.fee << "\n"
214 << Indent(level) << "free mb: " << conf.free << "\n"
215 << Indent(level) << "passive cost: " << conf.passiveCost << "\n"
216 << Indent(level) << "traff type: " << TraffTypeToString(conf.traffType) << "\n"
217 << Indent(level) << "period: " << PeriodToString(conf.period) << "\n";
220 void PrintTariff(const STG::GET_TARIFF::INFO & info, size_t level = 0)
222 PrintTariffConf(info.tariffConf, level);
223 std::cout << Indent(level) << "dir prices:\n";
224 for (size_t i = 0; i < info.dirPrice.size(); ++i)
225 PrintDirPriceData(i, info.dirPrice[i], level + 1);
228 std::vector<SGCONF::API_ACTION::PARAM> GetTariffParams()
230 std::vector<SGCONF::API_ACTION::PARAM> params;
231 params.push_back(SGCONF::API_ACTION::PARAM("fee", "<fee>", "\t\ttariff fee"));
232 params.push_back(SGCONF::API_ACTION::PARAM("free", "<free mb>", "\tprepaid traffic"));
233 params.push_back(SGCONF::API_ACTION::PARAM("passive-cost", "<cost>", "\tpassive cost"));
234 params.push_back(SGCONF::API_ACTION::PARAM("traff-type", "<type>", "\ttraffic type (up, down, up+down, max)"));
235 params.push_back(SGCONF::API_ACTION::PARAM("period", "<period>", "\ttarification period (daily, monthly)"));
236 params.push_back(SGCONF::API_ACTION::PARAM("times", "<hh:mm-hh:mm, ...>", "coma-separated day time-spans for each direction"));
237 params.push_back(SGCONF::API_ACTION::PARAM("day-prices", "<price/price, ...>", "coma-separated day prices for each direction"));
238 params.push_back(SGCONF::API_ACTION::PARAM("night-prices", "<price/price, ...>", "coma-separated day prices for each direction"));
239 params.push_back(SGCONF::API_ACTION::PARAM("thresholds", "<threshold, ...>", "coma-separated thresholds for each direction"));
243 void SimpleCallback(bool result,
244 const std::string & reason,
249 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
252 std::cout << "Success.\n";
255 void GetTariffsCallback(bool result,
256 const std::string & reason,
257 const std::vector<STG::GET_TARIFF::INFO> & info,
262 std::cerr << "Failed to get tariff list. Reason: '" << reason << "'." << std::endl;
265 std::cout << "Tariffs:\n";
266 for (size_t i = 0; i < info.size(); ++i)
267 PrintTariff(info[i], 1);
270 void GetTariffCallback(bool result,
271 const std::string & reason,
272 const std::vector<STG::GET_TARIFF::INFO> & info,
275 assert(data != NULL && "Expecting pointer to std::string with the tariff's name.");
276 const std::string & name = *static_cast<const std::string *>(data);
279 std::cerr << "Failed to get tariff. Reason: '" << reason << "'." << std::endl;
282 for (size_t i = 0; i < info.size(); ++i)
283 if (info[i].tariffConf.name == name)
284 PrintTariff(info[i]);
287 bool GetTariffsFunction(const SGCONF::CONFIG & config,
288 const std::string & /*arg*/,
289 const std::map<std::string, std::string> & /*options*/)
291 STG::SERVCONF proto(config.server.data(),
293 config.userName.data(),
294 config.userPass.data());
295 return proto.GetTariffs(GetTariffsCallback, NULL) == STG::st_ok;
298 bool GetTariffFunction(const SGCONF::CONFIG & config,
299 const std::string & arg,
300 const std::map<std::string, std::string> & /*options*/)
302 STG::SERVCONF proto(config.server.data(),
304 config.userName.data(),
305 config.userPass.data());
306 // STG currently doesn't support <GetTariff name="..."/>.
307 // So get a list of tariffs and filter it. 'data' param holds a pointer to 'name'.
308 std::string name(arg);
309 return proto.GetTariffs(GetTariffCallback, &name) == STG::st_ok;
312 bool DelTariffFunction(const SGCONF::CONFIG & config,
313 const std::string & arg,
314 const std::map<std::string, std::string> & /*options*/)
316 STG::SERVCONF proto(config.server.data(),
318 config.userName.data(),
319 config.userPass.data());
320 return proto.DelTariff(arg, SimpleCallback, NULL) == STG::st_ok;
323 bool AddTariffFunction(const SGCONF::CONFIG & config,
324 const std::string & arg,
325 const std::map<std::string, std::string> & options)
327 TARIFF_DATA_RES conf;
328 conf.tariffConf.name = arg;
329 SGCONF::MaybeSet(options, "fee", conf.tariffConf.fee);
330 SGCONF::MaybeSet(options, "free", conf.tariffConf.free);
331 SGCONF::MaybeSet(options, "passive-cost", conf.tariffConf.passiveCost);
332 SGCONF::MaybeSet(options, "traff-type", conf.tariffConf.traffType, ConvTraffType);
333 SGCONF::MaybeSet(options, "period", conf.tariffConf.period, ConvPeriod);
334 SGCONF::MaybeSet(options, "times", conf.dirPrice, ConvTimes);
335 SGCONF::MaybeSet(options, "day-prices", conf.dirPrice, ConvDayPrices);
336 SGCONF::MaybeSet(options, "night-prices", conf.dirPrice, ConvNightPrices);
337 SGCONF::MaybeSet(options, "thresholds", conf.dirPrice, ConvThresholds);
338 for (size_t i = 0; i < conf.dirPrice.size(); ++i)
340 if (!conf.dirPrice[i].priceDayA.empty() &&
341 !conf.dirPrice[i].priceNightA.empty() &&
342 !conf.dirPrice[i].priceDayB.empty() &&
343 !conf.dirPrice[i].priceNightB.empty())
344 conf.dirPrice[i].singlePrice = conf.dirPrice[i].priceDayA.data() == conf.dirPrice[i].priceNightA.data() &&
345 conf.dirPrice[i].priceDayB.data() == conf.dirPrice[i].priceNightB.data();
347 STG::SERVCONF proto(config.server.data(),
349 config.userName.data(),
350 config.userPass.data());
351 return proto.AddTariff(arg, conf, SimpleCallback, NULL) == STG::st_ok;
354 bool ChgTariffFunction(const SGCONF::CONFIG & config,
355 const std::string & arg,
356 const std::map<std::string, std::string> & options)
358 TARIFF_DATA_RES conf;
359 conf.tariffConf.name = arg;
360 SGCONF::MaybeSet(options, "fee", conf.tariffConf.fee);
361 SGCONF::MaybeSet(options, "free", conf.tariffConf.free);
362 SGCONF::MaybeSet(options, "passive-cost", conf.tariffConf.passiveCost);
363 SGCONF::MaybeSet(options, "traff-type", conf.tariffConf.traffType, ConvTraffType);
364 SGCONF::MaybeSet(options, "period", conf.tariffConf.period, ConvPeriod);
365 SGCONF::MaybeSet(options, "times", conf.dirPrice, ConvTimes);
366 SGCONF::MaybeSet(options, "day-prices", conf.dirPrice, ConvDayPrices);
367 SGCONF::MaybeSet(options, "night-prices", conf.dirPrice, ConvNightPrices);
368 SGCONF::MaybeSet(options, "thresholds", conf.dirPrice, ConvThresholds);
369 for (size_t i = 0; i < conf.dirPrice.size(); ++i)
371 if (!conf.dirPrice[i].priceDayA.empty() &&
372 !conf.dirPrice[i].priceNightA.empty() &&
373 !conf.dirPrice[i].priceDayB.empty() &&
374 !conf.dirPrice[i].priceNightB.empty())
375 conf.dirPrice[i].singlePrice = conf.dirPrice[i].priceDayA.data() == conf.dirPrice[i].priceNightA.data() &&
376 conf.dirPrice[i].priceDayB.data() == conf.dirPrice[i].priceNightB.data();
378 STG::SERVCONF proto(config.server.data(),
380 config.userName.data(),
381 config.userPass.data());
382 return proto.ChgTariff(conf, SimpleCallback, NULL) == STG::st_ok;
385 } // namespace anonymous
387 void SGCONF::AppendTariffsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
389 std::vector<API_ACTION::PARAM> params(GetTariffParams());
390 blocks.Add("Tariff management options")
391 .Add("get-tariffs", SGCONF::MakeAPIAction(commands, GetTariffsFunction), "\tget tariff list")
392 .Add("get-tariff", SGCONF::MakeAPIAction(commands, "<name>", GetTariffFunction), "get tariff")
393 .Add("add-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, AddTariffFunction), "add tariff")
394 .Add("del-tariff", SGCONF::MakeAPIAction(commands, "<name>", DelTariffFunction), "delete tariff")
395 .Add("chg-tariff", SGCONF::MakeAPIAction(commands, "<name>", params, ChgTariffFunction), "change tariff");