summaryrefslogtreecommitdiffstats
path: root/app/logfile/touchlogplay.cpp
blob: 02a53b495da7231edc7398f917b26c114037021f (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "touchlogplay.h"
#include <QDir>
#include <QDirIterator>
#include <QDateTime>
#include <QTextStream>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>

#include <QDebug>
#include <QCoreApplication>

const std::string TouchLogPlayImpl::touchPlayUtilsExec = std::string(getenv("AFM_APP_INSTALL_DIR")) + "/bin/tsplayer";
TouchLogPlayImpl *TouchLogPlayImpl::pointer = NULL;
bool TouchLogPlayImpl::touchPlayFlag = false;
void (*TouchLogPlayImpl::oldHandler)(int) = NULL;

TouchLogPlayImpl::TouchLogPlayImpl(QObject *parent) : QAbstractItemModel(parent)
{
    roles[nameRole] = "name";
    roles[folderRole] = "folder";
    roles[childrenRole] = "children";
    roles[pathRole] = "path";

    touchPlayUtils = -1;
    pointer = this;
}

TouchLogPlayImpl::~TouchLogPlayImpl()
{
    touchPlay(false);
}

int TouchLogPlayImpl::rowCount(const QModelIndex &parent) const
{
    (void)parent;
    return datas.size();
}

int TouchLogPlayImpl::columnCount(const QModelIndex &parent) const
{
    (void)parent;
    return 1;
}

QModelIndex TouchLogPlayImpl::index(int row, int column, const QModelIndex &parent) const
{
    (void)parent;
    if((row >= 0) && (row < datas.size()))
    {
        return createIndex(row,column);
    }
    else
    {
        return QModelIndex();
    }
}

QVariant TouchLogPlayImpl::data(const QModelIndex &index, int role) const
{
    if(index.isValid() && index.row()<datas.size())
    {
        return datas[index.row()][role];
    }
    else
    {
        QHash<int,QVariant> data;
        data[nameRole] = "";
        data[folderRole] = true;
        data[childrenRole] = 0;
        return data[role];
    }
}

QModelIndex TouchLogPlayImpl::parent(const QModelIndex &child) const
{
    (void)child;
    return QModelIndex();
}

QHash<int,QByteArray> TouchLogPlayImpl::roleNames() const
{
    return roles;
}

void TouchLogPlayImpl::refresh()
{
    QDir dir(QDir::homePath() + "/" + "LogDataFile");

    if(dir.exists())
    {
        beginResetModel();
        dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
        dir.setSorting(QDir::Name);

        QFileInfoList list = dir.entryInfoList();
        datas.clear();

        for(int i = 0; i < list.size(); i++)
        {
            QHash<int,QVariant> parent;
            QStringList children;

            parent[nameRole] = list.at(i).fileName();
            parent[folderRole] = true;
            parent[pathRole] = list.at(i).absoluteFilePath();

            children.clear();

            QDirIterator it(list.at(i).filePath(), QStringList() << "*.touch", QDir::Files | QDir::NoSymLinks, QDirIterator::Subdirectories);
            while(it.hasNext())
            {
                children << it.next();
            }

            parent[childrenRole] = children.count();
            datas.append(parent);

            if(children.count() > 0)
            {
                children.sort();

                for(int j = 0; j < children.count(); j++)
                {
                    QHash<int,QVariant> child;

                    child[nameRole] = children.at(j).section("/", -1);
                    child[folderRole] = false;
                    child[childrenRole] = 0;
                    child[pathRole] = children.at(j);

                    datas.append(child);
                }
            }
        }
        endResetModel();
    }
}

void TouchLogPlayImpl::setLogFileIndex(int index)
{
    if(index > 0 && index < datas.size())
    {
        QFile file(datas[index][pathRole].toString());

        if(file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            QTextStream filetxt(&file);
            QString line = "";
            QString port = "";
            QDateTime datetime;

            while(!filetxt.atEnd())
            {
                line = filetxt.readLine();
                if(!line.isEmpty())
                {
                    port = line.section(" ", 1, 1, QString::SectionSkipEmpty);
                    if(port.isEmpty())
                    {
                        port = line.section("\t", 1, 1, QString::SectionSkipEmpty);
                    }
                    datetime = QDateTime::fromTime_t(line.mid(line.indexOf("(")+1, line.indexOf(".")-line.indexOf("(")-1).toUInt());
                }
            }
            file.close();
        }
    }
}

bool TouchLogPlayImpl::touchPlay(bool play)
{
    qDebug() << "touchPlayFlag:" << touchPlayFlag;
    if(play)
    {
        oldHandler = signal(SIGCHLD, finalizeChild);
        if(oldHandler == SIG_ERR)
        {
            qDebug("signal error.");
            return false;
        }

        qDebug() << "current file" << touchPlayUtilsExec.c_str();

        touchPlayUtils = fork();
        switch(touchPlayUtils)
        {
        case 0:
            execl(touchPlayUtilsExec.c_str(), basename(touchPlayUtilsExec.c_str()),
                  "-d", "1",
                  "-f", touchplayUtilsArg["-f"].toStdString().c_str(),
                  "-e", "1",
                  (char*)NULL);
            break;
        default:
            touchPlayFlag = true;
            emit propTouchPlayFlagChanged();
            qDebug() << "default:";
            break;
        }
    }
    else
    {
        if(touchPlayFlag)
        {
            if((touchPlayUtils > 0) && (kill(touchPlayUtils, SIGUSR1) == 0))
            {
                waitpid(touchPlayUtils, NULL, 0);
            }
        }
    }

    return true;
}

void TouchLogPlayImpl::setTouchProperty(int index)
{
    if(index > 0 && index < datas.size())
    {
        touchplayUtilsArg["-f"] = datas[index][pathRole].toString();
    }
}

bool TouchLogPlayImpl::propTouchPlayFlag() const
{
    return touchPlayFlag;
}

void TouchLogPlayImpl::propTouchPlayFlagChangedDelegate()
{
    emit propTouchPlayFlagChanged();
}

void TouchLogPlayImpl::finalizeChild(int sig)
{
    if(sig == SIGCHLD)
    {
        qDebug("finalizeChild start.");
        waitpid(-1, NULL, 0);
        touchPlayFlag = false;
        pointer->propTouchPlayFlagChangedDelegate();
        qDebug("finalizeChild end.");
    }

    if(signal(SIGCHLD, oldHandler) == SIG_ERR)
    {
        qDebug("signal error.");
    }
}

void TouchLogPlayImpl::emitSignalAgent()
{
    qDebug("emitSignalAgent");
    pointer->propTouchPlayFlagChangedDelegate();
}