Subversion Repositories HomeAutomation

Rev

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

Rev 1046 Rev 1048
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 "canmessageexception.h"
-
 
23
 
21
 
24
 
22
#include "canvariable.h"
25
#include "canvariable.h"
23
 
26
 
24
 
27
 
25
#include <vector>
28
#include <vector>
Line 30... Line 33...
30
CanIdTranslator* CanIdTranslator::myInstance = NULL;
33
CanIdTranslator* CanIdTranslator::myInstance = NULL;
31
 
34
 
32
CanIdTranslator& CanIdTranslator::getInstance()
35
CanIdTranslator& CanIdTranslator::getInstance()
33
{
36
{
34
    if (myInstance == NULL)
37
    if (myInstance == NULL)
35
    {
38
    {
36
        myInstance = new CanIdTranslator();
39
        myInstance = new CanIdTranslator();
37
    }
40
    }
38
 
41
 
39
    return *myInstance;
42
    return *myInstance;
40
}
43
}
Line 44... Line 47...
44
    delete myInstance;
47
    delete myInstance;
45
    myInstance = NULL;
48
    myInstance = NULL;
46
}
49
}
47
 
50
 
48
CanIdTranslator::CanIdTranslator()
51
CanIdTranslator::CanIdTranslator()
49
{
52
{
50
    Logger &log = Logger::getInstance();
53
    Logger &log = Logger::getInstance();
51
 
54
 
52
    string filename = Settings::get("CanIdXMLFile");
55
    string filename = Settings::get("CanIdXMLFile");
53
 
56
 
54
    if (file_exists(filename))
57
    if (file_exists(filename))
Line 59... Line 62...
59
    }
62
    }
60
    else
63
    else
61
    {
64
    {
62
        log.add("CanIdXMLFile is not defined in the config file, this will probably not work at all.\n");
65
        log.add("CanIdXMLFile is not defined in the config file, this will probably not work at all.\n");
63
    }
66
    }
64
}
67
}
65
 
68
 
66
string CanIdTranslator::lookupClassName(int classId)
69
string CanIdTranslator::lookupClassName(int classId)
67
{
70
{
68
    XmlNode classesNode = xmlNode.findChild("classes");
71
    XmlNode classesNode = xmlNode.findChild("classes");
69
    vector<XmlNode> classNodes = classesNode.getChildren();
72
    vector<XmlNode> classNodes = classesNode.getChildren();
70
 
73
 
71
    for (int n = 0; n < classNodes.size(); n++)
74
    for (int n = 0; n < classNodes.size(); n++)
72
    {
75
    {
73
        map<string, string> attributes = classNodes[n].getAttributes();
76
        map<string, string> attributes = classNodes[n].getAttributes();
74
 
77
 
75
        if (stoi(attributes["id"]) == classId)
78
        if (stoi(attributes["id"]) == classId)
76
        {
79
        {
77
            return attributes["name"];
80
            return attributes["name"];
78
        }
81
        }
79
    }
82
    }
80
 
83
 
81
    return "";
84
    return "";
82
}
85
}
83
 
86
 
84
int CanIdTranslator::resolveClassId(string className)
87
int CanIdTranslator::resolveClassId(string className)
85
{
88
{
86
    XmlNode classesNode = xmlNode.findChild("classes");
89
    XmlNode classesNode = xmlNode.findChild("classes");
87
    vector<XmlNode> classNodes = classesNode.getChildren();
90
    vector<XmlNode> classNodes = classesNode.getChildren();
88
 
91
 
89
    for (int n = 0; n < classNodes.size(); n++)
92
    for (int n = 0; n < classNodes.size(); n++)
90
    {
93
    {
91
        map<string, string> attributes = classNodes[n].getAttributes();
94
        map<string, string> attributes = classNodes[n].getAttributes();
92
 
95
 
93
        if (attributes["name"].compare(className) == 0)
96
        if (attributes["name"].compare(className) == 0)
94
        {
97
        {
95
            return stoi(attributes["id"]);
98
            return stoi(attributes["id"]);
96
        }
99
        }
97
    }
100
    }
98
 
101
 
99
    return -1;
102
    return -1;
100
}
103
}
101
 
104
 
