-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
154 lines (132 loc) · 5.64 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//load fetch module for calling API
const fetch = require('node-fetch');
const bodyParser = require('body-parser');
const sunCalc = require('suncalc');
const tzlookup = require("@photostructure/tz-lookup");
//get API key from .env
require('dotenv').config();
const reverseGeoKey = process.env.REVERSE_GEO_KEY;
const forwardGeoKey = process.env.FORWARD_GEO_KEY;
//create instance of express app in app object
const express = require('express');
const app = express();
//extend express instance with websocket functions
const expressWs = require('express-ws');
expressWs(app);
//handle websocket connections
let sockets = [];
//let socket;
app.ws('/', (ws) => {
//socket = ws;
sockets.push(ws);
console.log('Client connected');
ws.on('message', (message) => {
console.log(`Received message: ${message}`);
if(message.includes("search:")) {
msg = message.slice(7);
getSearchSuggestions(msg, ws);
}else if (message.includes('date:')){
message = message.slice(5);
getSunData(message, ws);
} else {
//start geo and weather API calls
callApi(message, ws);
}
});
ws.on('close', () => {
const index = sockets.indexOf(ws);
if(index > -1) {
sockets.splice(index, 1);
console.log('Client disconnected');
}
});
});
//middleware
//serve static files
app.use(express.static('src'));
app.use(express.static('assets'));
//allow JSON data parsing
app.use(bodyParser.json());
async function getSearchSuggestions(location, ws) {
try {
const loc = location.split("+").join("%20");
const coordsResponse = await fetch("https://us1.locationiq.com/v1/search?key=" + forwardGeoKey + "&q=" + loc + "&format=json&");
const coordsData = await coordsResponse.json();
ws.send('suggestions:' + JSON.stringify(coordsData));
} catch(error) {
console.error("error: ", error);
ws.send("bad response");
return { error: true }
}
}
function getSunData(dateStr, ws) {
const dateObj = new Date(dateStr);
const times = sunCalc.getTimes(dateObj, lat, lon);
const sunRiseAzimuth = sunCalc.getPosition(times.sunrise, lat, lon);
const sunSetAzimuth = sunCalc.getPosition(times.sunset, lat, lon);
const sunData = {
riseAzimuth: sunRiseAzimuth,
setAzimuth: sunSetAzimuth,
};
ws.send('sun data:' + JSON.stringify(sunData))
}
//make API calls, send successful responses immediately to client
async function callApi(location, ws) {
//get lat and lon from location with API call and build URL
let weatherApiUrl;
try {
//get lat and lon from geocoding API
const loc = location.split("+").join("%20");
const coordsResponse = await fetch("https://us1.locationiq.com/v1/search?key=" + forwardGeoKey + "&q=" + loc + "&format=json&");
const coordsData = await coordsResponse.json();
lat = coordsData[0].lat;
lon = coordsData[0].lon;
const mapUrl = 'https://maps.locationiq.com/v3/staticmap?key=' + forwardGeoKey + '¢er=' + lat + ',' + lon + '&zoom='+'10' +'&size=550x325&format=png&maptype=street';
const mapData = await fetch(mapUrl);
// const coordsResponse = await fetch("https://geocoding-api.open-meteo.com/v1/search?name=" + location);
// const coordsData = await coordsResponse.json();
//get nice readable loaction name from openweather API
const reverseGeoUrl = "http://api.openweathermap.org/geo/1.0/reverse?lat=" + lat + "&lon=" + lon + "&limit=1&appid=" + reverseGeoKey;
const locationResponse = await fetch(reverseGeoUrl);
if(!locationResponse.ok) {
console.error("bad response: ", locationResponse);
} else {
const locationData = await locationResponse.json();
ws.send("location:" + JSON.stringify(locationData));
}
if(!mapData.ok) {
console.error("bad response: ", mapData);
} else {
ws.send('img:' + mapData.url);
}
const tz = tzlookup(lat, lon);
weatherApiUrl = "https://api.open-meteo.com/v1/forecast?latitude=" + lat +
"&longitude=" + lon +
"¤t=temperature_2m,relative_humidity_2m,is_day,precipitation,weather_code,cloud_cover,wind_speed_10m,wind_direction_10m&hourly=precipitation_probability,temperature_2m,relative_humidity_2m,precipitation,cloud_cover,wind_speed_10m,wind_direction_10m&daily=weather_code,temperature_2m_max,temperature_2m_min,sunrise,precipitation_probability_max,sunset,precipitation_sum&temperature_unit=fahrenheit&wind_speed_unit=mph&precipitation_unit=inch&past_days=3&forecast_days=8" +
"&timezone=" + tz;
} catch(error) {
console.error("error: ", error);
ws.send("bad response");
return { error: true }
}
//weather API call
try {
const response = await fetch(weatherApiUrl)
//send successful response to client with websocket
if (!response.ok) {
console.error("bad response: ", response);
} else {
const data = await response.json();
//send the response
ws.send(JSON.stringify(data));
}
} catch (error) {
console.error("fetch error: ", error);
return { error: true }
}
// }
}
//start express server on port 5500
app.listen(5500, () => {
console.log(`Server is listening on port ${5500}`);
});