Subversion Repositories HomeAutomation

Rev

Rev 1030 | Rev 1048 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1030 Rev 1046
Line 16... Line 16...
16
 *   You should have received a copy of the GNU General Public License     *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
20
 ***************************************************************************/
-
 
21
 
-
 
22
#include "canvariable.h"
-
 
23
 
-
 
24
 
-
 
25
#include <vector>
-
 
26
 
21
 
27
 
22
#include "canidtranslator.h"
28
#include "canidtranslator.h"
23
 
29
 
24
CanIdTranslator* CanIdTranslator::myInstance = NULL;
30
CanIdTranslator* CanIdTranslator::myInstance = NULL;
25
 
31
 
26
CanIdTranslator& CanIdTranslator::getInstance()
32
CanIdTranslator& CanIdTranslator::getInstance()
27
{
33
{
28
    if (myInstance == NULL)
34
    if (myInstance == NULL)
29
    {
35
    {
30
        myInstance = new CanIdTranslator();
36
        myInstance = new CanIdTranslator();
31
    }
37
    }
32
 
38
 
33
    return *myInstance;
39
    return *myInstance;
34
}
40
}
35
 
41
 
36
void CanIdTranslator::deleteInstance()
42
void CanIdTranslator::deleteInstance()
37
{
43
{
38
    delete myInstance;
44
    delete myInstance;
39
    myInstance = NULL;
45
    myInstance = NULL;
40
}
46
}
41
 
47
 
