Skip to content

Commit

Permalink
Merge pull request particle-iot#16 from AntonPuko/flowClean
Browse files Browse the repository at this point in the history
Flow clean
  • Loading branch information
jlkalberer authored Jan 3, 2017
2 parents fc9eaf1 + 5f4adff commit 05919ad
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 72 deletions.
41 changes: 20 additions & 21 deletions lib/clients/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <http://www.gnu.org/licenses/>.
*
*
*
*/

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 <http://www.gnu.org/licenses/>.
*
*
*
*/

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.
Expand Down
5 changes: 2 additions & 3 deletions lib/lib/EventPublisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)();
Expand Down
61 changes: 32 additions & 29 deletions lib/server/DeviceServer_v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -297,45 +297,39 @@ 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;


if (global.api) {
global.api.safeMode(deviceID, systemMessage.getPayload().toString());
}

case 22:
case 21:
if (!lowerEventName.match('spark')) {
_context4.next = 29;
_context4.next = 28;
break;
}

Expand All @@ -355,19 +349,19 @@ var DeviceServer = function () {
}

if (!eatMessage) {
_context4.next = 29;
_context4.next = 28;
break;
}

// short-circuit
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();
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
});
Expand Down
1 change: 1 addition & 0 deletions src/clients/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
6 changes: 3 additions & 3 deletions src/lib/EventPublisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
37 changes: 22 additions & 15 deletions src/server/DeviceServer_v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ class DeviceServer {
start() {
const server = net.createServer(
(socket: Socket): void =>
process.nextTick((): void => this._onNewSocketConnection(socket)),
process.nextTick((): Promise<void> =>
this._onNewSocketConnection(socket),
),
);

server.on('error', (error: Error): void =>
Expand All @@ -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}`),
Expand All @@ -115,7 +117,7 @@ class DeviceServer {

device.on(
DEVICE_EVENT_NAMES.READY,
(): void => this._onDeviceReady(device),
(): Promise<void> => this._onDeviceReady(device),
);

device.on(
Expand All @@ -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<void> =>
this._onDeviceSubscribe(message, device),
);

device.on(
DEVICE_MESSAGE_EVENTS_NAMES.PRIVATE_EVENT,
(message: Message): void =>
(message: Message): Promise<void> =>
this._onDeviceSentMessage(
message,
/* isPublic */false,
Expand All @@ -143,7 +145,7 @@ class DeviceServer {

device.on(
DEVICE_MESSAGE_EVENTS_NAMES.PUBLIC_EVENT,
(message: Message): void =>
(message: Message): Promise<void> =>
this._onDeviceSentMessage(
message,
/* isPublic */true,
Expand All @@ -159,7 +161,7 @@ class DeviceServer {

device.on(
DEVICE_EVENT_NAMES.FLASH_STARTED,
(): void => this.publishSpecialEvent(
(): Promise<void> => this.publishSpecialEvent(
'spark/flash/status',
'started',
device.getID(),
Expand All @@ -168,7 +170,7 @@ class DeviceServer {

device.on(
DEVICE_EVENT_NAMES.FLASH_SUCCESS,
(): void => this.publishSpecialEvent(
(): Promise<void> => this.publishSpecialEvent(
'spark/flash/status',
'success',
device.getID(),
Expand All @@ -177,7 +179,7 @@ class DeviceServer {

device.on(
DEVICE_EVENT_NAMES.FLASH_FAILED,
(): void => this.publishSpecialEvent(
(): Promise<void> => this.publishSpecialEvent(
'spark/flash/status',
'failed',
device.getID(),
Expand Down Expand Up @@ -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,
};


Expand All @@ -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);
// }
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -379,7 +386,7 @@ class DeviceServer {
this._eventPublisher.subscribe(
messageName,
device.onCoreEvent,
/* filterOptions */null,
/* filterOptions */{},
deviceID,
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type DeviceAttributes = {
deviceID: string,
ip: string,
name: string,
claimCode: ?string,
ownerID: ?string,
particleProductId: number,
productFirmwareVersion: number,
Expand All @@ -16,7 +17,7 @@ export type Event = EventData & {
};

export type EventData = {
data: ?Object,
data: ?Object | string,
deviceID?: ?string,
isPublic: boolean,
name: string,
Expand Down

0 comments on commit 05919ad

Please sign in to comment.