-
Notifications
You must be signed in to change notification settings - Fork 0
/
app1.ts
101 lines (66 loc) · 2.24 KB
/
app1.ts
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
/**
* Created by sheldonbarnes on 10/28/15.
*/
/**
* Created by sheldonbarnes on 10/28/15.
*/
import MongoClient = require('mongodb').MongoClient;
import assert = require('assert');
var ObjectId = require('assert');
var url = 'mongodb://192.168.2.111:27017/hummingbird';
import express = require('express');
var app = express();
var port = process.env.PORT || 8080;
var current_temp = 1.111111;
var current_humidity = 2.2222222;
app.listen(port);
var weatherobject = {temperature:0, humidity:0, weatherdate: new Date()}
var weatherdata = [];
var findWeatherData = function(res,db, callback) {
var query = {"temperature": {$gt: 71.50}};
var cursor =db.collection('weatherdata').find().sort({"date":-1}).limit(20);
var arrayData = [];
var bigDoc = "Hello ";
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null) {
//console.log(doc);
//res.write(JSON.stringify(doc));
arrayData.push(doc);
} else {
res.write(JSON.stringify(arrayData));
res.end();
callback();
}
});
};
console.log('Server started at http://localhost:' + port);
app.get('/api/getweather', function (req, res) {
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
findWeatherData(res,db, function() {
db.close();
});
});
});
var insertDocument = function(db, wd1, callback) {
db.collection('weatherdata').insertOne(wd1, function(err, result) {
assert.equal(err, null);
console.log("Inserted a weather data information.");
callback(result);
});
};
app.get('/api/temphum/:temperature/:humidity', function (req, res) {
current_temp = +req.params.temperature;
current_humidity = +req.params.humidity;
weatherdata.push({temperature:current_temp, humidity:current_humidity, weatherdate: new Date()});
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
insertDocument(db, {"temperature":current_temp, "humidity":current_humidity, "date": new Date() } , function() {
db.close();
});
});
console.log(weatherdata[0]);
res.send('Temperature recorded');
});
export var App = app;