forked from hugs/tapsterbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
78 lines (63 loc) · 2.23 KB
/
server.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
#! /usr/local/bin/node
// Load all Libraries and Config
var
five = require("johnny-five"),
calibration = require("./lib/calibration"),
Robot = require("./lib/robot").Robot,
kinematics = require("./lib/kinematics"),
motion = require("./lib/motion"),
config = require("./config");
const Hapi = require('hapi');
// Initialize HTTP Server
const server = Hapi.server({
port: config.port,
});
const init = async () => {
await server.register(require('inert'));
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: 'web',
index: true,
listing: true,
defaultExtension: 'html'
}
},
config: {
cache: {
expiresIn: 1 // prevent HTML caching
}
}
});
await server.start();
console.log(`Server running at: ${server.info.uri}`);
};
init();
// Initialize Socket.IO and make globally available
global.io = require('socket.io')(server.listener);
// Initiliaze Johhny-Five Board
var board = new five.Board({ debug: true, port: config.serialport || null });
board.on("ready", function() {
// Initialize servos
var s1 = five.Servo({ pin: config.s1.pin });
var s2 = five.Servo({ pin: config.s2.pin });
var s3 = five.Servo({ pin: config.s3.pin });
// Load calibration data
var calibrationData = calibration.getDataFromFilePath('calibration.json');
// Initialize kinematics
var k = new kinematics.Kinematics({
e: config.e,
f: config.f,
re: config.re,
rf: config.rf
});
var robot = new Robot(s1, s2, s3, calibrationData, k, config); // Initialize Robot instance
// var repl = require('./lib/repl')(board, robot); // Testing through command line
var rest = require('./lib/rest')(server, robot); // load REST API
var listeners = require('./lib/listeners')(io, robot, config, s1, s2, s3); // Lets Start socket Listeners
// server.start(); // And Finally Start HTTP server
global.ip = "127.0.0.1";
console.log('Board ready');
});