forked from kaysabelle/smokeythebear
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
283 lines (250 loc) · 8.27 KB
/
index.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
281
282
283
const LA_PANZA = 44904
const LAS_TABLAS = 44914
const SLO = 44915
const SAN_SIMEON = 44917
function init()
{
updateSmokey();
}
function low()
{
document.getElementById("smokey").setAttribute('src', './img/low.png');
}
function moderate()
{
document.getElementById("smokey").setAttribute('src', './img/moderate.png');
}
function high()
{
document.getElementById("smokey").setAttribute('src', './img/high.png');
}
function veryhigh()
{
document.getElementById("smokey").setAttribute('src', './img/veryhigh.png');
}
function extreme()
{
document.getElementById("smokey").setAttribute('src', './img/extreme.png');
}
function getTimestamps(station)
{
params = [ {name: "station", value: station}];
console.log("Getting Timestamps Test");
jQuery.get('./xml/timestamps', function(data, params) {
var stamps = data.split('\n');
var times = ['', '', '', ''];
// stamps[0]/times[0]: La Panza
// stamps[1]/times[0]: Las Tablas
// stamps[2]/times[0Grande
// stamps[3]/times[0]: San Simeon
/* Current time, in seconds */
var now = Math.floor((new Date).getTime()/1000);
//console.log(now);
for(var i=0; i<times.length; i++) {
/* Timestamps are in seconds, so get time in hours */
var units = "";
var hours = ((now - stamps[i])/3600);
//console.log(hours);
if (hours < 1) {
hours *= 60;
units = " minute";
}
else {
units = " hour";
}
hours = Math.floor(hours);
if (hours > 1) { //plurality
units += "s";
}
times[i] = hours + units;
}
if (station == "LP") document.getElementById("update_status").innerHTML = "Last updated " + times[0] + " ago";
if (station == "LT") document.getElementById("update_status").innerHTML = "Last updated " + times[1] + " ago";
if (station == "AG") document.getElementById("update_status").innerHTML = "Last updated " + times[2] + " ago";
if (station == "SLC") document.getElementById("update_status").innerHTML = "Last updated " + times[3] + " ago";
});
}
function getICIndex(ic)
{
console.log("Getting IC Index Test");
if (ic >= 0 && ic <= 20) {
return 0;
}
else if (ic >= 21 && ic <= 45) {
return 1;
}
else if (ic >= 46 && ic <= 65) {
return 2;
}
else if (ic >= 66 && ic <= 80) {
return 3;
}
else {
return 4;
}
}
function calc_rating(sl, ic, id)
{
console.log("Calculate Ratings Test");
var rating_matrix = [
['L', 'L', 'L', 'M', 'M'],
['L', 'M', 'M', 'M', 'H'],
['M', 'M', 'H', 'H', 'V'],
['M', 'H', 'V', 'V', 'E'],
['H', 'V', 'V', 'E', 'E']];
var ic_result = getICIndex(ic);
var rating_result = rating_matrix[sl - 1][ic_result];
console.log("RATING BELOW for " + id + " based on sl=" + sl + " and ic=" + ic + ": ");
console.log(rating_result);
switch (rating_result) {
case 'L':
low();
break;
case 'M':
moderate();
break;
case 'H':
high();
break;
case 'V':
veryhigh();
break;
case 'E':
extreme();
break;
default:
console.log("Something went wrong\n");
break;
}
console.log("Finished calcing rating.\n");
}
function updateSmokey()
{
console.log("Update Smokey Test");
var selection = document.getElementById("zone_selection");
var selected_area = document.getElementById("selected_area");
var selected_city = document.getElementById("selected_city");
var value = selection.options[selection.selectedIndex].value;
switch (value) {
case "AG":
data = getXML(SLO);
selected_area.innerHTML = 'Coastal Valley';
selected_city.innerHTML = 'San Luis Obispo';
break;
case "LP":
data = getXML(LA_PANZA);
selected_area.innerHTML = 'Inland Valley';
selected_city.innerHTML = 'La Panza';
break;
case "LT":
data = getXML(LAS_TABLAS);
selected_area.innerHTML = 'Coast Range';
selected_city.innerHTML = 'Las Tablas';
break;
case "SLC":
data = getXML(SAN_SIMEON);
selected_area.innerHTML = 'San Luis Coast';
selected_city.innerHTML = 'San Simeon';
break;
default:
console.log("Something went wrong updating smokey.\n");
}
getTimestamps(value);
readJSON(data, value, selected_city.innerHTML);
}
function getXML(stationID){
console.log("Get XML Test");
const start = "27-Jun-20";
const end = "28-Jun-20";
var url = `https://fam.nwcg.gov/wims/xsql/nfdrs.xsql?stn=${stationID}&start=${start}&end=${end}&user=4e1`;
window.open(url);
return fetchXML(url);
}
async function fetchXML(url) {
console.log("Fetch XML Test: URL");
console.log(url);
const res = await fetch(url,{mode: "no-cors"});
const data = await res.body;
console.log(res);
return data;
}
function readJSON(xml, station, city)
{
console.log("Read JSON Test 1");
var params = [
{ name: "station", value: station },
{ name:"city", value: city }
];
console.log("Read JSON Test 2");
jQuery.get(xml, function(data, params)
{
var json = xmlToJson(xml);
console.log("Data: "+json);
console.log("GOT JSON! FOR " + city);
console.log(json.hasOwnProperty("nfdrs"));
if (json.hasOwnProperty("nfdrs") && json.nfdrs.hasOwnProperty("row")) {
console.log("row exists");
for (var i = 0; i < json.nfdrs.row.length; i++) {
var curEntry = json.nfdrs.row[i];
if ((station == "LT" || station == "LP") && curEntry.nfdr_type['#text'] == "O" && curEntry.msgc['#text'] == "7G3A2")
{
calc_rating(curEntry.sl['#text'], curEntry.ic['#text'], station);
console.log("Smokey's Adjective Fire Danger Rating for " + city + " is up to date.");
break;
}
if ((station == "SLC" || station == "AG") && curEntry.nfdr_type['#text'] == "O" && curEntry.msgc['#text'] == "7G2A2")
{
calc_rating(curEntry.sl['#text'], curEntry.ic['#text'], station);
console.log("Smokey's Adjective Fire Danger Rating for " + city + " is up to date.");
break;
}
}
if (i == json.nfdrs.row.length) {
console.log("The Adjective Fire Danger Rating for " + city + " has not yet been updated today.");
document.getElementById("update_status").innerHtml = "*This station's rating is not up to date";
}
}
else {
console.log("The Adjective Fire Danger Rating for " + city + " has not yet been updated today.");
document.getElementById("update_status").innerHtml = "*This station's rating is not up to date";
}
console.log("Got " + city + " Fire Rating");
});
console.log("Read JSON Test End");
}
function xmlToJson(xml)
{
console.log("XML To JSON Test");
// Create the return object
var obj = {};
if (xml.nodeType == 1) { // element
// do attributes
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
for (var j = 0; j < xml.attributes.length; j++) {
var attribute = xml.attributes.item(j);
obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
}
}
} else if (xml.nodeType == 3) { // text
obj = xml.nodeValue;
}
// do children
if (xml.hasChildNodes()) {
for(var i = 0; i < xml.childNodes.length; i++) {
var item = xml.childNodes.item(i);
var nodeName = item.nodeName;
if (typeof(obj[nodeName]) == "undefined") {
obj[nodeName] = xmlToJson(item);
} else {
if (typeof(obj[nodeName].push) == "undefined") {
var old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xmlToJson(item));
}
}
}
return obj;
};