Skip to content

Commit

Permalink
Virtual assistant code simplification (#5400)
Browse files Browse the repository at this point in the history
* Moved duplicate plugin code to single location, and small improvements

* Defined _each()
  • Loading branch information
inventor96 authored and sulkaharo committed Jan 11, 2020
1 parent ddbda15 commit 4098689
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 180 deletions.
95 changes: 5 additions & 90 deletions lib/api/alexa/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

var moment = require('moment');
var _each = require('lodash/each');

function configure (app, wares, ctx, env) {
var entries = ctx.entries;
var express = require('express')
, api = express.Router( );
var translate = ctx.language.translate;
Expand All @@ -16,30 +14,7 @@ function configure (app, wares, ctx, env) {
// json body types get handled as parsed json
api.use(wares.bodyParser.json());

ctx.plugins.eachEnabledPlugin(function each(plugin){
if (plugin.virtAsst) {
if (plugin.virtAsst.intentHandlers) {
console.log('Alexa: Plugin ' + plugin.name + ' supports Virtual Assistants');
_each(plugin.virtAsst.intentHandlers, function (route) {
if (route) {
ctx.alexa.configureIntentHandler(route.intent, route.intentHandler, route.metrics);
}
});
}
if (plugin.virtAsst.rollupHandlers) {
console.log('Alexa: Plugin ' + plugin.name + ' supports rollups for Virtual Assistants');
_each(plugin.virtAsst.rollupHandlers, function (route) {
console.log('Route');
console.log(route);
if (route) {
ctx.alexa.addToRollup(route.rollupGroup, route.rollupHandler, route.rollupName);
}
});
}
} else {
console.log('Alexa: Plugin ' + plugin.name + ' does not support Virtual Assistants');
}
});
ctx.virtAsstBase.setupVirtAsstHandlers(ctx.alexa);

api.post('/alexa', ctx.authorization.isPermitted('api:*:read'), function (req, res, next) {
console.log('Incoming request from Alexa');
Expand Down Expand Up @@ -82,70 +57,7 @@ function configure (app, wares, ctx, env) {
}
});

ctx.alexa.addToRollup('Status', function bgRollupHandler(slots, sbx, callback) {
entries.list({count: 1}, function (err, records) {
var direction;
if (translate(records[0].direction)) {
direction = translate(records[0].direction);
} else {
direction = records[0].direction;
}
var status = translate('virtAsstStatus', {
params: [
sbx.scaleMgdl(records[0].sgv),
direction,
moment(records[0].date).from(moment(sbx.time))
]
});

callback(null, {results: status, priority: -1});
});
}, 'BG Status');

ctx.alexa.configureIntentHandler('MetricNow', function (callback, slots, sbx, locale) {
entries.list({count: 1}, function(err, records) {
var direction;
if(translate(records[0].direction)){
direction = translate(records[0].direction);
} else {
direction = records[0].direction;
}
var status = translate('virtAsstStatus', {
params: [
sbx.scaleMgdl(records[0].sgv),
direction,
moment(records[0].date).from(moment(sbx.time))]
});

callback(translate('virtAsstTitleCurrentBG'), status);
});
}, ['bg', 'blood glucose', 'number']);

ctx.alexa.configureIntentHandler('MetricNow', function (callback, slots, sbx, locale) {
if (sbx.properties.delta && sbx.properties.delta.display) {
entries.list({count: 2}, function(err, records) {
callback(
translate('virtAsstTitleDelta'),
translate('virtAsstDelta', {
params: [
sbx.properties.delta.display == '+0' ? '0' : sbx.properties.delta.display,
moment(records[0].date).from(moment(sbx.time)),
moment(records[1].date).from(moment(sbx.time))
]
})
);
});
} else {
callback(translate('virtAsstTitleDelta'), translate('virtAsstUnknown'));
}
}, ['delta']);

ctx.alexa.configureIntentHandler('NSStatus', function (callback, slots, sbx, locale) {
ctx.alexa.getRollup('Status', sbx, slots, locale, function (status) {
callback(translate('virtAsstTitleFullStatus'), status);
});
});

ctx.virtAsstBase.setupMutualIntents(ctx.alexa);

function onLaunch(next) {
console.log('Session launched');
Expand Down Expand Up @@ -181,15 +93,18 @@ function configure (app, wares, ctx, env) {
metric = slots.metric.resolutions.resolutionsPerAuthority[0].values[0].value.name;
} else {
next(translate('virtAsstUnknownIntentTitle'), translate('virtAsstUnknownIntentText'));
return;
}
}

var handler = ctx.alexa.getIntentHandler(intentName, metric);
if (handler){
var sbx = initializeSandbox();
handler(next, slots, sbx);
return;
} else {
next(translate('virtAsstUnknownIntentTitle'), translate('virtAsstUnknownIntentText'));
return;
}
}

Expand Down
95 changes: 5 additions & 90 deletions lib/api/googlehome/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

var moment = require('moment');
var _each = require('lodash/each');

function configure (app, wares, ctx, env) {
var entries = ctx.entries;
var express = require('express')
, api = express.Router( );
var translate = ctx.language.translate;
Expand All @@ -16,30 +14,7 @@ function configure (app, wares, ctx, env) {
// json body types get handled as parsed json
api.use(wares.bodyParser.json());

ctx.plugins.eachEnabledPlugin(function each(plugin){
if (plugin.virtAsst) {
if (plugin.virtAsst.intentHandlers) {
console.log('Google Home: Plugin ' + plugin.name + ' supports Virtual Assistants');
_each(plugin.virtAsst.intentHandlers, function (route) {
if (route) {
ctx.googleHome.configureIntentHandler(route.intent, route.intentHandler, route.metrics);
}
});
}
if (plugin.virtAsst.rollupHandlers) {
console.log('Google Home: Plugin ' + plugin.name + ' supports rollups for Virtual Assistants');
_each(plugin.virtAsst.rollupHandlers, function (route) {
console.log('Route');
console.log(route);
if (route) {
ctx.googleHome.addToRollup(route.rollupGroup, route.rollupHandler, route.rollupName);
}
});
}
} else {
console.log('Google Home: Plugin ' + plugin.name + ' does not support Virtual Assistants');
}
});
ctx.virtAsstBase.setupVirtAsstHandlers(ctx.googleHome);

api.post('/googlehome', ctx.authorization.isPermitted('api:*:read'), function (req, res, next) {
console.log('Incoming request from Google Home');
Expand All @@ -58,76 +33,16 @@ function configure (app, wares, ctx, env) {
handler(function (title, response) {
res.json(ctx.googleHome.buildSpeechletResponse(response, false));
next( );
return;
}, req.body.queryResult.parameters, sbx);
} else {
res.json(ctx.googleHome.buildSpeechletResponse('I\'m sorry. I don\'t know what you\'re asking for. Could you say that again?', true));
res.json(ctx.googleHome.buildSpeechletResponse(translate('virtAsstUnknownIntentText'), true));
next( );
return;
}
});

ctx.googleHome.addToRollup('Status', function bgRollupHandler(slots, sbx, callback) {
entries.list({count: 1}, function (err, records) {
var direction;
if (translate(records[0].direction)) {
direction = translate(records[0].direction);
} else {
direction = records[0].direction;
}
var status = translate('virtAsstStatus', {
params: [
sbx.scaleMgdl(records[0].sgv),
direction,
moment(records[0].date).from(moment(sbx.time))
]
});

callback(null, {results: status, priority: -1});
});
}, 'BG Status');

ctx.googleHome.configureIntentHandler('MetricNow', function (callback, slots, sbx, locale) {
entries.list({count: 1}, function(err, records) {
var direction;
if(translate(records[0].direction)){
direction = translate(records[0].direction);
} else {
direction = records[0].direction;
}
var status = translate('virtAsstStatus', {
params: [
sbx.scaleMgdl(records[0].sgv),
direction,
moment(records[0].date).from(moment(sbx.time))]
});

callback(translate('virtAsstTitleCurrentBG'), status);
});
}, ['bg', 'blood glucose', 'number']);

ctx.googleHome.configureIntentHandler('MetricNow', function (callback, slots, sbx, locale) {
if (sbx.properties.delta && sbx.properties.delta.display) {
entries.list({count: 2}, function(err, records) {
callback(
translate('virtAsstTitleDelta'),
translate('virtAsstDelta', {
params: [
sbx.properties.delta.display == '+0' ? '0' : sbx.properties.delta.display,
moment(records[0].date).from(moment(sbx.time)),
moment(records[1].date).from(moment(sbx.time))
]
})
);
});
} else {
callback(translate('virtAsstTitleDelta'), translate('virtAsstUnknown'));
}
}, ['delta']);

ctx.googleHome.configureIntentHandler('NSStatus', function (callback, slots, sbx, locale) {
ctx.googleHome.getRollup('Status', sbx, slots, locale, function (status) {
callback(translate('virtAsstTitleFullStatus'), status);
});
});
ctx.virtAsstBase.setupMutualIntents(ctx.googleHome);

function initializeSandbox() {
var sbx = require('../../sandbox')();
Expand Down
111 changes: 111 additions & 0 deletions lib/plugins/virtAsstBase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
'use strict';

var moment = require('moment');
var _each = require('lodash/each');

function init(env, ctx) {
function virtAsstBase() {
return virtAsstBase;
}

var entries = ctx.entries;
var translate = ctx.language.translate;

virtAsstBase.setupMutualIntents = function (configuredPlugin) {
// full status
configuredPlugin.addToRollup('Status', function (slots, sbx, callback) {
entries.list({count: 1}, function (err, records) {
var direction;
if (translate(records[0].direction)) {
direction = translate(records[0].direction);
} else {
direction = records[0].direction;
}
var status = translate('virtAsstStatus', {
params: [
sbx.scaleMgdl(records[0].sgv),
direction,
moment(records[0].date).from(moment(sbx.time))
]
});

callback(null, {results: status, priority: -1});
});
}, 'BG Status');

configuredPlugin.configureIntentHandler('NSStatus', function (callback, slots, sbx, locale) {
configuredPlugin.getRollup('Status', sbx, slots, locale, function (status) {
callback(translate('virtAsstTitleFullStatus'), status);
});
});

// blood sugar and direction
configuredPlugin.configureIntentHandler('MetricNow', function (callback, slots, sbx, locale) {
entries.list({count: 1}, function(err, records) {
var direction;
if(translate(records[0].direction)){
direction = translate(records[0].direction);
} else {
direction = records[0].direction;
}
var status = translate('virtAsstStatus', {
params: [
sbx.scaleMgdl(records[0].sgv),
direction,
moment(records[0].date).from(moment(sbx.time))]
});

callback(translate('virtAsstTitleCurrentBG'), status);
});
}, ['bg', 'blood glucose', 'number']);

// blood sugar delta
configuredPlugin.configureIntentHandler('MetricNow', function (callback, slots, sbx, locale) {
if (sbx.properties.delta && sbx.properties.delta.display) {
entries.list({count: 2}, function(err, records) {
callback(
translate('virtAsstTitleDelta'),
translate('virtAsstDelta', {
params: [
sbx.properties.delta.display == '+0' ? '0' : sbx.properties.delta.display,
moment(records[0].date).from(moment(sbx.time)),
moment(records[1].date).from(moment(sbx.time))
]
})
);
});
} else {
callback(translate('virtAsstTitleDelta'), translate('virtAsstUnknown'));
}
}, ['delta']);
};

virtAsstBase.setupVirtAsstHandlers = function (configuredPlugin) {
ctx.plugins.eachEnabledPlugin(function (plugin){
if (plugin.virtAsst) {
if (plugin.virtAsst.intentHandlers) {
console.log('Plugin "' + plugin.name + '" supports Virtual Assistants');
_each(plugin.virtAsst.intentHandlers, function (route) {
if (route) {
configuredPlugin.configureIntentHandler(route.intent, route.intentHandler, route.metrics);
}
});
}
if (plugin.virtAsst.rollupHandlers) {
console.log('Plugin "' + plugin.name + '" supports rollups for Virtual Assistants');
_each(plugin.virtAsst.rollupHandlers, function (route) {
if (route) {
configuredPlugin.addToRollup(route.rollupGroup, route.rollupHandler, route.rollupName);
}
});
}
} else {
console.log('Plugin "' + plugin.name + '" does not support Virtual Assistants');
}
});
};

return virtAsstBase;
}

module.exports = init;
4 changes: 4 additions & 0 deletions lib/server/bootevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ function boot (env, language) {
ctx.dataloader = require('../data/dataloader')(env, ctx);
ctx.notifications = require('../notifications')(env, ctx);

if (env.settings.isEnabled('alexa') || env.settings.isEnabled('googlehome')) {
ctx.virtAsstBase = require('../plugins/virtAsstBase')(env, ctx);
}

if (env.settings.isEnabled('alexa')) {
ctx.alexa = require('../plugins/alexa')(env, ctx);
}
Expand Down

0 comments on commit 4098689

Please sign in to comment.