Skip to content

Commit 1476e5e

Browse files
authored
Merge pull request #10 from DevifyPlatform/master
Release v3.0.0
2 parents f99b775 + 7fe1a30 commit 1476e5e

File tree

15 files changed

+174
-113
lines changed

15 files changed

+174
-113
lines changed

coap-broker-server-events.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

coap-broker-server.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
11
var server = require('./libs/coap-broker');
2-
server.start();
2+
3+
var onmessage = function(payload) {
4+
var obj = JSON.parse(payload.data);
5+
var paths = payload.pathname.split('/');
6+
var deviceId = paths[2];
7+
8+
console.log('[', deviceId, ']', payload.data);
9+
};
10+
11+
var onnewthing = function(thing) {
12+
var data = JSON.stringify(thing);
13+
14+
console.log('<NEW_THING> ' + data);
15+
};
16+
17+
server.start({
18+
onmessage: onmessage,
19+
onnewthing: onnewthing
20+
});

libs/coap-broker.js

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,16 @@ var coapHandlers = {
6060
* Prototype and Class
6161
*/
6262
var Server = function () {
63-
63+
this.server = null;
64+
this.callbacks = {
65+
ondata: function() { return 0; },
66+
onnewthing: function() { return 0; },
67+
onstart: function() { return 0; }
68+
};
6469
};
6570

6671
/**
67-
* The server event handlers
72+
* Event callback factory
6873
*/
6974
Server.prototype.onNewThing = function(thing) {
7075
// at this server, the thing description is included via a local file
@@ -74,16 +79,14 @@ Server.prototype.onNewThing = function(thing) {
7479

7580
// register a new thing to WoT framework
7681
this.registerThing(thing);
77-
78-
if (typeof(this._options.onnewthing) === 'function') {
79-
this._options.onnewthing(thing);
80-
}
82+
this.callbacks['onnewthing'](thing);
8183
};
8284

85+
/**
86+
* Event callback factory
87+
*/
8388
Server.prototype.onData = function(payload) {
84-
if (typeof(this._options.onmessage) === 'function') {
85-
this._options.onmessage(payload);
86-
}
89+
this.callbacks['ondata'](payload);
8790

8891
// Send hardware data to FBP network.
8992
var data = {
@@ -94,6 +97,13 @@ Server.prototype.onData = function(payload) {
9497
this._network.send(data);
9598
};
9699

100+
/**
101+
* Event callback factory
102+
*/
103+
Server.prototype.onStart = function(payload) {
104+
this.callbacks['onstart'](payload);
105+
};
106+
97107
/**
98108
* Create an WoT server.
99109
*
@@ -121,11 +131,14 @@ Server.prototype.start = function(options) {
121131
var host = process.env.HOST || 'localhost';
122132
var options = options || {};
123133

124-
for (var prop in options) {
125-
if (options.hasOwnProperty(prop)
126-
&& typeof(this._options[prop]) === 'undefined')
127-
this._options[prop] = options[prop];
128-
}
134+
if (options && options.ondata && typeof options.ondata === 'function')
135+
this.callbacks['ondata'] = options.ondata;
136+
137+
if (options && options.onnewthing && typeof options.onnewthing === 'function')
138+
this.callbacks['onnewthing'] = options.onnewthing;
139+
140+
if (options && options.onstart && typeof options.onstart === 'function')
141+
this.callbacks['onstart'] = options.onstart;
129142

130143
// Load components
131144
this._network.load(this._options.components || {});
@@ -139,20 +152,32 @@ Server.prototype.start = function(options) {
139152
});
140153
var router = new Router();
141154

142-
// Thing events from WoT framework
155+
// Events callback factory
143156
server.on('newThing', this.onNewThing.bind(this));
144157
server.on('data', this.onData.bind(this));
158+
server.on('start', this.onStart.bind(this));
145159

146160
server.start(router.route, coapHandlers);
161+
162+
this.server = server;
147163
};
148164

165+
/**
166+
* Shutdown the CoAP server.
167+
*
168+
* @param cb {Function} The complete callback
169+
* @return {}
170+
* @api public
171+
*/
172+
Server.prototype.shutdown = function(cb) {
173+
if (this.server)
174+
this.server.shutdown(cb);
175+
}
176+
149177
/**
150178
* Create the server instance.
151179
*/
152-
var coapBrokerImpl = createServer({
153-
events: {
154-
}
155-
});
180+
var coapBrokerImpl = createServer({});
156181

157182
/**
158183
* Combined server with framework instance.

libs/coap-to-websocket-broker.js renamed to libs/coap-proxy-websocket.js

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,16 @@ var coapHandlers = {
6060
* Prototype and Class
6161
*/
6262
var Server = function () {
63-
63+
this.server = null;
64+
this.callbacks = {
65+
ondata: function() { return 0; },
66+
onnewthing: function() { return 0; },
67+
onstart: function() { return 0; }
68+
};
6469
};
6570

6671
/**
67-
* The server event handlers
72+
* Event callback factory
6873
*/
6974
Server.prototype.onNewThing = function(thing) {
7075
// at this server, the thing description is included via a local file
@@ -75,15 +80,14 @@ Server.prototype.onNewThing = function(thing) {
7580
// register a new thing to WoT framework
7681
this.registerThing(thing);
7782

78-
if (typeof(this._options.onnewthing) === 'function') {
79-
this._options.onnewthing(thing);
80-
}
83+
this.callbacks['onnewthing'](thing);
8184
};
8285

86+
/**
87+
* Event callback factory
88+
*/
8389
Server.prototype.onData = function(payload) {
84-
if (typeof(this._options.onmessage) === 'function') {
85-
this._options.onmessage(payload);
86-
}
90+
this.callbacks['ondata'](payload);
8791

8892
// Send hardware data to FBP network.
8993
var data = {
@@ -94,6 +98,13 @@ Server.prototype.onData = function(payload) {
9498
this._network.send(data);
9599
};
96100

101+
/**
102+
* Event callback factory
103+
*/
104+
Server.prototype.onStart = function(payload) {
105+
this.callbacks['onstart'](payload);
106+
};
107+
97108
/**
98109
* Create an WoT server.
99110
*
@@ -122,11 +133,14 @@ Server.prototype.start = function(options) {
122133
var endpoint = process.env.ENDPOINT || 'wot.city';
123134
var options = options || {};
124135

125-
for (var prop in options) {
126-
if (options.hasOwnProperty(prop)
127-
&& typeof(this._options[prop]) === 'undefined')
128-
this._options[prop] = options[prop];
129-
}
136+
if (options && options.ondata && typeof options.ondata === 'function')
137+
this.callbacks['ondata'] = options.ondata;
138+
139+
if (options && options.onnewthing && typeof options.onnewthing === 'function')
140+
this.callbacks['onnewthing'] = options.onnewthing;
141+
142+
if (options && options.onstart && typeof options.onstart === 'function')
143+
this.callbacks['onstart'] = options.onstart;
130144

131145
// Load components
132146
this._network.load(this._options.components || {});
@@ -137,24 +151,38 @@ Server.prototype.start = function(options) {
137151
var server = new CoapBroker({
138152
port: port,
139153
host: host,
140-
endpoint: endpoint
154+
endpoint: [
155+
'localhost:8000'
156+
]
141157
});
142158
var router = new Router();
143159

144-
// Thing events from WoT framework
160+
// Events callback factory
145161
server.on('newThing', this.onNewThing.bind(this));
146162
server.on('data', this.onData.bind(this));
163+
server.on('start', this.onStart.bind(this));
147164

148165
server.start(router.route, coapHandlers);
166+
167+
this.server = server;
149168
};
150169

170+
/**
171+
* Shutdown the Websocket server.
172+
*
173+
* @param cb {Function} The complete callback
174+
* @return {}
175+
* @api public
176+
*/
177+
Server.prototype.shutdown = function(cb) {
178+
if (this.server)
179+
this.server.shutdown(cb);
180+
}
181+
151182
/**
152183
* Create the server instance.
153184
*/
154-
var coapBrokerImpl = createServer({
155-
events: {
156-
}
157-
});
185+
var coapBrokerImpl = createServer({});
158186

159187
/**
160188
* Combined server with framework instance.

libs/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
*
2727
*/
2828

29-
var coapBroker = require('./coap-broker')
30-
, coapToWebsocketBroker = require("./coap-to-websocket-broker")
31-
, websocketBroker = require("./websocket-broker");
29+
var CoapBroker = require('./coap-broker')
30+
, WebsocketBroker = require("./websocket-broker")
31+
, CoapToWebsocketProxy = require("./coap-proxy-websocket");
3232

3333
module.exports = {
34-
coapBroker: coapBroker,
35-
coapToWebsocketBroker: coapToWebsocketBroker,
36-
websocketBroker: websocketBroker
34+
CoapBroker: CoapBroker,
35+
WebsocketBroker: WebsocketBroker,
36+
CoapToWebsocketProxy: CoapToWebsocketProxy
3737
};

libs/websocket-broker.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,16 @@ var wsHandlers = {
6060
* Prototype and Class
6161
*/
6262
var Server = function () {
63-
this.server = null;
63+
this.server = null;
64+
this.callbacks = {
65+
ondata: function() { return 0; },
66+
onnewthing: function() { return 0; },
67+
onstart: function() { return 0; }
68+
};
6469
};
6570

6671
/**
67-
* The server event handlers
72+
* Event callback factory
6873
*/
6974
Server.prototype.onNewThing = function(thing) {
7075
// at this server, the thing description is included via a local file
@@ -75,15 +80,14 @@ Server.prototype.onNewThing = function(thing) {
7580
// register a new thing to WoT framework
7681
this.registerThing(thing);
7782

78-
if (typeof(this._options.onnewthing) === 'function') {
79-
this._options.onnewthing(thing);
80-
}
83+
this.callbacks['onnewthing'](thing);
8184
};
8285

86+
/**
87+
* Event callback factory
88+
*/
8389
Server.prototype.onData = function(payload) {
84-
if (typeof(this._options.onmessage) === 'function') {
85-
this._options.onmessage(payload);
86-
}
90+
this.callbacks['ondata'](payload);
8791

8892
// Send hardware data to FBP network.
8993
var data = {
@@ -94,6 +98,13 @@ Server.prototype.onData = function(payload) {
9498
this._network.send(data);
9599
};
96100

101+
/**
102+
* Event callback factory
103+
*/
104+
Server.prototype.onStart = function(payload) {
105+
this.callbacks['onstart'](payload);
106+
};
107+
97108
/**
98109
* Create an WoT server.
99110
*
@@ -121,11 +132,14 @@ Server.prototype.start = function(options) {
121132
var host = process.env.HOST || 'localhost';
122133
var options = options || {};
123134

124-
for (var prop in options) {
125-
if (options.hasOwnProperty(prop)
126-
&& typeof(this._options[prop]) === 'undefined')
127-
this._options[prop] = options[prop];
128-
}
135+
if (options && options.ondata && typeof options.ondata === 'function')
136+
this.callbacks['ondata'] = options.ondata;
137+
138+
if (options && options.onnewthing && typeof options.onnewthing === 'function')
139+
this.callbacks['onnewthing'] = options.onnewthing;
140+
141+
if (options && options.onstart && typeof options.onstart === 'function')
142+
this.callbacks['onstart'] = options.onstart;
129143

130144
// Load components
131145
this._network.load(this._options.components || {});
@@ -139,9 +153,10 @@ Server.prototype.start = function(options) {
139153
});
140154
var router = new WebsocketRouter();
141155

142-
// Thing events from WoT framework
156+
// Events callback factory
143157
server.on('newThing', this.onNewThing.bind(this));
144158
server.on('data', this.onData.bind(this));
159+
server.on('start', this.onStart.bind(this));
145160

146161
server.start(router.route, wsHandlers);
147162

@@ -156,10 +171,7 @@ Server.prototype.shutdown = function(cb) {
156171
/**
157172
* Create the server instance.
158173
*/
159-
var wsBrokerImpl = createServer({
160-
events: {
161-
}
162-
});
174+
var wsBrokerImpl = createServer({});
163175

164176
/**
165177
* Combined server with framework instance.

0 commit comments

Comments
 (0)