-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbu_parser_service.h
107 lines (89 loc) · 2.57 KB
/
nbu_parser_service.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pragma once
class nbu_service_parser : public service
{
using service::service;
protected:
virtual DWORD WINAPI worker(LPVOID)
{
auto last_execution_time = chrono::steady_clock::now() - chrono::milliseconds(settings::update_rate);
while (WaitForSingleObject(stop_event, 0) != WAIT_OBJECT_0)
{
if (WaitForSingleObject(pause_event, 0) != WAIT_OBJECT_0)
{
auto current_time = chrono::steady_clock::now();
auto elapsed_since_last_execution = chrono::duration_cast<chrono::milliseconds>(current_time - last_execution_time);
if (elapsed_since_last_execution.count() >= settings::update_rate)
{
string data;
CURLcode code = network::get_website_data("https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?json", data);
if (code == CURLE_OK)
{
json json_data;
fs::path dir(settings::path);
if (!fs::exists(dir)) //ïðîâåðÿåì ñóùåñòâóåò ëè ïóòü, åñëè íåò òî ñîçäà¸ì
fs::create_directories(dir);
//åñëè åñòü ñòàðûå äàííûå â ôàéëå, òî çàãðóæàåì èõ â json
ifstream infile(dir / settings::rates_filename);
if (infile.is_open())
{
infile >> json_data;
infile.close();
}
for (const auto& item : json::parse(data))
{
for (const auto& currency : settings::currencies)
{
//ïðîâåðÿåì âàëþòû è çàïèñûâàåì èõ â json
if (item["cc"] == currency)
json_data.push_back({ {"currency", item["cc"]}, {"price", item["rate"]} });
}
}
//íîâûå äàííûå çàïèñûâàåì â ôàéë
ofstream outfile(dir / settings::rates_filename);
if (outfile.is_open())
{
outfile << json_data.dump(4);
outfile.close();
}
}
last_execution_time = current_time;
}
//îñòàíàâëèâàåì ïîòîê íà êàêîå-òî âðåìÿ (äëÿ óìåíüøåíèÿ íàãðóçêè íà ïê)
std::this_thread::sleep_for(chrono::milliseconds(100));
}
else
{
confirm_pause();
WaitForSingleObject(continue_event, INFINITE);
confirm_continue();
}
}
return ERROR_SUCCESS;
}
//âûçûâàåòñÿ âî âðåìÿ çàïóñêà
virtual void on_startup()
{
logger::write_log("on_startup");
settings::load();
}
//âûçûâàåòñÿ, êîãäà àêòèâèðóåòñÿ ïàóçà
virtual void on_pause()
{
logger::write_log("on_pause");
}
//âûçûâàåòñÿ, êîãäà àêòèâèðóåòñÿ ïðîäîëæåíèå, ïîñëå ïàóçû
virtual void on_continue()
{
logger::write_log("on_continue");
}
//âûçûâàåòñÿ, êîãäà windows ñîîáùàåò î íåîáõîäèìîñòè îñòàíîâêè
virtual void on_stop()
{
logger::write_log("on_stop");
}
//âûçûâàåòñÿ â êîíöå æèçíåííîãî öèêëà
virtual void on_exit()
{
logger::write_log("on_exit");
}
};