-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherProfile.cpp
More file actions
185 lines (173 loc) · 9.42 KB
/
Copy pathWeatherProfile.cpp
File metadata and controls
185 lines (173 loc) · 9.42 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
#include "WeatherProfile.h"
#include <QJsonDocument>
#include <QFile>
#include <QDir>
#include <QDebug>
WeatherProfileDB::WeatherProfileDB()
{
initProfiles();
qDebug() << "[WeatherProfileDB] initialized, day profiles:"
<< m_dayProfiles.size() << "night overrides:" << m_nightOverrides.size();
}
// ===== fromCode =====
WeatherProfile WeatherProfileDB::fromCode(int iconCode, bool isDay) const
{
if (!isDay && m_nightOverrides.contains(iconCode))
return m_nightOverrides[iconCode];
if (m_dayProfiles.contains(iconCode))
return m_dayProfiles[iconCode];
WeatherProfile sunny;
sunny.cloudActive = false;
return sunny;
}
void WeatherProfileDB::dumpRegisteredCodes() const
{
qDebug() << "[WeatherProfileDB] === Day Profiles ===";
for (auto it = m_dayProfiles.cbegin(); it != m_dayProfiles.cend(); ++it) {
const auto &p = it.value();
qDebug() << " code:" << it.key()
<< "cloud:" << p.cloudCoverage
<< "rain:" << (p.weatherParticle == "rain" ? p.intensity : 0)
<< "snow:" << (p.weatherParticle == "snow" ? p.intensity : 0)
<< "lightning:" << p.lightningActive;
}
qDebug() << "[WeatherProfileDB] === Night Overrides ===";
for (auto it = m_nightOverrides.cbegin(); it != m_nightOverrides.cend(); ++it) {
const auto &p = it.value();
qDebug() << " code:" << it.key()
<< "cloud:" << p.cloudCoverage
<< "rain:" << (p.weatherParticle == "rain" ? p.intensity : 0)
<< "snow:" << (p.weatherParticle == "snow" ? p.intensity : 0)
<< "lightning:" << p.lightningActive;
}
}
// ===== initProfiles: 和风天气 68 码默认映射 =====
void WeatherProfileDB::initProfiles()
{
// { particle, intensity, weatherVariant, cloudActive, cloudCoverage, cloudVariant, fogActive, fogIntensity, fogVariant, lightningActive }
// 白天 晴天类 100–104
m_dayProfiles[100] = { "", 0.0f, 0, false, 0.00f, 0, false, 0.0f, 0, false };
m_dayProfiles[101] = { "", 0.0f, 0, true, 0.30f, 1, false, 0.0f, 0, false };
m_dayProfiles[102] = { "", 0.0f, 0, true, 0.15f, 0, false, 0.0f, 0, false };
m_dayProfiles[103] = { "", 0.0f, 0, true, 0.20f, 0, false, 0.0f, 0, false };
m_dayProfiles[104] = { "", 0.0f, 0, true, 0.90f, 2, false, 0.0f, 0, false };
// 白天 雨类 300–399
m_dayProfiles[300] = { "rain", 0.4f, 0, true, 0.60f, 1, false, 0.0f, 0, false };
m_dayProfiles[301] = { "rain", 0.7f, 0, true, 0.80f, 2, false, 0.0f, 0, false };
m_dayProfiles[302] = { "rain", 0.8f, 1, true, 0.90f, 2, false, 0.0f, 0, true };
m_dayProfiles[303] = { "rain", 1.0f, 3, true, 1.00f, 2, false, 0.0f, 0, true };
m_dayProfiles[304] = { "rain", 0.8f, 2, true, 0.90f, 2, false, 0.0f, 0, true };
m_dayProfiles[305] = { "rain", 0.2f, 0, true, 0.50f, 1, false, 0.0f, 0, false };
m_dayProfiles[306] = { "rain", 0.5f, 0, true, 0.70f, 1, false, 0.0f, 0, false };
m_dayProfiles[307] = { "rain", 0.7f, 0, true, 0.80f, 2, false, 0.0f, 0, false };
m_dayProfiles[308] = { "rain", 1.0f, 0, true, 1.00f, 2, false, 0.0f, 0, false };
m_dayProfiles[309] = { "rain", 0.1f, 0, true, 0.40f, 1, false, 0.0f, 0, false };
m_dayProfiles[310] = { "rain", 0.8f, 0, true, 0.90f, 2, false, 0.0f, 0, false };
m_dayProfiles[311] = { "rain", 0.9f, 0, true, 1.00f, 2, false, 0.0f, 0, false };
m_dayProfiles[312] = { "rain", 1.0f, 0, true, 1.00f, 2, false, 0.0f, 0, false };
m_dayProfiles[313] = { "rain", 0.4f, 2, true, 0.70f, 1, false, 0.0f, 0, false };
m_dayProfiles[314] = { "rain", 0.35f,0, true, 0.60f, 1, false, 0.0f, 0, false };
m_dayProfiles[315] = { "rain", 0.6f, 0, true, 0.75f, 1, false, 0.0f, 0, false };
m_dayProfiles[316] = { "rain", 0.8f, 0, true, 0.85f, 2, false, 0.0f, 0, false };
m_dayProfiles[317] = { "rain", 0.9f, 0, true, 0.95f, 2, false, 0.0f, 0, false };
m_dayProfiles[318] = { "rain", 1.0f, 0, true, 1.00f, 2, false, 0.0f, 0, false };
m_dayProfiles[399] = { "rain", 0.5f, 0, true, 0.60f, 1, false, 0.0f, 0, false };
// 白天 雪类 400–499
m_dayProfiles[400] = { "snow", 0.2f, 0, true, 0.50f, 1, false, 0.0f, 0, false };
m_dayProfiles[401] = { "snow", 0.5f, 0, true, 0.70f, 1, false, 0.0f, 0, false };
m_dayProfiles[402] = { "snow", 0.8f, 0, true, 0.85f, 2, false, 0.0f, 0, false };
m_dayProfiles[403] = { "snow", 1.0f, 0, true, 1.00f, 2, false, 0.0f, 0, false };
m_dayProfiles[404] = { "snow", 0.4f, 1, true, 0.60f, 1, false, 0.0f, 0, false };
m_dayProfiles[405] = { "snow", 0.5f, 1, true, 0.70f, 1, false, 0.0f, 0, false };
m_dayProfiles[406] = { "snow", 0.3f, 1, true, 0.50f, 1, false, 0.0f, 0, false };
m_dayProfiles[407] = { "snow", 0.3f, 0, true, 0.40f, 1, false, 0.0f, 0, false };
m_dayProfiles[408] = { "snow", 0.35f,0, true, 0.60f, 1, false, 0.0f, 0, false };
m_dayProfiles[409] = { "snow", 0.65f,0, true, 0.75f, 1, false, 0.0f, 0, false };
m_dayProfiles[410] = { "snow", 0.9f, 0, true, 0.90f, 2, false, 0.0f, 0, false };
// 白天 雾/霾/沙尘 500–515
m_dayProfiles[500] = { "", 0.0f, 0, false, 0.00f, 0, true, 0.20f, 0, false };
m_dayProfiles[501] = { "", 0.0f, 0, false, 0.00f, 0, true, 0.50f, 0, false };
m_dayProfiles[502] = { "", 0.0f, 0, false, 0.00f, 0, true, 0.40f, 1, false };
m_dayProfiles[503] = { "", 0.0f, 0, false, 0.00f, 0, true, 0.60f, 2, false };
m_dayProfiles[504] = { "", 0.0f, 0, false, 0.00f, 0, true, 0.50f, 2, false };
m_dayProfiles[507] = { "", 0.0f, 0, false, 0.00f, 0, true, 0.80f, 2, false };
m_dayProfiles[508] = { "", 0.0f, 0, false, 0.00f, 0, true, 1.00f, 2, false };
// 白天 极端/未知
m_dayProfiles[999] = { "", 0.0f, 0, false, 0.00f, 0, false, 0.0f, 0, false };
// 夜间覆盖 150–153
m_nightOverrides[150] = { "", 0.0f, 0, false, 0.00f, 0, false, 0.0f, 0, false };
m_nightOverrides[151] = { "", 0.0f, 0, true, 0.35f, 1, false, 0.0f, 0, false };
m_nightOverrides[152] = { "", 0.0f, 0, true, 0.85f, 2, false, 0.0f, 0, false };
m_nightOverrides[153] = { "", 0.0f, 0, true, 0.15f, 0, false, 0.0f, 0, false };
m_nightOverrides[350] = { "rain", 0.4f, 0, true, 0.60f, 1, false, 0.0f, 0, false };
m_nightOverrides[351] = { "rain", 0.7f, 0, true, 0.80f, 2, false, 0.0f, 0, false };
m_nightOverrides[456] = { "snow", 0.3f, 1, true, 0.50f, 1, false, 0.0f, 0, false };
m_nightOverrides[457] = { "snow", 0.3f, 0, true, 0.40f, 1, false, 0.0f, 0, false };
}
// ── JSON 序列化 ──
QJsonObject WeatherProfile::toJson() const {
return {
{"particle", weatherParticle},
{"intensity", static_cast<double>(intensity)},
{"weatherVariant", weatherVariant},
{"cloudActive", cloudActive},
{"cloudCoverage", static_cast<double>(cloudCoverage)},
{"cloudVariant", cloudVariant},
{"fogActive", fogActive},
{"fogIntensity", static_cast<double>(fogIntensity)},
{"fogVariant", fogVariant},
{"lightningActive", lightningActive},
{"exposureOffset", static_cast<double>(exposureOffset)}
};
}
WeatherProfile WeatherProfile::fromJson(const QJsonObject &o) {
WeatherProfile p;
p.weatherParticle = o["particle"].toString();
p.intensity = static_cast<float>(o["intensity"].toDouble());
p.weatherVariant = o["weatherVariant"].toInt();
p.cloudActive = o["cloudActive"].toBool();
p.cloudCoverage = static_cast<float>(o["cloudCoverage"].toDouble());
p.cloudVariant = o["cloudVariant"].toInt();
p.fogActive = o["fogActive"].toBool();
p.fogIntensity = static_cast<float>(o["fogIntensity"].toDouble());
p.fogVariant = o["fogVariant"].toInt();
p.lightningActive = o["lightningActive"].toBool();
p.exposureOffset = static_cast<float>(o["exposureOffset"].toDouble());
return p;
}
void WeatherProfileDB::loadFromFile(const QString &path) {
QFile f(path);
if (!f.open(QIODevice::ReadOnly)) {
qDebug() << "[WeatherProfileDB] no config file at" << path << "- using defaults";
return;
}
QJsonDocument doc = QJsonDocument::fromJson(f.readAll());
QJsonObject root = doc.object();
if (root.contains("day")) {
QJsonObject days = root["day"].toObject();
for (auto it = days.begin(); it != days.end(); ++it)
m_dayProfiles[it.key().toInt()] = WeatherProfile::fromJson(it.value().toObject());
}
if (root.contains("night")) {
QJsonObject nights = root["night"].toObject();
for (auto it = nights.begin(); it != nights.end(); ++it)
m_nightOverrides[it.key().toInt()] = WeatherProfile::fromJson(it.value().toObject());
}
qDebug() << "[WeatherProfileDB] loaded" << m_dayProfiles.size() << "day +"
<< m_nightOverrides.size() << "night from" << path;
}
void WeatherProfileDB::saveToFile(const QString &path) const {
QJsonObject root;
QJsonObject days, nights;
for (auto it = m_dayProfiles.begin(); it != m_dayProfiles.end(); ++it)
days.insert(QString::number(it.key()), it.value().toJson());
for (auto it = m_nightOverrides.begin(); it != m_nightOverrides.end(); ++it)
nights.insert(QString::number(it.key()), it.value().toJson());
root["day"] = days;
root["night"] = nights;
QFile f(path);
if (f.open(QIODevice::WriteOnly | QIODevice::Truncate))
f.write(QJsonDocument(root).toJson(QJsonDocument::Indented));
qDebug() << "[WeatherProfileDB] saved" << m_dayProfiles.size() << "day +"
<< m_nightOverrides.size() << "night to" << path;
}