-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.js
87 lines (69 loc) · 2.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const { createMachine, Machine, actions, interpret, spawn } = require('xstate');
/*
*
* https://github.com/nightscout/cgm-remote-monitor/blob/master/lib/server/bootevent.js
*
// ctx.nightscoutConnect = require('nightscout-connect')(env, ctx);
*/
var axios = require('axios');
var builder = require('./lib/builder');
var sources = require('./lib/sources');
var outputs = require('./lib/outputs');
function internalLoop (input, output) {
}
function manage (env, ctx) {
// source
// output
// env.extendedSettings.connect.source
var spec = { kind: 'disabled' };
if (!env.extendedSettings.connect) {
console.log("Skipping disabled nightscout-connect");
return;
}
if (!env.extendedSettings.connect.source) {
console.log("Skipping disabled nightscout-connect, no source driver spec");
return;
}
spec.kind = env.extendedSettings.connect.source;
var internal = { name: 'internal' };
var output = outputs(internal)(internal, ctx);
console.log("CONFIGURED OUTPUT", output);
// var things = internalLoop(input, output);
// everything known for output
// output must be passed into builder, before generate_driver is
// called.
var make = builder({ output });
// select an available input source implementation based on env
// variables/config
var driver = sources(spec);
var validated = driver.validate(env.extendedSettings.connect);
if (validated.errors) {
ctx.bootErrors.push(...validated.errors);
}
console.log("INPUT PARAMS", spec, validated.config);
if (!validated.ok) {
console.log("Invalid, disabling nightscout-connect", validated);
return;
}
var impl = driver(validated.config, axios);
impl.generate_driver(make);
var things = make( );
function handle ( ) { return actor; };
handle.run = () => {
actor.send({type: 'START'});
return Promise.resolve(handle);
}
handle.stop = () => {
actor.stop( );
return Promise.resolve(handle);
}
ctx.bus.on('tick', console.log.bind(console, 'DEBUG nightscout-connect'));
ctx.bus.once('data-processed', handle.run);
ctx.bus.once('tearDown', handle.stop);
// console.log(things);
var actor = interpret(things);
actor.start( );
// actor.send({type: 'START'});
return handle;
}
module.exports = manage;