1 #include <cstdio> // snprintf
4 #include "stg/tariffs.h"
7 const int pt_mega = 1024 * 1024;
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
11 int PARSER_GET_TARIFFS::ParseStart(void *, const char * el, const char **)
13 if (strcasecmp(el, "GetTariffs") == 0)
19 //-----------------------------------------------------------------------------
20 int PARSER_GET_TARIFFS::ParseEnd(void *, const char * el)
22 if (strcasecmp(el, "GetTariffs") == 0)
29 //-----------------------------------------------------------------------------
30 void PARSER_GET_TARIFFS::CreateAnswer()
36 answerList->erase(answerList->begin(), answerList->end());
38 answerList->push_back("<Tariffs>");
40 std::list<TARIFF_DATA> dataList;
41 tariffs->GetTariffsData(&dataList);
42 std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
43 for (; it != dataList.end(); ++it)
45 s = "<tariff name=\"" + it->tariffConf.name + "\">";
46 answerList->push_back(s);
48 for (int j = 0; j < DIR_NUM; j++)
50 hd = it->dirPrice[j].hDay;
51 md = it->dirPrice[j].mDay;
53 hn = it->dirPrice[j].hNight;
54 mn = it->dirPrice[j].mNight;
56 strprintf(&s, "<Time%d value=\"%d:%d-%d:%d\"/>", j, hd, md, hn, mn);
57 answerList->push_back(s);
60 strprintf(&s, " <PriceDayA value=\"");
61 for (int i = 0; i < DIR_NUM; i++)
63 snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceDayA * pt_mega, i+1 == DIR_NUM?"":"/");
67 answerList->push_back(s);
69 strprintf(&s, " <PriceDayB value=\"");
70 for (int i = 0; i < DIR_NUM; i++)
72 snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceDayB * pt_mega, i+1 == DIR_NUM?"":"/");
76 answerList->push_back(s);
78 strprintf(&s, " <PriceNightA value=\"");
79 for (int i = 0; i < DIR_NUM; i++)
81 snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceNightA * pt_mega, i+1 == DIR_NUM?"":"/");
85 answerList->push_back(s);
87 strprintf(&s, " <PriceNightB value=\"");
88 for (int i = 0; i < DIR_NUM; i++)
90 snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceNightB * pt_mega, i+1 == DIR_NUM?"":"/");
94 answerList->push_back(s);
96 strprintf(&s, " <Threshold value=\"");
97 for (int i = 0; i < DIR_NUM; i++)
99 snprintf(vs, 100, "%d%s", it->dirPrice[i].threshold, i+1 == DIR_NUM?"":"/");
103 answerList->push_back(s);
105 strprintf(&s, " <SinglePrice value=\"");
106 for (int i = 0; i < DIR_NUM; i++)
108 snprintf(vs, 100, "%d%s", it->dirPrice[i].singlePrice, i+1 == DIR_NUM?"":"/");
112 answerList->push_back(s);
114 strprintf(&s, " <NoDiscount value=\"");
115 for (int i = 0; i < DIR_NUM; i++)
117 snprintf(vs, 100, "%d%s", it->dirPrice[i].noDiscount, i+1 == DIR_NUM?"":"/");
121 answerList->push_back(s);
123 strprintf(&s, " <Fee value=\"%.5f\"/>", it->tariffConf.fee);
124 answerList->push_back(s);
126 strprintf(&s, " <PassiveCost value=\"%.5f\"/>", it->tariffConf.passiveCost);
127 answerList->push_back(s);
129 strprintf(&s, " <Free value=\"%.5f\"/>", it->tariffConf.free);
130 answerList->push_back(s);
132 switch (it->tariffConf.traffType)
135 answerList->push_back("<TraffType value=\"up\"/>");
138 answerList->push_back("<TraffType value=\"down\"/>");
141 answerList->push_back("<TraffType value=\"up+down\"/>");
144 answerList->push_back("<TraffType value=\"max\"/>");
148 answerList->push_back("<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>");
150 answerList->push_back("</tariff>");
152 answerList->push_back("</Tariffs>");
154 //-----------------------------------------------------------------------------
156 //-----------------------------------------------------------------------------
157 int PARSER_ADD_TARIFF::ParseStart(void *, const char * el, const char ** attr)
159 if (strcasecmp(el, "AddTariff") == 0)
163 tariffToAdd = attr[1];
169 //-----------------------------------------------------------------------------
170 int PARSER_ADD_TARIFF::ParseEnd(void *, const char * el)
172 if (strcasecmp(el, "AddTariff") == 0)
179 //-----------------------------------------------------------------------------
180 void PARSER_ADD_TARIFF::CreateAnswer()
182 //answerList->clear();
183 answerList->erase(answerList->begin(), answerList->end());
185 if (tariffs->Add(tariffToAdd, currAdmin) == 0)
187 answerList->push_back("<AddTariff Result=\"Ok\"/>");
192 strprintf(&s, "<AddTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
193 answerList->push_back(s);
196 //-----------------------------------------------------------------------------
198 //-----------------------------------------------------------------------------
199 int PARSER_DEL_TARIFF::ParseStart(void *, const char * el, const char ** attr)
202 if (strcasecmp(el, "DelTariff") == 0)
204 tariffToDel = attr[1];
209 //-----------------------------------------------------------------------------
210 int PARSER_DEL_TARIFF::ParseEnd(void *, const char * el)
212 if (strcasecmp(el, "DelTariff") == 0)
219 //-----------------------------------------------------------------------------
220 void PARSER_DEL_TARIFF::CreateAnswer()
222 //answerList->clear();
223 answerList->erase(answerList->begin(), answerList->end());
225 if (users->TariffInUse(tariffToDel))
228 strprintf(&s, "<DelTariff Result=\"Error. Tariff \'%s\' cannot be deleted. Tariff in use.\"/>", tariffToDel.c_str());
229 answerList->push_back(s);
233 if (tariffs->Del(tariffToDel, currAdmin) == 0)
235 answerList->push_back("<DelTariff Result=\"Ok\"/>");
240 strprintf(&s, "<DelTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
241 answerList->push_back(s);
244 //-----------------------------------------------------------------------------
245 //-----------------------------------------------------------------------------
247 //-----------------------------------------------------------------------------
248 //-----------------------------------------------------------------------------
249 int PARSER_CHG_TARIFF::ParseSlashedIntParams(int paramsNum, const std::string & s, int * params)
251 char * str = new char[s.size() + 1];
253 strcpy(str, s.c_str());
254 p = strtok(str, "/");
256 for (int i = 0; i < paramsNum; i++)
264 if (str2x(p, params[i]) != 0)
270 p = strtok(NULL, "/");
276 //-----------------------------------------------------------------------------
277 int PARSER_CHG_TARIFF::ParseSlashedDoubleParams(int paramsNum, const std::string & s, double * params)
279 char * str = new char[s.size() + 1];
281 strcpy(str, s.c_str());
282 p = strtok(str, "/");
284 for (int i = 0; i < paramsNum; i++)
292 if (strtodouble2(p, params[i]) != 0)
298 p = strtok(NULL, "/");
304 //-----------------------------------------------------------------------------
305 int PARSER_CHG_TARIFF::ParseStart(void *, const char * el, const char ** attr)
308 double price[DIR_NUM];
314 if (strcasecmp(el, "SetTariff") == 0)
316 td.tariffConf.name = attr[1];
324 if (strcasecmp(el, "PriceDayA") == 0)
327 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
328 for (int j = 0; j < DIR_NUM; j++)
329 td.dirPrice[j].priceDayA = price[j] / pt_mega;
333 if (strcasecmp(el, "PriceDayB") == 0)
336 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
337 for (int j = 0; j < DIR_NUM; j++)
338 td.dirPrice[j].priceDayB = price[j] / pt_mega;
343 if (strcasecmp(el, "PriceNightA") == 0)
346 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
347 for (int j = 0; j < DIR_NUM; j++)
348 td.dirPrice[j].priceNightA = price[j] / pt_mega;
352 if (strcasecmp(el, "PriceNightB") == 0)
355 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
356 for (int j = 0; j < DIR_NUM; j++)
357 td.dirPrice[j].priceNightB = price[j] / pt_mega;
361 if (strcasecmp(el, "Threshold") == 0)
364 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
365 for (int j = 0; j < DIR_NUM; j++)
366 td.dirPrice[j].threshold = t[j];
370 if (strcasecmp(el, "SinglePrice") == 0)
373 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
374 for (int j = 0; j < DIR_NUM; j++)
375 td.dirPrice[j].singlePrice = t[j];
379 if (strcasecmp(el, "NoDiscount") == 0)
382 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
383 for (int j = 0; j < DIR_NUM; j++)
384 td.dirPrice[j].noDiscount = t[j];
388 for (int j = 0; j < DIR_NUM; j++)
390 snprintf(st, 50, "Time%d", j);
391 if (strcasecmp(el, st) == 0)
397 if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
399 td.dirPrice[j].hDay = h1;
400 td.dirPrice[j].mDay = m1;
401 td.dirPrice[j].hNight = h2;
402 td.dirPrice[j].mNight = m2;
408 if (strcasecmp(el, "Fee") == 0)
411 if (strtodouble2(attr[1], fee) == 0)
412 td.tariffConf.fee = fee;
416 if (strcasecmp(el, "PassiveCost") == 0)
419 if (strtodouble2(attr[1], pc) == 0)
420 td.tariffConf.passiveCost = pc;
423 if (strcasecmp(el, "Free") == 0)
426 if (strtodouble2(attr[1], free) == 0)
427 td.tariffConf.free = free;
431 if (strcasecmp(el, "TraffType") == 0)
433 if (strcasecmp(attr[1], "up") == 0)
435 td.tariffConf.traffType = TRAFF_UP;
439 if (strcasecmp(attr[1], "down") == 0)
441 td.tariffConf.traffType = TRAFF_DOWN;
444 if (strcasecmp(attr[1], "up+down") == 0)
446 td.tariffConf.traffType = TRAFF_UP_DOWN;
449 if (strcasecmp(attr[1], "max") == 0)
451 td.tariffConf.traffType = TRAFF_MAX;
457 if (strcasecmp(el, "Period") == 0)
459 td.tariffConf.period = TARIFF::StringToPeriod(attr[1]);
465 //-----------------------------------------------------------------------------
466 int PARSER_CHG_TARIFF::ParseEnd(void *, const char * el)
470 if (strcasecmp(el, "SetTariff") == 0)
481 //-----------------------------------------------------------------------------
482 void PARSER_CHG_TARIFF::CreateAnswer()
484 answerList->erase(answerList->begin(), answerList->end());
486 if (!td.tariffConf.name.data().empty())
488 TARIFF_DATA tariffData = td.GetData();
489 if (tariffs->Chg(tariffData, currAdmin) == 0)
491 answerList->push_back("<SetTariff Result=\"ok\"/>");
497 strprintf(&s, "<SetTariff Result=\"Change tariff error! %s\"/>", tariffs->GetStrError().c_str());
498 answerList->push_back(s);
502 answerList->push_back("<SetTariff Result=\"Change tariff error!\"/>");
504 //-----------------------------------------------------------------------------