-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmock.js
126 lines (105 loc) · 3.01 KB
/
mock.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
//
// Copyright (c) 2017 Cisco Systems
// Licensed under the MIT License
//
/**
* Mock version of a REST API that exposes RoomAnalytics
*/
const debug = require("debug")("api");
const fine = require("debug")("api:fine");
var devices;
try {
devices = require("./devices.json");
}
catch (err) {
console.log("Please specify a list of devices.")
process.exit(-1)
}
//
// Web API
//
var express = require("express");
var app = express();
// Enable CORS
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Add JSON parsing
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var started = Date.now();
app.route("/")
// healthcheck
.get(function (req, res) {
res.json({
message: "Congrats, your RoomAnalytics Aggregator Mock is up and running",
since: new Date(started).toISOString()
});
})
app.get("/devices", function (req, res) {
const mapped = devices.map((device) => {
return {
id: device.id,
location: device.location,
ipAddress: device.ipAddress
};
});
res.json(mapped);
})
app.get("/devices/:device", function (req, res) {
const id = req.params.device;
let found = devices.find(function (device) {
return (device.id === id)
})
if (!found) {
res.status(404).json({
message: "device not found"
})
return
}
res.json({
id: found.id,
location: found.location,
ipAddress: found.ipAddress
})
})
app.get("/devices/:device/last", function (req, res) {
const id = req.params.device;
const count = Math.round(Math.random() * 5 + 2);
fine(`returned mock latest: ${count}, for device: ${id}`)
res.json({
id: id,
peopleCount: count
});
})
app.get("/devices/:device/average", function (req, res) {
const id = req.params.device;
// Get period (in seconds)
let period = req.query.period;
if (!period) {
period = 15; // default to 15s
}
// Mock'ed data
const count = Math.round(Math.random() * 7 - 1);
fine(`returned mock average: ${count}, for device: ${id}`)
res.json({
id: id,
peopleCount: count,
period: period,
unit: "seconds"
});
})
// Starts the service
//
var port = process.env.OVERRIDE_PORT || process.env.PORT || 8080;
app.listen(port, function () {
console.log("Collector API started at http://localhost:" + port + "/");
console.log(" GET / for healthcheck");
console.log(" GET /devices for the list of devices");
console.log(" GET /devices/{device} to get the details for the specified device");
console.log(" GET /devices/{device}/last for latest PeopleCount value received");
console.log(" GET /devices/{device}/average?period=30 for a computed average");
});