-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
148 lines (123 loc) · 3.18 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var hyperdeck = require('./hyperdeck');
var _ = require('lodash');
var express = require('express');
var bodyParser = require('body-parser');
// var mongoose = require('mongoose');
// var Schema = mongoose.Schema;
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8888 });
//mongoose.connect('mongodb://localhost/test');
// var hyperdeckSchema = new Schema({
// name:String,
// description:String,
// ipAddress:String,
// active:Boolean,
// connectionStatus:String,
// remoteEnabled:Boolean
// });
// var eventSchema = new Schema({
// hyperDeck:Schema.Types.ObjectId,
// timeStamp:Date,
// message:String,
// params: {}
// });
// var HyperDeck = mongoose.model('HyperDeck', hyperdeckSchema, 'hyperdecks');
// var Event = mongoose.model('Event', eventSchema, 'events');
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
client.send(JSON.stringify(data));
});
};
var deckDoc = {
_id: 123456,
name: 'Prospect House',
description: 'July 4th Prospect House Record Deck',
ipAddress: '192.168.1.58',
active: true,
connectionStatus: '',
remoteEnabled: false
};
var events = [];
// initialize hyperdeck controller
var deck = new hyperdeck(deckDoc.ipAddress);
var decks = {};
decks[deckDoc._id] = deck;
deck.on('data', function (data) {
events.unshift(data);
events = events.splice(0,50);
wss.broadcast({
_id: deckDoc._id,
event: data
});
});
deck.on('ready', function() {
wss.broadcast({
_id: deckDoc._id,
connectionStatus: 'Connected'
});
});
deck.on('timeout', function() {
wss.broadcast({
_id: deckDoc._id,
connectionStatus: 'Timed Out'
});
});
var app = express();
app.use(express.static('app'));
app.use(bodyParser.json());
app.get('/', function(req, res) {
res.sendFile('app/index.html');
});
app.get('/api/hyperdecks', function (req, res) {
var doc = [deckDoc];
doc = _.map(doc, annotateHyperdeck);
res.send(doc);
});
app.get('/api/hyperdecks/:id/transport-info', function (req, res) {
if (decks[req.params.id]) {
decks[req.params.id].getTransportInfo(function (info) {
res.send(info);
});
} else {
res.send([]);
}
});
app.get('/api/hyperdecks/:id/configuration', function (req, res) {
if (decks[req.params.id]) {
decks[req.params.id].getConfiguration(function (info) {
res.send(info);
});
} else {
req.send([]);
}
});
app.get('/api/hyperdecks/:id/slot/:slot', function (req, res) {
if (decks[req.params.id]) {
decks[req.params.id].getSlotInfo(req.params.slot, function (info) {
res.send(info);
});
} else {
req.send([]);
}
});
app.post('/api/hyperdecks/:id/command', function (req, res) {
if (_.has(req.body, 'remote')) {
deck.setRemote(req.body.remote);
}
if (_.has(req.body, 'transportCommand')) {
deck.transportCommand(req.body.transportCommand);
}
if (_.has(req.body, 'connect')) {
deck.connect();
}
res.send('command executed');
});
app.get('/api/events', function (req, res) {
res.send(events);
});
app.listen(3000);
function annotateHyperdeck(deckDoc) {
deckDoc.connectionStatus = deck.connectionStatus;
deckDoc.remoteEnabled = deck.remoteEnabled;
return deckDoc;
}