Subversion Repositories HomeAutomation

Rev

Rev 981 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 981 Rev 986
Line 40... Line 40...
40
    {
40
    {
41
        //slog << "Malformed can message received from canDaemon: \"" << rawHex << "\"\n";
41
        //slog << "Malformed can message received from canDaemon: \"" << rawHex << "\"\n";
42
        throw new CanMessageException("Malformed can message received from canDaemon");
42
        throw new CanMessageException("Malformed can message received from canDaemon");
43
    }
43
    }
44
 
44
 
45
    myRawHeaderBin = hex2bin(parts[1]);
45
    string rawHeaderBin = hex2bin(parts[1]);
46
 
46
 
47
    myIsHeartbeat = (parts[1] == translator.getDefineId("HeartbeatHeader", "None"));
47
    myClassName = translator.lookupClassName(bin2uint(rawHeaderBin.substr(3, 4)));
48
 
48
 
49
    // This is not a module id header, but heartbeats are sent this way so we want to check it anyway
-
 
50
    if (myIsHeartbeat)
-
 
51
    {
-
 
52
        string rawHexData = "";
-
 
53
        for (int n = 4; n < parts.size(); n++)
-
 
54
        {
-
 
55
            rawHexData += parts[n];
-
 
56
        }
-
 
57
 
-
 
58
        myData = translator.translateHeartbeatData(rawHexData);
-
 
59
    }
-
 
60
    else
-
 
61
    {
-
 
62
        myClassName = translator.lookupClassName(bin2uint(myRawHeaderBin.substr(3, 4)));
-
 
63
 
-
 
64
        if (myClassName == "")
49
    if (myClassName == "")
65
        {
50
    {
66
            myIsUnknown = true;
51
        myIsUnknown = true;
67
            return;
52
        return;
68
        }
53
    }
69
 
-
 
70
        myDirectionFlag = translator.lookupDirectionFlag(bin2uint(myRawHeaderBin.substr(7, 1)));
-
 
71
        myModuleName = translator.lookupModuleName(bin2uint(myRawHeaderBin.substr(8, 8)));
-
 
72
 
-
 
73
        myModuleId = bin2uint(myRawHeaderBin.substr(16, 8));
-
 
74
 
-
 
75
        int commandId = bin2uint(myRawHeaderBin.substr(24, 8));
-
 
76
        myCommandName = translator.lookupCommandName(commandId, myModuleName);
-
 
77
 
54
 
78
        string rawHexData = "";
55
    string rawHexData = "";
79
        for (int n = 4; n < parts.size(); n++)
56
    for (int n = 4; n < parts.size(); n++)
80
        {
57
    {
81
            rawHexData += parts[n];
58
        rawHexData += parts[n];
82
        }
59
    }
-
 
60
 
-
 
61
    if (myClassName == "nmt")
-
 
62
    {
-
 
63
        int commandId = bin2uint(rawHeaderBin.substr(8, 8));
-
 
64
        myCommandName = translator.lookupNMTCommandName(commandId);
-
 
65
       
-
 
66
        myData = translator.translateNMTData(commandId, rawHexData);
-
 
67
    }
-
 
68
    else
-
 
69
    {
-
 
70
        myDirectionFlag = translator.lookupDirectionFlag(bin2uint(rawHeaderBin.substr(7, 1)));
-
 
71
        myModuleName = translator.lookupModuleName(bin2uint(rawHeaderBin.substr(8, 8)));
-
 
72
 
-
 
73
        myModuleId = bin2uint(rawHeaderBin.substr(16, 8));
-
 
74
 
-
 
75
        int commandId = bin2uint(rawHeaderBin.substr(24, 8));
-
 
76
        myCommandName = translator.lookupCommandName(commandId, myModuleName);
83
       
77
 
84
        myData = translator.translateData(commandId, myModuleName, rawHexData);
78
        myData = translator.translateData(commandId, myModuleName, rawHexData);
85
    }
79
    }
86
}
80
}
87
 
81
 
