Skip to content

Commit

Permalink
feat: add JS variant of LoraMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
joscha committed Aug 15, 2016
1 parent bdb0d64 commit 3259c7d
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 2 deletions.
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

17 changes: 17 additions & 0 deletions example/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../.eslintrc.js",
"rules": {
"no-unused-vars": 0
},
"globals": {
"encode": false,
"decode": false,
"LoraMessage": false,
"unixtime": false,
"latLng": false,
"uint16": false,
"uint8": false,
"temperature": false,
"humidity": false,
}
}
File renamed without changes.
13 changes: 13 additions & 0 deletions example/main_encoder_LoraMessage.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <LoraMessage.h>

void setup() {
LoraMessage message;
message.addUnixtime(1468075322);
message.addLatLng(-33.905024, 151.26648);

do_send(message.getLength(), message.getBytes());
}

void loop(void) {
os_runloop_once();
}
2 changes: 1 addition & 1 deletion example/ttn_decoder.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function(bytes) {
function ttn_decoder(bytes) {
// bytes is of type Buffer

// IMPORTANT: paste code from src/decoder.js here
Expand Down
8 changes: 8 additions & 0 deletions example/ttn_encoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function ttn_encoder() {
// IMPORTANT: paste code from src/encoder.js here

return encode(
[ Date.now() / 1000, [-33.905052, 151.26641] ],
[ unixtime, latLng ]
);
}
8 changes: 8 additions & 0 deletions example/ttn_encoder_LoraMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function ttn_encoder() {
// IMPORTANT: paste code from src/encoder.js here
// IMPORTANT: paste code from src/LoraMessage.js here

return new LoraMessage()
.addUnixtime(Date.now() / 1000)
.addLatLng(-33.905052, 151.26641);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"scripts": {
"lint": "eslint .",
"tdd": "mocha --watch",
"test": "mocha"
},
"keywords": [
Expand Down
65 changes: 65 additions & 0 deletions src/LoraMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
(function(root) {
function LoraMessage(encoder) {
this.dataTuples = [];
this.encoder = encoder || root;
}

LoraMessage.prototype.addTuple = function(data, fnName) {
this.dataTuples.push({
data: data,
fn: this.encoder[fnName]
});
};


LoraMessage.prototype.addUnixtime = function(unixtime) {
this.addTuple([unixtime], 'unixtime');
return this;
};

LoraMessage.prototype.addLatLng = function(latitude, longitude) {
this.addTuple([latitude, longitude], 'latLng');
return this;
};

LoraMessage.prototype.addUint16 = function(uint16) {
this.addTuple([uint16], 'uint16');
return this;
};

LoraMessage.prototype.addTemperature = function(temperature) {
this.addTuple([temperature], 'temperature');
return this;
};

LoraMessage.prototype.addUint8 = function(uint8) {
this.addTuple([uint8], 'uint8');
return this;
};

LoraMessage.prototype.addHumidity = function(humidity) {
this.addTuple([humidity], 'humidity');
return this;
};

LoraMessage.prototype.getBytes = function() {
var buffer = new Buffer(this.getLength());
var offset = 0;
this.dataTuples.forEach(function(tuple) {
var current = tuple.fn.apply(null, tuple.data);
current.copy(buffer, offset);
offset += tuple.fn.BYTES;
});
return buffer;
};

LoraMessage.prototype.getLength = function() {
return this.dataTuples.reduce(function(previous, tuple) {
return previous + tuple.fn.BYTES;
}, 0);
};

if (typeof module === 'object' && typeof module.exports !== 'undefined') {
module.exports = LoraMessage;
}
})(this);
38 changes: 38 additions & 0 deletions test/spec.LoraMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const path = require('path');
const chai = require('chai');
const encoder = require(path.join(__dirname, '..', 'src', 'encoder.js'));
const base = require('./base.js');
const LoraMessage = require(path.join(__dirname, '..', 'src', 'LoraMessage.js'));

chai.should();

describe('LoraMessage', () => {

it('should be possible to construct a simple message', () => {
const m = new LoraMessage(encoder);
m.addUnixtime(base.unixtime);
m.getLength().should.equal(4);
m.getBytes().should.deep.equal(base.unixtimeBytes);
});

it('should be possible to chain message parts', () => {
new LoraMessage(encoder)
.addLatLng(base.latLng[0], base.latLng[1])
.addUnixtime(base.unixtime)
.addUint16(base.uint16)
.addTemperature(base.temp)
.addUint8(base.uint8)
.addHumidity(base.humidity)
.getBytes()
.should.be.deep.equal(
Buffer.concat([
base.latLngBytes,
base.unixtimeBytes,
base.uint16Bytes,
base.tempBytes,
base.uint8Bytes,
base.humidityBytes
])
);
});
});

0 comments on commit 3259c7d

Please sign in to comment.