* @brief Posts a notification on transition into a Warning or Critical band; the value is * clamped to the dataset's widget range first to mirror analog-widget semantics. */
| 159 | * clamped to the dataset's widget range first to mirror analog-widget semantics. |
| 160 | */ |
| 161 | void UI::AlarmMonitor::processValue(Tracker& tracker, double value) |
| 162 | { |
| 163 | if (tracker.rangeMax > tracker.rangeMin) |
| 164 | value = qBound(tracker.rangeMin, value, tracker.rangeMax); |
| 165 | |
| 166 | const int idx = bandIndexFor(tracker, value); |
| 167 | if (idx >= 0) |
| 168 | tracker.hint = idx; |
| 169 | |
| 170 | if (!tracker.initialized) { |
| 171 | tracker.initialized = true; |
| 172 | tracker.lastFiredBand = idx; |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | if (idx == tracker.lastFiredBand) |
| 177 | return; |
| 178 | |
| 179 | tracker.lastFiredBand = idx; |
| 180 | if (idx < 0) |
| 181 | return; |
| 182 | |
| 183 | const auto& band = tracker.bands[idx]; |
| 184 | if (band.severity < 2) |
| 185 | return; |
| 186 | |
| 187 | const qint64 now = QDateTime::currentMSecsSinceEpoch(); |
| 188 | for (int s = band.severity; s <= 3; ++s) |
| 189 | if (now - tracker.lastFireMs[s] < kMinNotifyIntervalMs) |
| 190 | return; |
| 191 | |
| 192 | tracker.lastFireMs[band.severity] = now; |
| 193 | |
| 194 | const QString unit = tracker.units.isEmpty() ? QString() : QStringLiteral(" ") + tracker.units; |
| 195 | const QString name = tracker.title.isEmpty() ? tr("Alarm") : tracker.title; |
| 196 | const QString tier = band.severity >= 3 ? tr("critical") : tr("warning"); |
| 197 | const QString lbl = band.label.isEmpty() ? tier : band.label; |
| 198 | |
| 199 | const QString subtitle = |
| 200 | tr("Value %1%2 entered the %3 band (%4–%5).") |
| 201 | .arg(QString::number(value), unit, lbl, QString::number(band.min), QString::number(band.max)); |
| 202 | |
| 203 | const auto level = band.severity >= 3 ? DataModel::NotificationCenter::Critical |
| 204 | : DataModel::NotificationCenter::Warning; |
| 205 | DataModel::NotificationCenter::instance().post(level, tr("Alarms"), name, subtitle); |
| 206 | } |