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