-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
271 lines (238 loc) · 8.34 KB
/
Model.cpp
File metadata and controls
271 lines (238 loc) · 8.34 KB
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
#include "Model.h"
Model::Model() : dashboard(new SensorsDashboard) {}
Model::~Model()
{
delete dashboard;
}
void Model::addElettroSensor(const std::string &name, const std::string &model, const std::string &description, const std::vector<double> &misurations) const
{
CreatorSensors *create = new CreatorSensors();
dashboard->addSensor(create->createElettroSensor(name, model, description, misurations));
delete create;
}
void Model::addNewGiroscopeSensor(const std::string &name, const std::string &model, const std::string &description, const std::vector<double> &misurations) const
{
CreatorSensors *create = new CreatorSensors();
dashboard->addSensor(create->createGiroscopeSensor(name, model, description, misurations));
delete create;
}
void Model::addNewAccelerometerSensor(const std::string &name, const std::string &model, const std::string &description, const std::vector<double> &misurations) const
{
CreatorSensors *create = new CreatorSensors();
dashboard->addSensor(create->createAccellSensor(name, model, description, misurations));
delete create;
}
void Model::addNewTermoCouplesSensor(const std::string &name, const std::string &model, const std::string &description, const std::vector<double> &misurations) const
{
CreatorSensors *create = new CreatorSensors();
dashboard->addSensor(create->createTermocouplesSensor(name, model, description, misurations));
delete create;
}
void Model::addTermoResistance(const std::string &name, const std::string &model, const std::string &description, const std::vector<double> &misurations) const
{
CreatorSensors *create = new CreatorSensors();
dashboard->addSensor(create->createTermoresistanceSensor(name, model, description, misurations));
delete create;
}
void Model::addNewSemiSensor(const std::string &name, const std::string &model, const std::string &description, const std::vector<double> &misurations) const
{
CreatorSensors *create = new CreatorSensors();
dashboard->addSensor(create->createSemiSensor(name, model, description, misurations));
delete create;
}
void Model::removeSensor(unsigned int index)
{
dashboard->removeSensor(index);
}
void Model::nextSensor(const unsigned int& index) const
{
dashboard->setCurrentIndex(index);
dashboard->next();
}
void Model::previousSensor(const unsigned int& index) const
{
dashboard->setCurrentIndex(index);
dashboard->previous();
}
void Model::nextSensorTyped(const unsigned int &index) const
{
dashboard->setCurrentIndex(index);
dashboard->nextTyped(index);
}
void Model::previousSensorTyped(const unsigned int &index) const
{
dashboard->setCurrentIndex(index);
dashboard->previousTyped(index);
}
void Model::modifySensor(const unsigned int& index, const std::string &name, const std::string &model, const std::string &descritpion)
{
dashboard->modifySensor(index, name, model, descritpion);
}
Sensor *Model::getSensor(unsigned int index) const
{
return dashboard->getSensor(index);
}
unsigned int Model::getCurrentSensor() const
{
return dashboard->getCurrentSensor();
}
unsigned int Model::getSensorsSize() const
{
return dashboard->getSize();
}
std::vector<Sensor *> Model::getGasSensors() const
{
return dashboard->getSensorsOfGasType();
}
std::vector<Sensor *> Model::getMovmentSensors() const
{
return dashboard->getSensorsOfMovmentType();
}
std::vector<Sensor *> Model::getTemperatureSensors() const
{
return dashboard->getSensorsOfTemperatureType();
}
std::vector<Sensor *> Model::getSensors() const
{
return dashboard->getSensors();
}
void Model::load(const std::string &filename)
{
std::ifstream inputFile(filename);
if (!inputFile.is_open())
{
throw std::runtime_error("Impossible to open file");
}
std::string line;
while (std::getline(inputFile >> std::ws, line))
{
if (line.find("{") == std::string::npos)
{
continue;
}
std::string category;
std::string name;
std::string model;
std::string desc;
std::vector<double> misurations;
while (std::getline(inputFile >> std::ws, line))
{
if (line.find("}") != std::string::npos)
{
break;
}
size_t colonPos = line.find(":");
size_t quotePos1 = line.find("\"", colonPos + 1);
size_t quotePos2 = line.find("\"", quotePos1 + 1);
std::string key = line.substr(0, colonPos);
std::string value = line.substr(quotePos1 + 1, quotePos2 - quotePos1 - 1);
if (key == "\"Categoria\"")
{
category = value;
}
else if (key == "\"Name\"")
{
name = value;
}
else if (key == "\"Model\"")
{
model = value;
}
else if (key == "\"Description\"")
{
desc = value;
}
else if (key == "\"Misuration\"")
{
value = value.substr(1, value.size() - 2);
std::istringstream ss(value);
double numero;
while (ss >> numero) {
misurations.push_back(numero);
ss.ignore();
}
}
}
if (category == "Accellerometer")
addNewAccelerometerSensor(name, model, desc, misurations);
else if (category == "SemiConductors")
addNewSemiSensor(name, model, desc, misurations);
else if (category == "ElettroSensor")
addElettroSensor(name, model, desc, misurations);
else if (category == "Giroscope")
addNewGiroscopeSensor(name, model, desc, misurations);
else if (category == "TermoResistance")
addTermoResistance(name, model, desc, misurations);
else if (category == "TermoCouples")
addNewTermoCouplesSensor(name, model, desc, misurations);
}
inputFile.close();
}
void Model::save(const std::string &filename)
{
std::ofstream outputFile(filename);
if (outputFile.is_open())
{
outputFile << "[\n";
std::string sensorData;
std::string category;
std::vector<Sensor *> sensors = dashboard->getSensors();
size_t lastSensorIndex = sensors.size() - 1;
for (size_t i = 0; i < sensors.size(); ++i)
{
if (dynamic_cast<Accellerometer *>(sensors[i]))
{
category = "Accellerometer";
}
else if (dynamic_cast<ElettroSensor *>(sensors[i]))
{
category = "ElettroSensor";
}
else if (dynamic_cast<SemiConductors *>(sensors[i]))
{
category = "SemiConductors";
}
else if (dynamic_cast<Giroscope *>(sensors[i]))
{
category = "Giroscope";
}
else if (dynamic_cast<TermoResistance *>(sensors[i]))
{
category = "TermoResistance";
}
else if (dynamic_cast<TermoCouples *>(sensors[i]))
{
category = "TermoCouples";
}
sensorData += "{\n";
sensorData += " \"Categoria\": \"" + category + "\"" + ",\n";
sensorData += " \"Name\": \"" + sensors[i]->getName() + "\"" + ",\n";
sensorData += " \"Model\": \"" + sensors[i]->getModel() + "\"" + ",\n";
sensorData += " \"Description\": \"" + sensors[i]->getDescription() + "\"" + ",\n";
sensorData +=" \"Misuration\": \"[";
unsigned int size = sensors[i]->getMisurations().size();
for (double misuration : sensors[i]->getMisurations())
{
std::string mis = std::to_string(misuration);
std::replace(mis.begin(), mis.end(), ',', '.');
sensorData += mis + ", ";
}
if(size!=0)
sensorData = sensorData.substr(0, sensorData.size() - 2);
sensorData += "]\"";
sensorData += "\n}";
if (i < lastSensorIndex)
{
sensorData += ",";
}
sensorData += "\n";
}
outputFile << sensorData;
outputFile << "]\n";
outputFile.close();
}
else
{
throw std::runtime_error("Impossible to open file");
}
}