-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcocoservice.cpp
161 lines (128 loc) · 5.4 KB
/
cocoservice.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
/*
* Copyright (C) 2012-2013 Matt Broadstone
* Contact: http://bitbucket.org/devonit/qjsonrpc
*
* This file is part of the QJsonRpc Library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include <QDebug>
#include <QTimer>
#include <QEventLoop>
#include <QThreadPool>
#include <QRunnable>
#include "cocoservice.h"
CocoService::CocoService(QObject *parent)
: QJsonRpcService(parent)
{
QThreadPool::globalInstance()->setMaxThreadCount(10);
}
void CocoService::testMethod()
{
qDebug() << Q_FUNC_INFO << "called" << endl;
}
void CocoService::testMethodWithParams(const QString &first, bool second, double third)
{
qDebug() << Q_FUNC_INFO << "called with parameters: " << endl
<< " first: " << first << endl
<< "second: " << second << endl
<< " third: " << third << endl;
}
QString CocoService::uploadArduinoScript(const QString &code)
{
QString tmpDir = "/tmp/CocoTmp/";
QString tmpDirCompile = "/tmp/CocoTmpCompile/";
QString scriptName = "CocoTmp.ino";
QString uploadCmd = "/Users/xcorex/Documents/Arduino/hardware/CocoMake7/avr/tools/avrdude/macosx/avrdude -C/Users/xcorex/Documents/Arduino/hardware/CocoMake7/avr/tools/avrdude/macosx/avrdude.conf -pattiny85 -cusbasp -P/dev/cu.usbmodem1411 -b19200 -D -Uflash:w:" + tmpDirCompile + scriptName + ".hex:i";
QString workingdir = QDir::currentPath();
QProcess makeProcess;
makeProcess.setProcessChannelMode(QProcess::MergedChannels);
makeProcess.setWorkingDirectory(workingdir);
makeProcess.start(uploadCmd);
makeProcess.waitForFinished();
qDebug() << makeProcess.readAll();
return "DONE";
}
QString CocoService::compileArduinoScript(const QString &code)
{
QString tmpDir = "/tmp/CocoTmp/";
QString tmpDirCompile = "/tmp/CocoTmpCompile/";
QString scriptName = "CocoTmp.ino";
QDir tmp;
tmp.mkdir(tmpDir);
tmp.mkdir(tmpDirCompile);
QFile tmpScript(tmpDir+scriptName);
tmpScript.open(QIODevice::WriteOnly);
QTextStream out(&tmpScript);
out << code;
tmpScript.close();
QString scriptPath = tmpDir+scriptName;
QString compileCmd = "/Users/xcorex/Downloads/Arduino-2.app/Contents/Java/arduino-builder -compile -logger=machine -hardware \"/Users/xcorex/Downloads/Arduino-2.app/Contents/Java/hardware\" -hardware \"/Users/xcorex/Library/Arduino15/packages\" -hardware \"/Users/xcorex/Documents/Arduino/hardware\" -tools \"/Users/xcorex/Downloads/Arduino-2.app/Contents/Java/tools-builder\" -tools \"/Users/xcorex/Downloads/Arduino-2.app/Contents/Java/hardware/tools/avr\" -tools \"/Users/xcorex/Library/Arduino15/packages\" -built-in-libraries \"/Users/xcorex/Downloads/Arduino-2.app/Contents/Java/libraries\" -libraries \"/Users/xcorex/Documents/Arduino/libraries\" -fqbn=CocoMake7:avr:cocomake -ide-version=10609 -build-path \"" + tmpDirCompile + "\" -warnings=none -prefs=build.warn_data_percentage=75 -verbose \"" + scriptPath + "\"";
QString uploadCmd = "/Users/xcorex/Documents/Arduino/hardware/CocoMake7/avr/tools/avrdude/macosx/avrdude -C/Users/xcorex/Documents/Arduino/hardware/CocoMake7/avr/tools/avrdude/macosx/avrdude.conf -pattiny85 -cusbasp -P/dev/cu.usbmodem1411 -b19200 -D -Uflash:w:" + tmpDirCompile + scriptName + ".hex:i";
QString workingdir = QDir::currentPath();
QProcess makeProcess;
makeProcess.setProcessChannelMode(QProcess::MergedChannels);
makeProcess.setWorkingDirectory(workingdir);
makeProcess.start(compileCmd);
makeProcess.waitForFinished();
qDebug() << makeProcess.readAll();
makeProcess.start(uploadCmd);
makeProcess.waitForFinished();
qDebug() << makeProcess.readAll();
return "DONE";
}
QString CocoService::testMethodWithParamsAndReturnValue(const QString &name)
{
return QString("Hello %1").arg(name);
}
void CocoService::testMethodWithDefaultParameter(const QString &first, const QString &second)
{
qDebug() << Q_FUNC_INFO << endl
<< "first: " << first << endl
<< (second.isEmpty() ? "not defined, default parameter" : second) << endl;
}
QString CocoService::immediateResponse()
{
return "immediate";
}
QString CocoService::longTaskWithImmediateResponse()
{
QEventLoop loop;
QTimer::singleShot(1000, &loop, SLOT(quit()));
loop.exec();
return "long immediate";
}
class DelayedResponseJob : public QRunnable
{
public:
DelayedResponseJob(const QJsonRpcServiceRequest &request)
: m_request(request)
{
}
protected:
virtual void run() {
// do some work
QEventLoop loop;
QTimer::singleShot(1000, &loop, SLOT(quit()));
loop.exec();
// respond
QJsonRpcMessage response = m_request.request().createResponse(QLatin1String("long delayed"));
m_request.respond(response);
}
private:
QJsonRpcServiceRequest m_request;
};
QString CocoService::longTaskWithDelayedResponse()
{
beginDelayedResponse();
QThreadPool::globalInstance()->start(new DelayedResponseJob(currentRequest()));
return QString();
}