-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.js
177 lines (146 loc) · 5.11 KB
/
rest.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Copyright 2014 Open Ag Data Alliance
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var REST = (function() {
var express = require('express');
var _config = express.Router();
var _data = express.Router();
var _resources = express.Router();
var isobusParser = require('./oada_parse');
var database = require('./database');
var pgnConfig = require('./config/streamConfig.json');
var streamTypes = require('./config/streamTypes.json'); //TODO remove
var _init = function(callback){
//Do everything we need to do before we start listening to requests.
database.init(callback);
//Configuure isobusParser
isobusParser.configure(pgnConfig);
//Load streams types from db (hardcoded with require for now)
};
//Get the url and pgns to post
_config.get('/*', function(req, res, next) {
console.log('Getting config for udid: ' + req.path.substr(1));
if(req.path.length > 1){
var toSend = {
url: req.protocol+'://'+ req.get('host') + '/data' + req.path,
pgns:[1234,5678,9123]
};
res.json(toSend);
} else {
res.sendStatus(400);
}
});
//Get the posted data
_data.get('/*', function(req, res, next) {
console.log('Request for data with udid: ' + req.path.substr(1));
console.log(req.query);
var view = null;
if(typeof req.query.view != 'undefined'){
try {
view = JSON.parse(req.query.view);
} catch(exception) {
res.sendStatus(400);
return;
}
}
if(view !== null){
//TODO process view syntax
if(view['$each'] !== undefined){
view = view['$each'];
} else {
res.sendStatus(400);
return;
}
console.log(JSON.stringify(view));
}
database.get(req.path.substr(1), view, function(docs){
res.json(docs);
});
});
//Post resource
_data.post('/*', function(req, res, next) {
console.log('Post recieved from ' + req.path.substr(1) +
' with data: ' + JSON.stringify(req.body));
var udid = req.path.substr(1); //Used as collection
var messages = req.body;
//Insert messages into ISOBus database
database.insert(udid, messages, function(inserted){
//Success now save into streamData collection
//Parse messages with our pgnConfig file
//Convert messages to array if it is just one message
if(!(req.body instanceof Array)) {
//Convert single message to an array
messages = [messages];
}
console.log('Messages:' + JSON.stringify(messages));
for(var msg in messages){
if(typeof messages[msg].timestamp != 'number'){
//Convert timestamp from hex.hex to decimal.decimal
var decPos = messages[msg].timestamp.indexOf('.');
if (decPos == -1) {
messages[msg].timestamp = parseInt(messages[msg].timestamp,16);
} else {
var beforeD = messages[msg].timestamp.substr(0,decPos);
var afterD = null;
if (messages[msg].timestamp.length > (decPos + 1)) {
afterD = messages[msg].timestamp.substr(decPos+1);
}
messages[msg].timestamp = parseInt(beforeD,16);
if(afterD !== null){
messages[msg].timestamp = messages[msg].timestamp + (parseInt(afterD,16) / 1000000);
}
}
}
//Convert pgn from hex to decimal
messages[msg].pgn = parseInt(messages[msg].pgn,16);
//Convert data from hex string to byte array
messages[msg].data = isobusParser.getBytes(messages[msg].data);
}
var streams = isobusParser.parse(messages);
console.log('Streams:' + JSON.stringify(streams));
if(Object.keys(streams).length > 0){
for(var stream in streams){
//Save messages from each stream in database
for(msg in streams[stream]){
//Add keys to each
streams[stream][msg].machine = udid;
//TODO check if this stream type exists, if not add another to db
streams[stream][msg].stream = streamTypes[stream];
}
database.insert('streamData', streams[stream]);
}
}
});
res.sendStatus(200);
});
//OADA rest
_resources.get('/*', function(req, res, next) {
var resId = req.path.substr(1);
console.log('Request for /resources resId: ' + resId);
//TODO resId's are hardcoded at the moment.
if (resId == '123') {
//Return streams resource
} else {
//Check this resId in our streams resource
}
});
return {
init: _init,
config: _config,
data: _data,
resources: _resources
};
}());
module.exports = REST;