-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodecontrols.cpp
136 lines (99 loc) · 3.29 KB
/
nodecontrols.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
#include "nodecontrols.h"
nodeControls::nodeControls(QObject *parent) : QObject(parent) {}
/*
* Creates Node model to be used in Setup view, loads data
* from node.json
*/
QVariantList nodeControls::createNodeModel(void) {
QVariantList dataList;
QFile loadFile(configDir + "/nodes.json");
if (!loadFile.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open node data file.");
return dataList;
}
QByteArray nodeData = loadFile.readAll();
QJsonDocument loadDoc(QJsonDocument::fromJson(nodeData));
const QJsonObject &objDoc = loadDoc.object();
QJsonDocument doc(objDoc);
QString strJson(doc.toJson(QJsonDocument::Compact));
QJsonArray nodesArray = objDoc["nodes"].toArray();
for(int i = 0; i < nodesArray.count(); i++) {
dataList.append(nodesArray[i].toVariant());
}
return dataList;
}
bool nodeControls::remove(QString label) {
qDebug() << "Removing " << label << "!";
QFile saveFile(configDir + "/nodes.json");
if (!saveFile.open(QIODevice::ReadWrite)) {
qWarning("Couldn't open save file.");
return false;
}
QJsonDocument loadDoc(QJsonDocument::fromJson(saveFile.readAll()));
const QJsonObject &objDoc = loadDoc.object();
saveFile.close();
if (!saveFile.open(QIODevice::WriteOnly)) {
qWarning("Couldn't open save file.");
return false;
}
QJsonArray nodeArray = objDoc["nodes"].toArray();
for(int i = 0; i < nodeArray.count(); i++) {
if(nodeArray[i].toObject()["label"] == label) {
nodeArray.removeAt(i);
break;
}
}
QJsonObject nodeObject;
nodeObject["nodes"] = nodeArray;
QJsonDocument saveDoc(nodeObject);
saveFile.write(saveDoc.toJson());
return true;
}
// TODO write firmware version grabber from Github API
QString nodeControls::getFirmwareVersion(QString type) {
if(type.compare("AVR"))
return "0.1";
else
return "Unknown";
}
/*
* Returns a blank node object
*/
QJsonObject nodeControls::createBlankNodeObject(QString label, QString type) {
QJsonObject nodeObject;
nodeObject["label"] = label;
nodeObject["lastOnline"] = "N/A";
nodeObject["romLeft"] = "Unknown";
QJsonObject firmware;
firmware["type"] = type;
firmware["version"] = getFirmwareVersion(type);
nodeObject["firmware"] = firmware;
QJsonArray modules;
nodeObject["modules"] = modules;
QDir().mkdir(configDir + "/" + label);
return nodeObject;
}
/*
* Adds a blank node objects and writes it to nodes.json
*/
bool nodeControls::addEmpty(QString label, QString type) {
QFile saveFile(configDir + "/nodes.json");
if (!saveFile.open(QIODevice::ReadWrite)) {
qWarning("Couldn't open save file.");
return false;
}
QJsonDocument loadDoc(QJsonDocument::fromJson(saveFile.readAll()));
const QJsonObject &objDoc = loadDoc.object();
saveFile.close();
if (!saveFile.open(QIODevice::WriteOnly)) {
qWarning("Couldn't open save file.");
return false;
}
QJsonArray nodeArray = objDoc["nodes"].toArray();
nodeArray.append(createBlankNodeObject(label, type));
QJsonObject nodeObject;
nodeObject["nodes"] = nodeArray;
QJsonDocument saveDoc(nodeObject);
saveFile.write(saveDoc.toJson());
return true;
}