Commit 92c30966 authored by David Haynes's avatar David Haynes 🙆
Browse files

Merge commit '527713e2' into issue28

parents ef021983 527713e2
cd weather
meteor --settings config/settings.json
cd ..
...@@ -9,39 +9,19 @@ ...@@ -9,39 +9,19 @@
</div> </div>
<!-- Tab Bar Container , and Tab links --> <!-- Tab Bar Container , and Tab links -->
<div class="mdl-layout__tab-bar mdl-js-ripple-effect header-bg" style="background: #006633;"> <div class="mdl-layout__tab-bar mdl-js-ripple-effect header-bg" style="background: #006633;">
<a id="fairfax-tab-button" href="#fairfax" class="mdl-layout__tab is-active">Fairfax</a> <a id="fairfax-tab-button" href="#weather" class="mdl-layout__tab is-active">Fairfax</a>
<!--a href="#arlington" class="mdl-layout__tab">Arlington</a> <a id="arlington-tab-button" href="#weather" class="mdl-layout__tab">Arlington</a>
<a href="#scitech" class="mdl-layout__tab">SciTech</a> <a id="scitech-tab-button" href="#weather" class="mdl-layout__tab">SciTech</a>
<a href="#korea" class="mdl-layout__tab">Mason Korea</a --> <a id="korea-tab-button" href="#weather" class="mdl-layout__tab">Mason Korea</a>
</div> </div>
</header> </header>
<!-- MDL Layout Header --> <!-- MDL Layout Header -->
<main class="mdl-layout__content"> <main class="mdl-layout__content">
<!-- "is-active" class to set the default active tab --> <section class="mdl-layout__tab-panel is-active" id="weather">
<section class="mdl-layout__tab-panel is-active" id="fairfax">
<div class="page-content"> <div class="page-content">
<div class="page-content">{{> weather}}</div> <div class="page-content">{{> weather}}</div>
</div> </div>
</section> </section>
<!--
<section class="mdl-layout__tab-panel" id="arlington">
<div class="page-content">
<div class="page-content">{{> weather}}</div>
</div>
</section>
<section class="mdl-layout__tab-panel" id="scitech">
<div class="page-content">
<div class="page-content">{{> weather}}</div>
</div>
</section>
<section class="mdl-layout__tab-panel" id="korea">
<div class="page-content">
<div class="page-content">{{> weather}}</div>
</div>
</section>
-->
{{> footer}} {{> footer}}
</main> </main>
</div> </div>
......
Template.home.onRendered(function(){
// Add listeners to each tab to change the current location.
// When locationName changes, the weather data is obained for the new location.
//TODO: Use Meteor's built in event system (see below)
document.getElementById('fairfax-tab-button').addEventListener('click', function(){
Session.set('locationName', 'FAIRFAX');
});
document.getElementById('arlington-tab-button').addEventListener('click', function(){
Session.set('locationName', 'ARLINGTON');
});
document.getElementById('scitech-tab-button').addEventListener('click', function(){
Session.set('locationName', 'SCITECH');
});
document.getElementById('korea-tab-button').addEventListener('click', function(){
Session.set('locationName', 'KOREA');
});
});
/*
This is the proper way to handle event listeners in Meteor, but
for some reason the events don't fire this way. Can't find a fix, so I
implmented them using normal JavaScript + HTML seen above.
Template.home.events({ Template.home.events({
'click #fairfax-tab-button' (event) { 'click #fairfax-tab-button': function() {
Session.set('locationName', 'FAIRFAX');
}, },
'click a.mdl-layout__tab-bar' (event) { 'click #arlington-tab-button': function() {
console.log(event); Session.set('locationName', 'ARLINGTON');
},
'click #scitech-tab-button': function() {
Session.set('locationName', 'SCITECH');
},
'click #korea-tab-button': function() {
Session.set('locationName', 'KOREA');
} }
}); });
Template.home.onRendered(function() { */
document.getElementById("#fairfax-tab-button").addEventListener("click", function(){
});
});
...@@ -101,7 +101,7 @@ Template.weather.helpers({ ...@@ -101,7 +101,7 @@ Template.weather.helpers({
}, },
getDayNameFromTime: function(timestamp) { getDayNameFromTime: function(timestamp) {
var given = new Date(timestamp * 1000); var given = new Date(timestamp * 1000);
console.log(given); //console.log(given);
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
return days[ given.getDay() ]; return days[ given.getDay() ];
}, },
...@@ -147,13 +147,16 @@ Template.weather.helpers({ ...@@ -147,13 +147,16 @@ Template.weather.helpers({
var weatherData = {}; var weatherData = {};
var weatherDataDependency = new Tracker.Dependency; var weatherDataDependency = new Tracker.Dependency;
Template.weather.onCreated(function(){ Template.weather.onCreated(function(){
// Set default location to fairfax
Session.set("locationName", "FAIRFAX"); Session.set("locationName", "FAIRFAX");
// Function that is ran when any dependencies change
Tracker.autorun(function () { Tracker.autorun(function () {
var locName = Session.get("locationName"); var locName = Session.get("locationName");
var location = LOCATIONS[locName]; var location = LOCATIONS[locName];
// Get weather for current location
Meteor.call('getWeather', location.lat, location.long, function(error, result) { Meteor.call('getWeather', location, function(error, result) {
weatherData = result; weatherData = result;
document.title = "SRCT Weather • "+Math.round(result.data.currently.temperature)+"° F" document.title = "SRCT Weather • "+Math.round(result.data.currently.temperature)+"° F"
weatherDataDependency.changed(); weatherDataDependency.changed();
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
* https://guide.meteor.com/methods.html * https://guide.meteor.com/methods.html
*/ */
Meteor.methods({ Meteor.methods({
getWeather: function(latitude, longitude) { getWeather: function(location) {
return getWeather(latitude, longitude); return getWeather(location);
} }
}); });
...@@ -15,21 +15,23 @@ Meteor.methods({ ...@@ -15,21 +15,23 @@ Meteor.methods({
* Gets the most recent wether data based on the given Lat. and Long. and then * Gets the most recent wether data based on the given Lat. and Long. and then
* returns it. Only gets new data if we have left the acceptable cache time. * returns it. Only gets new data if we have left the acceptable cache time.
* *
* @param latitude - A long that represents the Lat. of the weather data we * @param location - A dictionary from locations.js containing latitude, longitude,
* want. * and name of the campus we want to retrive the weather for.
* @param longitude - A long that represents the Long. of the weather data we *
* want.
* @return weatherData - An object holding the API call data and retrieval time. * @return weatherData - An object holding the API call data and retrieval time.
*/ */
function getWeather(latitude, longitude) { function getWeather(location) {
var curWeatherData = WeatherData.find({}, {sort: {retrievalTime: -1, limit: 1}}).fetch()[0]; // Query the database for the weather data at given location
var curWeatherData = WeatherData.find({ locationName: { $eq: location.name }}, {sort: {retrievalTime: -1, limit: 1}}).fetch()[0];
// Check if there is data at all // Check if there is data at all
if(curWeatherData === undefined) { if(curWeatherData === undefined) {
console.log("Getting first time data.") console.log("Getting first time data.")
var weatherData = getWeatherFromAPI(latitude, longitude); // If not, get data from API
var weatherData = getWeatherFromAPI(location.lat, location.long);
// Insert data into database
WeatherData.insert(weatherData); WeatherData.insert(weatherData);
return curWeatherData; return weatherData;
} }
// Get current time // Get current time
...@@ -47,7 +49,7 @@ function getWeather(latitude, longitude) { ...@@ -47,7 +49,7 @@ function getWeather(latitude, longitude) {
// Data is expired, retrieve again. // Data is expired, retrieve again.
console.log("Data Age: " + timeDiff) console.log("Data Age: " + timeDiff)
console.log("Cache expired. Retrieving...") console.log("Cache expired. Retrieving...")
var weatherData = getWeatherFromAPI(latitude, longitude); var weatherData = getWeatherFromAPI(location.lat, location.long);
// Check if there was an error in the API call. // Check if there was an error in the API call.
if(weatherData === undefined) { if(weatherData === undefined) {
...@@ -93,9 +95,27 @@ function getWeatherFromAPI(latitude, longitude) { ...@@ -93,9 +95,27 @@ function getWeatherFromAPI(latitude, longitude) {
var date = new Date(); var date = new Date();
var linuxTime = date.getTime(); // convert millis to seconds var linuxTime = date.getTime(); // convert millis to seconds
// Find which location the data is for
var location = "undefined";
switch(latitude) {
case LOCATIONS.FAIRFAX.lat:
location = LOCATIONS.FAIRFAX.name;
break;
case LOCATIONS.ARLINGTON.lat:
location = LOCATIONS.ARLINGTON.name;
break;
case LOCATIONS.SCITECH.lat:
location = LOCATIONS.SCITECH.name;
break;
case LOCATIONS.KOREA.lat:
location = LOCATIONS.KOREA.name;
break;
}
// Store the retrieval time with the data. We shouldn't get the same data // Store the retrieval time with the data. We shouldn't get the same data
// every time someone goes to the page. // every time someone goes to the page.
var weatherData = { var weatherData = {
locationName: location,
retrievalTime: linuxTime, retrievalTime: linuxTime,
data: result.data, data: result.data,
error: false error: false
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment