forked from zhaw-iwi/particle-backend-demo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
84 lines (64 loc) · 2.5 KB
/
server.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
const express = require('express');
const cors = require('cors');
const http = require('http');
const path = require('path');
const axios = require('axios');
const bodyParser = require('body-parser');
const EventSource = require("eventsource");
var constants = {}
try {
constants = require('./constants')
} catch (error) {
console.log("Module 'constants' not found, trying Heroku config vars.")
}
const eventListeners = require('./eventListeners.js');
var SSE = require('express-sse');
var sse = new SSE([]);
eventListeners.sse = sse;
const app = express();
app.use(cors())
app.use(bodyParser.json()); // support json encoded bodies
app.use('/', express.static(path.join(__dirname, './client/src'))); //TODO: is das notwendig? ja, sonst nix events mit lokaler url :/
const port = process.env.PORT || '3001';
app.set('port', port);
const access_token = process.env.ACCESS_TOKEN || constants.access_token;
const device_id = process.env.DEVICE_ID || constants.device_id;
const server = http.createServer(app);
let eventURL = 'https://api.particle.io/v1/devices/' + device_id + '/events?access_token=' + access_token
var source = new EventSource(eventURL);
source.addEventListener('data', eventListeners.handleDataChanged)
// Read a variable. Example:
// GET /api/device/0/variable/buttonState
app.get('/api/device/variable/:name', (req, res) => {
let variableName = req.params.name;
let url = 'https://api.particle.io/v1/devices/' + device_id + '/' + variableName + '?access_token=' + access_token;
axios.get(url)
.then(response => {
res.send({
timeStamp: response.data.coreInfo.last_heard,
result: response.data.result,
});
})
.catch(error => {
res.status(500).send({error: "could not read current value"});
});
})
// Call a function. Example:
// POST /api/device/0/function/blinkRed
app.post('/api/device/function/:name', (req, res) => {
let functionName = req.params.name;
var data = {arg: req.body.arg};
let url = 'https://api.particle.io/v1/devices/' + device_id + '/' + functionName + '?access_token=' + access_token;
axios.post(url, data)
.then(response => {
res.send({result: response.data.return_value})
})
.catch(error => {
res.status(500).send({error: "could not execute function " + functionName})
});
})
// GET /api/events
app.get('/api/events', sse.init);
server.listen(port, () => {
console.log("app listening on port " + port);
});