-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWeatherFXLite.cpp
More file actions
267 lines (214 loc) · 7.59 KB
/
Copy pathWeatherFXLite.cpp
File metadata and controls
267 lines (214 loc) · 7.59 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
weatherfxlite
A lite replacement for the venerable Bushnell WeatherFX station. RIP.
Copyright (C) 2024 iAchieved.it LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "WeatherFXLite.h"
#include "config.h"
#include <QThread>
#include "ZmqListener.h"
#include "icons/gps_icon.h"
#define CURRENT_CONDITION_TICKS 60
#define CURRENT_FORECAST_TICKS 60
WeatherFXLite::WeatherFXLite() {
// Listen for optional GPS coordinates to be published on ZeroMQ
QString zmqAddress("tcp://localhost:11205");
ZmqListener* listener = new ZmqListener(zmqAddress);
QThread* thread = new QThread();
listener->moveToThread(thread);
connect(thread,
SIGNAL(started()),
listener,
SLOT(startListening()));
connect(listener,
SIGNAL(gpsMessageReceived(QString)),
this,
SLOT(gpsCoordinatesReceived(QString)));
// Set up the UI
window = new QWidget();
ui.setupUi(window);
// Pin the condition label to its 60pt height so font auto-shrinking doesn't reflow the layout
{
QFont f = ui.currentCondition->font();
f.setPointSize(60);
ui.currentCondition->setFixedHeight(QFontMetrics(f).height());
}
// Pin the temperature label to the widest 2-digit width so 3-digit values trigger auto-shrinking
{
QFont f = ui.currentTemperature->font();
f.setPointSize(114);
ui.currentTemperature->setFixedWidth(QFontMetrics(f).horizontalAdvance("99°"));
}
// Pin the wind speed label to the widest 2-digit width so 3-digit values trigger auto-shrinking
{
QFont f = ui.windSpeed->font();
f.setPointSize(36);
ui.windSpeed->setFixedWidth(QFontMetrics(f).horizontalAdvance("99"));
}
window->setStyleSheet("background-color:black;");
#ifdef METRIC
ui.windUnits->setText("kph");
#endif
#ifdef Q_OS_LINUX
window->setWindowFlags(Qt::FramelessWindowHint);
window->setWindowState(Qt::WindowFullScreen);
QRect screenRect = QApplication::desktop()->screenGeometry(1);
window->move(QPoint(screenRect.x(), screenRect.y()));
#endif
ui.versionLabel->setText("v1.0.2");
window->show();
// Use the Apple WeatherKit API
weatherAPI = new WeatherKitAPI();
timer = new QTimer(this);
connect(weatherAPI, SIGNAL(currentConditionsUpdate()), this, SLOT(updateWeatherDisplay()));
connect(weatherAPI, SIGNAL(currentForecastUpdate()), this, SLOT(updateForecastDisplay()));
connect(timer, SIGNAL(timeout()), this, SLOT(timerTick()));
// Set up GPS scene
gpsScene = new QGraphicsScene();
QPixmap icon;
if (icon.loadFromData(gps_png, gps_png_len, "png")) {
gpsItem = new QGraphicsPixmapItem(icon);
gpsScene->addItem(gpsItem);
}
ui.gpsAvailable->hide();
ui.gpsAvailable->setScene(gpsScene);
timer->start(1000); // 1 second
// Start the thread to listen for GPS coordinates
thread->start();
}
void WeatherFXLite::gpsCoordinatesReceived(const QString& message) {
qDebug() << "Received GPS coordinates: " << message;
lastGPSUpdate = QDateTime().currentDateTime().toSecsSinceEpoch();
qDebug() << "Last GPS update: " << lastGPSUpdate;
weatherAPI->setCurrentLocation(message);
ui.gpsAvailable->show();
}
void WeatherFXLite::updateWeatherDisplay(void) {
CurrentConditions current = weatherAPI->getCurrentConditions();
ui.currentCondition->setText(current.condition);
{
QFont f = ui.currentCondition->font();
f.setPointSize(60);
QFontMetrics fm(f);
while (fm.horizontalAdvance(current.condition) > ui.currentCondition->width() && f.pointSize() > 20) {
f.setPointSize(f.pointSize() - 1);
fm = QFontMetrics(f);
}
ui.currentCondition->setFont(f);
}
{
QString tempText = QString::number(current.temperature) + QString("°");
ui.currentTemperature->setText(tempText);
QFont f = ui.currentTemperature->font();
f.setPointSize(114);
QFontMetrics fm(f);
while (fm.horizontalAdvance(tempText) > ui.currentTemperature->width() && f.pointSize() > 60) {
f.setPointSize(f.pointSize() - 1);
fm = QFontMetrics(f);
}
ui.currentTemperature->setFont(f);
}
{
QString speedText = QString::number(current.windSpeed);
ui.windSpeed->setText(speedText);
QFont f = ui.windSpeed->font();
f.setPointSize(36);
QFontMetrics fm(f);
while (fm.horizontalAdvance(speedText) > ui.windSpeed->width() && f.pointSize() > 18) {
f.setPointSize(f.pointSize() - 1);
fm = QFontMetrics(f);
}
ui.windSpeed->setFont(f);
}
ui.windArrow->setDirection(current.windDirection);
std::string background = "background-color:" + backgroundForTemperature(current.temperature) + ";";
window->setStyleSheet(background.c_str());
if (scene == nullptr) {
scene = new QGraphicsScene();
}
if (item) {
delete item;
}
QPixmap icon;
if (icon.loadFromData(current.icon.data, current.icon.len, "png")) {
item = new QGraphicsPixmapItem(icon);
scene->addItem(item);
ui.graphicsView->setScene(scene);
} else {
qDebug() << "Unable to load icon data";
}
qint64 now = QDateTime().currentDateTime().toSecsSinceEpoch();
if (now - lastGPSUpdate > 60) {
qDebug() << "GPS data stale";
ui.gpsAvailable->hide();
}
}
void WeatherFXLite::updateForecastDisplay(void) {
CurrentConditions current = weatherAPI->getCurrentConditions();
ui.hiTemp->setText(QString::number(current.high) + QString("°"));
ui.loTemp->setText(QString::number(current.low) + QString("°"));
ui.precipChance->setText(QString::number(current.precipitationChance) + "%");
if (current.precipWeatherIcon.data) {
QPixmap px;
px.loadFromData(current.precipWeatherIcon.data, current.precipWeatherIcon.len, "png");
ui.precipIcon->setPixmap(px.scaled(48, 48, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
}
void WeatherFXLite::timerTick(void) {
currentConditionTicks++; currentForecastTicks++;
QDateTime now = QDateTime().currentDateTime();
#ifdef TWENTYFOUR_HOUR_FORMAT
QString timeNow = now.toString("HH:mm");
#else
QString timeNow = now.toString("h:mm A");
#endif
QString dateNow = now.toString("MMM dd");
ui.currentTime->setText(timeNow);
ui.currentDate->setText(dateNow);
if (currentForecastTicks % CURRENT_FORECAST_TICKS == 0 || boot) {
weatherAPI->updateCurrentForecast();
currentForecastTicks = 0;
}
if (currentConditionTicks % CURRENT_CONDITION_TICKS == 0 || boot) {
weatherAPI->updateCurrentConditions();
currentConditionTicks = 0;
boot = false;
}
}
const std::string backgrounds[] = {
"#ABA5C2", // 0s
"#ABA5C2", // 10s
"#ABA5C2", // 20s
"#0089C6", // 30s
"#262A62", // 40s
"#35713D", // 50s
"#74AB46", // 60s
"#F3D13C", // 70s
"#D37733", // 80s
"#BB352B" // 90+
};
/// @brief Look up background color for temperature
/// @param temp
/// @return
std::string WeatherFXLite::backgroundForTemperature(short temp) {
#ifdef METRIC
// Convert to Fahrenheit before indexing
temp = floor((temp * 9.0) / 5.0 + 32);
#endif
qDebug() << "Temperature: " << temp;
int tIndex = temp/10;
if (tIndex <= 0) tIndex = 0;
if (tIndex >= 9) tIndex = 9;
return backgrounds[tIndex];
}