-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dispatcher.js
56 lines (43 loc) · 1.51 KB
/
Dispatcher.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
'use strict'
var _ = require('lodash');
var Queue = require('bee-queue');
var StateManager = require('./StateManager');
var CommandBuilder = require('./CommandBuilder');
var JMRIService = require('./JMRIService');
var Promise = require("bluebird");
var config = require('./config');
var tca = require('./trainCommandActions');
function Dispatcher() {
var self = this;
self.commandBuilder = new CommandBuilder(config.train)
self.JMRI = new JMRIService(config.JMRI_IP, self.commandBuilder)
self.trainState = new StateManager(config.state0.train, tca);
self.commandQueue = new Queue('command');
self.commandQueue.process(function(job, done){
console.log('JOB');
console.log(job);
var stateDiff = processCommand.bind(self)(job.data);
Promise.map(stateDiff, function(feature) {
var message = self.commandBuilder.fillTemplate(feature, self.trainState.state);
return self.JMRI.send(message);
}, {concurrency: 1}).then(function() {
return self.awsUpdate(self.trainState.state);
}).then(done);
});
self.commandQueue.on('succeeded', function (job, result) {
console.log('Job ' + job.id + ' succeeded with result: ' + result);
});
}
Dispatcher.prototype.put = function (state) {
this.commandQueue
.createJob(state)
.timeout(1000)
.save();
}
Dispatcher.prototype.setUpdateCallback = function (callback) {
this.awsUpdate = callback;
};
var processCommand = function (aws_state) {
return this.trainState.change(aws_state.command);
}
module.exports = Dispatcher;