Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
Merge branch 'sc/matrix-protos' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
eighteyes committed Feb 2, 2018
2 parents f0542f2 + 7f355e5 commit 6374731
Show file tree
Hide file tree
Showing 41 changed files with 1,982 additions and 1,961 deletions.
11 changes: 10 additions & 1 deletion DEVELOPERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ To exclude engine-io from the output, do
```
MATRIX_API_SERVER https://dev-api.admobilize.com
MATRIX_STREAMING_SERVER http://dev-mxss.admobilize.com:80
MATRIX_NOMXSS - don't use streaming server
NO_INSTALL - disable auto install from firebase for apps
```

## Device Specific Server Configurations
Expand All @@ -54,7 +56,7 @@ Run with `MATRIX_MODE=service` to have the system look for `apps` and `db` in `v
```
## Start your MATRIX OS with an installed App
Please note that the application must be registered with the infrastructure to launch. Internet connectivity is only required on boot.
Please note that the application must be registered with the infrastructure to launch. Internet connectivity is only required on boot. It can be disconnected, so long as an app doesn't use GRPC services or tries to save data.
```
START_APP=monitor node index.js
```
Expand Down Expand Up @@ -263,6 +265,13 @@ vim -nb or :nbs from inside vim
Be sure to deploy `test/fixtures/test.matrix` to the device you are going to use. You can run `matrix deploy` from inside the folder. We have to install the application vs manually copy it so there are firebase records created.
```
cd test/fixtures/test.matrix/
matrix deploy
cd ../../../
npm test
```
## Maintainers
Sean Canton <[email protected]>
3 changes: 1 addition & 2 deletions apps/lib/led.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ module.exports = function(c) {
var tcColors = _.times('#000000', 35);

if (_.isPlainObject(color)) {
// console.log('o>', color)
if ( color.color.indexOf('rgba') !== -1 ){
if ( _.isString(color.color) && color.color.indexOf('rgba') !== -1 ){
console.log('rgba is not supported. results may vary.');
}
// shape
Expand Down
4 changes: 2 additions & 2 deletions apps/lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var service = function(name, options) {

then: function(cb){
var phraseRegex = self.strictPhraseMatch ? new RegExp(self.phrases.join('|'),'i') : new RegExp('.*?', 'i');
process.on('message', function(data) {
process.on('message', function(data) {
console.log('VOICE SERVICE >> ', data, phraseRegex)
if (data.eventType === 'service-emit' &&
data.engine === self.engine &&
Expand Down Expand Up @@ -227,7 +227,7 @@ var service = function(name, options) {
if (_.isFunction(cb)) {
cb(_.omit(data.payload, 'serviceType', 'engine', 'type'));
} else {
console.log('No callback passed to service>%s.then', self.name);
cb(console.warn('No callback passed to service>%s.then', self.name));
}
}
});
Expand Down
13 changes: 13 additions & 0 deletions apps/lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


module.exports = {
/**
* Is this application configured for this event?
* @param {string} ev - event name to test for
* @return {Boolean} - is in the config
*/
hasEvent: (ev) => {
return ( matrix.config.hasOwnProperty('events') &&
matrix.config.events.indexOf(ev) > -1 )
}
}
28 changes: 22 additions & 6 deletions apps/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ _ = require('lodash');
//needs sudo for audio commands disable until we figure this out
var request = require('request');
var lib = require('./lib');
var util = require('./lib/util.js');

var fs = require('fs');
var Datastore = require('nedb');
Expand Down Expand Up @@ -113,20 +114,34 @@ var matrixDebug = false;
* @param {{}Payload} p
*/
function interAppNotification(appName, eventName, p) {
var payload = p;
var payload = ( _.isUndefined(p) ) ? '' : p;
var type;
var event;


if (arguments.length === 1) {
// global form
type = 'app-message';
payload = arguments[0];
if ( util.hasEvent(appName) ){
// event trigger - matrix.emit('eventName')
log('>>>>>>>>')
event = arguments[0];
} else {
// global trigger - matrix.emit({ foo: bar })
payload = arguments[0];
}
} else if (arguments.length === 2) {
if ( util.hasEvent(appName)){
// event trigger with payload = matrix.emit('eventName', {foo:bar})
type = 'app-message';
event = arguments[0]
} else {
//global message for application = matrix.emit('appName', {foo:bar})
type = 'app-' + appName + '-message';
payload = arguments[1];
}
//app specific
type = 'app-' + appName + '-message';
payload = arguments[1];
} else {
// app specific event namespaced
// app specific event namespaced = matrix.emit('appName','eventName', { foo:bar })
type = 'app-' + appName + '-message';
event = eventName;
}
Expand Down Expand Up @@ -365,6 +380,7 @@ var Matrix = {
},
service: require('./lib/service.js'),
sensor: require('./lib/sensor.js'),
util: require('./lib/util'),
set: require('./lib/setting.js')
};

Expand Down
47 changes: 30 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// Welcome to MatrixOS - A JavaScript environment for IoT Applications
/**
* MATRIX OS - is an orchestration system for IoT applications. It uses ZeroMQ and Protocol buffers to communicate with the hardware layer via MALOS/CORE
* @see https://github.com/matrix-io/matrix-malos-lib MALOS + HAL is otherwise known as CORE
* @see https://github.com/matrix-io/matrix-cli MATRIX CLI enables user configuration via command line
* @see https://matrix-io.github.io/matrix-documentation/ System documentation
*
* We welcome feedback on github or via email. -> [email protected]
*/

var optional = require('optional');
var bleno = optional('bleno');
// bluetooth lib, doesn't work on non bt platforms, so optional
const bleno = optional('bleno');

const mosRepoURL = 'https://raw.githubusercontent.com/matrix-io/matrix-os/master/package.json';
// check gh
Expand All @@ -21,30 +30,34 @@ var checks = {
};

/* GLOBALS */
require('colors');
_ = require('lodash');
async = require('async');
exec = require('child_process').exec;
execSync = require('child_process').execSync;
os = require('os');

require('colors');

var fs = require('fs');

warn = console.log;
log = console.log;
error = console.error;

// local system vars
var fs = require('fs');

// based on NODE_ENV, set sensible defaults
var envSettings = getEnvSettings();
// if NODE_ENV=dev then set sane debug

// if NODE_ENV=dev then set sane debug settings
if (envSettings.debug === true && !_.has(process.env, 'DEBUG')) {
process.env.DEBUG = '*,-engine*,-Component*,-*led*,-gatt,-bleno,-bt-characteristic,-hci';
// process.env.DEBUG = '*,-engine*';
}

// system wide debug setup function
debugLog = require('debug');
debug = debugLog('matrix');

// debug for this file
const debug = debugLog('matrix');

// Core Library - Creates Matrix.device, Matrix.event, Matrix.service
Matrix = require('./lib/index.js');
Expand Down Expand Up @@ -94,7 +107,6 @@ for (let i = 0; i < 24; i++) {
Matrix.dailyDPCount[i] = 0;
}

debug('', 'ENV:'.grey, Matrix.env.blue, 'API:'.grey, Matrix.apiServer.blue, 'MXSS:'.grey, Matrix.streamingServer.blue);
debug('', 'ENV:'.grey, Matrix.env.blue, 'API:'.grey, Matrix.apiServer.blue, 'MXSS:'.grey, Matrix.streamingServer.blue);

var events = require('events');
Expand Down Expand Up @@ -142,8 +154,9 @@ Matrix.api.makeUrls(Matrix.apiServer);

var malosInfoOut = '';
Matrix.device.malos.info(function (data) {
// console.log(data);
_.each(data.info, function (i) {
malosInfoOut += ' ⚙ '.yellow + i.driver_name.blue + ':' + i.base_port + ' | ' + i.notes_for_human.grey + '\n';
malosInfoOut += ' ⚙ '.yellow + i.driverName.blue + ':' + i.basePort + ' | ' + i.notesForHuman.grey + '\n';
});
});

Expand Down Expand Up @@ -372,6 +385,7 @@ function onlineSetup(callback) {
// Lets login to the streaming server
function mxssInit(cb) {

// for debugging use MATRIX_NOMXSS env var to avoid MXSS . still needs internet tho.
if (!process.env.hasOwnProperty('MATRIX_NOMXSS')) {
Matrix.service.stream.initSocket(cb);
} else {
Expand Down Expand Up @@ -548,7 +562,7 @@ function onlineSetup(callback) {
});
});

//App install update
//When application status changes on firebase, install on device
Matrix.service.firebase.user.watchAppInstall(Matrix.deviceId, function (app, appId) {
if (!_.isUndefined(app) && !_.isUndefined(appId)) {
Matrix.localApps[appId] = app;
Expand All @@ -562,6 +576,7 @@ function onlineSetup(callback) {
name: appName,
version: app.meta.version || app.version, //TODO only use meta
id: appId,
// is this application running or not
running: Matrix.activeApplications.some((a) => {
return (a.name === appName);
})
Expand All @@ -570,10 +585,7 @@ function onlineSetup(callback) {
debug('Trying to install: ' + appName.yellow);
Matrix.service.manager.stop(appName, function (err, appStopped) {
Matrix.service.manager.install(installOptions, function (err) {
debug('Finished index install');
console.log(appName, installOptions.version, 'installed from', installOptions.url);
//TODO Start the app if it was running before deployment?
//if (appStopped) Matrix.service.manager.start(appName);
});
});
});
Expand All @@ -582,6 +594,7 @@ function onlineSetup(callback) {
}
});

// continue setup
cb();
},

Expand Down Expand Up @@ -621,8 +634,8 @@ function onlineSetup(callback) {
else error('MXSS Unavailable'.red);

// MALOS
if (malosInfoOut.length > 0) log('MALOS COMPONENTS', malosInfoOut);
else error('MALOS Unavailable'.red);
if (malosInfoOut.length > 0) log('CORE COMPONENTS', malosInfoOut);
else error('MALOS/CORE Unavailable'.red);

// MOS
log(Matrix.is.green.bold, '['.grey + Matrix.deviceId.grey + ']'.grey, 'ready'.yellow.bold);
Expand All @@ -638,7 +651,7 @@ function onlineSetup(callback) {
console.log('MATRIX OS can be upgraded.'.yellow, Matrix.latestVersion, 'available!'.yellow, Matrix.version);
}

// CLI uses IPC for tests
// CLI uses IPC for tests, will boot MOS with send method
if (process.hasOwnProperty('send')) process.send({ 'matrix-ready': true });

if (process.env.hasOwnProperty('REPL')) {
Expand Down
29 changes: 14 additions & 15 deletions lib/device/drivers/accelerometer.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
var protoBuilder, matrixMalosBuilder;
var SenseDriver, DeviceDriver;

var debug = debugLog('accelerometer')

module.exports = {
commands: ['accelerometer'],
// init runs automatically, wait for app to request component creation
init: function() {
protoBuilder = Matrix.service.protobuf.malos.driver
// Parse matrix_malos package (namespace).
matrixMalosBuilder = protoBuilder.build('matrix_malos')
init: function () {
DeviceDriver = Matrix.service.protobuf.malos.driver
SenseDriver = Matrix.service.protobuf.malos.sense;
},
read: function(buffer) {
var a = new matrixMalosBuilder.Imu.decode(buffer)
return { x: a.accel_x, y: a.accel_y, z: a.accel_z };
read: function (buffer) {
var a = SenseDriver.Imu.decode(buffer);
return { x: a.accelX, y: a.accelY, z: a.accelZ };
},
prepare: function(options, cb) {
prepare: function (options, cb) {
if (_.isFunction(options)) {
cb = options;
options = {};
Expand All @@ -35,15 +34,15 @@ module.exports = {
}

// map options to protobuf config
var driverConfigProto = new matrixMalosBuilder.DriverConfig
// 2 seconds between updates.
driverConfigProto.delay_between_updates = options.refresh;
var config = new DeviceDriver.DriverConfig
// 2 seconds between updates.
config.delayBetweenUpdates = options.refresh;
// Stop sending updates 6 seconds after pings.
driverConfigProto.timeout_after_last_ping = options.timeout;
config.timeoutAfterLastPing = options.timeout;
debug('gyro start')
cb(driverConfigProto.encode().toBuffer());
cb(DeviceDriver.DriverConfig.encode(config).finish());
},
ping: function() {
ping: function () {
if (_.has(Matrix.components, 'accelerometer')) {
Matrix.components.accelerometer.ping();
} else {
Expand Down
27 changes: 13 additions & 14 deletions lib/device/drivers/altitude.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
var protoBuilder, matrixMalosBuilder;
var DeviceDriver, SenseDriver;

var debug = debugLog('altitude');

module.exports = {
// init runs automatically, wait for app to request component creation
init: function() {
protoBuilder = Matrix.service.protobuf.malos.driver;
// Parse matrix_malos package (namespace).
matrixMalosBuilder = protoBuilder.build('matrix_malos');
init: function () {
DeviceDriver = Matrix.service.protobuf.malos.driver;
SenseDriver = Matrix.service.protobuf.malos.sense;
},
read: function(buffer) {
read: function (buffer) {
return {
value: new matrixMalosBuilder.Pressure.decode(buffer).altitude
value: SenseDriver.Pressure.decode(buffer).altitude
};
},
prepare: function(options, cb) {
prepare: function (options, cb) {
if (_.isFunction(options)) {
cb = options;
options = {};
Expand All @@ -35,15 +34,15 @@ module.exports = {
}

// map options to protobuf config
var driverConfigProto = new matrixMalosBuilder.DriverConfig;
// 2 seconds between updates.
driverConfigProto.delay_between_updates = options.refresh;
var config = new DeviceDriver.DriverConfig;
// 2 seconds between updates.
config.delayBetweenUpdates = options.refresh;
// Stop sending updates 6 seconds after pings.
driverConfigProto.timeout_after_last_ping = options.timeout;
config.timeoutAfterLastPing = options.timeout;
debug('altitude start');
cb(driverConfigProto.encode().toBuffer());
cb(DeviceDriver.DriverConfig.encode(config).finish());
},
ping: function() {
ping: function () {
if (_.has(Matrix.components, 'altitude')) {
Matrix.components.altitude.ping();
} else {
Expand Down
Loading

0 comments on commit 6374731

Please sign in to comment.