42
CanIdTranslator::CanIdTranslator()
48
CanIdTranslator::CanIdTranslator()
43
{
49
{
44
    Logger &log = Logger::getInstance();
50
    Logger &log = Logger::getInstance();
45
 
51
 
46
    string filename = Settings::get("CanIdXMLFile");
52
    string filename = Settings::get("CanIdXMLFile");
47
 
53
 
48
    if (file_exists(filename))
54
    if (file_exists(filename))
49
    {
55
    {
50
        xmlNode.load(filename.c_str());
56
        xmlNode.load(filename.c_str());
Line 63... Line 69...
63
    vector<XmlNode> classNodes = classesNode.getChildren();
69
    vector<XmlNode> classNodes = classesNode.getChildren();
64
 
70
 
65
    for (int n = 0; n < classNodes.size(); n++)
71
    for (int n = 0; n < classNodes.size(); n++)
66
    {
72
    {
67
        map<string, string> attributes = classNodes[n].getAttributes();
73
        map<string, string> attributes = classNodes[n].getAttributes();
68
 
74
 
69
        if (stoi(attributes["id"]) == classId)
75
        if (stoi(attributes["id"]) == classId)
70
        {
76
        {
71
            return attributes["name"];
77
            return attributes["name"];
72
        }
78
        }
73
    }
79
    }
Line 76... Line 82...
76
}
82
}
77
 
83
 
78
int CanIdTranslator::resolveClassId(string className)
84
int CanIdTranslator::resolveClassId(string className)
79
{
85
{
80
    XmlNode classesNode = xmlNode.findChild("classes");
86
    XmlNode classesNode = xmlNode.findChild("classes");
81
    vector<XmlNode> classNodes = classesNode.getChildren();
87
    vector<XmlNode> classNodes = classesNode.getChildren();
82
 
88
 
83
    for (int n = 0; n < classNodes.size(); n++)
89
    for (int n = 0; n < classNodes.size(); n++)
84
    {
90
    {
85
        map<string, string> attributes = classNodes[n].getAttributes();
91
        map<string, string> attributes = classNodes[n].getAttributes();
86
 
92
 
87
        if (attributes["name"].compare(className) == 0)
93
        if (attributes["name"].compare(className) == 0)
88
        {
94
        {
89
            return stoi(attributes["id"]);
95
            return stoi(attributes["id"]);
90
        }
96
        }
91
    }
97
    }
92
 
98
 
93
    return -1;
99
    return -1;
94
}
100
}
95
 
101
 
96
string CanIdTranslator::lookupCommandName(int commandId, string moduleName)
102
string CanIdTranslator::lookupCommandName(int commandId, string moduleName)
97
{
103
{
98
    XmlNode commandsNode = xmlNode.findChild("commands");
104
    XmlNode commandsNode = xmlNode.findChild("commands");
99
    vector<XmlNode> commandNodes = commandsNode.getChildren();
105
    vector<XmlNode> commandNodes = commandsNode.getChildren();
100
 
106
 
101
    for (int n = 0; n < commandNodes.size(); n++)
107
    for (int n = 0; n < commandNodes.size(); n++)
102
    {
108
    {
103
        map<string, string> attributes = commandNodes[n].getAttributes();
109
        map<string, string> attributes = commandNodes[n].getAttributes();
104
 
110
 
105
        if (commandId >= 128)
111
        if (commandId >= 128)
106
        {
112
        {
107
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
113
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
108
            {
114
            {
109
                return attributes["name"];
115
                return attributes["name"];
110
            }
116
            }
111
        }
117
        }
112
        else if (stoi(attributes["id"]) == commandId)
118
        else if (stoi(attributes["id"]) == commandId)
113
        {
119
        {
114
            return attributes["name"];
120
            return attributes["name"];
115
        }
121
        }
116
    }
122
    }
117
 
123
 
118
    return "";
124
    return "";
119
}
125
}
120
 
126
 
121
int CanIdTranslator::resolveCommandId(string commandName, string moduleName)
127
int CanIdTranslator::resolveCommandId(string commandName, string moduleName)
122
{
128
{
123
    XmlNode commandsNode = xmlNode.findChild("commands");
129
    XmlNode commandsNode = xmlNode.findChild("commands");
124
    vector<XmlNode> commandNodes = commandsNode.getChildren();
130
    vector<XmlNode> commandNodes = commandsNode.getChildren();
125
 
131
 
126
    for (int n = 0; n < commandNodes.size(); n++)
132
    for (int n = 0; n < commandNodes.size(); n++)
127
    {
133
    {
128
        map<string, string> attributes = commandNodes[n].getAttributes();
134
        map<string, string> attributes = commandNodes[n].getAttributes();
129
 
135
 
130
        if (attributes["name"].compare(commandName) == 0)
136
        if (attributes["name"].compare(commandName) == 0)
131
        {
137
        {
132
            int commandId = stoi(attributes["id"]);
138
            int commandId = stoi(attributes["id"]);
133
 
139
 
134
            if (commandId >= 128)
140
            if (commandId >= 128)
135
            {
141
            {
136
                if (attributes["module"] == moduleName)
142
                if (attributes["module"] == moduleName)
137
                {
143
                {
138
                    return commandId;
144
                    return commandId;
139
                }
145
                }
140
            }
146
            }
141
            else
147
            else
142
            {
148
            {
143
                return commandId;
149
                return commandId;
144
            }
150
            }
145
        }
151
        }
146
    }
152
    }
147
 
153
 
148
    return -1;
154
    return -1;
149
}
155
}
150
 
156
 
151
string CanIdTranslator::lookupNMTCommandName(int commandId)
157
string CanIdTranslator::lookupNMTCommandName(int commandId)
152
{
158
{
153
    XmlNode commandsNode = xmlNode.findChild("nmt_messages");
159
    XmlNode commandsNode = xmlNode.findChild("nmt_messages");
154
    vector<XmlNode> commandNodes = commandsNode.getChildren();
160
    vector<XmlNode> commandNodes = commandsNode.getChildren();
155
 
161
 
156
    for (int n = 0; n < commandNodes.size(); n++)
162
    for (int n = 0; n < commandNodes.size(); n++)
157
    {
163
    {
158
        map<string, string> attributes = commandNodes[n].getAttributes();
164
        map<string, string> attributes = commandNodes[n].getAttributes();
159
 
165
 
160
        if (stoi(attributes["id"]) == commandId)
166
        if (stoi(attributes["id"]) == commandId)
161
        {
167
        {
162
            return attributes["name"];
168
            return attributes["name"];
163
        }
169
        }
164
    }
170
    }
165
 
171
 
166
    return "";
172
    return "";
167
}
173
}
168
 
174
 
169
int CanIdTranslator::resolveNMTCommandId(string commandName)
175
int CanIdTranslator::resolveNMTCommandId(string commandName)
170
{
176
{
171
    XmlNode commandsNode = xmlNode.findChild("nmt_messages");
177
    XmlNode commandsNode = xmlNode.findChild("nmt_messages");
172
    vector<XmlNode> commandNodes = commandsNode.getChildren();
178
    vector<XmlNode> commandNodes = commandsNode.getChildren();
173
 
179
 
174
    for (int n = 0; n < commandNodes.size(); n++)
180
    for (int n = 0; n < commandNodes.size(); n++)
175
    {
181
    {
176
        map<string, string> attributes = commandNodes[n].getAttributes();
182
        map<string, string> attributes = commandNodes[n].getAttributes();
177
 
183
 
178
        if (attributes["name"].compare(commandName) == 0)
184
        if (attributes["name"].compare(commandName) == 0)
179
        {
185
        {
180
            return stoi(attributes["id"]);
186
            return stoi(attributes["id"]);
181
        }
187
        }
182
    }
188
    }
183
 
189
 
184
    return -1;
190
    return -1;
185
}
191
}
186
 
192
 
187
string CanIdTranslator::getDefineId(string name, string group)
193
string CanIdTranslator::getDefineId(string name, string group)
188
{
194
{
189
    XmlNode definesNode = xmlNode.findChild("defines");
195
    XmlNode definesNode = xmlNode.findChild("defines");
190
    vector<XmlNode> defineNodes = definesNode.getChildren();
196
    vector<XmlNode> defineNodes = definesNode.getChildren();
191
 
197
 
192
    for (int n = 0; n < defineNodes.size(); n++)
198
    for (int n = 0; n < defineNodes.size(); n++)
193
    {
199
    {
194
        map<string, string> attributes = defineNodes[n].getAttributes();
200
        map<string, string> attributes = defineNodes[n].getAttributes();
195
 
201
 
196
        if (attributes["group"] == group && attributes["name"] == name)
202
        if (attributes["group"] == group && attributes["name"] == name)
197
        {
203
        {
198
            return attributes["id"];
204
            return attributes["id"];
199
        }
205
        }
200
    }
206
    }
201
 
207
 
202
    return "";
208
    return "";
203
}
209
}
204
 
210
 
205
string CanIdTranslator::lookupDirectionFlag(int directionFlag)
211
string CanIdTranslator::lookupDirectionFlag(int directionFlag)
206
{
212
{
207
    XmlNode definesNode = xmlNode.findChild("defines");
213
    XmlNode definesNode = xmlNode.findChild("defines");
208
    vector<XmlNode> defineNodes = definesNode.getChildren();
214
    vector<XmlNode> defineNodes = definesNode.getChildren();
209
 
215
 
210
    for (int n = 0; n < defineNodes.size(); n++)
216
    for (int n = 0; n < defineNodes.size(); n++)
211
    {
217
    {
212
        map<string, string> attributes = defineNodes[n].getAttributes();
218
        map<string, string> attributes = defineNodes[n].getAttributes();
213
 
219
 
214
        if (attributes["group"].compare("DirectionFlag") == 0 && stoi(attributes["id"]) == directionFlag)
220
        if (attributes["group"].compare("DirectionFlag") == 0 && stoi(attributes["id"]) == directionFlag)
215
        {
221
        {
216
            return attributes["name"];
222
            return attributes["name"];
217
        }
223
        }
218
    }
224
    }
219
 
225
 
220
    return "";
226
    return "";
Line 222... Line 228...
222
 
228
 
223
int CanIdTranslator::resolveDirectionFlag(string directionName)
229
int CanIdTranslator::resolveDirectionFlag(string directionName)
224
{
230
{
225
    XmlNode definesNode = xmlNode.findChild("defines");
231
    XmlNode definesNode = xmlNode.findChild("defines");
226
    vector<XmlNode> defineNodes = definesNode.getChildren();
232
    vector<XmlNode> defineNodes = definesNode.getChildren();
227
 
233
 
228
    for (int n = 0; n < defineNodes.size(); n++)
234
    for (int n = 0; n < defineNodes.size(); n++)
229
    {
-
 
230
        map<string, string> attributes = defineNodes[n].getAttributes();
-
 
231
 
-
 
232
        if (attributes["group"].compare("DirectionFlag") == 0 && attributes["name"].compare(directionName) == 0)
-
 
233
        {
235
    {
234
            return stoi(attributes["id"]);
236
        map<string, string> attributes = defineNodes[n].getAttributes();
235
        }
-
 
236
    }
-
 
237
 
237
 
-
 
238
        if (attributes["group"].compare("DirectionFlag") == 0 && attributes["name"].compare(directionName) == 0)
-
 
239
        {
-
 
240
            return stoi(attributes["id"]);
-
 
241
        }
-
 
242
    }
-
 
243
 
238
    return -1;
244
    return -1;
239
}
245
}
240
 
246
 
241
string CanIdTranslator::lookupModuleName(int moduleId)
247
string CanIdTranslator::lookupModuleName(int moduleId)
242
{
248
{
243
    XmlNode modulesNode = xmlNode.findChild("modules");
249
    XmlNode modulesNode = xmlNode.findChild("modules");
244
    vector<XmlNode> moduleNodes = modulesNode.getChildren();
250
    vector<XmlNode> moduleNodes = modulesNode.getChildren();
245
 
251
 
246
    for (int n = 0; n < moduleNodes.size(); n++)
252
    for (int n = 0; n < moduleNodes.size(); n++)
247
    {
253
    {
248
        map<string, string> attributes = moduleNodes[n].getAttributes();
254
        map<string, string> attributes = moduleNodes[n].getAttributes();
249
 
255
 
250
        if (stoi(attributes["id"]) == moduleId)
256
        if (stoi(attributes["id"]) == moduleId)
251
        {
257
        {
252
            return attributes["name"];
258
            return attributes["name"];
253
        }
259
        }
254
    }
260
    }
255
 
261
 
256
    return "";
262
    return "";
257
}
263
}
258
 
264
 
259
int CanIdTranslator::resolveModuleId(string moduleName)
265
int CanIdTranslator::resolveModuleId(string moduleName)
260
{
266
{
261
    XmlNode modulesNode = xmlNode.findChild("modules");
267
    XmlNode modulesNode = xmlNode.findChild("modules");
262
    vector<XmlNode> moduleNodes = modulesNode.getChildren();
268
    vector<XmlNode> moduleNodes = modulesNode.getChildren();
263
 
269
 
264
    for (int n = 0; n < moduleNodes.size(); n++)
270
    for (int n = 0; n < moduleNodes.size(); n++)
265
    {
271
    {
266
        map<string, string> attributes = moduleNodes[n].getAttributes();
272
        map<string, string> attributes = moduleNodes[n].getAttributes();
267
 
273
 
268
        if (attributes["name"].compare(moduleName) == 0)
274
        if (attributes["name"].compare(moduleName) == 0)
269
        {
275
        {
270
            return stoi(attributes["id"]);
276
            return stoi(attributes["id"]);
271
        }
277
        }
272
    }
278
    }
273
 
279
 
274
    return -1;
280
    return -1;
Line 337... Line 343...
337
        {
343
        {
338
            data[attributes["name"]].setType(attributes["type"]);
344
            data[attributes["name"]].setType(attributes["type"]);
339
            data[attributes["name"]].setUnit(attributes["unit"]);
345
            data[attributes["name"]].setUnit(attributes["unit"]);
340
            data[attributes["name"]].setBitLength(stoi(attributes["bit_length"]));
346
            data[attributes["name"]].setBitLength(stoi(attributes["bit_length"]));
341
            data[attributes["name"]].setStartBit(stoi(attributes["start_bit"]));
347
            data[attributes["name"]].setStartBit(stoi(attributes["start_bit"]));
-
 
348
 
-
 
349
            if (attributes["type"] == "enum")
-
 
350
            {
-
 
351
                vector<XmlNode> values = variableNodes[c].getChildren();
-
 
352
 
-
 
353
                for (int k = 0; k < values.size(); k++)
-
 
354
                {
-
 
355
                    map<string, string> valueAttributes = values[k].getAttributes();
-
 
356
                    data[attributes["name"]].addEnumValue(valueAttributes["id"], valueAttributes["name"]);
-
 
357
                }
-
 
358
            }
342
        }
359
        }
343
    }
360
    }
344
}
361
}
345
 
362
 
346
string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> &data)
363
string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> &data)
347
{
364
{
348
    makeDataValid(commandId, moduleName, data);
365
    makeDataValid(commandId, moduleName, data);
349
 
366
 
350
    return translateValidDataToHex(data);
367
    return translateValidDataToHex(data);
351
}
368
}
352
 
