Skip to content

Commit

Permalink
Merge pull request particle-iot#9 from AntonPuko/knownApps
Browse files Browse the repository at this point in the history
Known apps
  • Loading branch information
jlkalberer authored Dec 20, 2016
2 parents b781360 + 5e87072 commit 1eb69e7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 71 deletions.
51 changes: 11 additions & 40 deletions lib/clients/SparkCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ var SparkCore = function (_EventEmitter) {

_this.onApiMessage = function () {
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3(sender, message) {
var isBusy, result, response, _response, _result, _result2, sendResult, _sendResult, buffer, showSignal, _result3, _result4;
var isBusy, result, response, _response, _result, _result2, sendResult, _sendResult, showSignal, _result3, _result4;

return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
Expand All @@ -319,7 +319,7 @@ var SparkCore = function (_EventEmitter) {

case 4:
_context3.t0 = message.cmd;
_context3.next = _context3.t0 === 'Describe' ? 7 : _context3.t0 === 'GetVar' ? 19 : _context3.t0 === 'SetVar' ? 34 : _context3.t0 === 'CallFn' ? 40 : _context3.t0 === 'UFlash' ? 55 : _context3.t0 === 'FlashKnown' ? 59 : _context3.t0 === 'RaiseHand' ? 71 : _context3.t0 === 'Ping' ? 78 : 82;
_context3.next = _context3.t0 === 'Describe' ? 7 : _context3.t0 === 'GetVar' ? 19 : _context3.t0 === 'SetVar' ? 34 : _context3.t0 === 'CallFn' ? 40 : _context3.t0 === 'UFlash' ? 55 : _context3.t0 === 'RaiseHand' ? 59 : _context3.t0 === 'Ping' ? 66 : 70;
break;

case 7:
Expand Down Expand Up @@ -353,7 +353,7 @@ var SparkCore = function (_EventEmitter) {
});

case 18:
return _context3.abrupt('break', 83);
return _context3.abrupt('break', 71);

case 19:
if (_settings2.default.logApiMessages) {
Expand Down Expand Up @@ -402,7 +402,7 @@ var SparkCore = function (_EventEmitter) {
name: message.name,
result: _result.getPayload().toString()
});
return _context3.abrupt('break', 83);
return _context3.abrupt('break', 71);

case 40:
if (_settings2.default.logApiMessages) {
Expand Down Expand Up @@ -448,50 +448,21 @@ var SparkCore = function (_EventEmitter) {
return _context3.abrupt('return', _context3.sent);

case 59:
if (_settings2.default.logApiMessages) {
_logger2.default.log('FlashKnown', { app: message.app, coreID: _this._coreId });
}

// Responsibility for sanitizing app names lies with API Service
// This includes only allowing apps whose binaries are deployed and
// thus exist

// TODO: this should use a repository to fetch these binary files
_context3.prev = 60;
buffer = _fs2.default.readFileSync('known_firmware/' + message.app + '_' + _settings2.default.environment + '.bin');
_context3.next = 64;
return _this.flashCore(message.args.data, sender);

case 64:
return _context3.abrupt('return', _context3.sent);

case 67:
_context3.prev = 67;
_context3.t4 = _context3['catch'](60);

_logger2.default.log('Error flashing known firmware', { coreID: _this._coreId, error: _context3.t4 });
return _context3.abrupt('return', {
cmd: 'Event',
message: 'Update failed - ' + JSON.stringify(_context3.t4),
name: 'Update'
});

case 71:
if (_settings2.default.logApiMessages) {
_logger2.default.log('SignalCore', { coreID: _this._coreId });
}

showSignal = message.args && message.args.signal;
_context3.next = 75;
_context3.next = 63;
return _this._raiseYourHand(showSignal);

case 75:
case 63:
_result3 = _context3.sent;

_this.sendApiResponse(sender, { cmd: 'RaiseHandReturn', result: _result3 });
return _context3.abrupt('break', 83);
return _context3.abrupt('break', 71);

case 78:
case 66:
if (_settings2.default.logApiMessages) {
_logger2.default.log('Pinged, replying', { coreID: _this._coreId });
}
Expand All @@ -505,15 +476,15 @@ var SparkCore = function (_EventEmitter) {

return _context3.abrupt('return', _result4);

case 82:
case 70:
_this.sendApiResponse(sender, { error: new Error('unknown message') });

case 83:
case 71:
case 'end':
return _context3.stop();
}
}
}, _callee3, _this2, [[8, 15], [20, 29], [41, 50], [60, 67]]);
}, _callee3, _this2, [[8, 15], [20, 29], [41, 50]]);
}));

return function (_x, _x2) {
Expand Down
10 changes: 10 additions & 0 deletions lib/repository/FileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ var FileManager = function () {

return _fs2.default.readFileSync(filePath, 'utf8');
}
}, {
key: 'getFileBuffer',
value: function getFileBuffer(fileName) {
var filePath = _path2.default.join(this._path, fileName);
if (!_fs2.default.existsSync(filePath)) {
return null;
}

return _fs2.default.readFileSync(filePath);
}
}, {
key: 'writeFile',
value: function writeFile(fileName, data) {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-eslint": "^7.1.1",
"babel-plugin-transform-class-properties": "^6.19.0",
"babel-plugin-transform-es2015-destructuring": "^6.19.0",
"babel-plugin-transform-es2015-spread": "^6.8.0",
"babel-plugin-transform-flow-strip-types": "^6.18.0",
"babel-preset-latest": "^6.16.0",
"babel-preset-stage-0": "^6.16.0",
Expand Down
31 changes: 0 additions & 31 deletions src/clients/SparkCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,37 +373,6 @@ class SparkCore extends EventEmitter {
return await this.flashCore(message.args.data, sender);
}

case 'FlashKnown': {
if (settings.logApiMessages) {
logger.log(
'FlashKnown',
{ app: message.app, coreID: this._coreId },
);
}

// Responsibility for sanitizing app names lies with API Service
// This includes only allowing apps whose binaries are deployed and
// thus exist

// TODO: this should use a repository to fetch these binary files
try {
const buffer = fs.readFileSync(
`known_firmware/${message.app}_${settings.environment}.bin`,
);
return await this.flashCore(message.args.data, sender);
} catch (error) {
logger.log(
'Error flashing known firmware',
{ coreID: this._coreId, error },
);
return {
cmd: 'Event',
message: 'Update failed - ' + JSON.stringify(error),
name: 'Update',
};
}
}

case 'RaiseHand': {
if (settings.logApiMessages) {
logger.log('SignalCore', { coreID: this._coreId });
Expand Down
9 changes: 9 additions & 0 deletions src/repository/FileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ class FileManager {
return fs.readFileSync(filePath, 'utf8');
}

getFileBuffer(fileName: string): ?Buffer {
const filePath = path.join(this._path, fileName);
if (!fs.existsSync(filePath)) {
return null;
}

return fs.readFileSync(filePath);
}

writeFile(fileName: string, data: string): void {
fs.writeFileSync(
path.join(this._path, fileName),
Expand Down

0 comments on commit 1eb69e7

Please sign in to comment.