diff --git a/.gitignore b/.gitignore index ba1fafd..3bf8a4d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ node_modules public/ePub views/header2.ejs index-temp.ejs +utils.js +public/AirQIdx_Guide.png \ No newline at end of file diff --git a/crypto.js b/crypto.js index 968bfc1..295487b 100644 --- a/crypto.js +++ b/crypto.js @@ -8,6 +8,21 @@ const decrypt = (data) => { return CryptoJS.enc.Base64.parse(data).toString(CryptoJS.enc.Utf8); }; +const toLocalDt = (unix_ts) => { + // Create a new JavaScript Date object based on the timestamp + // multiplied by 1000 so that the argument is in milliseconds, not seconds. + var date = new Date(unix_ts * 1000); + // Hours part from the timestamp + var hours = date.getHours(); + // Minutes part from the timestamp + var minutes = "0" + date.getMinutes(); + // Seconds part from the timestamp + var seconds = "0" + date.getSeconds(); + + // Will display time in 10:30:23 format + return hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2); +} + function test() { let enStr = encrypt("e88e1a8096cd4897b79b230a9c49b243"); console.log("Encrypted Key: " + enStr); @@ -16,4 +31,4 @@ function test() { } // test(); -export {encrypt, decrypt}; \ No newline at end of file +export {encrypt, decrypt, toLocalDt}; \ No newline at end of file diff --git a/index.js b/index.js index 799048b..1d15224 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,8 @@ dotenv.config() import request from 'request'; import express from 'express'; import bodyParser from 'body-parser'; -import {encrypt, decrypt} from './crypto.js' +import {encrypt, decrypt} from './crypto.js'; + // Create network routing const app = express(); @@ -31,7 +32,7 @@ app.get('/weatherbit', (req, res) => { // console.dir(req.params) // console.dir(req.body); let apikey = encrypt(process.env.WEATHERBIT_KEY); - res.render('pages/weatherbit', { key: apikey}); + res.render('pages/weatherbit', { key: apikey }); }) // Posting data to the client-side requires two API calls. diff --git a/public/utils.ejs b/public/utils.ejs new file mode 100644 index 0000000..dcf3d75 --- /dev/null +++ b/public/utils.ejs @@ -0,0 +1,18 @@ +<% + +unixTsToLocalTime = (unix_ts) => { + // Create a new JavaScript Date object based on the timestamp + // multiplied by 1000 so that the argument is in milliseconds, not seconds. + var date = new Date(unix_ts * 1000); + // Hours part from the timestamp + var hours = date.getHours(); + // Minutes part from the timestamp + var minutes = "0" + date.getMinutes(); + // Seconds part from the timestamp + var seconds = "0" + date.getSeconds(); + + // Will display time in 10:30:23 format + return hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2); +} + +%> \ No newline at end of file diff --git a/views/pages/weatherbit.ejs b/views/pages/weatherbit.ejs index 2bf5fd1..9239651 100644 --- a/views/pages/weatherbit.ejs +++ b/views/pages/weatherbit.ejs @@ -3,13 +3,14 @@ <%- include('./../partials/head'); %> + <%- include ('./../../public/utils'); %>
<%- include('./../partials/header'); %>
-
+

@@ -27,7 +28,7 @@
Current Conditions
-
+
<% if( typeof(error)==="undefined" || curStatus===400 ){ %>

Waiting for data.

<% } else { %> @@ -62,7 +63,7 @@ <%= curData.app_temp %> - Relative humidity (%): + Relative humidity(%): <%= curData.rh %> @@ -84,12 +85,7 @@ Wind Speed(m/s): - <%= curData.wind_spd %> - - - Wind Direction: - - <%= curData.wind_cdir_full %> + <%= curData.wind_spd %> <%= curData.wind_cdir_full %> Snowfall(mm/hr): @@ -121,27 +117,38 @@ <% } %>
+
Current Air Quality
-
+
<% if( typeof(error)==="undefined" || airqStatus===400 ) { %>

Waiting for data.

<% } else { %>

Local hourly: <%= airqData.timestamp_local.split('T')[1] %> -
+


- + <% if (airqData.aqi <= 50) { %> + + <% } else if (airqData.api >= 51 && airqData.api <= 100) { %> + + <% } else if (airqData.api >= 101 && airqData.api <= 150) { %> + + <% } else if (airqData.api >= 151 && airqData.api <= 200) { %> + + <% } else if (airqData.api >= 201 && airqData.api <= 300) { %> + + <% } else if (airqData.api >= 301) { %> + + <% } %> @@ -190,76 +197,70 @@
Extended Forecast
-
+
<% if( typeof(error)==="undefined" || foreStatus===400 ){ %>

Waiting for data.

<% } else { %>
Air Quality Index: - <%= airqData.aqi %> - <%= airqData.aqi %> (Good) <%= airqData.aqi %> (Moderate) <%= airqData.aqi %> (Sensitive to Alergy) <%= airqData.aqi %> (Unhealthy) <%= airqData.aqi %> (Very unhealthy) <%= airqData.aqi %> (Hazardous)
O3 Concentration(µg/m³):
- - - - - - - - - - - - - - + + + + + + + + + + + + + + <% let forecast=foreData.data; %> <% for (let i=0; i < forecast.length; i++) { %> - - - - - - - - - - - - - - - @@ -274,6 +275,9 @@ <% let longi, lati = "" %>
DateConditionMax/Min TemperatureTemperature(F)PressureHumidityWind GustWind SpeedWind DirectionCloud CoverVisibilityUV IndexSunrise/SunsetMoonphase Date Condition Max/Min Temperature(°F) Temperature(°F) Pressure(㎧) Humidity Wind Speed Wind Gust Cloud Cover(%) Visibility UV Index Sunrise/Sunset Moonphase
+ <%= forecast[i].datetime %> + <%= forecast[i].weather.description %> - <%= forecast[i].app_max_temp %> , <%= forecast[i].app_min_temp %> + + <%= forecast[i].app_max_temp %>/<%= forecast[i].app_min_temp %> + <%= forecast[i].temp %> - <%= forecast[i].slp %> + + <%= forecast[i].pres %> + <%= forecast[i].rh %> - <%= forecast[i].windgust %> - - <%= forecast[i].wind_spd %> - + <%= forecast[i].wind_gust_spd %> - <%= forecast[i].wind_cdir_full %> + + <%= forecast[i].wind_gust_spd %> <%= forecast[i].wind_cdir %> + <%= forecast[i].clouds %> + <%= forecast[i].vis %> + <%= forecast[i].uv %> - <%= forecast[i].sunrise_ts %> , <%= forecast[i].sunset_ts %> + + <%= unixTsToLocalTime(forecast[i].sunrise_ts) %> / <%= unixTsToLocalTime(forecast[i].sunset_ts) %> + <%= forecast[i].moon_phase_lunation %>