forked from openmovementproject/openmovement-axsys-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
123 lines (97 loc) · 3.49 KB
/
app.js
File metadata and controls
123 lines (97 loc) · 3.49 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Created by Praveen on 09/09/2015.
*
* @flow
*/
/* System imports */
import path from 'path';
/* Third party imports */
import log4js from 'log4js';
log4js.replaceConsole();
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('axsys-server.log'));
import http from 'http';
import { Server as WebSocketServer } from 'ws';
import express from 'express';
import {createStore} from 'redux';
/* Internal imports */
import * as register_module from './lib/register-client';
import * as discoveryService from './lib/services/devicediscovery-service';
import { EventBus } from './lib/services/bus';
import * as constants from './lib/constants/event-name-constants';
import * as websocketFacade from './lib/api/websocket-api';
import {AxsysError, Payload} from './lib/api/payload';
import * as stringUtils from './lib/utils/string-utils';
//import cacheReducer from './lib/reducers/cache-reducer';
import reducers from './lib/reducers/reducers';
import * as actionCreators from './lib/action-creators/cache-action-creator';
import timeSyncServer from 'timesync/server';
const store = createStore(reducers);
//CORS middleware
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
/* main */
function main() {
var server = http.createServer();
var wss = new WebSocketServer( {'server': server} );
var app = express();
console.log(timeSyncServer);
app.use(allowCrossDomain);
app.use('/timesync', timeSyncServer.requestHandler);
register_module.register(app);
let eventBus = new EventBus();
let deviceDiscoverer = new discoveryService.DeviceDiscovery({
eventBus: eventBus,
vidPids: [
0x04D80057
]
});
deviceDiscoverer.start();
consumeDeviceAddedOrRemovedEventsIntoCacheStore(eventBus);
websocketFacade.websocketSetup(wss, store);
// setup static route for client.min.js
setUpRouteForClientLibrary(app);
server.on('request', app);
server.listen(9693);
}
function consumeDeviceAddedOrRemovedEventsIntoCacheStore(eventBus) {
eventBus.subscribe(constants.AX_DEVICE_ADDED, function(device) {
let path = stringUtils.removeWindowsPrefixToSerialPath(device.port);
let devicePath = stringUtils.constructSerialPath(path);
store.dispatch(actionCreators.createDeviceWithAttributes({
devicePath: devicePath,
deviceAttributes: device
}));
console.log('Added new device');
});
eventBus.subscribe(constants.AX_DEVICE_REMOVED, function(device) {
let path = stringUtils.removeWindowsPrefixToSerialPath(device.port);
let devicePath = stringUtils.constructSerialPath(path);
store.dispatch(actionCreators.removeDeviceWithAttributes({
devicePath: devicePath
}));
console.log('Removed a device');
});
}
function secureOriginsToServe(app) {
// setup origins - TODO: this list should be externalized
app.io.set('origins', 'http://localhost:9692');
}
function setUpRouteForClientLibrary(app) {
// setup route for serving client
app.get('/client.min.js', (req, res) => {
res.sendFile(path.join(__dirname, 'dist-client/client.min.js'));
});
}
/* Init app */
main();