]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/tariff_helper.cpp
Minor changes.
[stg.git] / projects / stargazer / plugins / configuration / rpcconfig / tariff_helper.cpp
1 #include <ostream> // xmlrpc-c devs have missed something :)
2
3 #include "tariff_helper.h"
4 #include "stg/common.h"
5
6 void TARIFF_HELPER::GetTariffInfo(xmlrpc_c::value * info) const
7 {
8 std::map<std::string, xmlrpc_c::value> structVal;
9
10 structVal["result"] = xmlrpc_c::value_boolean(true);
11 structVal["name"] = xmlrpc_c::value_string(data.tariffConf.name);
12 structVal["fee"] = xmlrpc_c::value_double(data.tariffConf.fee);
13 structVal["freemb"] = xmlrpc_c::value_double(data.tariffConf.free);
14 structVal["passivecost"] = xmlrpc_c::value_double(data.tariffConf.passiveCost);
15 structVal["traffType"] = xmlrpc_c::value_int(data.tariffConf.traffType);
16 structVal["period"] = xmlrpc_c::value_string(TARIFF::PeriodToString(data.tariffConf.period));
17 structVal["changePolicy"] = xmlrpc_c::value_string(TARIFF::ChangePolicyToString(data.tariffConf.changePolicy));
18 structVal["changePolicyTimeout"] = xmlrpc_c::value_string(formatTime(data.tariffConf.changePolicyTimeout));
19
20 std::vector<xmlrpc_c::value> prices(DIR_NUM);
21
22 for (unsigned i = 0; i < DIR_NUM; ++i)
23     {
24     std::map<std::string, xmlrpc_c::value> dirPrice;
25     dirPrice["hday"] = xmlrpc_c::value_int(data.dirPrice[i].hDay);
26     dirPrice["mday"] = xmlrpc_c::value_int(data.dirPrice[i].mDay);
27     dirPrice["hnight"] = xmlrpc_c::value_int(data.dirPrice[i].hNight);
28     dirPrice["mnight"] = xmlrpc_c::value_int(data.dirPrice[i].mNight);
29     dirPrice["pricedaya"] = xmlrpc_c::value_double(data.dirPrice[i].priceDayA * 1024 * 1024);
30     dirPrice["pricedayb"] = xmlrpc_c::value_double(data.dirPrice[i].priceDayB * 1024 * 1024);
31     dirPrice["pricenighta"] = xmlrpc_c::value_double(data.dirPrice[i].priceNightA * 1024 * 1024);
32     dirPrice["pricenightb"] = xmlrpc_c::value_double(data.dirPrice[i].priceNightB * 1024 * 1024);
33     dirPrice["threshold"] = xmlrpc_c::value_int(data.dirPrice[i].threshold);
34     dirPrice["singleprice"] = xmlrpc_c::value_boolean(data.dirPrice[i].singlePrice);
35     dirPrice["nodiscount"] = xmlrpc_c::value_boolean(data.dirPrice[i].noDiscount);
36     prices[i] = xmlrpc_c::value_struct(dirPrice);
37     }
38
39 structVal["dirprices"] = xmlrpc_c::value_array(prices);
40
41 *info = xmlrpc_c::value_struct(structVal);
42 }
43
44 bool TARIFF_HELPER::SetTariffInfo(const xmlrpc_c::value & info)
45 {
46 std::map<std::string, xmlrpc_c::value> structVal(
47     static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(info))
48     );
49
50 std::map<std::string, xmlrpc_c::value>::iterator it;
51
52 if ((it = structVal.find("fee")) != structVal.end())
53     {
54     data.tariffConf.fee = xmlrpc_c::value_double(it->second);
55     }
56
57 if ((it = structVal.find("freemb")) != structVal.end())
58     {
59     data.tariffConf.free = xmlrpc_c::value_double(it->second);
60     }
61
62 if ((it = structVal.find("passivecost")) != structVal.end())
63     {
64     data.tariffConf.passiveCost = xmlrpc_c::value_double(it->second);
65     }
66
67 if ((it = structVal.find("traffType")) != structVal.end())
68     {
69     data.tariffConf.traffType = static_cast<TARIFF::TRAFF_TYPE>(xmlrpc_c::value_int(it->second).cvalue());
70     }
71
72 if ((it = structVal.find("period")) != structVal.end())
73     {
74     data.tariffConf.period = TARIFF::StringToPeriod(xmlrpc_c::value_string(it->second));
75     }
76
77 if ((it = structVal.find("changePolicy")) != structVal.end())
78     {
79     data.tariffConf.changePolicy = TARIFF::StringToChangePolicy(xmlrpc_c::value_string(it->second));
80     }
81
82 if ((it = structVal.find("changePolicyTimeout")) != structVal.end())
83     {
84     data.tariffConf.changePolicyTimeout = readTime(xmlrpc_c::value_string(it->second));
85     }
86
87 if ((it = structVal.find("dirprices")) != structVal.end())
88     {
89     std::vector<xmlrpc_c::value> prices(
90             xmlrpc_c::value_array(it->second).vectorValueValue()
91             );
92
93     for (unsigned i = 0; i < DIR_NUM; ++i)
94         {
95         std::map<std::string, xmlrpc_c::value> dirPrice(
96             static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(prices[i]))
97             );
98         data.dirPrice[i].mDay = xmlrpc_c::value_int(dirPrice["mday"]);
99         data.dirPrice[i].hDay = xmlrpc_c::value_int(dirPrice["hday"]);
100         data.dirPrice[i].mNight = xmlrpc_c::value_int(dirPrice["mnight"]);
101         data.dirPrice[i].hNight = xmlrpc_c::value_int(dirPrice["hnight"]);
102         data.dirPrice[i].priceDayA = xmlrpc_c::value_double(dirPrice["pricedaya"]) / 1024 / 1024;
103         data.dirPrice[i].priceDayB = xmlrpc_c::value_double(dirPrice["pricedayb"]) / 1024 / 1024;
104         data.dirPrice[i].priceNightA = xmlrpc_c::value_double(dirPrice["pricenighta"]) / 1024 / 1024;
105         data.dirPrice[i].priceNightB = xmlrpc_c::value_double(dirPrice["pricenightb"]) / 1024 / 1024;
106         data.dirPrice[i].threshold = xmlrpc_c::value_int(dirPrice["threshold"]);
107         data.dirPrice[i].singlePrice = xmlrpc_c::value_boolean(dirPrice["singleprice"]);
108         data.dirPrice[i].noDiscount = xmlrpc_c::value_boolean(dirPrice["nodiscount"]);
109         }
110     }
111
112 return false;
113 }