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("</tariff>");
150 answerList->push_back("</Tariffs>");
152 //-----------------------------------------------------------------------------
154 //-----------------------------------------------------------------------------
155 int PARSER_ADD_TARIFF::ParseStart(void *, const char * el, const char ** attr)
157 if (strcasecmp(el, "AddTariff") == 0)
161 tariffToAdd = attr[1];
167 //-----------------------------------------------------------------------------
168 int PARSER_ADD_TARIFF::ParseEnd(void *, const char * el)
170 if (strcasecmp(el, "AddTariff") == 0)
177 //-----------------------------------------------------------------------------
178 void PARSER_ADD_TARIFF::CreateAnswer()
180 //answerList->clear();
181 answerList->erase(answerList->begin(), answerList->end());
183 if (tariffs->Add(tariffToAdd, currAdmin) == 0)
185 answerList->push_back("<AddTariff Result=\"Ok\"/>");
190 strprintf(&s, "<AddTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
191 answerList->push_back(s);
194 //-----------------------------------------------------------------------------
196 //-----------------------------------------------------------------------------
197 int PARSER_DEL_TARIFF::ParseStart(void *, const char * el, const char ** attr)
200 if (strcasecmp(el, "DelTariff") == 0)
202 tariffToDel = attr[1];
207 //-----------------------------------------------------------------------------
208 int PARSER_DEL_TARIFF::ParseEnd(void *, const char * el)
210 if (strcasecmp(el, "DelTariff") == 0)
217 //-----------------------------------------------------------------------------
218 void PARSER_DEL_TARIFF::CreateAnswer()
220 //answerList->clear();
221 answerList->erase(answerList->begin(), answerList->end());
223 if (users->TariffInUse(tariffToDel))
226 strprintf(&s, "<DelTariff Result=\"Error. Tariff \'%s\' cannot be deleted. Tariff in use.\"/>", tariffToDel.c_str());
227 answerList->push_back(s);
231 if (tariffs->Del(tariffToDel, currAdmin) == 0)
233 answerList->push_back("<DelTariff Result=\"Ok\"/>");
238 strprintf(&s, "<DelTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
239 answerList->push_back(s);
242 //-----------------------------------------------------------------------------
243 //-----------------------------------------------------------------------------
245 //-----------------------------------------------------------------------------
246 //-----------------------------------------------------------------------------
247 int PARSER_CHG_TARIFF::ParseSlashedIntParams(int paramsNum, const std::string & s, int * params)
249 char * str = new char[s.size() + 1];
251 strcpy(str, s.c_str());
252 p = strtok(str, "/");
254 for (int i = 0; i < paramsNum; i++)
262 if (str2x(p, params[i]) != 0)
268 p = strtok(NULL, "/");
274 //-----------------------------------------------------------------------------
275 int PARSER_CHG_TARIFF::ParseSlashedDoubleParams(int paramsNum, const std::string & s, double * params)
277 char * str = new char[s.size() + 1];
279 strcpy(str, s.c_str());
280 p = strtok(str, "/");
282 for (int i = 0; i < paramsNum; i++)
290 if (strtodouble2(p, params[i]) != 0)
296 p = strtok(NULL, "/");
302 //-----------------------------------------------------------------------------
303 int PARSER_CHG_TARIFF::ParseStart(void *, const char * el, const char ** attr)
306 double price[DIR_NUM];
312 if (strcasecmp(el, "SetTariff") == 0)
314 td.tariffConf.name = attr[1];
322 if (strcasecmp(el, "PriceDayA") == 0)
325 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
326 for (int j = 0; j < DIR_NUM; j++)
327 td.dirPrice[j].priceDayA = price[j] / pt_mega;
331 if (strcasecmp(el, "PriceDayB") == 0)
334 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
335 for (int j = 0; j < DIR_NUM; j++)
336 td.dirPrice[j].priceDayB = price[j] / pt_mega;
341 if (strcasecmp(el, "PriceNightA") == 0)
344 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
345 for (int j = 0; j < DIR_NUM; j++)
346 td.dirPrice[j].priceNightA = price[j] / pt_mega;
350 if (strcasecmp(el, "PriceNightB") == 0)
353 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
354 for (int j = 0; j < DIR_NUM; j++)
355 td.dirPrice[j].priceNightB = price[j] / pt_mega;
359 if (strcasecmp(el, "Threshold") == 0)
362 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
363 for (int j = 0; j < DIR_NUM; j++)
364 td.dirPrice[j].threshold = t[j];
368 if (strcasecmp(el, "SinglePrice") == 0)
371 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
372 for (int j = 0; j < DIR_NUM; j++)
373 td.dirPrice[j].singlePrice = t[j];
377 if (strcasecmp(el, "NoDiscount") == 0)
380 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
381 for (int j = 0; j < DIR_NUM; j++)
382 td.dirPrice[j].noDiscount = t[j];
386 for (int j = 0; j < DIR_NUM; j++)
388 snprintf(st, 50, "Time%d", j);
389 if (strcasecmp(el, st) == 0)
395 if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
397 td.dirPrice[j].hDay = h1;
398 td.dirPrice[j].mDay = m1;
399 td.dirPrice[j].hNight = h2;
400 td.dirPrice[j].mNight = m2;
406 if (strcasecmp(el, "Fee") == 0)
409 if (strtodouble2(attr[1], fee) == 0)
410 td.tariffConf.fee = fee;
414 if (strcasecmp(el, "PassiveCost") == 0)
417 if (strtodouble2(attr[1], pc) == 0)
418 td.tariffConf.passiveCost = pc;
421 if (strcasecmp(el, "Free") == 0)
424 if (strtodouble2(attr[1], free) == 0)
425 td.tariffConf.free = free;
429 if (strcasecmp(el, "TraffType") == 0)
431 if (strcasecmp(attr[1], "up") == 0)
433 td.tariffConf.traffType = TRAFF_UP;
437 if (strcasecmp(attr[1], "down") == 0)
439 td.tariffConf.traffType = TRAFF_DOWN;
442 if (strcasecmp(attr[1], "up+down") == 0)
444 td.tariffConf.traffType = TRAFF_UP_DOWN;
447 if (strcasecmp(attr[1], "max") == 0)
449 td.tariffConf.traffType = TRAFF_MAX;
457 //-----------------------------------------------------------------------------
458 int PARSER_CHG_TARIFF::ParseEnd(void *, const char * el)
462 if (strcasecmp(el, "SetTariff") == 0)
473 //-----------------------------------------------------------------------------
474 void PARSER_CHG_TARIFF::CreateAnswer()
476 answerList->erase(answerList->begin(), answerList->end());
478 if (!td.tariffConf.name.data().empty())
480 TARIFF_DATA tariffData = td.GetData();
481 if (tariffs->Chg(tariffData, currAdmin) == 0)
483 answerList->push_back("<SetTariff Result=\"ok\"/>");
489 strprintf(&s, "<SetTariff Result=\"Change tariff error! %s\"/>", tariffs->GetStrError().c_str());
490 answerList->push_back(s);
494 answerList->push_back("<SetTariff Result=\"Change tariff error!\"/>");
496 //-----------------------------------------------------------------------------