369
 
353
 
370
 
Line 356... Line 373...
356
    vector<XmlNode> commandNodes = xmlNode.findChild("commands").getChildren();
373
    vector<XmlNode> commandNodes = xmlNode.findChild("commands").getChildren();
357
 
374
 
358
    for (int n = 0; n < commandNodes.size(); n++)
375
    for (int n = 0; n < commandNodes.size(); n++)
359
    {
376
    {
360
        map<string, string> attributes = commandNodes[n].getAttributes();
377
        map<string, string> attributes = commandNodes[n].getAttributes();
361
 
378
 
362
        bool correct = false;
379
        bool correct = false;
363
 
380
 
364
        if (commandId >= 128)
381
        if (commandId >= 128)
365
        {
382
        {
366
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
383
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
367
            {
384
            {
368
                correct = true;
385
                correct = true;
369
            }
386
            }
370
        }
387
        }
371
        else if (stoi(attributes["id"]) == commandId)
388
        else if (stoi(attributes["id"]) == commandId)
372
        {
389
        {
373
            correct = true;
390
            correct = true;
374
        }
391
        }
375
 
392
 
376
        if (correct)
393
        if (correct)
377
        {
394
        {
378
            XmlNode varablesNode = commandNodes[n].findChild("variables");
395
            XmlNode varablesNode = commandNodes[n].findChild("variables");
379
 
396
 
Line 384... Line 401...
384
    map<string, CanVariable> variables;
401
    map<string, CanVariable> variables;
385
    return variables;
402
    return variables;
386
}
403
}
387
 
404
 
388
map<string, CanVariable> CanIdTranslator::translateNMTData(int commandId, string dataHex)
405
map<string, CanVariable> CanIdTranslator::translateNMTData(int commandId, string dataHex)
389
{
406
{
390
    vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren();
407
    vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren();
391
 
408
 
392
    for (int n = 0; n < commandNodes.size(); n++)
409
    for (int n = 0; n < commandNodes.size(); n++)
393
    {
410
    {
394
        map<string, string> attributes = commandNodes[n].getAttributes();
411
        map<string, string> attributes = commandNodes[n].getAttributes();
395
 
412
 
396
        if (stoi(attributes["id"]) == commandId)
413
        if (stoi(attributes["id"]) == commandId)
397
        {
414
        {
398
            XmlNode varablesNode = commandNodes[n].findChild("variables");
415
            XmlNode varablesNode = commandNodes[n].findChild("variables");
399
 
416
 
400
            return translateData(varablesNode.getChildren(), dataHex);
417
            return translateData(varablesNode.getChildren(), dataHex);
401
        }
418
        }
402
    }
419
    }
403
 
420
 
404
    map<string, CanVariable> variables;
421
    map<string, CanVariable> variables;
Line 418... Line 435...
418
    int highestBit = 0;
435
    int highestBit = 0;
419
 
436
 
420
    for (map<string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++)
437
    for (map<string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++)
421
    {
438
    {
422
        if (iter->second.getType() == "uint")
439
        if (iter->second.getType() == "uint")
423
        {
440
        {
424
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
441
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
425
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
442
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
426
 
443
 
427
            unsigned int a = stou(iter->second.getValue());
444
            unsigned int a = stou(iter->second.getValue());
428
 
445
 
429
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));
446
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));
430
        }
447
        }
431
        else if (iter->second.getType() == "float")
448
        else if (iter->second.getType() == "float")
432
        {
449
        {
433
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
450
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
434
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
451
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
435
 
452
 
436
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength()));
453
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength()));
-
 
