* @brief Configures dashboard actions and associated timers from the given DataModel frame. */
| 2819 | * @brief Configures dashboard actions and associated timers from the given DataModel frame. |
| 2820 | */ |
| 2821 | void UI::Dashboard::configureActions(const DataModel::Frame& frame) |
| 2822 | { |
| 2823 | if (frame.groups.size() <= 0) |
| 2824 | return; |
| 2825 | |
| 2826 | m_actions.clear(); |
| 2827 | m_actions.squeeze(); |
| 2828 | |
| 2829 | for (auto it = m_timers.begin(); it != m_timers.end(); ++it) { |
| 2830 | if (it.value()) { |
| 2831 | disconnect(it.value()); |
| 2832 | it.value()->stop(); |
| 2833 | delete it.value(); |
| 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | m_timers.clear(); |
| 2838 | m_repeatCounters.clear(); |
| 2839 | |
| 2840 | for (const auto& action : frame.actions) |
| 2841 | m_actions.append(action); |
| 2842 | |
| 2843 | if (!IO::ConnectionManager::instance().isConnected()) { |
| 2844 | Q_EMIT actionStatusChanged(); |
| 2845 | return; |
| 2846 | } |
| 2847 | |
| 2848 | for (int i = 0; i < m_actions.count(); ++i) { |
| 2849 | const auto& action = m_actions[i]; |
| 2850 | if (action.timerMode == DataModel::TimerMode::Off) |
| 2851 | continue; |
| 2852 | |
| 2853 | const auto interval = action.timerIntervalMs; |
| 2854 | if (interval <= 0) { |
| 2855 | qWarning() << "Interval for action" << action.title << "must be greater than 0!"; |
| 2856 | continue; |
| 2857 | } |
| 2858 | |
| 2859 | auto* timer = new QTimer(this); |
| 2860 | timer->setInterval(interval); |
| 2861 | timer->setTimerType(Qt::PreciseTimer); |
| 2862 | connect(timer, &QTimer::timeout, this, [this, i]() { activateAction(i, false); }); |
| 2863 | |
| 2864 | const bool isRepeat = action.timerMode == DataModel::TimerMode::RepeatNTimes; |
| 2865 | if (isRepeat && action.autoExecuteOnConnect) { |
| 2866 | m_repeatCounters[i] = qMax(1, action.repeatCount); |
| 2867 | timer->start(); |
| 2868 | } |
| 2869 | |
| 2870 | else if (!isRepeat |
| 2871 | && (action.timerMode == DataModel::TimerMode::AutoStart |
| 2872 | || action.autoExecuteOnConnect)) |
| 2873 | timer->start(); |
| 2874 | |
| 2875 | m_timers.insert(i, timer); |
| 2876 | } |
| 2877 | |
| 2878 | Q_EMIT actionStatusChanged(); |