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