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