-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (89 loc) · 3.1 KB
/
app.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var express = require("express");
var bodyParser = require("body-parser");
var WebSocket = require("ws");
const serialport = require('./robot_serial');
const ctrl = require('./robot_controller');
const Robot = require('./robot_command');
// var config = require("./config.js")
var app = express();
const robot = new Robot(serialport);
let current_position = "";
// Start the gamecontroller handler
ctrl.start(robot);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,Authorization");
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
next();
});
// var router = express.Router();
app.route("/").get(function (req, res) {
res.status(200).send("Welcome to our robot restful API");
});
app.route("/home").get(async function (req, res) {
robot.moveHome();
res.status(200).send("Home command was Sent..");
current_position = await robot.getPosition();
});
app.route("/power/:io").get(function (req, res) {
var power = req.query.io;
if (power == 1) {
robot.powerUpSteppers();
} else {
robot.powerDownSteppers();
}
res.status(200).send("Power command was Sent..");
});
app.route("/move").post(async function (req, res) {
// Movement {
// axis: string;
// mode: String;
// value: string;
// speed: number;
// }
var movement = req.body;
console.log(movement);
robot.relMoveSingle(movement)
current_position = await robot.getPosition();
res.status(200).send({ message: "Move Sent.." });
});
app.route("/movefull").post(async function (req, res) {
// absolutePosition {
// X: number;
// Y: number;
// X: number;
// speed: number;
// }
var absolutePosition = req.body;
console.log("Inside movefull..");
console.log(absolutePosition);
robot.absoluteMove(absolutePosition);
current_position = await robot.getPosition();
res.status(200).send("Move Sent..");
});
app.route("/info").get(async function (req, res) {
current_position = await robot.getPosition()
res.status(200).send(current_position);
});
/*********************************************************************
* Start the servers (API & Angular client)
* Get port from environment and store in Express.
*********************************************************************/
var port = parseInt(process.env.PORT || '3000');
var server = app.listen(port, function () {
console.log("app running on port.", server.address().port);
});
const wss = new WebSocket.Server({ port: 8088 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
});
setInterval(function () {
wss.clients.forEach(async function each(client) {
current_position = await robot.getPosition();
client.send(JSON.stringify(current_position));
});
}, 2000)