-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation.js
281 lines (222 loc) · 8.54 KB
/
location.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
const weatherKey = 'a279ccc8d332ee8c593b4e7fcf96b089';
const timeKey = 'nishantchaudhari49g';
const token ='2ls3gmc5dm1bti24'
const mobile ="Mobile Number with Country Code"
const audio = "https://buibuibucket.s3.amazonaws.com/audio/Sunny+Day/Pharrell_Williams_-_Happy_CeeNaija.com_.mp3";
const message ="Hey love, as the rain falls, it reminds me of your laughter – a beautiful melody. Wish I could be there to dance in the rain with you. Miss you lots ❤❤❤✨"
class Location {
//declaration of all variables, preliminary setup
constructor(name, location, num) {
this.name = name;
this.location = location;
this.num = num;
this.weatherData = '';
this.lon;
this.lat;
this.temperatureMin;
this.temperatureMax;
this.temperature;
this.suniness;
this.humidity;
this.wind;
this.timeData = '';
this.time;
this.timezone;
this.sunrise;
this.sunset;
//loading feedback
document.getElementById(this.num + '-name').innerHTML = this.name;
document.getElementById(this.num + '-location').innerHTML = this.location;
document.getElementById(this.num + '-tags').innerHTML = 'Loading content.';
}
//load all data and fill page (uses .bind(this) to retain class context)
loadData () {
loadWeather(this.location, function(response) {
this.weatherData = JSON.parse(response);
if (this.weatherData == null || this.weatherData == '') {
document.getElementById(this.num + '-tags').innerHTML = "Couldn't load data.";
} else {
//fill all weather data variables
this.lon = this.weatherData['coord']['lon'];
this.lat = this.weatherData['coord']['lat'];
this.temperatureMin = kelvinToCelsius(this.weatherData['main']['temp_min']);
this.temperatureMax = kelvinToCelsius(this.weatherData['main']['temp_max']);
this.temperature = kelvinToCelsius(this.weatherData['main']['temp']);
this.suniness = this.weatherData['weather'][0]['description'];
this.humidity = this.weatherData['main']['humidity'];
this.wind = mpsToKmph(this.weatherData['wind']['speed']);
//load time data (depends on location, so fetched sequentially after location data)
loadTime(this.lon, this.lat, function(response) {
this.timeData = JSON.parse(response);
//create tag elements for time data
document.getElementById(this.num + '-tags').innerHTML = "";
//error if inappropriate response is returned
if (this.timeData == null || this.timeData == '') {
document.getElementById(this.num + '-tags').innerHTML = "Couldn't load time data.";
document.getElementById(this.num + '-tags').appendChild(document.createElement('br'));
document.getElementById(this.num + '-tags').appendChild(document.createElement('br'));
} else {
//fill all time data variables
this.time = formatTime(this.timeData['time']);
//color panel to express call appropriateness
var left = document.getElementById('left');
var right = document.getElementById('right');
if (parseInt(this.time.substring(0, 2)) < earliestCall && parseInt(this.time.substring(0, 2)) >= latestCall) {
document.getElementById(this.num + '-panel').style.backgroundColor = '#ccc';
document.getElementById(this.num + '-panel').style.color = '#fff';
document.getElementById(this.num + '-tags').style.color = '#fff';
document.getElementById(this.num + '-tags').style.borderColor = '#fff';
} else {
document.getElementById(this.num + '-panel').style.color = '#777';
document.getElementById(this.num + '-tags').style.color = '#777';
document.getElementById(this.num + '-tags').style.borderColor = '#ccc';
}
this.timezone = this.timeData['dstOffset'];
this.sunrise = formatTime(this.timeData['sunrise']);
this.sunset = formatTime(this.timeData['sunset']);
//add subtitle data (time and temperature)
document.getElementById(this.num + '-main-data').innerHTML = this.time + ', ' + this.temperature + '°';
//create tag elements for time data
createTag(this.time, this.num + '-tags');
createTag('UTC' + this.timezone, this.num + '-tags');
createTag('◒ ' + this.sunrise, this.num + '-tags');
createTag('◓ ' + this.sunset, this.num + '-tags');
document.getElementById(this.num + '-tags').appendChild(document.createElement('br'));
}
//create tag elements for weather data
createTag(this.temperature + '°', this.num + '-tags');
createTag('> ' + this.temperatureMin + '°', this.num + '-tags');
createTag('< ' + this.temperatureMax + '°', this.num + '-tags');
createTag(this.suniness, this.num + '-tags');
createTag(this.humidity + '%', this.num + '-tags');
createTag(this.wind + ' km/h', this.num + '-tags');
}.bind(this));
}
}.bind(this));
}
}
//-------HELPERS
function sendWhatsapp () {
// Sending Whatsapp Message
var data = "token="+token+"&to="+mobile+"&body="+message;
var xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
xhr.open("POST", "https://api.ultramsg.com/instance70153/messages/chat");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
}
function sendAudio() {
var data = "token="+token+"&to="+mobile+"=&audio="+audio;
var xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
xhr.open("POST", "https://api.ultramsg.com/instance70153/messages/audio");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
}
//get weather data from openweathermap
function loadWeather(loc, callback) {
var xhr = new XMLHttpRequest();
var link = 'https://api.openweathermap.org/data/2.5/weather?q=' + loc + '&APPID=' + weatherKey;
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
if (xhr.status == 200) {
clearTimeout(xmlHttpTimeout);
// console.log(xhr.responseText);
var responseData = JSON.parse(xhr.responseText); // Parse JSON response
// Access relevant properties
var weatherDescription = responseData.weather[0].description;
var cityName = responseData.name;
// Use the data as needed
console.log('City Name:', cityName);
if(cityName == "Bengaluru") {
console.log('Weather Description:', weatherDescription);
sendWhatsapp();
sendAudio();
}
callback(xhr.responseText);
}
else if (xhr.status == 400) {
console.log('There was an error 400: ' + xhr.responseText);
callback(null);
} else {
console.log('Something else other than 200 was returned: ' + xhr.status);
callback(null);
}
}
};
xhr.open("GET", link, true);
xhr.send();
//set timeout
var xmlHttpTimeout = setTimeout(ajaxTimeout, 40000);
function ajaxTimeout(){
xhr.abort();
callback(null);
}
}
//get time data from geonames
function loadTime(lon, lat, callback) {
var xhr = new XMLHttpRequest();
var link = ' https://api.geonames.org/timezoneJSON?lat=' + lat + '&lng=' + lon + '&username=' + timeKey;
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
if (xhr.status == 200) {
clearTimeout(xmlHttpTimeout);
callback(xhr.responseText);
}
else if (xhr.status == 400) {
console.log('There was an error 400: ' + xhr.responseText);
callback(null);
} else {
console.log('Something else other than 200 was returned: ' + xhr.status);
callback(null);
}
}
};
xhr.open("GET", link, true);
xhr.send();
//set timeout
var xmlHttpTimeout = setTimeout(ajaxTimeout, 40000);
function ajaxTimeout(){
xhr.abort();
callback(null);
}
}
//math and string helpers
function kelvinToCelsius(n) {
return roundToSingleDecimal(n - 273.15);
}
function roundToSingleDecimal(n) {
return Math.round(n * 10) / 10;
}
function mpsToKmph(n) {
return roundToSingleDecimal(n * 3.6);
}
function formatTime(s) {
return s.substring(s.length - 5);
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
//create and append tag element
function createTag(content, loc) {
var container = document.createElement('DIV');
container.className = 'tag-holder';
var text = document.createElement('SPAN');
var textNode = document.createTextNode(content);
text.appendChild(textNode);
text.className = 'tag-content';
text.innerHTML = content;
container.appendChild(text);
var location = document.getElementById(loc);
location.appendChild(container);
}