forked from LazyT/rrcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.cpp
207 lines (179 loc) · 5.98 KB
/
timer.cpp
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
#include "timer.h"
timerDialog::timerDialog(QWidget *parent) : QDialog(parent)
{
setupUi(this);
layout()->setSizeConstraint(QLayout::SetFixedSize);
setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
if(reinterpret_cast<MainWindow*>(parent)->robo.timers.size())
{
foreach(TIMER val, reinterpret_cast<MainWindow*>(parent)->robo.timers)
{
comboBox->addItem(QString("ID %1").arg(val.id), val.id);
}
}
else
{
addTimer();
}
buttonBox->button(QDialogButtonBox::Yes)->setText(tr("Add Timer"));
buttonBox->button(QDialogButtonBox::No)->setText(tr("Del Timer"));
}
void timerDialog::addTimer()
{
buttonBox->button(QDialogButtonBox::Yes)->setEnabled(false);
buttonBox->button(QDialogButtonBox::No)->setEnabled(false);
comboBox->setEnabled(false);
comboBox_fanspeed->setCurrentIndex(1);
dial_hour->setValue(10);
dial_minute->setValue(0);
dial_day->setValue(0);
dial_month->setValue(0);
lcdNumber_day->display("-");
lcdNumber_month->display("-");
toolButton_mon->setChecked(true);
toolButton_tue->setChecked(false);
toolButton_wed->setChecked(true);
toolButton_thu->setChecked(false);
toolButton_fri->setChecked(true);
toolButton_sat->setChecked(false);
toolButton_sun->setChecked(false);
pushButton_state->setChecked(true);
comboBox->addItem(QString("ID %1").arg(QDateTime::currentDateTime().toTime_t()), QDateTime::currentDateTime().toTime_t());
comboBox->setCurrentIndex(comboBox->count() - 1);
}
void timerDialog::delTimer()
{
if(reinterpret_cast<MainWindow*>(parent())->sendUDP(QString(MIIO_DEL_TIMER).arg(comboBox->itemData(comboBox->currentIndex()).toLongLong()).arg("%1")))
{
reinterpret_cast<MainWindow*>(parent())->robo.timers.remove(comboBox->currentIndex(), 1);
comboBox->removeItem(comboBox->currentIndex());
if(!comboBox->count())
{
close();
}
}
}
void timerDialog::on_comboBox_currentIndexChanged(int index)
{
if(reinterpret_cast<MainWindow*>(parent())->robo.timers.size() && reinterpret_cast<MainWindow*>(parent())->robo.timers.size() > index)
{
QStringList entries = reinterpret_cast<MainWindow*>(parent())->robo.timers.at(index).crontab.split(' ');
if(reinterpret_cast<MainWindow*>(parent())->robo.timers.at(index).fanspeed > FANSPEED_TURBO)
{
comboBox_fanspeed->setCurrentIndex(3);
}
else if(reinterpret_cast<MainWindow*>(parent())->robo.timers.at(index).fanspeed > FANSPEED_BALANCED)
{
comboBox_fanspeed->setCurrentIndex(2);
}
else if(reinterpret_cast<MainWindow*>(parent())->robo.timers.at(index).fanspeed > FANSPEED_QUIET)
{
comboBox_fanspeed->setCurrentIndex(1);
}
else
{
comboBox_fanspeed->setCurrentIndex(0);
}
pushButton_state->setChecked(reinterpret_cast<MainWindow*>(parent())->robo.timers.at(index).state == "off" ? 0 : 1);
dial_hour->setValue(entries.at(1) == "*" ? -1 : entries.at(1).toInt());
dial_minute->setValue(entries.at(0) == "*" ? -1 : entries.at(0).toInt());
dial_day->setValue(entries.at(2) == "*" ? 0 : entries.at(2).toInt());
dial_month->setValue(entries.at(3) == "*" ? 0 : entries.at(3).toInt());
emit on_dial_hour_valueChanged(dial_hour->value());
emit on_dial_minute_valueChanged(dial_minute->value());
emit on_dial_day_valueChanged(dial_day->value());
emit on_dial_month_valueChanged(dial_month->value());
toolButton_sun->setChecked(entries.at(4).contains("0"));
toolButton_mon->setChecked(entries.at(4).contains("1"));
toolButton_tue->setChecked(entries.at(4).contains("2"));
toolButton_wed->setChecked(entries.at(4).contains("3"));
toolButton_thu->setChecked(entries.at(4).contains("4"));
toolButton_fri->setChecked(entries.at(4).contains("5"));
toolButton_sat->setChecked(entries.at(4).contains("6"));
}
}
void timerDialog::on_dial_hour_valueChanged(int value)
{
lcdNumber_hour->display(value < 0 ? "-" : QString::number(value));
}
void timerDialog::on_dial_minute_valueChanged(int value)
{
lcdNumber_minute->display(value < 0 ? "-" : QString::number(value));
}
void timerDialog::on_dial_day_valueChanged(int value)
{
lcdNumber_day->display(value ? QString::number(value) : "-");
}
void timerDialog::on_dial_month_valueChanged(int value)
{
lcdNumber_month->display(value ? QString::number(value) : "-");
}
void timerDialog::on_buttonBox_clicked(QAbstractButton *button)
{
if(buttonBox->standardButton(button) == QDialogButtonBox::Apply)
{
int hour = dial_hour->value();
int minute = dial_minute->value();
int day = dial_day->value();
int month = dial_month->value();
QString weekdays;
int fanspeeds[] = {FANSPEED_QUIET, FANSPEED_BALANCED, FANSPEED_TURBO, FANSPEED_MAXIMUM};
if(toolButton_sun->isChecked())
{
weekdays.append("0,");
}
if(toolButton_mon->isChecked())
{
weekdays.append("1,");
}
if(toolButton_tue->isChecked())
{
weekdays.append("2,");
}
if(toolButton_wed->isChecked())
{
weekdays.append("3,");
}
if(toolButton_thu->isChecked())
{
weekdays.append("4,");
}
if(toolButton_fri->isChecked())
{
weekdays.append("5,");
}
if(toolButton_sat->isChecked())
{
weekdays.append("6,");
}
if(weekdays.isEmpty())
{
weekdays = "*";
}
else
{
weekdays.chop(1);
}
QString crontab = QString("%1 %2 %3 %4 %5").arg(minute < 0 ? "*" : QString::number(minute)).arg(hour < 0 ? "*" : QString::number(hour)).arg(day == 0 ? "*" : QString::number(day)).arg(month == 0 ? "*" : QString::number(month)).arg(weekdays);
if(reinterpret_cast<MainWindow*>(parent())->sendUDP(QString(MIIO_SET_TIMER).arg(comboBox->itemData(comboBox->currentIndex()).toLongLong()).arg(crontab).arg(fanspeeds[comboBox_fanspeed->currentIndex()]).arg("%1")))
{
if(!pushButton_state->isChecked())
{
reinterpret_cast<MainWindow*>(parent())->sendUDP(QString(MIIO_UPD_TIMER).arg(comboBox->itemData(comboBox->currentIndex()).toLongLong()).arg("off").arg("%1"));
}
close();
}
}
else if(buttonBox->standardButton(button) == QDialogButtonBox::Yes)
{
addTimer();
}
else if(buttonBox->standardButton(button) == QDialogButtonBox::No)
{
delTimer();
}
else if(buttonBox->standardButton(button) == QDialogButtonBox::Close)
{
close();
}
}