102
string CanIdTranslator::lookupCommandName(int commandId, string moduleName)
105
string CanIdTranslator::lookupCommandName(int commandId, string moduleName)
103
{
106
{
104
    XmlNode commandsNode = xmlNode.findChild("commands");
107
    XmlNode commandsNode = xmlNode.findChild("commands");
105
    vector<XmlNode> commandNodes = commandsNode.getChildren();
108
    vector<XmlNode> commandNodes = commandsNode.getChildren();
106
 
109
 
107
    for (int n = 0; n < commandNodes.size(); n++)
110
    for (int n = 0; n < commandNodes.size(); n++)
108
    {
111
    {
109
        map<string, string> attributes = commandNodes[n].getAttributes();
112
        map<string, string> attributes = commandNodes[n].getAttributes();
110
 
113
 
111
        if (commandId >= 128)
114
        if (commandId >= 128)
112
        {
115
        {
113
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
116
            if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)
114
            {
117
            {
115
                return attributes["name"];
118
                return attributes["name"];
116
            }
119
            }
117
        }
120
        }
118
        else if (stoi(attributes["id"]) == commandId)
121
        else if (stoi(attributes["id"]) == commandId)
119
        {
122
        {
120
            return attributes["name"];
123
            return attributes["name"];
121
        }
124
        }
122
    }
125
    }
123
 
126
 
124
    return "";
127
    return "";
125
}
128
}
126
 
129
 
127
int CanIdTranslator::resolveCommandId(string commandName, string moduleName)
130
int CanIdTranslator::resolveCommandId(string commandName, string moduleName)
128
{
131
{
129
    XmlNode commandsNode = xmlNode.findChild("commands");
132
    XmlNode commandsNode = xmlNode.findChild("commands");
130
    vector<XmlNode> commandNodes = commandsNode.getChildren();
133
    vector<XmlNode> commandNodes = commandsNode.getChildren();
131
 
134
 
132
    for (int n = 0; n < commandNodes.size(); n++)
135
    for (int n = 0; n < commandNodes.size(); n++)
133
    {
136
    {
134
        map<string, string> attributes = commandNodes[n].getAttributes();
137
        map<string, string> attributes = commandNodes[n].getAttributes();
135
 
138
 
136
        if (attributes["name"].compare(commandName) == 0)
139
        if (attributes["name"].compare(commandName) == 0)
137
        {
140
        {
138
            int commandId = stoi(attributes["id"]);
141
            int commandId = stoi(attributes["id"]);
139
 
142
 
140
            if (commandId >= 128)
143
            if (commandId >= 128)
141
            {
144
            {
142
                if (attributes["module"] == moduleName)
145
                if (attributes["module"] == moduleName)
143
                {
146
                {
144
                    return commandId;
147
                    return commandId;
145
                }
148
                }
146
            }
149
            }
147
            else
150
            else
148
            {
151
            {
149
                return commandId;
152
                return commandId;
150
            }
153
            }
151
        }
154
        }
152
    }
155
    }
153
 
156
 
154
    return -1;
157
    return -1;
155
}
158
}
156
 
159
 
