Skip to content

Commit

Permalink
Ensure unix timestamp for measuredAt (#240)
Browse files Browse the repository at this point in the history
Ensure the measuredAt timestamp is an Unix Timestamp (milliseconds)
  • Loading branch information
Adrien Maret committed Dec 16, 2022
1 parent 92343d3 commit 6718883
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
11 changes: 9 additions & 2 deletions features/Decoder/PayloadController.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Feature: Payloads Controller
| engineId | null |
| assetId | null |

Scenario: Reject if measuredAt is not unix timestamp
Given I try to send the following "dummy-temp" payloads:
| deviceEUI | temperature | measuredAt |
| "12345" | 21 | 1671007889 |
Then I should receive an error matching:
| message | "Invalid payload: \"measuredAt\" should be a timestamp in milliseconds" |

Scenario: Reject with error a DummyTemp payload
Given I try to send the following "dummy-temp" payloads:
| deviceEUI | temperature |
Expand Down Expand Up @@ -61,12 +68,12 @@ Feature: Payloads Controller
And I should receive a result matching:
| hits[0]._source.type | "temperature" |
| hits[0]._source.measuredAt | "_DATE_NOW_" |
| hits[0]._source.origin._id | "DummyTemp-linked1" |
| hits[0]._source.origin._id | "DummyTemp-linked1" |
| hits[0]._source.origin.type | "device" |
| hits[0]._source.origin.measureName | "temperature" |
| hits[0]._source.origin.deviceModel | "DummyTemp" |
| hits[0]._source.origin.reference | "linked1" |
| hits[0]._source.asset._id | "container-linked1" |
| hits[0]._source.asset._id | "container-linked1" |
| hits[0]._source.asset.measureName | "temperatureExt" |
| hits[0]._source.asset.metadata.weight | 10 |
| hits[0]._source.asset.metadata.height | 11 |
Expand Down
2 changes: 1 addition & 1 deletion features/fixtures/application/decoders/DummyTempDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class DummyTempDecoder extends Decoder {
payload.deviceEUI,
"temperature",
{
measuredAt: Date.now(),
measuredAt: payload.measuredAt || Date.now(),
type: "temperature",
values: {
temperature: payload.temperature,
Expand Down
13 changes: 13 additions & 0 deletions lib/modules/decoder/DecodedPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ export class DecodedPayload<TDecoder extends Decoder = Decoder> {
);
}

this.validateMeasurement(measurement);

if (!this.measurementsByDevice[deviceReference]) {
this.measurementsByDevice[deviceReference] = [];
}

const decodedMeasurement: DecodedMeasurement<TMeasureValues> = {
measureName,
...measurement,
Expand Down Expand Up @@ -91,4 +94,14 @@ export class DecodedPayload<TDecoder extends Decoder = Decoder> {
getMetadata(deviceReference: string): JSONObject {
return this.metadataByDevice[deviceReference];
}

private validateMeasurement<TMeasureValues>(
measurement: Omit<DecodedMeasurement<TMeasureValues>, "measureName">
) {
if (measurement.measuredAt.toString().length !== 13) {
throw new BadRequestError(
`Invalid payload: "measuredAt" should be a timestamp in milliseconds`
);
}
}
}

0 comments on commit 6718883

Please sign in to comment.