This repository has been archived by the owner on May 22, 2024. It is now read-only.
/
weatherServer.js
94 lines (78 loc) · 3.23 KB
/
weatherServer.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
fs = require('fs');
// Load the Ruby module
console.log("Initializing Openweather");
var weatherInitScript = fs.readFileSync("weatherInit.rb", "utf8");
Interop.eval("application/x-ruby", weatherInitScript);
// Ruby overrides Node.js signal handler, we override it back
process.on('SIGINT', function() { process.exit(0); });
Weather = Interop.import('weather')
Interop.export('tempInCity', function(name) {
return Weather.temperature_in_city(name);
});
// Load the R module
console.log("Preparing weather model... This may take a while depending on your connection");
var weatherModelScript = fs.readFileSync("weatherModel.r", "utf8");
Interop.eval("application/x-r", weatherModelScript);
// Import the function exported from the R module
createModel = Interop.import('createModel');
predictTemp = Interop.import('do_predict');
plotModel = Interop.import('plotModel');
isCity = Interop.import('isCity');
const cityServiceType = Java.type('com.oracle.graalvm.demo.weather.CityService');
var cityService = new cityServiceType();
var updateTemperatures = function() {
let cities = cityService.getAll();
for (var i = 0; i < cities.length; i++) {
console.log("Updating temperature of " + cities[i].getName());
cityService.updateTemperature(cities[i].getId(), Weather.temperature_in_city(cities[i].getName()));
}
}
// Create the linear regression model
var updateModel = function(size) {
var cities = cityService.getAll();
let getName = function(i) { return cities[i-1].getName(); }
let getLatitude = function(i) { return cities[i-1].getLatitude(); }
let getLongitude = function(i) { return cities[i-1].getLongitude(); }
let getTemperature = function(i) { return cities[i-1].getTemperature(); }
return createModel(size, cities.length, getName, getLatitude, getLongitude, getTemperature);
}
var model = updateModel(5);
// Expressjs application:
var express = require('express');
var app = express();
app.get('/cities', function (req, res) {
let cities = cityService.getAllPaged(req.query.skip, req.query.limit);
let jsCities = [];
for(let i = 0; i < cities.length; ++i) {
jsCities.push({
name: cities[i].getName(),
country: cities[i].getCountry(),
population: cities[i].getPopulation(),
latitude: cities[i].getLatitude(),
longitude: cities[i].getLongitude(),
temperature: cities[i].getTemperature()
});
}
res.send({data = jsCities, totalCount = cityService.getTotalCount()});
});
app.get('/predict/:city', function (req, res) {
var city = cityService.findByName(req.params.city);
if (city) {
res.send({predicted: predictTemp(model, city.getLatitude()), real: Weather.temperature_in_city(req.params.city)});
} else {
res.status(404).send('City not found');
}
});
app.post('/regenerate/:size', function(req, res) {
model = updateModel(req.params.size);
res.status(200).send('');
});
app.post('/update-tempratures', function(req, res) {
updateTemperatures();
res.status(200).send('');
});
app.get('/model-plot', (req, res) => res.send(plotModel(model)));
app.use(express.static(__dirname + "/public"));
app.listen(12836, function() {
console.log("Server listening on port 12836");
});