3 #include "stg/tariffs.h"
5 #include "stg/common.h"
7 #include <cstdio> // snprintf
10 const int pt_mega = 1024 * 1024;
11 //-----------------------------------------------------------------------------
13 //-----------------------------------------------------------------------------
14 int PARSER_GET_TARIFFS::ParseStart(void *, const char * el, const char **)
16 if (strcasecmp(el, "GetTariffs") == 0)
22 //-----------------------------------------------------------------------------
23 int PARSER_GET_TARIFFS::ParseEnd(void *, const char * el)
25 if (strcasecmp(el, "GetTariffs") == 0)
32 //-----------------------------------------------------------------------------
33 void PARSER_GET_TARIFFS::CreateAnswer()
37 std::list<TARIFF_DATA> dataList;
38 tariffs->GetTariffsData(&dataList);
39 std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
40 for (; it != dataList.end(); ++it)
42 answer += "<tariff name=\"" + it->tariffConf.name + "\">";
44 for (size_t i = 0; i < DIR_NUM; i++)
45 answer += "<Time" + x2str(i) + " value=\"" +
46 x2str(it->dirPrice[i].hDay) + ":" + x2str(it->dirPrice[i].mDay) + "-" +
47 x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>";
49 answer += "<PriceDayA value=\"";
51 for (size_t i = 0; i < DIR_NUM; i++)
57 answer += x2str(it->dirPrice[i].priceDayA * pt_mega);
61 answer += "<PriceDayB value=\"";
63 for (size_t i = 0; i < DIR_NUM; i++)
69 answer += x2str(it->dirPrice[i].priceDayB * pt_mega);
73 answer += "<PriceNightA value=\"";
75 for (size_t i = 0; i < DIR_NUM; i++)
81 answer += x2str(it->dirPrice[i].priceNightA * pt_mega);
85 answer += "<PriceNightB value=\"";
87 for (size_t i = 0; i < DIR_NUM; i++)
93 answer += x2str(it->dirPrice[i].priceNightB * pt_mega);
97 answer += "<Threshold value=\"";
99 for (size_t i = 0; i < DIR_NUM; i++)
105 answer += x2str(it->dirPrice[i].threshold);
109 answer += "<SinglePrice value=\"";
111 for (size_t i = 0; i < DIR_NUM; i++)
117 answer += (it->dirPrice[i].singlePrice ? "1" : "0");
121 answer += "<NoDiscount value=\"";
123 for (size_t i = 0; i < DIR_NUM; i++)
129 answer += (it->dirPrice[i].noDiscount ? "1" : "0");
133 answer += "<Fee value=\"" + x2str(it->tariffConf.fee) + "\"/>";
135 answer += "<PassiveCost value=\"" + x2str(it->tariffConf.passiveCost) + "\"/>";
137 answer += "<Free value=\"" + x2str(it->tariffConf.free) + "\"/>";
139 switch (it->tariffConf.traffType)
142 answer += "<TraffType value=\"up\"/>";
145 answer += "<TraffType value=\"down\"/>";
148 answer += "<TraffType value=\"up+down\"/>";
151 answer += "<TraffType value=\"max\"/>";
155 answer += "<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>";
157 answer += "</tariff>";
159 answer += "</Tariffs>";
161 //-----------------------------------------------------------------------------
163 //-----------------------------------------------------------------------------
164 int PARSER_ADD_TARIFF::ParseStart(void *, const char * el, const char ** attr)
166 if (strcasecmp(el, "AddTariff") == 0)
170 tariffToAdd = attr[1];
176 //-----------------------------------------------------------------------------
177 int PARSER_ADD_TARIFF::ParseEnd(void *, const char * el)
179 if (strcasecmp(el, "AddTariff") == 0)
186 //-----------------------------------------------------------------------------
187 void PARSER_ADD_TARIFF::CreateAnswer()
189 if (tariffs->Add(tariffToAdd, currAdmin) == 0)
190 answer = "<AddTariff Result=\"Ok\"/>";
192 answer = "<AddTariff Result=\"Error. " + tariffs->GetStrError() + "\"/>";
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 if (users->TariffInUse(tariffToDel))
221 answer = "<DelTariff Result=\"Error. Tariff \'" + tariffToDel + "\' cannot be deleted. Tariff in use.\"/>";
222 else if (tariffs->Del(tariffToDel, currAdmin) == 0)
223 answer = "<DelTariff Result=\"Ok\"/>";
225 answer = "<DelTariff Result=\"Error. " + tariffs->GetStrError() + "\"/>";
227 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
230 //-----------------------------------------------------------------------------
231 //-----------------------------------------------------------------------------
232 int PARSER_CHG_TARIFF::ParseSlashedIntParams(int paramsNum, const std::string & s, int * params)
234 char * str = new char[s.size() + 1];
236 strcpy(str, s.c_str());
237 p = strtok(str, "/");
239 for (int i = 0; i < paramsNum; i++)
247 if (str2x(p, params[i]) != 0)
253 p = strtok(NULL, "/");
259 //-----------------------------------------------------------------------------
260 int PARSER_CHG_TARIFF::ParseSlashedDoubleParams(int paramsNum, const std::string & s, double * params)
262 char * str = new char[s.size() + 1];
264 strcpy(str, s.c_str());
265 p = strtok(str, "/");
267 for (int i = 0; i < paramsNum; i++)
275 if (strtodouble2(p, params[i]) != 0)
281 p = strtok(NULL, "/");
287 //-----------------------------------------------------------------------------
288 int PARSER_CHG_TARIFF::ParseStart(void *, const char * el, const char ** attr)
290 double price[DIR_NUM];
296 if (strcasecmp(el, "SetTariff") == 0)
298 td.tariffConf.name = attr[1];
306 if (strcasecmp(el, "PriceDayA") == 0)
309 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
310 for (int j = 0; j < DIR_NUM; j++)
311 td.dirPrice[j].priceDayA = price[j] / pt_mega;
315 if (strcasecmp(el, "PriceDayB") == 0)
318 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
319 for (int j = 0; j < DIR_NUM; j++)
320 td.dirPrice[j].priceDayB = price[j] / pt_mega;
325 if (strcasecmp(el, "PriceNightA") == 0)
328 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
329 for (int j = 0; j < DIR_NUM; j++)
330 td.dirPrice[j].priceNightA = price[j] / pt_mega;
334 if (strcasecmp(el, "PriceNightB") == 0)
337 if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
338 for (int j = 0; j < DIR_NUM; j++)
339 td.dirPrice[j].priceNightB = price[j] / pt_mega;
343 if (strcasecmp(el, "Threshold") == 0)
346 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
347 for (int j = 0; j < DIR_NUM; j++)
348 td.dirPrice[j].threshold = t[j];
352 if (strcasecmp(el, "SinglePrice") == 0)
355 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
356 for (int j = 0; j < DIR_NUM; j++)
357 td.dirPrice[j].singlePrice = t[j];
361 if (strcasecmp(el, "NoDiscount") == 0)
364 if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
365 for (int j = 0; j < DIR_NUM; j++)
366 td.dirPrice[j].noDiscount = t[j];
370 for (int j = 0; j < DIR_NUM; j++)
373 snprintf(st, 50, "Time%d", j);
374 if (strcasecmp(el, st) == 0)
380 if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
382 td.dirPrice[j].hDay = h1;
383 td.dirPrice[j].mDay = m1;
384 td.dirPrice[j].hNight = h2;
385 td.dirPrice[j].mNight = m2;
391 if (strcasecmp(el, "Fee") == 0)
394 if (strtodouble2(attr[1], fee) == 0)
395 td.tariffConf.fee = fee;
399 if (strcasecmp(el, "PassiveCost") == 0)
402 if (strtodouble2(attr[1], pc) == 0)
403 td.tariffConf.passiveCost = pc;
406 if (strcasecmp(el, "Free") == 0)
409 if (strtodouble2(attr[1], free) == 0)
410 td.tariffConf.free = free;
414 if (strcasecmp(el, "TraffType") == 0)
416 if (strcasecmp(attr[1], "up") == 0)
418 td.tariffConf.traffType = TRAFF_UP;
422 if (strcasecmp(attr[1], "down") == 0)
424 td.tariffConf.traffType = TRAFF_DOWN;
427 if (strcasecmp(attr[1], "up+down") == 0)
429 td.tariffConf.traffType = TRAFF_UP_DOWN;
432 if (strcasecmp(attr[1], "max") == 0)
434 td.tariffConf.traffType = TRAFF_MAX;
440 if (strcasecmp(el, "Period") == 0)
442 td.tariffConf.period = TARIFF::StringToPeriod(attr[1]);
448 //-----------------------------------------------------------------------------
449 int PARSER_CHG_TARIFF::ParseEnd(void *, const char * el)
453 if (strcasecmp(el, "SetTariff") == 0)
464 //-----------------------------------------------------------------------------
465 void PARSER_CHG_TARIFF::CreateAnswer()
467 if (!td.tariffConf.name.data().empty())
469 TARIFF_DATA tariffData = td.GetData();
470 if (tariffs->Chg(tariffData, currAdmin) == 0)
472 answer = "<SetTariff Result=\"ok\"/>";
477 answer = "<SetTariff Result=\"Change tariff error! " + tariffs->GetStrError() + "\"/>";
481 answer = "<SetTariff Result=\"Change tariff error!\"/>";
483 //-----------------------------------------------------------------------------