454
        }
-
 
455
        else if (iter->second.getType() == "enum")
-
 
456
        {
-
 
457
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
-
 
458
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
-
 
459
 
-
 
460
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(stou(iter->second.getEnumIdValue()), iter->second.getBitLength()));
-
 
461
 
437
        }
462
        }
438
        else if (iter->second.getType() == "ascii")
463
        else if (iter->second.getType() == "ascii")
439
        {
464
        {
440
            for (int n = 0; n < iter->second.getValue().length(); n++)
465
            for (int n = 0; n < iter->second.getValue().length(); n++)
441
            {
466
            {
Line 513... Line 538...
513
            variable.setValue(utos(bin2uint(bits)));
538
            variable.setValue(utos(bin2uint(bits)));
514
        }
539
        }
515
        else if (attributes["type"] == "float")
540
        else if (attributes["type"] == "float")
516
        {
541
        {
517
            variable.setValue(ftos(bin2float(bits)));
542
            variable.setValue(ftos(bin2float(bits)));
-
 
543
        }
-
 
544
        else if (attributes["type"] == "enum")
-
 
545
        {
-
 
546
            vector<XmlNode> values = variableNodes[c].getChildren();
-
 
547
 
-
 
548
            string value = utos(bin2uint(bits));
-
 
549
 
-
 
550
            for (int k = 0; k < values.size(); k++)
-
 
551
            {
-
 
552
                map<string, string> valueAttributes = values[k].getAttributes();
-
 
553
                variable.addEnumValue(valueAttributes["id"], valueAttributes["name"]);
-
 
554
 
-
 
555
                if (valueAttributes["id"] == value)
-
 
556
                {
-
 
557
                    variable.setValue(valueAttributes["name"]);
-
 
558
                }
-
 
559
            }
518
        }
560
        }
519
        else if (attributes["type"] == "ascii")
561
        else if (attributes["type"] == "ascii")
520
        {
562
        {
521
            string str;
563
            string str;
522
 
564