88
string CanMessage::getRaw()
82
string CanMessage::getRaw()
89
{
83
{
90
    CanIdTranslator &translator = CanIdTranslator::getInstance();
84
    CanIdTranslator &translator = CanIdTranslator::getInstance();
-
 
85
 
-
 
86
    string dataHex;
91
 
87
 
92
    string rawHex = "PKT ";
88
    string rawHex = "PKT ";
93
 
89
 
94
    string bin = "000";
90
    string bin = "000";
95
 
91
 
Line 97... Line 93...
97
    if (classId == -1)
93
    if (classId == -1)
98
        bin += "0000";
94
        bin += "0000";
99
    else
95
    else
100
        bin += uint2bin(classId, 4);
96
        bin += uint2bin(classId, 4);
101
 
97
 
-
 
98
    if (myClassName == "nmt")
-
 
99
    {
-
 
100
        bin +="0";
102
 
101
 
-
 
102
        int commandNameId = translator.resolveNMTCommandId(myCommandName);
-
 
103
        if (commandNameId == -1)
-
 
104
            bin += "00000000";
-
 
105
        else
-
 
106
            bin += uint2bin(commandNameId, 8);
-
 
107
 
-
 
108
        bin += "0000000000000000";
-
 
109
 
-
 
110
        dataHex = translator.translateNMTDataToHex(commandNameId, myData);
-
 
111
    }
-
 
112
    else
-
 
113
    {
103
    bin += uint2bin(translator.resolveDirectionFlag(myDirectionFlag), 1);
114
        bin += uint2bin(translator.resolveDirectionFlag(myDirectionFlag), 1);
104
 
115
 
105
 
116
 
106
    int moduleTypeId = translator.resolveModuleId(myModuleName);
117
        int moduleTypeId = translator.resolveModuleId(myModuleName);
107
    if (moduleTypeId == -1)
118
        if (moduleTypeId == -1)
Line 118... Line 129...
118
    if (commandNameId == -1)
129
        if (commandNameId == -1)
119
        bin += "00000000";
130
            bin += "00000000";
120
    else
131
        else
121
        bin += uint2bin(commandNameId, 8);
132
            bin += uint2bin(commandNameId, 8);
122
 
133
 
-
 
134
        dataHex = translator.translateDataToHex(commandNameId, myModuleName, myData);
123
 
135
    }
124
   
136
 
125
    rawHex += bin2hex(bin);
137
    rawHex += bin2hex(bin);
126
   
138
 
127
    rawHex += " 1 0";
139
    rawHex += " 1 0";
128
 
140
 
129
    string dataHex = translator.translateDataToHex(commandNameId, myModuleName, myData);
-
 
130
 
-
 
131
    while (dataHex.size() > 0)
141
    while (dataHex.size() > 0)
132
    {
142
    {
133
        string hex = dataHex.substr(0, 2);
143
        string hex = dataHex.substr(0, 2);
134
        dataHex.erase(0, 2);
144
        dataHex.erase(0, 2);
135
        rawHex += " " + hex;
145
        rawHex += " " + hex;
136
    }
146
    }
137
 
147
 
138
    rawHex += "\n";
148
    rawHex += "\n";
139
 
149
 
140
    return rawHex;
150
    return rawHex;
141
}
-
 
142
 
-
 
143
void CanMessage::setJSON(string json)
-
 
144
{
-
 
145
    cout << json << "\n";
-
 
146
    string data = trim(json, '(');
-
 
147
    data = trim(data, ')');
-
 
148
    data = trim(data, '{');
-
 
149
    data = trim(data, '{');
-
 
150
    data = trim(data, ' ');
-
 
151
 
-
 
152
    vector<string> parts = explode(",", data);
-
 
153
 
-
 
154
    for (int n = 0; n < parts.size(); n++)
-
 
155
    {
-
 
156
        string part = trim(parts[n], ' ');
-
 
157
        vector<string> innerParts = explode(":", part);
-
 
158
 
-
 
159
        int c = 0;
-
 
160
        if (innerParts[0] == "data")
-
 
161
            c++;
-
 
162
 
-
 
163
        string name = trim(innerParts[c], ' ');
-
 
164
        name = trim(name, '{');
-
 
165
        name = trim(name, '}');
-
 
166
        string value = trim(innerParts[c+1], ' ');
-
 
167
        value = trim(value, '{');
-
 
168
        value = trim(value, '}');
-
 
169
        value = trim(value, '"');
-
 
170
       
-
 
171
        cout << "\n" << name << "=" << value << "\n";
-
 
172
 
-
 
173
        if (name == "className")
-
 
174
            myClassName = value;
-
 
175
        else if (name == "directionFlag")
-
 
176
            myDirectionFlag = value;
-
 
177
        else if (name == "moduleName")
-
 
178
            myModuleName = value;
-
 
179
        else if (name == "moduleId")
-
 
180
            myModuleId = stoi(value);
-
 
181
        else if (name == "commandName")
-
 
182
            myCommandName = value;
-
 
183
        else
-
 
184
        {
-
 
185
            CanVariable canVariable(name, value, "", "");
-
 
186
            myData[name] = canVariable;
-
 
187
        }
-
 
188
    }
-
 
189
}
-
 
190
 
-
 
191
string CanMessage::getJSON()
-
 
192
{
-
 
193
    string json = "{ ";
-
 
194
 
-
 
195
    json += "className : '" + myClassName + "', ";
-
 
196
    json += "directionFlag : '" + myDirectionFlag + "', ";
-
 
197
    json += "moduleName : '" + myModuleName + "', ";
-
 
198
    json += "moduleId : " + itos(myModuleId) + ", ";
-
 
199
    json += "commandName : '" + myCommandName + "', ";
-
 
200
 
-
 
201
    json += "data : {";
-
 
202
 
-
 
203
    map<string, CanVariable>::iterator iter;
-
 
204
 
-
 
205
    for (iter = myData.begin(); iter != myData.end(); iter++)
-
 
206
    {
-
 
207
        json += " " + iter->first + " : { ";
-
 
208
 
-
 
209
        json += "unit : '" + iter->second.getUnit() + "', ";
-
 
210
        json += "type : '" + iter->second.getType() + "', ";
-
 
211
        json += "value : ";
-
 
212
 
-
 
213
        if (iter->second.getType() == "uint")
-
 
214
        {
-
 
215
            json += iter->second.getValue();
-
 
216
        }
-
 
217
        else if (iter->second.getType() == "float")
-
 
218
        {
-
 
219
            json += iter->second.getValue();
-
 
220
        }
-
 
221
        else if (iter->second.getType() == "ascii" || iter->second.getType() == "hexstring")
-
 
222
        {
-
 
223
            json += "'" + iter->second.getValue() + "'";
-
 
224
        }
-
 
225
 
-
 
226
        json += " },";
-
 
227
    }
-
 
228
 
-
 
229
    json = trim(json, ',');
-
 
230
 
-
 
231
    json += " }";
-
 
232
 
-
 
233
    return json + " }";
-
 
234
}
151
}
235
 
152
 
236
string CanMessage::getJSONData()
153
string CanMessage::getJSONData()
237
{
154
{
238
    string json = "{";
155
    string json = "{";