Skip to content

Commit

Permalink
Add possibility to stream data to REST endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Temmermans committed May 5, 2017
1 parent ce6f1f6 commit db72e29
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 30 deletions.
44 changes: 44 additions & 0 deletions app/iot/controllers/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var Data = require('../models/data');

exports.all = function(req, res) {
// route handlers go here
Data.find({}, function(err, docs) {
if(err) throw err;

res.json(docs);
});
}

exports.write = function(req, res, next) {

req.body.forEach(function(r, i){

const newData = new Data({
ESP_OPS: req.body[i]['ESP_OPS'],
attributeName: req.body[i]['attributeName'],
categories: req.body[i]['categories'] || [''],
dataType: req.body[i]['dataType'],
deviceName: req.body[i]['deviceName'],
timestamp: req.body[i]['timestamp'],
value: req.body[i]['value'],
fixedValue: req.body[i]['fixedValue'] || '',
min: req.body[i]['min'] || '',
max: req.body[i]['max'] || ''
});

newData.save(function(err) {
if(err) throw err;
res.end("Post succesfull");
});
})
}

exports.delete = function(req, res, next) {

Data.find({}).remove(function(err) {
if (err) throw err;

res.end('data deleted')
});

}
25 changes: 25 additions & 0 deletions app/iot/models/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//here goes everything that interacts with the database, starting by creating or requiring the db
var mongo = require('mongodb');
var mongoose = require('mongoose');
var mongoUrl = require('../../shared/config').credentials.mongodb.url;

// set up our mongodb database
mongoose.connect(mongoUrl);

//set a variable to hold this connection
var db = mongoose.connection;

var dataSchema = mongoose.Schema({
ESP_OPS: {type: String},
attributeName: {type: String},
categories: [String],
dataType: {type: String},
deviceName: {type: String},
timestamp: {type: String},
value: {type: String},
fixedValue: {type: String},
min: {type: String},
max: {type: String}
});

var Data = module.exports = mongoose.model('Data', dataSchema);
9 changes: 9 additions & 0 deletions app/iot/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,14 @@ module.exports = function (app) {
app.get('/simulator', function (req, res) {
res.render('iot/simulator');
});

// route to display all the data that is generated
app.get('/simulator/data', require('./controllers/data').all);

// route to write data to the database
app.post('/simulator/data', require('./controllers/data').write);

// delete the data when the stream is stopped or when the app is closed
app.get('/simulator/data/delete', require('./controllers/data').delete);

}
2 changes: 1 addition & 1 deletion app/shared/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
credentials: {
mongodb: {
url: process.env.MONGODB_URI || "mongodb://127.0.0.1/test"
url: process.env.MONGODB_URI || "mongodb://127.0.0.1/simulator"
},
redis: {
url: process.env.REDISTOGO_URL || "redis://127.0.0.1:6379"
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "iot-device-simulator",
"version": "1.0.0",
"private": true,
"description": "Device Simulator to easily prototype",
"description": "Device Simulator to easily build demo's and prototypes using streaming data",
"main": "server.js",
"babel": {
"presets": [
Expand Down Expand Up @@ -45,6 +45,8 @@
"express": "^4.13.4",
"express-handlebars": "^3.0.0",
"helmet": "^2.1.0",
"mongodb": "^2.2.26",
"mongoose": "^4.9.7",
"morgan": "^1.7.0",
"ms": "^0.7.1",
"subdomain": "^1.2.0"
Expand Down
2 changes: 1 addition & 1 deletion public/js/app.min.js

Large diffs are not rendered by default.

29 changes: 23 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,48 @@ This package can be used to deploy an IoT device simulator on your local machine
```
If you get an error, uninstall and install again.

3. Navigate to a folder on your local machine where you want to store the application.
3. Download and install mongodb from the following location (only needed of you want to stream to other things outside SDS):
- for Mac: [with homebrew](https://docs.mongodb.com/v3.0/tutorial/install-mongodb-on-os-x/) or [installer](https://www.mongodb.com/download-center#community)
- for Windows: [mongodb for windows](https://www.mongodb.com/download-center#community)

Check to see if you can run mongod from the command prompt.

4. Navigate to a folder on your local machine where you want to store the application.

4. Open a command prompt and navigate to that specific folder (to get the path, just copy paste it from the finder window):
5. Open a command prompt and navigate to that specific folder (to get the path, just copy paste it from the finder window):
```
$ cd <path/to/folder> // omit the <>
```
5. Clone the project into the folder iot-device-simulator and move into the folder on the command Prompt:
6. Clone the project into the folder iot-device-simulator and move into the folder on the command Prompt:
```
$ git clone https://github.com/Temmermans/iot-device-simulator.git iot-device-simulator && cd iot-device-simulator
```
6. Install all the necessary libs via the following command:
7. Install all the necessary libs via the following command:
```
$ npm install
```
7. Start the local server with the following command:
8. Start the local server with the following command:
```
$ npm run dev
```
8. Navigate to the following url on the localhost: [localhost](http://localhost:3000/simulator)
9. Navigate to the following url on the localhost: [localhost](http://localhost:3000/simulator)


If you an access-Control-Allow-Origin error install following chrome extension: Allow-Control-Allow-Origin and enable it, make sure the icon is green and then try again.
If using other sites, makes sure to turn it off again.

![alt text](./readme-images/chrome-extension.png)

#### SDS

For SDS projects, provide the necessary details in the SDS panel. With the correct credentials, you should see data coming in in the windows of yout SDS project.

#### Other

If you skip the SDS project, the app will start streaming data to the following endpoint: '/simulator/data' as JSON. This can then be consumed by a front end tool capable of doing REST requests. Keep in mind that to use this, you will need to install mongodb on your local machine. (see above).

Every time you press the stop streaming button, the db will be flushed, to avoid taking up to much space when streaming regularly.

### ToDo

- Add support for more than SAP SDS
Expand Down
37 changes: 34 additions & 3 deletions src/assets/js/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ function removeCreatedDevice(e) {
function deviceInputFieldClicked() {

if (document.querySelectorAll('.createDevice').length <= 20) {

numberOfCreateDevicesInputFields++;

const deviceId = `device${numberOfCreateDevicesInputFields}`;

// build the new input field
Expand Down Expand Up @@ -814,10 +812,29 @@ function startStreaming() {
}
}

function flushDb() {
let settings = {
"async": true,
"url": '/simulator/data/delete',
"method": "GET",
"headers": {
"cache-control": "no-cache"
}
};

$.ajax(settings).done(function (response) {
console.log("Success: " + response);
});
}

function stopStreaming() {
clearInterval(streamingInterval[0]);
// reset to empty array
streamingInterval = [];

if (!isSdsProject) {
flushDb();
}
}

function controlPanelDropDownSelected(e) {
Expand Down Expand Up @@ -874,7 +891,21 @@ function getDataToSend() {

function sendData() {

console.log(getDataToSend());
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost:3000/simulator/data",
"method": "POST",
"headers": {
"content-type": "application/json",
"cache-control": "no-cache"
},
"processData": false,
"data": JSON.stringify(getDataToSend())
}
$.ajax(settings).done(function (response) {
console.log("posted successfully, " + response);
});

}

Expand Down
Loading

0 comments on commit db72e29

Please sign in to comment.