Skip to content

Commit

Permalink
Merge pull request #760 from telefonicaid/prelanding/fix-binary-data
Browse files Browse the repository at this point in the history
Prelanding/fix binary data
  • Loading branch information
fgalan authored Oct 6, 2023
2 parents 555a350 + 4352f18 commit 18d3568
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Fix: binary data representation when sending data through HTTP & MQTT (#690)
- Fix: ensure service and subservice from device in logs about error proccesing message
- Remove: legacy code about unused parsedMessageError flag
- Remove: legacy code about unused parsedMessageError flag
4 changes: 2 additions & 2 deletions docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ This product is a FIWARE Generic Enabler. If you would like to learn about the o

### Introduction

This IoT Agent uses the [FIWARE IoT Agent Node.js Library](https://github.com/telefonicaid/iotagent-node-lib) framework.
This is why most of the functionalities of this component depend on the new features of the library itself.
This IoT Agent uses the [FIWARE IoT Agent Node.js Library](https://github.com/telefonicaid/iotagent-node-lib) framework.
This is why most of the functionalities of this component depend on the new features of the library itself.

For this reason, the roadmap of this component would be defined by the IoT Agent Node Library Roadmap that you can check
[here](https://github.com/telefonicaid/iotagent-node-lib/blob/master/doc/roadmap.md)
18 changes: 17 additions & 1 deletion docs/usermanual.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,23 @@ It is possible to send a single measure to IoT Platform using an HTTP POST reque
`/iot/json/attrs/<attributeName>` and the previously explained query parameters.

In this case, sending a single measure, there is possible to send other kinds of payloads like `text/plain` and
`application/octet-stream`, not just `application/json`
`application/octet-stream`, not just `application/json`. In case of using `application/octet-stream`, data will be
treated as binary data, saved in the attribute maped as hex string. I.E:

For a measure sent to `/iot/json/attrs/attrHex` with value `hello` the resulting attribute value persisten in the context
broker will be:

```json
{
...
"attrHex":"68656c6c6f"
...
}
```

where every group of 2 character (I.E, the first group, `68`) corresponds to a single ASCII character or byte received in
the payload (in this case, the value `0x68` corresponds to `h` in ASCII). You can use one of the multiple tools available
online like [this one](https://string-functions.com/string-hex.aspx)

#### Configuration retrieval

Expand Down
6 changes: 5 additions & 1 deletion lib/bindings/HTTPBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ function parseData(req, res, next) {
next(error);
} else {
req.jsonPayload = data;

// This is just for log data
if (req.body !== undefined ) {
data = data.toString('hex');
}
config.getLogger().debug(context, 'Parsed data: [%j]', data);
next();
}
Expand Down Expand Up @@ -290,6 +293,7 @@ function handleIncomingMeasure(req, res, next) {

if (req.attr && req.jsonPayload) {
config.getLogger().debug(context, 'Parsing attr [%s] with value [%s]', req.attr, req.jsonPayload);
req.jsonPayload = req.jsonPayload.toString('hex');
const theAttr = [{ name: req.attr, value: req.jsonPayload, type: 'None' }];
attributeArr.push(theAttr);
} else {
Expand Down
20 changes: 20 additions & 0 deletions test/unit/ngsiv2/MQTT_receive_measures-test2.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,24 @@ describe('MQTT: Measure reception ', function () {
});
});
});
describe('When a POST single Raw measure arrives for the HTTP binding', function () {
beforeEach(function () {
contextBrokerMock
.matchHeader('fiware-service', 'smartgondor')
.matchHeader('fiware-servicepath', '/gardens')
.post(
'/v2/entities?options=upsert',
utils.readExampleFile('./test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json')
)
.reply(204);
});
it('should send its value to the Context Broker', function (done) {
mqttClient.publish('/1234/MQTT_2/attrs/humidity', 'A$Ci1', null, function (error) {
setTimeout(function () {
contextBrokerMock.done();
done();
}, 100);
});
});
});
});
8 changes: 1 addition & 7 deletions test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
"type":"AnMQTTDevice",
"humidity":{
"type": "degrees",
"value": {
"type": "Buffer",
"data": [
51,
50
]
}
"value": "3332"
}
}
8 changes: 8 additions & 0 deletions test/unit/ngsiv2/contextRequests/singleMeasuresRawTypes1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id":"Second MQTT Device",
"type":"AnMQTTDevice",
"humidity":{
"type": "degrees",
"value": "4124436931"
}
}

0 comments on commit 18d3568

Please sign in to comment.