157
string CanIdTranslator::lookupNMTCommandName(int commandId)
160
string CanIdTranslator::lookupNMTCommandName(int commandId)
158
{
161
{
159
    XmlNode commandsNode = xmlNode.findChild("nmt_messages");
162
    XmlNode commandsNode = xmlNode.findChild("nmt_messages");
160
    vector<XmlNode> commandNodes = commandsNode.getChildren();
163
    vector<XmlNode> commandNodes = commandsNode.getChildren();
161
 
164
 
162
    for (int n = 0; n < commandNodes.size(); n++)
165
    for (int n = 0; n < commandNodes.size(); n++)
163
    {
166
    {
164
        map<string, string> attributes = commandNodes[n].getAttributes();
167
        map<string, string> attributes = commandNodes[n].getAttributes();
Line 173... Line 176...
173
}
176
}
174
 
177
 
175
int CanIdTranslator::resolveNMTCommandId(string commandName)
178
int CanIdTranslator::resolveNMTCommandId(string commandName)
176
{
179
{
177
    XmlNode commandsNode = xmlNode.findChild("nmt_messages");
180
    XmlNode commandsNode = xmlNode.findChild("nmt_messages");
178
    vector<XmlNode> commandNodes = commandsNode.getChildren();
181
    vector<XmlNode> commandNodes = commandsNode.getChildren();
179
 
182
 
180
    for (int n = 0; n < commandNodes.size(); n++)
183
    for (int n = 0; n < commandNodes.size(); n++)
181
    {
184
    {
182
        map<string, string> attributes = commandNodes[n].getAttributes();
185
        map<string, string> attributes = commandNodes[n].getAttributes();
183
 
186
 
184
        if (attributes["name"].compare(commandName) == 0)
187
        if (attributes["name"].compare(commandName) == 0)
185
        {
188
        {
186
            return stoi(attributes["id"]);
189
            return stoi(attributes["id"]);
187
        }
190
        }
188
    }
191
    }
189
 
192
 
190
    return -1;
193
    return -1;
191
}
194
}
192
 
195
 
193
string CanIdTranslator::getDefineId(string name, string group)
196
string CanIdTranslator::getDefineId(string name, string group)
194
{
197
{
195
    XmlNode definesNode = xmlNode.findChild("defines");
198
    XmlNode definesNode = xmlNode.findChild("defines");
196
    vector<XmlNode> defineNodes = definesNode.getChildren();
199
    vector<XmlNode> defineNodes = definesNode.getChildren();
197
 
200
 
198
    for (int n = 0; n < defineNodes.size(); n++)
201
    for (int n = 0; n < defineNodes.size(); n++)
199
    {
202
    {
200
        map<string, string> attributes = defineNodes[n].getAttributes();
203
        map<string, string> attributes = defineNodes[n].getAttributes();
201
 
204
 
202
        if (attributes["group"] == group && attributes["name"] == name)
205
        if (attributes["group"] == group && attributes["name"] == name)
203
        {
206
        {
204
            return attributes["id"];
207
            return attributes["id"];
205
        }
208
        }
206
    }
209
    }
207
 
210
 
208
    return "";
211
    return "";
209
}
212
}
210
 
213
 
211
string CanIdTranslator::lookupDirectionFlag(int directionFlag)
214
string CanIdTranslator::lookupDirectionFlag(int directionFlag)
212
{
215
{
213
    XmlNode definesNode = xmlNode.findChild("defines");
216
    XmlNode definesNode = xmlNode.findChild("defines");
214
    vector<XmlNode> defineNodes = definesNode.getChildren();
217
    vector<XmlNode> defineNodes = definesNode.getChildren();
215
 
218
 
216
    for (int n = 0; n < defineNodes.size(); n++)
219
    for (int n = 0; n < defineNodes.size(); n++)
217
    {
220
    {
218
        map<string, string> attributes = defineNodes[n].getAttributes();
221
        map<string, string> attributes = defineNodes[n].getAttributes();
219
 
222
 
220
        if (attributes["group"].compare("DirectionFlag") == 0 && stoi(attributes["id"]) == directionFlag)
223
        if (attributes["group"].compare("DirectionFlag") == 0 && stoi(attributes["id"]) == directionFlag)
221
        {
224
        {
222
            return attributes["name"];
225
            return attributes["name"];
223
        }
226
        }
224
    }
227
    }
225
 
228
 
226
    return "";
229
    return "";
227
}
230
}
228
 
231
 
229
int CanIdTranslator::resolveDirectionFlag(string directionName)
232
int CanIdTranslator::resolveDirectionFlag(string directionName)
230
{
233
{
231
    XmlNode definesNode = xmlNode.findChild("defines");
234
    XmlNode definesNode = xmlNode.findChild("defines");
232
    vector<XmlNode> defineNodes = definesNode.getChildren();
235
    vector<XmlNode> defineNodes = definesNode.getChildren();
233
 
236
 
234
    for (int n = 0; n < defineNodes.size(); n++)
237
    for (int n = 0; n < defineNodes.size(); n++)
235
    {
238
    {
236
        map<string, string> attributes = defineNodes[n].getAttributes();
239
        map<string, string> attributes = defineNodes[n].getAttributes();
237
 
240
 
238
        if (attributes["group"].compare("DirectionFlag") == 0 && attributes["name"].compare(directionName) == 0)
241
        if (attributes["group"].compare("DirectionFlag") == 0 && attributes["name"].compare(directionName) == 0)
239
        {
242
        {
240
            return stoi(attributes["id"]);
243
            return stoi(attributes["id"]);
241
        }
244
        }
242
    }
245
    }
243
 
246
 
244
    return -1;
247
    return -1;
245
}
248
}
246
 
249
 
247
string CanIdTranslator::lookupModuleName(int moduleId)
250
string CanIdTranslator::lookupModuleName(int moduleId)
248
{
251
{
249
    XmlNode modulesNode = xmlNode.findChild("modules");
252
    XmlNode modulesNode = xmlNode.findChild("modules");
250
    vector<XmlNode> moduleNodes = modulesNode.getChildren();
253
    vector<XmlNode> moduleNodes = modulesNode.getChildren();
251
 
254
 
252
    for (int n = 0; n < moduleNodes.size(); n++)
255
    for (int n = 0; n < moduleNodes.size(); n++)
253
    {
256
    {
254
        map<string, string> attributes = moduleNodes[n].getAttributes();
257
        map<string, string> attributes = moduleNodes[n].getAttributes();
255
 
258
 
256
        if (stoi(attributes["id"]) == moduleId)
259
        if (stoi(attributes["id"]) == moduleId)
257
        {
260
        {
Line 287... Line 290...
287
    for (int n = 0; n < commandNodes.size(); n++)
290
    for (int n = 0; n < commandNodes.size(); n++)
288
    {
291
    {
289
        map<string, string> attributes = commandNodes[n].getAttributes();
292
        map<string, string> attributes = commandNodes[n].getAttributes();
290
 
293
 
291
        if (attributes["name"] == commandName)
294
        if (attributes["name"] == commandName)
292
        {
295
        {
293
            XmlNode varablesNode = commandNodes[n].findChild("variables");
296
            XmlNode varablesNode = commandNodes[n].findChild("variables");
294
 
297
 
295
            makeDataValid(varablesNode.getChildren(), data);
298
            makeDataValid(varablesNode.getChildren(), data);
296
 
299
 
297
            return;
300
            return;
298
        }
301
        }
299
    }
302
    }
300
}
303
}
301
 
304
 
Line 401... Line 404...
401
    map<string, CanVariable> variables;
404
    map<string, CanVariable> variables;
402
    return variables;
405
    return variables;
403
}
406
}
404
 
407
 
405
map<string, CanVariable> CanIdTranslator::translateNMTData(int commandId, string dataHex)
408
map<string, CanVariable> CanIdTranslator::translateNMTData(int commandId, string dataHex)
406
{
409
{
407
    vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren();
410
    vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren();
408
 
411
 
409
    for (int n = 0; n < commandNodes.size(); n++)
412
    for (int n = 0; n < commandNodes.size(); n++)
410
    {
413
    {
411
        map<string, string> attributes = commandNodes[n].getAttributes();
414
        map<string, string> attributes = commandNodes[n].getAttributes();
Line 413... Line 416...
413
        if (stoi(attributes["id"]) == commandId)
416
        if (stoi(attributes["id"]) == commandId)
414
        {
417
        {
415
            XmlNode varablesNode = commandNodes[n].findChild("variables");
418
            XmlNode varablesNode = commandNodes[n].findChild("variables");
416
 
419
 
417
            return translateData(varablesNode.getChildren(), dataHex);
420
            return translateData(varablesNode.getChildren(), dataHex);
418
        }
421
        }
419
    }
422
    }
420
 
423
 
421
    map<string, CanVariable> variables;
424
    map<string, CanVariable> variables;
422
    return variables;
425
    return variables;
423
}
426
}
424
 
427
 
425
string CanIdTranslator::translateNMTDataToHex(string commandName, map<string, CanVariable> &data)
428
string CanIdTranslator::translateNMTDataToHex(string commandName, map<string, CanVariable> &data)
Line 428... Line 431...
428
 
431
 
429
    return translateValidDataToHex(data);
432
    return translateValidDataToHex(data);
430
}
433
}
431
 
434
 
432
string CanIdTranslator::translateValidDataToHex(map<string, CanVariable> &data)
435
string CanIdTranslator::translateValidDataToHex(map<string, CanVariable> &data)
433
{
436
{
434
    string bin = "0000000000000000000000000000000000000000000000000000000000000000";
437
    string bin = "0000000000000000000000000000000000000000000000000000000000000000";
435
    int highestBit = 0;
438
    int highestBit = 0;
436
 
439
 
437
    for (map<string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++)
440
    for (map<string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++)
438
    {
441
    {
439
        if (iter->second.getType() == "uint")
442
        if (iter->second.getType() == "uint")
440
        {
443
        {
441
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
444
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
442
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
445
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
443
 
446
 
444
            unsigned int a = stou(iter->second.getValue());
447
            unsigned int a = stou(iter->second.getValue());
445
 
448
 
446
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));
449
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));
447
        }
450
        }
448
        else if (iter->second.getType() == "float")
451
        else if (iter->second.getType() == "float")
449
        {
452
        {
450
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
453
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
451
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
454
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
452
 
455
 
453
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength()));
456
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength()));
454
        }
457
        }
455
        else if (iter->second.getType() == "enum")
458
        else if (iter->second.getType() == "enum")
456
        {
459
        {
457
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
460
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
458
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
461
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
459
 
462
 
460
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(stou(iter->second.getEnumIdValue()), iter->second.getBitLength()));
463
            string value = iter->second.getEnumIdValue();
461
 
464
 
-
 
465
            if (value == "")
-
 
466
            {
-
 
467
                throw new CanMessageException("Got enum that could not be converterd to a value. value = \"" + iter->second.getValue() + "\" enumString is \"" + iter->second.getEnumsString() + "\"\n");
-
 
468
            }
-
 
469
            else
-
 
470
            {
-
 
471
                bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(stou(value), iter->second.getBitLength()));
-
 
472
            }
462
        }
473
        }
463
        else if (iter->second.getType() == "ascii")
474
        else if (iter->second.getType() == "ascii")
464
        {
475
        {
465
            for (int n = 0; n < iter->second.getValue().length(); n++)
476
            for (int n = 0; n < iter->second.getValue().length(); n++)
466
            {
477
            {