-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgftprogram.cpp
160 lines (139 loc) · 4.48 KB
/
gftprogram.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
#include <assert.h>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QStringList>
#include <QTimer>
#include "gftprogram.h"
#include "gft.h"
#include "trace.h"
enum GftMethod {GftGet, GftPost};
GftProgram::GftProgram(QObject *parent): QObject(parent) , requestId(-1) {
qRegisterMetaType<GftIdList>();
manager = new QNetworkAccessManager(this);
requestor = new O2Requestor(manager, Gft::instance(), this);
connect(requestor, SIGNAL(finished(int,QNetworkReply::NetworkError,QByteArray)), this, SLOT(stepDone(int,QNetworkReply::NetworkError,QByteArray)));
}
GftProgram::~GftProgram() {
}
void GftProgram::setInstructions(const QList<GftInstruction> instructions_) {
instructions = instructions_;
ic = 0;
status = Idle;
}
void GftProgram::step() {
Trace t("GftProgram::step");
assert(status != Completed);
if (status == Failed) {
qDebug() << "Failed";
emit programCompleted(true);
return;
}
if (ic >= instructions.length()) {
qDebug() << "Completed";
emit programCompleted(false);
return;
}
if (status == Idle) {
qDebug() << "Idle -> Running";
status = Running;
}
Gft *gft = Gft::instance();
if (!gft->enabled()) {
qDebug() << "Not enabled";
emit programCompleted(false);
return;
}
if (!gft->linked()) {
qDebug() << "Not logged in";
emit programCompleted(false);
return;
}
// Build SQL statement based on current instruction
QString sql;
GftMethod method;
switch (instructions[ic].op) {
case GftFindTable:
sql = QString("SHOW TABLES");
method = GftGet;
break;
case GftCreateTableIf:
if (!tableId.isNull()) {
// Table exists, so there is no need to create table. Execute next step instead
ic++;
QTimer::singleShot(0, this, SLOT(step()));
return;
}
sql = QString("CREATE TABLE '%1' (steps: NUMBER, date: DATETIME, tags: STRING, device: STRING)").arg(instructions[ic].param);
method = GftPost;
break;
default:
// At this point, we should have a table ID. If we don't, fail.
if (tableId.isNull()) {
qDebug() << "No table ID";
emit programCompleted(true);
return;
}
sql = instructions[ic].param;
sql.replace("$T", tableId);
method = GftPost;
}
// Execute request
qDebug() << "Sending request to Google";
QUrl url(GFT_SQL_URL);
if (method == GftGet) {
url.addQueryItem("sql", sql);
}
QNetworkRequest request(url);
if (method == GftGet) {
requestId = requestor->get(request);
} else {
QByteArray data;
data.append("sql=");
data.append(QUrl::toPercentEncoding(sql.toUtf8()));
requestId = requestor->post(request, data);
}
}
void GftProgram::stepDone(int id, QNetworkReply::NetworkError error, const QByteArray &data) {
Trace t("GftProgram::stepDone");
if (id != requestId) {
qWarning() << "GftProgram::stepDone: Unknown request ID" << id;
return;
}
if (error == QNetworkReply::NoError) {
QStringList lines = QString(data).split("\n");
switch (instructions[ic].op) {
case GftFindTable: {
QString gftName = instructions[ic].param;
foreach (QString line, lines) {
int commaIndex = line.indexOf(',');
if (commaIndex >= 0) {
QString id = line.left(commaIndex);
QString name = line.mid(commaIndex + 1);
if (name == gftName) {
qDebug() << "Found fusion table, name:" << name << ", ID:" << id;
tableId = id;
break;
}
}
}
break;
}
case GftCreateTableIf:
if ((lines.length() >= 2) && (lines[0] == "tableid")) {
tableId = lines[1];
qDebug() << "Created fusion table, ID:" << tableId;
} else {
status = Failed;
qCritical() << "GftProgram::stepDone: Could not create fusion table";
}
break;
default:
emit stepCompleted(instructions[ic].idList);
}
} else {
qCritical() << "GftProgram::stepDone: Error" << error;
status = Failed;
}
ic++;
QTimer::singleShot(0, this, SLOT(step()));
}