Skip to content

Commit

Permalink
Fixed parsing consumer information
Browse files Browse the repository at this point in the history
  • Loading branch information
sebi2k1 committed Aug 3, 2016
1 parent f36419c commit e562603
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
45 changes: 23 additions & 22 deletions parse_kcd.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ exports.parseKcdFile = function(file) {
for (m in d['Bus'][b]['Message']) {
var message = d['Bus'][b]['Message'][m]['$'];
var producers = d['Bus'][b]['Message'][m]['Producer'];
var consumers = d['Bus'][b]['Message'][m]['Consumer'];

var multiplex = d['Bus'][b]['Message'][m]['Multiplex'];

Expand All @@ -102,23 +101,9 @@ exports.parseKcdFile = function(file) {
if (result.nodes[id])
{
if (result.nodes[id].buses[bus['name']] == undefined)
result.nodes[id].buses[bus['name']] = { produces: [], consume: []}
result.nodes[id].buses[bus['name']] = { produces: [], consumes: []}

result.nodes[id].buses[bus['name']].produces.push(_m);
}
}
}
// Add listeners / targets for the message.
for (c in consumers) {
for (n in consumers[c]['NodeRef']) {
var id = consumers[c]['NodeRef'][n]['$']['id'];

if (result.nodes[id])
{
if (result.nodes[id].buses[bus['name']] == undefined)
result.nodes[id].buses[bus['name']] = { produces: [], consume: []}

result.nodes[id].buses[bus['name']].consumes.push(_m);
result.nodes[id].buses[bus['name']].produces.push(_m.id);
}
}
}
Expand Down Expand Up @@ -162,10 +147,10 @@ exports.parseKcdFile = function(file) {
}

// add label sets from the database.
if( Array.isArray( labelset )){
if (Array.isArray(labelset)) {
var labels = labelset[0]['Label'];
for ( var i = 0 ; i < labels.length; i++ ){
_s.labels[labels[i]['$'].value] = labels[i]['$'].name ;
for ( var i = 0 ; i < labels.length; i++ ) {
_s.labels[labels[i]['$'].value] = labels[i]['$'].name;
}
}

Expand All @@ -177,7 +162,6 @@ exports.parseKcdFile = function(file) {
_s.bitOffset = parseInt(signal.offset);

_m.signals.push(_s);

}
}

Expand All @@ -187,6 +171,7 @@ exports.parseKcdFile = function(file) {
var signal = d['Bus'][b]['Message'][m]['Signal'][s]['$'];
var value = d['Bus'][b]['Message'][m]['Signal'][s]['Value'];
var labelset = d['Bus'][b]['Message'][m]['Signal'][s]['LabelSet'];
var consumers = d['Bus'][b]['Message'][m]['Signal'][s]['Consumer'];

var _s = {
name: signal.name,
Expand All @@ -207,12 +192,28 @@ exports.parseKcdFile = function(file) {
}

// add label sets from the database.
if( Array.isArray(labelset) ) {
if (Array.isArray(labelset)) {
var labels = labelset[0]['Label'];
for ( var i = 0 ; i < labels.length; i++ ){
_s.labels[labels[i]['$'].value] = labels[i]['$'].name;
}
}

// Add listeners / targets for the message.
for (c in consumers) {
for (n in consumers[c]['NodeRef']) {
var id = consumers[c]['NodeRef'][n]['$']['id'];

if (result.nodes[id])
{
if (result.nodes[id].buses[bus['name']] == undefined)
result.nodes[id].buses[bus['name']] = { produces: [], consumes: []}

result.nodes[id].buses[bus['name']].consumes.push({ id: _m.id, signal_name: _s.name });
}
}
}

var offset_num = parseInt(signal.offset) + _s.bitLength;

if (offset_num > maxOffset)
Expand Down
7 changes: 6 additions & 1 deletion tests/samples.kcd
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<NetworkDefinition>
<Node id="12" name="BodyComputer"/>

<Bus name="Motor">
<Message id="0x37F" name="CruiseControlStatus">
<Signal name="SpeedKm" offset="0" length="8" endianess="little">
<Consumer>
<NodeRef id="12"/>
</Consumer>
<Value type="signed"/>
</Signal>
</Message>
</Message>
</Bus>
</NetworkDefinition>

Expand Down
10 changes: 10 additions & 0 deletions tests/test-parsing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var can = require('socketcan');

exports['test_sample_consumes'] = function(test) {
// Parse database
var network = can.parseNetworkDescription("./tests/samples.kcd");

test.equals(network.nodes['12'].buses['Motor'].consumes[0].id, 895);

test.done();
}

0 comments on commit e562603

Please sign in to comment.