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
|
#ifndef FILE_OPERATION_H
#define FILE_OPERATION_H
#include <QObject>
#include <QString>
#include <QFile>
#include <QFile>
#include <QJsonObject>
#include <QJsonDocument>
/******************************************************
* Write access token of mapbox in /etc/mapAccessToken
******************************************************/
#define MAP_ACCESS_TOKEN_FILEPATH "/etc/mapAccessToken"
class File_Operation: public QObject{
Q_OBJECT
public:
Q_INVOKABLE QString getMapAccessToken() {
#if 0
char buf[512];
QString mapAccessToken = "";
FILE* filep = fopen(qPrintable(MAP_ACCESS_TOKEN_FILEPATH), "r");
if (!filep) {
fprintf(stderr,"Failed to open mapAccessToken file \"%s\": %m", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
return mapAccessToken;
}
if (!fgets(buf, 512, filep)) {
fprintf(stderr,"Failed to read mapAccessToken from mapAccessToken file \"%s\"", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
fclose(filep);
return mapAccessToken;
}
if (strlen(buf) > 0 && buf[strlen(buf)-1] == '\n') {
buf[strlen(buf)-1] = '\0';
}
mapAccessToken = QString(buf);
fclose(filep);
return mapAccessToken;
#else
QString mapAccessToken = "";
QFile file(MAP_ACCESS_TOKEN_FILEPATH);
if (!file.open(QIODevice::ReadOnly)){
fprintf(stderr,"Failed to open mapAccessToken file \"%s\": %m", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
return mapAccessToken;
}
QByteArray data = file.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject jsonObj(jsonDoc.object());
if(jsonObj.contains("mapAccessToken")){
mapAccessToken = jsonObj["mapAccessToken"].toString();
}else{
fprintf(stderr,"Failed to find mapAccessToken data \"%s\": %m", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
}
file.close();
return mapAccessToken;
#endif
}
Q_INVOKABLE double getSpeed() {
double speed = 60; // km/h
QFile file(MAP_ACCESS_TOKEN_FILEPATH);
if (!file.open(QIODevice::ReadOnly)){
fprintf(stderr,"Failed to open mapAccessToken file \"%s\": %m", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
return speed;
}
QByteArray data = file.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject jsonObj(jsonDoc.object());
if(jsonObj.contains("speed")){
speed = jsonObj["speed"].toDouble();
}else{
fprintf(stderr,"Failed to find speed data \"%s\": %m", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
}
file.close();
return speed;
}
Q_INVOKABLE int getInterval() {
int interval = 15; // ms
QFile file(MAP_ACCESS_TOKEN_FILEPATH);
if (!file.open(QIODevice::ReadOnly)){
fprintf(stderr,"Failed to open mapAccessToken file \"%s\": %m", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
return interval;
}
QByteArray data = file.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject jsonObj(jsonDoc.object());
if(jsonObj.contains("interval")){
interval = (int)jsonObj["interval"].toDouble();
}else{
fprintf(stderr,"Failed to find interval data \"%s\": %m", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
}
file.close();
return interval;
}
Q_INVOKABLE double getLatitude() {
double latitude = 36.136261;
QFile file(MAP_ACCESS_TOKEN_FILEPATH);
if (!file.open(QIODevice::ReadOnly)){
fprintf(stderr,"Failed to open mapAccessToken file \"%s\": %m", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
return latitude;
}
QByteArray data = file.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject jsonObj(jsonDoc.object());
if(jsonObj.contains("latitude")){
latitude = jsonObj["latitude"].toDouble();
}else{
fprintf(stderr,"Failed to find latitude data \"%s\": %m", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
}
file.close();
return latitude;
}
Q_INVOKABLE double getLongitude() {
double longitute = -115.151254;
QFile file(MAP_ACCESS_TOKEN_FILEPATH);
if (!file.open(QIODevice::ReadOnly)){
fprintf(stderr,"Failed to open mapAccessToken file \"%s\": %m", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
return longitute;
}
QByteArray data = file.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
QJsonObject jsonObj(jsonDoc.object());
if(jsonObj.contains("longitute")){
longitute = jsonObj["longitute"].toDouble();
}else{
fprintf(stderr,"Failed to find longitute data \"%s\": %m", qPrintable(MAP_ACCESS_TOKEN_FILEPATH));
}
file.close();
return longitute;
}
};
#endif // FILE_OPERATION_H
|