]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_tariffs.cpp
Finished new implementation of sgconfig plugin.
[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/common.h"
27 #include "stg/resetable.h"
28
29 #include <cstdio> // snprintf
30 #include <cstring>
31
32 using STG::PARSER::GET_TARIFFS;
33 using STG::PARSER::ADD_TARIFF;
34 using STG::PARSER::DEL_TARIFF;
35 using STG::PARSER::CHG_TARIFF;
36
37 const char * GET_TARIFFS::tag = "GetTariffs";
38 const char * ADD_TARIFF::tag  = "AddTariff";
39 const char * DEL_TARIFF::tag  = "DelTariff";
40 const char * CHG_TARIFF::tag  = "SetTariff";
41
42 namespace
43 {
44
45 const double pt_mega = 1024 * 1024;
46
47 template <typename A, typename C, typename F>
48 std::string AOS2String(const A & array, size_t size, const F C::* field, F multiplier)
49 {
50     std::string res;
51     for (size_t i = 0; i < size; ++i)
52     {
53         if (!res.empty())
54             res += "/";
55         res += x2str((array[i].*field) * multiplier);
56     }
57     return res;
58 }
59
60 template <typename A, typename C, typename F>
61 bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE<F> C::* field, F divisor)
62 {
63     size_t index = 0;
64     std::string::size_type from = 0;
65     std::string::size_type pos = 0;
66     while (index < size && (pos = source.find('/', from)) != std::string::npos)
67     {
68         if (str2x(source.substr(from, pos - from), (array[index].*field).data()))
69             return false;
70         (array[index].*field).data() /= divisor;
71         from = pos + 1;
72         ++index;
73     }
74     if (str2x(source.substr(from), (array[index].*field).data()))
75         return false;
76     (array[index].*field).data() /= divisor;
77     return true;
78 }
79
80 }
81
82 void GET_TARIFFS::CreateAnswer()
83 {
84     m_answer = "<Tariffs>";
85
86     std::list<TARIFF_DATA> dataList;
87     m_tariffs.GetTariffsData(&dataList);
88     std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
89     for (; it != dataList.end(); ++it)
90         {
91         m_answer += "<tariff name=\"" + it->tariffConf.name + "\">";
92
93         for (size_t i = 0; i < DIR_NUM; i++)
94             m_answer += "<Time" + x2str(i) + " value=\"" +
95                 x2str(it->dirPrice[i].hDay)   + ":" + x2str(it->dirPrice[i].mDay)   + "-" +
96                 x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>";
97
98         m_answer += "<PriceDayA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayA, pt_mega) + "\"/>" +
99                   "<PriceDayB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayB, pt_mega) + "\"/>" +
100                   "<PriceNightA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightA, pt_mega) + "\"/>" +
101                   "<PriceNightB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightB, pt_mega) + "\"/>" +
102                   "<Threshold value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::threshold, 1) + "\"/>" +
103                   "<SinglePrice value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::singlePrice, 1) + "\"/>" +
104                   "<NoDiscount value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::noDiscount, 1) + "\"/>" +
105                   "<Fee value=\"" + x2str(it->tariffConf.fee) + "\"/>" +
106                   "<PassiveCost value=\"" + x2str(it->tariffConf.passiveCost) + "\"/>" +
107                   "<Free value=\"" + x2str(it->tariffConf.free) + "\"/>" +
108                   "<TraffType value=\"" + TARIFF::TraffTypeToString(it->tariffConf.traffType) + "\"/>" +
109                   "<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>" +
110                   "</tariff>";
111         }
112
113     m_answer += "</Tariffs>";
114 }
115
116 int ADD_TARIFF::Start(void *, const char * el, const char ** attr)
117 {
118     if (strcasecmp(el, m_tag.c_str()) != 0)
119         return -1;
120
121     if (attr[1] == NULL)
122         return -1;
123
124     tariff = attr[1];
125     return 0;
126 }
127
128 void ADD_TARIFF::CreateAnswer()
129 {
130     if (m_tariffs.Add(tariff, &m_currAdmin) == 0)
131         m_answer = "<" + m_tag + " Result=\"Ok\"/>";
132     else
133         m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
134 }
135
136 int DEL_TARIFF::Start(void *, const char * el, const char ** attr)
137 {
138     if (strcasecmp(el, m_tag.c_str()) != 0)
139         return -1;
140
141     if (attr[1] == NULL)
142         return -1;
143
144     tariff = attr[1];
145     return 0;
146 }
147
148 void DEL_TARIFF::CreateAnswer()
149 {
150     if (m_users.TariffInUse(tariff))
151         m_answer = "<" + m_tag + " Result=\"Error. Tariff \'" + tariff + "\' cannot be deleted, it is in use.\"/>";
152     else if (m_tariffs.Del(tariff, &m_currAdmin) == 0)
153         m_answer = "<" + m_tag + " Result=\"Ok\"/>";
154     else
155         m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
156 }
157
158 int CHG_TARIFF::Start(void *, const char * el, const char ** attr)
159 {
160     m_depth++;
161
162     if (m_depth == 1)
163     {
164         if (strcasecmp(el, m_tag.c_str()) == 0)
165         {
166             td.tariffConf.name = attr[1];
167             return 0;
168         }
169     }
170     else
171     {
172         if (strcasecmp(el, "PriceDayA") == 0)
173         {
174             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayA, pt_mega))
175                 return -1; // TODO: log it
176             else
177                 return 0;
178         }
179
180         if (strcasecmp(el, "PriceDayB") == 0)
181         {
182             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayB, pt_mega))
183                 return -1; // TODO: log it
184             else
185                 return 0;
186         }
187
188         if (strcasecmp(el, "PriceNightA") == 0)
189         {
190             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightA, pt_mega))
191                 return -1; // TODO: log it
192             else
193                 return 0;
194         }
195
196         if (strcasecmp(el, "PriceNightB") == 0)
197         {
198             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightB, pt_mega))
199                 return -1; // TODO: log it
200             else
201                 return 0;
202         }
203
204         if (strcasecmp(el, "Threshold") == 0)
205         {
206             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::threshold, 1))
207                 return -1; // TODO: log it
208             else
209                 return 0;
210         }
211
212         if (strcasecmp(el, "SinglePrice") == 0)
213         {
214             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::singlePrice, 1))
215                 return -1; // TODO: log it
216             else
217                 return 0;
218         }
219
220         if (strcasecmp(el, "NoDiscount") == 0)
221         {
222             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::noDiscount, 1))
223                 return -1; // TODO: log it
224             else
225                 return 0;
226         }
227
228         for (int j = 0; j < DIR_NUM; j++)
229         {
230             char st[50];
231             snprintf(st, 50, "Time%d", j);
232             if (strcasecmp(el, st) == 0)
233             {
234                 int h1 = 0;
235                 int m1 = 0;
236                 int h2 = 0;
237                 int m2 = 0;
238                 if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
239                     {
240                     td.dirPrice[j].hDay = h1;
241                     td.dirPrice[j].mDay = m1;
242                     td.dirPrice[j].hNight = h2;
243                     td.dirPrice[j].mNight = m2;
244                     }
245                 return 0;
246             }
247         }
248
249         if (strcasecmp(el, "Fee") == 0)
250         {
251             double fee;
252             if (strtodouble2(attr[1], fee) == 0)
253                 td.tariffConf.fee = fee;
254             return 0;
255         }
256
257         if (strcasecmp(el, "PassiveCost") == 0)
258         {
259             double pc;
260             if (strtodouble2(attr[1], pc) == 0)
261                 td.tariffConf.passiveCost = pc;
262             return 0;
263         }
264
265         if (strcasecmp(el, "Free") == 0)
266         {
267             double free;
268             if (strtodouble2(attr[1], free) == 0)
269                 td.tariffConf.free = free;
270             return 0;
271         }
272
273         if (strcasecmp(el, "TraffType") == 0)
274         {
275             td.tariffConf.traffType = TARIFF::StringToTraffType(attr[1]);
276             return 0;
277         }
278
279         if (strcasecmp(el, "Period") == 0)
280         {
281             td.tariffConf.period = TARIFF::StringToPeriod(attr[1]);
282             return 0;
283         }
284     }
285     return -1;
286 }
287
288 void CHG_TARIFF::CreateAnswer()
289 {
290     if (!td.tariffConf.name.data().empty())
291     {
292         TARIFF_DATA tariffData = td.GetData();
293         if (m_tariffs.Chg(tariffData, &m_currAdmin) == 0)
294             m_answer = "<" + m_tag + " Result=\"ok\"/>";
295         else
296             m_answer = "<" + m_tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>";
297     }
298     else
299         m_answer = "<" + m_tag + " Result=\"Change tariff error!\"/>";
300 }