]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp
Merge remote-tracking branch 'origin/stg-2.409' into ticket37
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_tariffs.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include "parser_tariffs.h"
23
24 #include "stg/tariffs.h"
25 #include "stg/users.h"
26 #include "stg/resetable.h"
27
28 #include <cstdio> // snprintf
29 #include <cstring>
30
31 using STG::PARSER::GET_TARIFFS;
32 using STG::PARSER::ADD_TARIFF;
33 using STG::PARSER::DEL_TARIFF;
34 using STG::PARSER::CHG_TARIFF;
35
36 const char * GET_TARIFFS::tag = "GetTariffs";
37 const char * ADD_TARIFF::tag  = "AddTariff";
38 const char * DEL_TARIFF::tag  = "DelTariff";
39 const char * CHG_TARIFF::tag  = "SetTariff";
40
41 namespace
42 {
43
44 const double pt_mega = 1024 * 1024;
45
46 template <typename A, typename C, typename F>
47 std::string AOS2String(const A & array, size_t size, const F C::* field, F multiplier)
48 {
49     std::string res;
50     for (size_t i = 0; i < size; ++i)
51     {
52         if (!res.empty())
53             res += "/";
54         res += x2str((array[i].*field) * multiplier);
55     }
56     return res;
57 }
58
59 template <typename T>
60 bool str2res(const std::string& source, RESETABLE<T>& dest, T divisor)
61 {
62     T value = 0;
63     if (str2x(source, value))
64         return false;
65     dest = value / divisor;
66     return true;
67 }
68
69 template <typename A, typename C, typename F>
70 bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE<F> C::* field, F divisor)
71 {
72     size_t index = 0;
73     std::string::size_type from = 0;
74     std::string::size_type pos = 0;
75     while (index < size && (pos = source.find('/', from)) != std::string::npos)
76     {
77         if (!str2res(source.substr(from, pos - from), array[index].*field, divisor))
78             return false;
79         from = pos + 1;
80         ++index;
81     }
82     if (str2res(source.substr(from), array[index].*field, divisor))
83         return false;
84     return true;
85 }
86
87 }
88
89 void GET_TARIFFS::CreateAnswer()
90 {
91     m_answer = "<Tariffs>";
92
93     std::list<TARIFF_DATA> dataList;
94     m_tariffs.GetTariffsData(&dataList);
95     std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
96     for (; it != dataList.end(); ++it)
97         {
98         m_answer += "<tariff name=\"" + it->tariffConf.name + "\">";
99
100         for (size_t i = 0; i < DIR_NUM; i++)
101             m_answer += "<Time" + x2str(i) + " value=\"" +
102                 x2str(it->dirPrice[i].hDay)   + ":" + x2str(it->dirPrice[i].mDay)   + "-" +
103                 x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>";
104
105         m_answer += "<PriceDayA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayA, pt_mega) + "\"/>" +
106                   "<PriceDayB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayB, pt_mega) + "\"/>" +
107                   "<PriceNightA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightA, pt_mega) + "\"/>" +
108                   "<PriceNightB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightB, pt_mega) + "\"/>" +
109                   "<Threshold value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::threshold, 1) + "\"/>" +
110                   "<SinglePrice value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::singlePrice, 1) + "\"/>" +
111                   "<NoDiscount value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::noDiscount, 1) + "\"/>" +
112                   "<Fee value=\"" + x2str(it->tariffConf.fee) + "\"/>" +
113                   "<PassiveCost value=\"" + x2str(it->tariffConf.passiveCost) + "\"/>" +
114                   "<Free value=\"" + x2str(it->tariffConf.free) + "\"/>" +
115                   "<TraffType value=\"" + TARIFF::TraffTypeToString(it->tariffConf.traffType) + "\"/>" +
116                   "<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>" +
117                   "<ChangePolicy value=\"" + TARIFF::ChangePolicyToString(it->tariffConf.changePolicy) + "\"/>" +
118                   "</tariff>";
119         }
120
121     m_answer += "</Tariffs>";
122 }
123
124 int ADD_TARIFF::Start(void *, const char * el, const char ** attr)
125 {
126     if (strcasecmp(el, m_tag.c_str()) != 0)
127         return -1;
128
129     if (attr[1] == NULL)
130         return -1;
131
132     tariff = attr[1];
133     return 0;
134 }
135
136 void ADD_TARIFF::CreateAnswer()
137 {
138     if (m_tariffs.Add(tariff, &m_currAdmin) == 0)
139         m_answer = "<" + m_tag + " Result=\"Ok\"/>";
140     else
141         m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
142 }
143
144 int DEL_TARIFF::Start(void *, const char * el, const char ** attr)
145 {
146     if (strcasecmp(el, m_tag.c_str()) != 0)
147         return -1;
148
149     if (attr[1] == NULL)
150         return -1;
151
152     tariff = attr[1];
153     return 0;
154 }
155
156 void DEL_TARIFF::CreateAnswer()
157 {
158     if (m_users.TariffInUse(tariff))
159         m_answer = "<" + m_tag + " Result=\"Error. Tariff \'" + tariff + "\' cannot be deleted, it is in use.\"/>";
160     else if (m_tariffs.Del(tariff, &m_currAdmin) == 0)
161         m_answer = "<" + m_tag + " Result=\"Ok\"/>";
162     else
163         m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
164 }
165
166 int CHG_TARIFF::Start(void *, const char * el, const char ** attr)
167 {
168     m_depth++;
169
170     if (m_depth == 1)
171     {
172         if (strcasecmp(el, m_tag.c_str()) == 0)
173         {
174             const TARIFF * tariff = m_tariffs.FindByName(attr[1]);
175             if (tariff != NULL)
176                 td = tariff->GetTariffData();
177             else
178                 return -1;
179             return 0;
180         }
181     }
182     else
183     {
184         if (strcasecmp(el, "PriceDayA") == 0)
185         {
186             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayA, pt_mega))
187                 return -1; // TODO: log it
188             else
189                 return 0;
190         }
191
192         if (strcasecmp(el, "PriceDayB") == 0)
193         {
194             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayB, pt_mega))
195                 return -1; // TODO: log it
196             else
197                 return 0;
198         }
199
200         if (strcasecmp(el, "PriceNightA") == 0)
201         {
202             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightA, pt_mega))
203                 return -1; // TODO: log it
204             else
205                 return 0;
206         }
207
208         if (strcasecmp(el, "PriceNightB") == 0)
209         {
210             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightB, pt_mega))
211                 return -1; // TODO: log it
212             else
213                 return 0;
214         }
215
216         if (strcasecmp(el, "Threshold") == 0)
217         {
218             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::threshold, 1))
219                 return -1; // TODO: log it
220             else
221                 return 0;
222         }
223
224         if (strcasecmp(el, "SinglePrice") == 0)
225         {
226             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::singlePrice, 1))
227                 return -1; // TODO: log it
228             else
229                 return 0;
230         }
231
232         if (strcasecmp(el, "NoDiscount") == 0)
233         {
234             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::noDiscount, 1))
235                 return -1; // TODO: log it
236             else
237                 return 0;
238         }
239
240         for (int j = 0; j < DIR_NUM; j++)
241         {
242             char st[50];
243             snprintf(st, 50, "Time%d", j);
244             if (strcasecmp(el, st) == 0)
245             {
246                 int h1 = 0;
247                 int m1 = 0;
248                 int h2 = 0;
249                 int m2 = 0;
250                 if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
251                     {
252                     td.dirPrice[j].hDay = h1;
253                     td.dirPrice[j].mDay = m1;
254                     td.dirPrice[j].hNight = h2;
255                     td.dirPrice[j].mNight = m2;
256                     }
257                 return 0;
258             }
259         }
260
261         if (strcasecmp(el, "Fee") == 0)
262         {
263             double fee;
264             if (strtodouble2(attr[1], fee) == 0)
265                 td.tariffConf.fee = fee;
266             return 0;
267         }
268
269         if (strcasecmp(el, "PassiveCost") == 0)
270         {
271             double pc;
272             if (strtodouble2(attr[1], pc) == 0)
273                 td.tariffConf.passiveCost = pc;
274             return 0;
275         }
276
277         if (strcasecmp(el, "Free") == 0)
278         {
279             double free;
280             if (strtodouble2(attr[1], free) == 0)
281                 td.tariffConf.free = free;
282             return 0;
283         }
284
285         if (strcasecmp(el, "TraffType") == 0)
286         {
287             td.tariffConf.traffType = TARIFF::StringToTraffType(attr[1]);
288             return 0;
289         }
290
291         if (strcasecmp(el, "Period") == 0)
292         {
293             td.tariffConf.period = TARIFF::StringToPeriod(attr[1]);
294             return 0;
295         }
296
297         if (strcasecmp(el, "ChangePolicy") == 0)
298         {
299             td.tariffConf.changePolicy = TARIFF::StringToChangePolicy(attr[1]);
300             return 0;
301         }
302     }
303     return -1;
304 }
305
306 void CHG_TARIFF::CreateAnswer()
307 {
308     if (!td.tariffConf.name.data().empty())
309     {
310         TARIFF_DATA tariffData = td.GetData();
311         if (m_tariffs.Chg(tariffData, &m_currAdmin) == 0)
312             m_answer = "<" + m_tag + " Result=\"ok\"/>";
313         else
314             m_answer = "<" + m_tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>";
315     }
316     else
317         m_answer = "<" + m_tag + " Result=\"Change tariff error!\"/>";
318 }