-
Notifications
You must be signed in to change notification settings - Fork 71.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Virtual assistant code simplification (#5400)
* Moved duplicate plugin code to single location, and small improvements * Defined _each()
- Loading branch information
1 parent
ddbda15
commit 4098689
Showing
4 changed files
with
125 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters