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()
34 std::list<TARIFF_DATA> dataList;
35 tariffs->GetTariffsData(&dataList);
36 std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
37 for (; it != dataList.end(); ++it)
39 answer += "<tariff name=\"" + it->tariffConf.name + "\">";
41 for (size_t i = 0; i < DIR_NUM; i++)
42 answer += "<Time" + x2str(i) + " value=\"" +
43 x2str(it->dirPrice[i].hDay) + ":" + x2str(it->dirPrice[i].mDay) + "-" +
44 x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>";
46 answer += "<PriceDayA value=\"";
48 for (size_t i = 0; i < DIR_NUM; i++)
54 answer += x2str(it->dirPrice[i].priceDayA * pt_mega);
58 answer += "<PriceDayB value=\"";
60 for (size_t i = 0; i < DIR_NUM; i++)
66 answer += x2str(it->dirPrice[i].priceDayB * pt_mega);
70 answer += "<PriceNightA value=\"";
72 for (size_t i = 0; i < DIR_NUM; i++)
78 answer += x2str(it->dirPrice[i].priceNightA * pt_mega);
82 answer += "<PriceNightB value=\"";
84 for (size_t i = 0; i < DIR_NUM; i++)
90 answer += x2str(it->dirPrice[i].priceNightB * pt_mega);
94 answer += "<Threshold value=\"";
96 for (size_t i = 0; i < DIR_NUM; i++)
102 answer += x2str(it->dirPrice[i].threshold);
106 answer += "<SinglePrice value=\"";
108 for (size_t i = 0; i < DIR_NUM; i++)
114 answer += (it->dirPrice[i].singlePrice ? "1" : "0");
118 answer += "<NoDiscount value=\"";
120 for (size_t i = 0; i < DIR_NUM; i++)
126 answer += (it->dirPrice[i].noDiscount ? "1" : "0");
130 answer += "<Fee value=\"" + x2str(it->tariffConf.fee) + "\"/>";
132 answer += "<PassiveCost value=\"" + x2str(it->tariffConf.passiveCost) + "\"/>";
134 answer += "<Free value=\"" + x2str(it->tariffConf.free) + "\"/>";
136 switch (it->tariffConf.traffType)
139 answer += "<TraffType value=\"up\"/>";
142 answer += "<TraffType value=\"down\"/>";
145 answer += "<TraffType value=\"up+down\"/>";
148 answer += "<TraffType value=\"max\"/>";
152 answer += "<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>";
154 answer += "</tariff>";
156 answer += "</Tariffs>";
158 //-----------------------------------------------------------------------------
160 //-----------------------------------------------------------------------------
161 int PARSER_ADD_TARIFF::ParseStart(void *, const char * el, const char ** attr)
163 if (strcasecmp(el, "AddTariff") == 0)
167 tariffToAdd = attr[1];
173 //-----------------------------------------------------------------------------
174 int PARSER_ADD_TARIFF::ParseEnd(void *, const char * el)
176 if (strcasecmp(el, "AddTariff") == 0)
183 //-----------------------------------------------------------------------------
184 void PARSER_ADD_TARIFF::CreateAnswer()
186 if (tariffs->Add(tariffToAdd, currAdmin) == 0)
187 answer = "<AddTariff Result=\"Ok\"/>";
189 answer = "<AddTariff Result=\"Error. " + tariffs->GetStrError() + "\"/>";
191 //-----------------------------------------------------------------------------
193 //-----------------------------------------------------------------------------
194 int PARSER_DEL_TARIFF::ParseStart(void *, const char * el, const char ** attr)
197 if (strcasecmp(el, "DelTariff") == 0)
199 tariffToDel = attr[1];
204 //-----------------------------------------------------------------------------
205 int PARSER_DEL_TARIFF::ParseEnd(void *, const char * el)
207 if (strcasecmp(el, "DelTariff") == 0)
214 //-----------------------------------------------------------------------------
215 void PARSER_DEL_TARIFF::CreateAnswer()
217 if (users->TariffInUse(tariffToDel))
218 answer = "<DelTariff Result=\"Error. Tariff \'" + tariffToDel + "\' cannot be deleted. Tariff in use.\"/>";
219 else if (tariffs->Del(tariffToDel, currAdmin) == 0)
220 answer = "<DelTariff Result=\"Ok\"/>";
222 answer = "<DelTariff Result=\"Error. " + tariffs->GetStrError() + "\"/>";
224 //-----------------------------------------------------------------------------
225 //-----------------------------------------------------------------------------
227 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
229 int PARSER_CHG_TARIFF::ParseSlashedIntParams(int paramsNum, const std::string & s, int * params)
231 char * str = new char[s.size() + 1];
233 strcpy(str, s.c_str());
234 p = strtok(str, "/");
236 for (int i = 0; i < paramsNum; i++)
244 if (str2x(p, params[i]) != 0)
250 p = strtok(NULL, "/");
256 //-----------------------------------------------------------------------------
257 int PARSER_CHG_TARIFF::ParseSlashedDoubleParams(int paramsNum, const std::string & s, double * params)
259 char * str = new char[s.size() + 1];
261 strcpy(str, s.c_str());
262 p = strtok(str, "/");
264 for (int i = 0; i < paramsNum; i++)
272 if (strtodouble2(p, params[i]) != 0)
278 p = strtok(NULL, "/");
284 //-----------------------------------------------------------------------------
285 int PARSER_CHG_TARIFF::ParseStart(void *, const char * el, const char ** attr)
287 double price[DIR_NUM];
293 if (strcasecmp(el, "SetTariff") == 0)
295 td.tariffConf.name = attr[1];
303 if (strcasecmp(el, "PriceDayA") == 0)
306 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
307 for (int j = 0; j < DIR_NUM; j++)
308 td.dirPrice[j].priceDayA = price[j] / pt_mega;
312 if (strcasecmp(el, "PriceDayB") == 0)
315 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
316 for (int j = 0; j < DIR_NUM; j++)
317 td.dirPrice[j].priceDayB = price[j] / pt_mega;
322 if (strcasecmp(el, "PriceNightA") == 0)
325 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
326 for (int j = 0; j < DIR_NUM; j++)
327 td.dirPrice[j].priceNightA = price[j] / pt_mega;
331 if (strcasecmp(el, "PriceNightB") == 0)
334 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
335 for (int j = 0; j < DIR_NUM; j++)
336 td.dirPrice[j].priceNightB = price[j] / pt_mega;
340 if (strcasecmp(el, "Threshold") == 0)
343 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
344 for (int j = 0; j < DIR_NUM; j++)
345 td.dirPrice[j].threshold = t[j];
349 if (strcasecmp(el, "SinglePrice") == 0)
352 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
353 for (int j = 0; j < DIR_NUM; j++)
354 td.dirPrice[j].singlePrice = t[j];
358 if (strcasecmp(el, "NoDiscount") == 0)
361 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
362 for (int j = 0; j < DIR_NUM; j++)
363 td.dirPrice[j].noDiscount = t[j];
367 for (int j = 0; j < DIR_NUM; j++)
370 snprintf(st, 50, "Time%d", j);
371 if (strcasecmp(el, st) == 0)
377 if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
379 td.dirPrice[j].hDay = h1;
380 td.dirPrice[j].mDay = m1;
381 td.dirPrice[j].hNight = h2;
382 td.dirPrice[j].mNight = m2;
388 if (strcasecmp(el, "Fee") == 0)
391 if (strtodouble2(attr[1], fee) == 0)
392 td.tariffConf.fee = fee;
396 if (strcasecmp(el, "PassiveCost") == 0)
399 if (strtodouble2(attr[1], pc) == 0)
400 td.tariffConf.passiveCost = pc;
403 if (strcasecmp(el, "Free") == 0)
406 if (strtodouble2(attr[1], free) == 0)
407 td.tariffConf.free = free;
411 if (strcasecmp(el, "TraffType") == 0)
413 if (strcasecmp(attr[1], "up") == 0)
415 td.tariffConf.traffType = TRAFF_UP;
419 if (strcasecmp(attr[1], "down") == 0)
421 td.tariffConf.traffType = TRAFF_DOWN;
424 if (strcasecmp(attr[1], "up+down") == 0)
426 td.tariffConf.traffType = TRAFF_UP_DOWN;
429 if (strcasecmp(attr[1], "max") == 0)
431 td.tariffConf.traffType = TRAFF_MAX;
437 if (strcasecmp(el, "Period") == 0)
439 td.tariffConf.period = TARIFF::StringToPeriod(attr[1]);
445 //-----------------------------------------------------------------------------
446 int PARSER_CHG_TARIFF::ParseEnd(void *, const char * el)
450 if (strcasecmp(el, "SetTariff") == 0)
461 //-----------------------------------------------------------------------------
462 void PARSER_CHG_TARIFF::CreateAnswer()
464 if (!td.tariffConf.name.data().empty())
466 TARIFF_DATA tariffData = td.GetData();
467 if (tariffs->Chg(tariffData, currAdmin) == 0)
469 answer = "<SetTariff Result=\"ok\"/>";
474 answer = "<SetTariff Result=\"Change tariff error! " + tariffs->GetStrError() + "\"/>";
478 answer = "<SetTariff Result=\"Change tariff error!\"/>";
480 //-----------------------------------------------------------------------------