-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwWxApp.cpp
More file actions
155 lines (126 loc) · 3.84 KB
/
wWxApp.cpp
File metadata and controls
155 lines (126 loc) · 3.84 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <map>
#include "wWxApp.h"
#include "wWxFrame.h"
#include "weatherProvider.h"
#include "weather.h"
#include "dot.h"
#include "sqlite3.h"
wxIMPLEMENT_APP(wWxApp);
WwxFrame* framePtr = NULL;
using Record = std::vector<std::string>;
using Records = std::vector<Record>;
wxBEGIN_EVENT_TABLE(WwxButton, wxButton)
EVT_BUTTON(wxID_ANY, WwxFrame::OnButton)
wxEND_EVENT_TABLE()
bool wWxApp::OnInit()
{
if (!wxApp::OnInit())
return false;
// Create the main frame window
WwxFrame* frame =new WwxFrame("weather now", 50, 50);
framePtr = frame;
wxMenu* file_menu = new wxMenu;
file_menu->Append(TEXT_QUIT, "E&xit\tAlt-X", "Quit");
wxMenuBar* menu_bar = new wxMenuBar(wxMB_DOCKABLE);
menu_bar->Append(file_menu, "&File");
frame->SetMenuBar(menu_bar);
frame->Show(true);
tempFetcherThread_ = std::thread{ &wWxApp::InitializeWeatherFetcher, this };
tempUpdaterThread_ = std::thread{ &wWxApp::UpdateTempFromWeather, this };
// report success
return true;
}
int select_callback(void* p_data, int num_fields, char** p_fields, char** p_col_names)
{
Records* records = static_cast<Records*>(p_data);
try {
records->emplace_back(p_fields, p_fields + num_fields);
}
catch (...) {
// abort select on failure, don't let exception propogate thru sqlite3 call-stack
return 1;
}
return 0;
}
Records select_stmt(const std::string& stmt, sqlite3* db)
{
Records records;
char* errmsg;
int ret = sqlite3_exec(db, stmt.c_str(), select_callback, &records, &errmsg);
if (ret != SQLITE_OK) {
std::cerr << "Error in select statement " << stmt << "[" << errmsg << "]\n";
}
else {
std::cerr << records.size() << " records returned.\n";
}
return records;
}
void wWxApp::InitializeWeatherFetcher()
{
std::this_thread::sleep_for(std::chrono::seconds(2));
auto& weather = Weather::getInstance();
WeatherProvider provider;
sqlite3* db = NULL;
int exit = -1;
exit = sqlite3_open("weather.db", &db);
// Test if there was an error
if (exit) {
std::cout << "DB Open Error: " << sqlite3_errmsg(db) << std::endl;
}
else {
std::cout << "Opened Database Successfully!" << std::endl;
}
std::string query = "SELECT * FROM provider;";
Records records = select_stmt(query, db);
if (records.size() > 0)
{
const auto& providerRow = records[0];
provider.name_ = providerRow[1];
provider.apiKey_ = providerRow[3];
provider.url_ = providerRow[2];
}
records.clear();
query = "SELECT * FROM setting WHERE name = 'interval';";
records = select_stmt(query, db);
if (records.size() > 0)
{
uint32_t interval = std::stoul(records[0][2]);
weather.SetConfig(interval, provider);
}
//Get Interval from database and pass it on.
sqlite3_close(db);
}
void wWxApp::UpdateTempFromWeather()
{
auto& weather = Weather::getInstance();
while (!stop_)
{
//std::cout << "Fetching data: " << weather.GetWeather().temp_ << std::endl;
if (weather.CheckIfFetcherIsRunning())
{
if (panelPtr != NULL)
{
std::string msg = std::to_string(weather.GetWeather().temp_);
msg += Dot::GetInstance().GetDot();
panelPtr->data_->SetValue(msg.c_str());
}
}
else
{
if (panelPtr != NULL)
{
std::string msg{ "Waiting " };
msg += Dot::GetInstance().GetDot();
panelPtr->data_->SetValue(msg.c_str());
}
}
std::this_thread::sleep_for(std::chrono::seconds(2));
}
}
wWxApp::~wWxApp()
{
stop_ = true;
std::this_thread::sleep_for(std::chrono::seconds(2));
tempFetcherThread_.join();
tempUpdaterThread_.join();
}