]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_tariff.cpp
e3b265206405c2e8a63942c9366d70bd3eed3703
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_tariff.cpp
1 #include <cstdio> // snprintf
2 #include <cstring>
3
4 #include "parser.h"
5
6 const int pt_mega = 1024 * 1024;
7 //-----------------------------------------------------------------------------
8 //  GET TARIFFS
9 //-----------------------------------------------------------------------------
10 int PARSER_GET_TARIFFS::ParseStart(void *, const char * el, const char **)
11 {
12 if (strcasecmp(el, "GetTariffs") == 0)
13     {
14     return 0;
15     }
16 return -1;
17 }
18 //-----------------------------------------------------------------------------
19 int PARSER_GET_TARIFFS::ParseEnd(void *, const char * el)
20 {
21 if (strcasecmp(el, "GetTariffs") == 0)
22     {
23     CreateAnswer();
24     return 0;
25     }
26 return -1;
27 }
28 //-----------------------------------------------------------------------------
29 void PARSER_GET_TARIFFS::CreateAnswer()
30 {
31 string s;
32 char vs[100];
33 int hd, hn, md, mn;
34
35 answerList->erase(answerList->begin(), answerList->end());
36
37 answerList->push_back("<Tariffs>");
38
39 std::list<TARIFF_DATA> dataList;
40 tariffs->GetTariffsData(&dataList);
41 std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
42 for (; it != dataList.end(); ++it)
43     {
44     s = "<tariff name=\"" + it->tariffConf.name + "\">";
45     answerList->push_back(s);
46
47     for (int j = 0; j < DIR_NUM; j++)
48         {
49         hd = it->dirPrice[j].hDay;
50         md = it->dirPrice[j].mDay;
51
52         hn = it->dirPrice[j].hNight;
53         mn = it->dirPrice[j].mNight;
54
55         strprintf(&s, "<Time%d value=\"%d:%d-%d:%d\"/>", j, hd, md, hn, mn);
56         answerList->push_back(s);
57         }
58
59     strprintf(&s, "    <PriceDayA value=\"");
60     for (int i = 0; i < DIR_NUM; i++)
61         {
62         snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceDayA * pt_mega, i+1 == DIR_NUM?"":"/");
63         s += vs;
64         }
65     s += "\"/>";
66     answerList->push_back(s);
67
68     strprintf(&s, "    <PriceDayB value=\"");
69     for (int i = 0; i < DIR_NUM; i++)
70         {
71         snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceDayB * pt_mega, i+1 == DIR_NUM?"":"/");
72         s += vs;
73         }
74     s += "\"/>";
75     answerList->push_back(s);
76
77     strprintf(&s, "    <PriceNightA value=\"");
78     for (int i = 0; i < DIR_NUM; i++)
79         {
80         snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceNightA * pt_mega, i+1 == DIR_NUM?"":"/");
81         s += vs;
82         }
83     s += "\"/>";
84     answerList->push_back(s);
85
86     strprintf(&s, "    <PriceNightB value=\"");
87     for (int i = 0; i < DIR_NUM; i++)
88         {
89         snprintf(vs, 100, "%.5f%s", it->dirPrice[i].priceNightB * pt_mega, i+1 == DIR_NUM?"":"/");
90         s += vs;
91         }
92     s += "\"/>";
93     answerList->push_back(s);
94
95     strprintf(&s, "    <Threshold value=\"");
96     for (int i = 0; i < DIR_NUM; i++)
97         {
98         snprintf(vs, 100, "%d%s", it->dirPrice[i].threshold, i+1 == DIR_NUM?"":"/");
99         s += vs;
100         }
101     s += "\"/>";
102     answerList->push_back(s);
103
104     strprintf(&s, "    <SinglePrice value=\"");
105     for (int i = 0; i < DIR_NUM; i++)
106         {
107         snprintf(vs, 100, "%d%s", it->dirPrice[i].singlePrice, i+1 == DIR_NUM?"":"/");
108         s += vs;
109         }
110     s += "\"/>";
111     answerList->push_back(s);
112
113     strprintf(&s, "    <NoDiscount value=\"");
114     for (int i = 0; i < DIR_NUM; i++)
115         {
116         snprintf(vs, 100, "%d%s", it->dirPrice[i].noDiscount, i+1 == DIR_NUM?"":"/");
117         s += vs;
118         }
119     s += "\"/>";
120     answerList->push_back(s);
121
122     strprintf(&s, "    <Fee value=\"%.5f\"/>", it->tariffConf.fee);
123     answerList->push_back(s);
124
125     strprintf(&s, "    <PassiveCost value=\"%.5f\"/>", it->tariffConf.passiveCost);
126     answerList->push_back(s);
127
128     strprintf(&s, "    <Free value=\"%.5f\"/>", it->tariffConf.free);
129     answerList->push_back(s);
130
131     switch (it->tariffConf.traffType)
132         {
133         case TRAFF_UP:
134             answerList->push_back("<TraffType value=\"up\"/>");
135             break;
136         case TRAFF_DOWN:
137             answerList->push_back("<TraffType value=\"down\"/>");
138             break;
139         case TRAFF_UP_DOWN:
140             answerList->push_back("<TraffType value=\"up+down\"/>");
141             break;
142         case TRAFF_MAX:
143             answerList->push_back("<TraffType value=\"max\"/>");
144             break;
145         }
146
147     answerList->push_back("</tariff>");
148     }
149 answerList->push_back("</Tariffs>");
150 }
151 //-----------------------------------------------------------------------------
152 //  ADD TARIFF
153 //-----------------------------------------------------------------------------
154 int PARSER_ADD_TARIFF::ParseStart(void *, const char * el, const char ** attr)
155 {
156 if (strcasecmp(el, "AddTariff") == 0)
157     {
158     if (attr[1])
159         {
160         tariffToAdd = attr[1];
161         }
162     return 0;
163     }
164 return -1;
165 }
166 //-----------------------------------------------------------------------------
167 int PARSER_ADD_TARIFF::ParseEnd(void *, const char * el)
168 {
169 if (strcasecmp(el, "AddTariff") == 0)
170     {
171     CreateAnswer();
172     return 0;
173     }
174 return -1;
175 }
176 //-----------------------------------------------------------------------------
177 void PARSER_ADD_TARIFF::CreateAnswer()
178 {
179 //answerList->clear();
180 answerList->erase(answerList->begin(), answerList->end());
181
182 if (tariffs->Add(tariffToAdd, currAdmin) == 0)
183     {
184     answerList->push_back("<AddTariff Result=\"Ok\"/>");
185     }
186 else
187     {
188     string s;
189     strprintf(&s, "<AddTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
190     answerList->push_back(s);
191     }
192 }
193 //-----------------------------------------------------------------------------
194 //  DEL TARIFF
195 //-----------------------------------------------------------------------------
196 int PARSER_DEL_TARIFF::ParseStart(void *, const char * el, const char ** attr)
197 {
198 strError = "";
199 if (strcasecmp(el, "DelTariff") == 0)
200     {
201     tariffToDel = attr[1];
202     return 0;
203     }
204 return -1;
205 }
206 //-----------------------------------------------------------------------------
207 int PARSER_DEL_TARIFF::ParseEnd(void *, const char * el)
208 {
209 if (strcasecmp(el, "DelTariff") == 0)
210     {
211     CreateAnswer();
212     return 0;
213     }
214 return -1;
215 }
216 //-----------------------------------------------------------------------------
217 void PARSER_DEL_TARIFF::CreateAnswer()
218 {
219 //answerList->clear();
220 answerList->erase(answerList->begin(), answerList->end());
221
222 if (users->TariffInUse(tariffToDel))
223     {
224     string s;
225     strprintf(&s, "<DelTariff Result=\"Error. Tariff \'%s\' cannot be deleted. Tariff in use.\"/>", tariffToDel.c_str());
226     answerList->push_back(s);
227     return;
228     }
229
230 if (tariffs->Del(tariffToDel, currAdmin) == 0)
231     {
232     answerList->push_back("<DelTariff Result=\"Ok\"/>");
233     }
234 else
235     {
236     string s;
237     strprintf(&s, "<DelTariff Result=\"Error. %s\"/>", tariffs->GetStrError().c_str());
238     answerList->push_back(s);
239     }
240 }
241 //-----------------------------------------------------------------------------
242 //-----------------------------------------------------------------------------
243 //  CHG TARIFF
244 //-----------------------------------------------------------------------------
245 //-----------------------------------------------------------------------------
246 int PARSER_CHG_TARIFF::ParseSlashedIntParams(int paramsNum, const string & s, int * params)
247 {
248 char * str = new char[s.size() + 1];
249 char * p;
250 strcpy(str, s.c_str());
251 p = strtok(str, "/");
252
253 for (int i = 0; i < paramsNum; i++)
254     {
255     if (p == NULL)
256         {
257         delete[] str;
258         return -1;
259         }
260
261     if (str2x(p, params[i]) != 0)
262         {
263         delete[] str;
264         return -1;
265         }
266
267     p = strtok(NULL, "/");
268     }
269
270 delete[] str;
271 return 0;
272 }
273 //-----------------------------------------------------------------------------
274 int PARSER_CHG_TARIFF::ParseSlashedDoubleParams(int paramsNum, const string & s, double * params)
275 {
276 char * str = new char[s.size() + 1];
277 char * p;
278 strcpy(str, s.c_str());
279 p = strtok(str, "/");
280
281 for (int i = 0; i < paramsNum; i++)
282     {
283     if (p == NULL)
284         {
285         delete[] str;
286         return -1;
287         }
288
289     if (strtodouble2(p, params[i]) != 0)
290         {
291         delete[] str;
292         return -1;
293         }
294
295     p = strtok(NULL, "/");
296     }
297
298 delete[] str;
299 return 0;
300 }
301 //-----------------------------------------------------------------------------
302 int PARSER_CHG_TARIFF::ParseStart(void *, const char * el, const char ** attr)
303 {
304 char st[50];
305 double price[DIR_NUM];
306 int t[DIR_NUM];
307 depth++;
308
309 if (depth == 1)
310     {
311     if (strcasecmp(el, "SetTariff") == 0)
312         {
313         td.tariffConf.name = attr[1];
314         return 0;
315         }
316     }
317 else
318     {
319     string s;
320
321     if (strcasecmp(el, "PriceDayA") == 0)
322         {
323         s = attr[1];
324         if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
325             for (int j = 0; j < DIR_NUM; j++)
326                 td.dirPrice[j].priceDayA = price[j] / pt_mega;
327         return 0;
328         }
329
330     if (strcasecmp(el, "PriceDayB") == 0)
331         {
332         s = attr[1];
333         if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
334             for (int j = 0; j < DIR_NUM; j++)
335                 td.dirPrice[j].priceDayB = price[j] / pt_mega;
336         return 0;
337         }
338
339
340     if (strcasecmp(el, "PriceNightA") == 0)
341         {
342         s = attr[1];
343         if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
344             for (int j = 0; j < DIR_NUM; j++)
345                 td.dirPrice[j].priceNightA = price[j] / pt_mega;
346         return 0;
347         }
348
349     if (strcasecmp(el, "PriceNightB") == 0)
350         {
351         s = attr[1];
352         if (ParseSlashedDoubleParams(DIR_NUM, s, price) == 0)
353             for (int j = 0; j < DIR_NUM; j++)
354                 td.dirPrice[j].priceNightB = price[j] / pt_mega;
355         return 0;
356         }
357
358     if (strcasecmp(el, "Threshold") == 0)
359         {
360         s = attr[1];
361         if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
362             for (int j = 0; j < DIR_NUM; j++)
363                 td.dirPrice[j].threshold = t[j];
364         return 0;
365         }
366
367     if (strcasecmp(el, "SinglePrice") == 0)
368         {
369         s = attr[1];
370         if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
371             for (int j = 0; j < DIR_NUM; j++)
372                 td.dirPrice[j].singlePrice = t[j];
373         return 0;
374         }
375
376     if (strcasecmp(el, "NoDiscount") == 0)
377         {
378         s = attr[1];
379         if (ParseSlashedIntParams(DIR_NUM, s, t) == 0)
380             for (int j = 0; j < DIR_NUM; j++)
381                 td.dirPrice[j].noDiscount = t[j];
382         return 0;
383         }
384
385     for (int j = 0; j < DIR_NUM; j++)
386         {
387         snprintf(st, 50, "Time%d", j);
388         if (strcasecmp(el, st) == 0)
389             {
390             int h1, m1, h2, m2;
391             if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
392                 {
393                 td.dirPrice[j].hDay = h1;
394                 td.dirPrice[j].mDay = m1;
395                 td.dirPrice[j].hNight = h2;
396                 td.dirPrice[j].mNight = m2;
397                 }
398             return 0;
399             }
400         }
401
402     if (strcasecmp(el, "Fee") == 0)
403         {
404         double fee;
405         if (strtodouble2(attr[1], fee) == 0)
406             td.tariffConf.fee = fee;
407         return 0;
408         }
409
410     if (strcasecmp(el, "PassiveCost") == 0)
411         {
412         double pc;
413         if (strtodouble2(attr[1], pc) == 0)
414             td.tariffConf.passiveCost = pc;
415         return 0;
416         }
417     if (strcasecmp(el, "Free") == 0)
418         {
419         double free;
420         if (strtodouble2(attr[1], free) == 0)
421             td.tariffConf.free = free;
422         return 0;
423         }
424
425     if (strcasecmp(el, "TraffType") == 0)
426         {
427         if (strcasecmp(attr[1], "up") == 0)
428             {
429             td.tariffConf.traffType = TRAFF_UP;
430             return 0;
431             }
432
433         if (strcasecmp(attr[1], "down") == 0)
434             {
435             td.tariffConf.traffType = TRAFF_DOWN;
436             return 0;
437             }
438         if (strcasecmp(attr[1], "up+down") == 0)
439             {
440             td.tariffConf.traffType = TRAFF_UP_DOWN;
441             return 0;
442             }
443         if (strcasecmp(attr[1], "max") == 0)
444             {
445             td.tariffConf.traffType = TRAFF_MAX;
446             return 0;
447             }
448         return 0;
449         }
450     }
451 return -1;
452 }
453 //-----------------------------------------------------------------------------
454 int PARSER_CHG_TARIFF::ParseEnd(void *, const char * el)
455 {
456 if (depth == 1)
457     {
458     if (strcasecmp(el, "SetTariff") == 0)
459         {
460         CreateAnswer();
461         depth--;
462         return 0;
463         }
464     }
465
466 depth--;
467 return -1;
468 }
469 //-----------------------------------------------------------------------------
470 void PARSER_CHG_TARIFF::CreateAnswer()
471 {
472 answerList->erase(answerList->begin(), answerList->end());
473
474 if (!td.tariffConf.name.data().empty())
475     {
476     TARIFF_DATA tariffData = td.GetData();
477     if (tariffs->Chg(tariffData, currAdmin) == 0)
478         {
479         answerList->push_back("<SetTariff Result=\"ok\"/>");
480         return;
481         }
482     else
483         {
484         string s;
485         strprintf(&s, "<SetTariff Result=\"Change tariff error! %s\"/>", tariffs->GetStrError().c_str());
486         answerList->push_back(s);
487         return;
488         }
489     }
490 answerList->push_back("<SetTariff Result=\"Change tariff error!\"/>");
491 }
492 //-----------------------------------------------------------------------------