-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.cpp
224 lines (206 loc) · 5.38 KB
/
control.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "control.h"
//#define RAND_MAX 100
Control::Control(QObject *parent) : QObject(parent)
{
dataHndlr = new DataHandler();
blHndlr = new bluetooth();
dataThread = new QThread();
btlThread = new QThread();
dataHndlr->moveToThread(dataThread);
blHndlr->moveToThread(btlThread);
dataThread->start();
btlThread->start();
connect(blHndlr,SIGNAL(newData(QString,QByteArray)),this,SLOT(handleData(QString,QByteArray)));
connect(blHndlr,SIGNAL(dConnect(QString)),this,SLOT(connectDevice(QString)));
connect(blHndlr,SIGNAL(dDisconnect(QString)),this,SLOT(deviceDisconnected(QString)));
connect(blHndlr,SIGNAL(errorMsg(QString)),this,SLOT(errorMsg_(QString)));
}
Control::~Control()
{
delete dataHndlr;
delete blHndlr;
qDeleteAll(deviceList);
}
int Control::getActiveDev()
{
int i = 0;
for(auto &a : deviceList)
{
if(a->getStatus() == true)
{
i++;
}
}
return i;
}
void Control::registerNewDevice(QString id)
{
Devices *nDevice = new Devices(id);
deviceList.push_back(nDevice);
if(deviceExists(id))
{
//connect(nDevice,SIGNAL(Timeout(QString)),this,SLOT(sendHumidty(QString)));
emit devConnected(id);
}
}
void Control::sendHumidty(QString id)
{
blHndlr->write(id,QString("getHumidity"));
}
void Control::deviceDisconnected(QString id)
{
if(deviceExists(id))
{
auto dv = getSettings(id);
if(dv->getId() != NULL)
{
dv->setStatus(false);
}
emit devDisconnected(id);
return;
}
}
void Control::connectDevice(QString id)
{
if(!deviceExists(id) && !isWaitingToBeRegistered(id))
{
blHndlr->write(id,"register");
devicesToRegister.append(id);
}
else if(!isWaitingToBeRegistered(id))
{
sendHumidty(id);
}
}
bool Control::deviceExists(QString id)
{
for(auto &d : deviceList)
{
if(d->getId() == id)
{
return true;
}
}
return false;
}
Devices *Control::getSettings(QString id)
{
for(auto d : deviceList)
{
if(d->getId() == id)
{
return d;
}
}
return NULL;
}
void Control::chooseAction(QString id, double hum)
{
auto device = getSettings(id);
if(device == NULL)
{
emit errorMsg(QString("Error: Could not find the device with id %1 when choosing action").arg(id));
}
else
{
if(device->getReqHum() > hum)
{
if(dataHndlr->WriteData(id, HumidtyData(QDateTime::currentDateTime().toString(),hum)))
{
auto tmz = getSettings(id);
if(tmz != NULL)
{
getSettings(id)->setLastHum(hum);
getSettings(id)->setLastUpdt(QDateTime::currentDateTime().toString());
}
initIrrigation(device->getId());
}
return;
}
initTimeout(device->getId());
}
}
void Control::initIrrigation(QString id)
{
blHndlr->write(id,QString("irrigate"));
}
void Control::initTimeout(QString id)
{
auto dev = getSettings(id);
if(dev != NULL)
{
//blHndlr->write(id,QString("TIMEOUT"));
qDebug() << "Timeout was set for device:" << id;
}
else
{
emit errorMsg("Error: Could not retrive device with id % when trying to initiate a timeout for device!");
}
}
void Control::handleData(QString id, QByteArray data)
{
QString cmd(data.toStdString().c_str());
if(deviceExists(id) || isWaitingToBeRegistered(id))
{
if(isWaitingToBeRegistered(id))
{
if(cmd.contains("GER"))
{
registerNewDevice(id);
}
}
else
{
blHndlr->disconnectDevice(id);
}
if(cmd.contains("HUM:"))
{
auto humidty = cmd.midRef(4).toDouble();
qDebug() << "Device with id:" << id << "send humidity: " << humidty;
chooseAction(id,humidty);
}
if(cmd.contains("ERR:"))
{
//Error Codes
//1 - Can't irrigate while irrigation process is active
auto err = cmd.midRef(4).toInt();
switch(err)
{
case 1:
emit errorMsg(QString("Error(%1): Can't initiate irrigation process while its already active").arg(id));
break;
default:
emit errorMsg(QString("Error(%1): Unhandled error").arg(id));
}
}
}
else
{
blHndlr->disconnectDevice(id);
}
}
void Control::falseData(QString id)
{
for(int i=0;i < 50; i++)
{
auto dub = QString::number(qrand() % 100).toDouble();
dataHndlr->WriteData(id,HumidtyData(QDateTime::currentDateTime().toString(),dub));
getSettings(id)->setLastHum(dub);
getSettings(id)->setLastUpdt(QDateTime::currentDateTime().toString());
}
}
bool Control::isWaitingToBeRegistered(QString id)
{
for(auto &a : devicesToRegister)
{
if(a == id)
{
return true;
}
}
return false;
}
void Control::errorMsg_(QString msg)
{
emit errorMsg(msg);
}