diff --git a/lib/clients/Device.js b/lib/clients/Device.js
index 51efae30..6a5a55e0 100644
--- a/lib/clients/Device.js
+++ b/lib/clients/Device.js
@@ -107,27 +107,26 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
// an HTTP callback to a 3rd party
// KeyChange — sent by Server to change the AES credentials
-/*
-* Copyright (c) 2015 Particle Industries, Inc. All rights reserved.
-*
-* This program is free software; you can redistribute it and/or
-* modify it under the terms of the GNU Lesser General Public
-* License as published by the Free Software Foundation, either
-* version 3 of the License, or (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-* Lesser General Public License for more .
-*
-* You should have received a copy of the GNU Lesser General Public
-* License along with this program; if not, see .
-*
-*
-*
-*/
-
-var COUNTER_MAX = _settings2.default.message_counter_max;
+var COUNTER_MAX = _settings2.default.message_counter_max; /*
+ * Copyright (c) 2015 Particle Industries, Inc. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more .
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, see .
+ *
+ *
+ *
+ */
+
var KEEP_ALIVE_TIMEOUT = _settings2.default.keepaliveTimeout;
var SOCKET_TIMEOUT = _settings2.default.socketTimeout;
var MAX_BINARY_SIZE = 108000; // According to the forums this is the max size.
diff --git a/lib/lib/EventPublisher.js b/lib/lib/EventPublisher.js
index 7aa3087e..fa515738 100644
--- a/lib/lib/EventPublisher.js
+++ b/lib/lib/EventPublisher.js
@@ -86,8 +86,7 @@ var EventPublisher = function (_EventEmitter) {
args[_key] = arguments[_key];
}
- return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = EventPublisher.__proto__ || (0, _getPrototypeOf2.default)(EventPublisher)).call.apply(_ref, [this].concat(args))), _this), _this._subscriptionsByID = new _map2.default(), _this._filterEvents = function (eventHandler) {
- var filterOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
+ return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = EventPublisher.__proto__ || (0, _getPrototypeOf2.default)(EventPublisher)).call.apply(_ref, [this].concat(args))), _this), _this._subscriptionsByID = new _map2.default(), _this._filterEvents = function (eventHandler, filterOptions) {
return function (event) {
var userID = filterOptions.userID,
deviceID = filterOptions.deviceID;
@@ -112,7 +111,7 @@ var EventPublisher = function (_EventEmitter) {
}, _this.subscribe = function () {
var eventName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ALL_EVENTS;
var eventHandler = arguments[1];
- var filterOptions = arguments[2];
+ var filterOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var subscriberID = arguments[3];
var subscriptionID = (0, _uuid2.default)();
diff --git a/lib/server/DeviceServer_v2.js b/lib/server/DeviceServer_v2.js
index faf1ad24..595c94e7 100644
--- a/lib/server/DeviceServer_v2.js
+++ b/lib/server/DeviceServer_v2.js
@@ -275,19 +275,19 @@ var DeviceServer = function () {
isPublic: isPublic,
name: message.getUriPath().substr(3),
ttl: message.getMaxAge() > 0 ? message.getMaxAge() : 60,
- userID: deviceAttributes.ownerID
+ userID: deviceAttributes && deviceAttributes.ownerID
};
lowerEventName = eventData.name.toLowerCase();
if (!lowerEventName.match('spark/device/claim/code')) {
- _context4.next = 12;
+ _context4.next = 11;
break;
}
claimCode = message.getPayload().toString();
- if (!(deviceAttributes.claimCode !== claimCode)) {
- _context4.next = 12;
+ if (!(deviceAttributes && deviceAttributes.claimCode !== claimCode)) {
+ _context4.next = 11;
break;
}
@@ -297,35 +297,29 @@ var DeviceServer = function () {
}));
case 11:
- // todo figure this out
- if (global.api) {
- global.api.linkDevice(deviceID, claimCode, _this._particleProductId);
- }
-
- case 12:
if (!lowerEventName.match('spark/device/system/version')) {
- _context4.next = 16;
+ _context4.next = 15;
break;
}
deviceSystemVersion = message.getPayload().toString();
- _context4.next = 16;
+ _context4.next = 15;
return _this._deviceAttributeRepository.update((0, _extends3.default)({}, deviceAttributes, {
// TODO should it be this key?:
spark_system_version: deviceSystemVersion
}));
- case 16:
+ case 15:
if (!(lowerEventName.indexOf('spark/device/safemode') === 0)) {
- _context4.next = 22;
+ _context4.next = 21;
break;
}
token = device.sendMessage('Describe');
- _context4.next = 20;
+ _context4.next = 19;
return device.listenFor('DescribeReturn', null, token);
- case 20:
+ case 19:
systemMessage = _context4.sent;
@@ -333,9 +327,9 @@ var DeviceServer = function () {
global.api.safeMode(deviceID, systemMessage.getPayload().toString());
}
- case 22:
+ case 21:
if (!lowerEventName.match('spark')) {
- _context4.next = 29;
+ _context4.next = 28;
break;
}
@@ -355,7 +349,7 @@ var DeviceServer = function () {
}
if (!eatMessage) {
- _context4.next = 29;
+ _context4.next = 28;
break;
}
@@ -363,11 +357,11 @@ var DeviceServer = function () {
device.sendReply('EventAck', message.getId());
return _context4.abrupt('return');
- case 29:
- _context4.next = 31;
+ case 28:
+ _context4.next = 30;
return _this._eventPublisher.publish(eventData);
- case 31:
+ case 30:
case 'end':
return _context4.stop();
}
@@ -412,7 +406,7 @@ var DeviceServer = function () {
_logger2.default.log('Got subscribe request from device with ID ' + deviceID + ' ' + ('on event: \'' + messageName + '\' ') + ('from my devices only: ' + (isFromMyDevices || false)));
if (!isFromMyDevices) {
- _context5.next = 15;
+ _context5.next = 18;
break;
}
@@ -422,20 +416,29 @@ var DeviceServer = function () {
case 11:
deviceAttributes = _context5.sent;
+ if (!(!deviceAttributes || !deviceAttributes.ownerID)) {
+ _context5.next = 15;
+ break;
+ }
+
+ device.sendReply('SubscribeFail', message.getId());
+ return _context5.abrupt('return');
+
+ case 15:
_this._eventPublisher.subscribe(messageName, device.onCoreEvent, { userID: deviceAttributes.ownerID }, deviceID);
- _context5.next = 16;
+ _context5.next = 19;
break;
- case 15:
+ case 18:
_this._eventPublisher.subscribe(messageName, device.onCoreEvent,
- /* filterOptions */null, deviceID);
+ /* filterOptions */{}, deviceID);
- case 16:
+ case 19:
device.sendReply('SubscribeAck', message.getId());
- case 17:
+ case 20:
case 'end':
return _context5.stop();
}
@@ -487,7 +490,7 @@ var DeviceServer = function () {
//
// Wait for the keys to be ready, then start accepting connections
//
- var serverPort = this._config.port;
+ var serverPort = this._config.port.toString();
server.listen(serverPort, function () {
return _logger2.default.log('Server started on port: ' + serverPort);
});
diff --git a/src/clients/Device.js b/src/clients/Device.js
index a0320096..b08441d8 100644
--- a/src/clients/Device.js
+++ b/src/clients/Device.js
@@ -20,6 +20,7 @@
import type { Socket } from 'net';
import type { Duplex } from 'stream';
+import type { Event } from '../types';
import EventEmitter from 'events';
import moment from 'moment';
diff --git a/src/lib/EventPublisher.js b/src/lib/EventPublisher.js
index 3e5e7ec2..68339589 100644
--- a/src/lib/EventPublisher.js
+++ b/src/lib/EventPublisher.js
@@ -45,7 +45,7 @@ class EventPublisher extends EventEmitter {
_filterEvents = (
eventHandler: (event: Event) => void,
- filterOptions: FilterOptions = {},
+ filterOptions: FilterOptions,
): (event: Event) => void =>
(event: Event) => {
const { userID, deviceID } = filterOptions;
@@ -77,9 +77,9 @@ class EventPublisher extends EventEmitter {
subscribe = (
- eventName: ?string = ALL_EVENTS,
+ eventName: string = ALL_EVENTS,
eventHandler: (event: Event) => void,
- filterOptions?: FilterOptions,
+ filterOptions?: FilterOptions = {},
subscriberID?: string,
): void => {
const subscriptionID = uuid();
diff --git a/src/server/DeviceServer_v2.js b/src/server/DeviceServer_v2.js
index efb55907..95e7a22c 100644
--- a/src/server/DeviceServer_v2.js
+++ b/src/server/DeviceServer_v2.js
@@ -75,7 +75,9 @@ class DeviceServer {
start() {
const server = net.createServer(
(socket: Socket): void =>
- process.nextTick((): void => this._onNewSocketConnection(socket)),
+ process.nextTick((): Promise =>
+ this._onNewSocketConnection(socket),
+ ),
);
server.on('error', (error: Error): void =>
@@ -100,7 +102,7 @@ class DeviceServer {
//
// Wait for the keys to be ready, then start accepting connections
//
- const serverPort = this._config.port;
+ const serverPort = this._config.port.toString();
server.listen(
serverPort,
(): void => logger.log(`Server started on port: ${serverPort}`),
@@ -115,7 +117,7 @@ class DeviceServer {
device.on(
DEVICE_EVENT_NAMES.READY,
- (): void => this._onDeviceReady(device),
+ (): Promise => this._onDeviceReady(device),
);
device.on(
@@ -127,13 +129,13 @@ class DeviceServer {
// TODO figure out is this message for subscriptions on public events or
// public + private
DEVICE_MESSAGE_EVENTS_NAMES.SUBSCRIBE,
- (message: Message): void =>
+ (message: Message): Promise =>
this._onDeviceSubscribe(message, device),
);
device.on(
DEVICE_MESSAGE_EVENTS_NAMES.PRIVATE_EVENT,
- (message: Message): void =>
+ (message: Message): Promise =>
this._onDeviceSentMessage(
message,
/* isPublic */false,
@@ -143,7 +145,7 @@ class DeviceServer {
device.on(
DEVICE_MESSAGE_EVENTS_NAMES.PUBLIC_EVENT,
- (message: Message): void =>
+ (message: Message): Promise =>
this._onDeviceSentMessage(
message,
/* isPublic */true,
@@ -159,7 +161,7 @@ class DeviceServer {
device.on(
DEVICE_EVENT_NAMES.FLASH_STARTED,
- (): void => this.publishSpecialEvent(
+ (): Promise => this.publishSpecialEvent(
'spark/flash/status',
'started',
device.getID(),
@@ -168,7 +170,7 @@ class DeviceServer {
device.on(
DEVICE_EVENT_NAMES.FLASH_SUCCESS,
- (): void => this.publishSpecialEvent(
+ (): Promise => this.publishSpecialEvent(
'spark/flash/status',
'success',
device.getID(),
@@ -177,7 +179,7 @@ class DeviceServer {
device.on(
DEVICE_EVENT_NAMES.FLASH_FAILED,
- (): void => this.publishSpecialEvent(
+ (): Promise => this.publishSpecialEvent(
'spark/flash/status',
'failed',
device.getID(),
@@ -265,7 +267,7 @@ class DeviceServer {
isPublic,
name: message.getUriPath().substr(3),
ttl: message.getMaxAge() > 0 ? message.getMaxAge() : 60,
- userID: deviceAttributes.ownerID,
+ userID: deviceAttributes && deviceAttributes.ownerID,
};
@@ -274,15 +276,15 @@ class DeviceServer {
if (lowerEventName.match('spark/device/claim/code')) {
const claimCode = message.getPayload().toString();
- if (deviceAttributes.claimCode !== claimCode) {
+ if (deviceAttributes && deviceAttributes.claimCode !== claimCode) {
await this._deviceAttributeRepository.update({
...deviceAttributes,
claimCode,
});
// todo figure this out
- if (global.api) {
- global.api.linkDevice(deviceID, claimCode, this._particleProductId);
- }
+ // if (global.api) {
+ // global.api.linkDevice(deviceID, claimCode, this._particleProductId);
+ // }
}
}
@@ -369,6 +371,11 @@ class DeviceServer {
const deviceAttributes =
await this._deviceAttributeRepository.getById(deviceID);
+ if (!deviceAttributes || !deviceAttributes.ownerID) {
+ device.sendReply('SubscribeFail', message.getId());
+ return;
+ }
+
this._eventPublisher.subscribe(
messageName,
device.onCoreEvent,
@@ -379,7 +386,7 @@ class DeviceServer {
this._eventPublisher.subscribe(
messageName,
device.onCoreEvent,
- /* filterOptions */null,
+ /* filterOptions */{},
deviceID,
);
}
diff --git a/src/types.js b/src/types.js
index 87f8da3d..652937aa 100644
--- a/src/types.js
+++ b/src/types.js
@@ -4,6 +4,7 @@ export type DeviceAttributes = {
deviceID: string,
ip: string,
name: string,
+ claimCode: ?string,
ownerID: ?string,
particleProductId: number,
productFirmwareVersion: number,
@@ -16,7 +17,7 @@ export type Event = EventData & {
};
export type EventData = {
- data: ?Object,
+ data: ?Object | string,
deviceID?: ?string,
isPublic: boolean,
name: string,