Unverified Commit dfe51d31 authored by David Haynes's avatar David Haynes
Browse files

Update some comments, fix due to meteor update, npm init

- meteor npm init needed to install package
parent 2098cf60
...@@ -18,3 +18,5 @@ Session.vim ...@@ -18,3 +18,5 @@ Session.vim
# auto-generated tag files # auto-generated tag files
tags tags
weather/node_modules
...@@ -24,47 +24,3 @@ WeatherData.schema = new SimpleSchema({ ...@@ -24,47 +24,3 @@ WeatherData.schema = new SimpleSchema({
label: "The last time that this campus's current forecast was updated" label: "The last time that this campus's current forecast was updated"
} }
}) })
//I'm pretty sure these functions aren't used in the project anymore
/*
Meteor.methods({
'weatherDataForLoc': function (lat, long) {
// API_KEY is an environmental veriable, you can set it with a JS file in
// server/lib or in your server control panel.
var API_KEY = process.env.API_KEY;
var apiURL = 'https://api.forecast.io/forecast/' + API_KEY + '/' + lat + ',' + long;
var response = HTTP.get(apiURL).data;
return response;
}
});
export function refreshData() {
for(var key in LOCATIONS) {
const current = LOCATIONS[key];
var currentData
Meteor.call('weatherDataForLoc', current.lat, current.long, function(err, res){
currentData = res
});
/**
WeatherData.insert ({
name: current.name,
lat: current.lat,
long: current.long,
data: currentData,
lastUpdated: new Date(),
});
*/
/*
let location = {
name: current.name,
lat: current.lat,
long: current.long,
data: currentData,
lastUpdated: new Date(),
}
WeatherData.schema.validate(location);
WeatherData.insert(location);
}
}
*/
{
"name": "weather",
"version": "1.0.0",
"description": "",
"main": "collections.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-runtime": "^6.22.0"
}
}
//
Meteor.methods({ Meteor.methods({
getWeather: function(latitude, longitude) { getWeather: function(latitude, longitude) {
return getWeather(latitude, longitude); return getWeather(latitude, longitude);
} }
}); });
/**
* [getWeather description]
* @param {[type]} latitude [description]
* @param {[type]} longitude [description]
* @return {[type]} [description]
*/
function getWeather(latitude, longitude) { function getWeather(latitude, longitude) {
var curWeatherData = WeatherData.find({}, {sort: {retrievalTime: -1, limit: 1}}).fetch()[0]; var curWeatherData = WeatherData.find({}, {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); var weatherData = getWeatherFromAPI(latitude, longitude);
...@@ -17,54 +24,63 @@ function getWeather(latitude, longitude) { ...@@ -17,54 +24,63 @@ function getWeather(latitude, longitude) {
var date = new Date(); var date = new Date();
var timeDiff = (date.getTime()) - curWeatherData.retrievalTime; //Subtract the data timestamp form cur time to get age var timeDiff = (date.getTime()) - curWeatherData.retrievalTime; //Subtract the data timestamp form cur time to get age
if(timeDiff > (Meteor.settings.weatherCacheExpireTime*10)) { if(timeDiff > (Meteor.settings.weatherCacheExpireTime*10)) {
//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(latitude, longitude);
if(weatherData === undefined) { if(weatherData === undefined) {
//There was an error. Mark the latest record as error state. // There was an error. Mark the latest record as error state.
curWeatherData.error = true; curWeatherData.error = true;
//Return it // Return it
return curWeatherData; return curWeatherData;
} else { }
//Technically the else isn't needed but it looks nice. else {
// Technically the else isn't needed but it looks nice.
//Save this data. // Save this data.
WeatherData.insert(weatherData); WeatherData.insert(weatherData);
return curWeatherData; return curWeatherData;
} }
} else { } else {
console.log("Returning cached data.") console.log("Returning cached data.")
//The data is still valid, return it // The data is still valid, return it
return curWeatherData; return curWeatherData;
} }
} }
/**
* [getWeatherFromAPI description]
* @param {[type]} latitude [description]
* @param {[type]} longitude [description]
* @return {[type]} [description]
*/
function getWeatherFromAPI(latitude, longitude) { function getWeatherFromAPI(latitude, longitude) {
//Keep the key out of the keybase // Keep the key out of the keybase
var darkskyAPIKey = process.env.DARKSKY_API_KEY; var darkskyAPIKey = process.env.DARKSKY_API_KEY;
var urlBase = "https://api.darksky.net/forecast/"+darkskyAPIKey+"/"+latitude+","+longitude var urlBase = "https://api.darksky.net/forecast/"+darkskyAPIKey+"/"+latitude+","+longitude
console.log(urlBase); console.log(urlBase);
try { try {
//Make a blocking call // Make a blocking call
//I don't think we should add this.unblock() in case that comes up. // I don't think we should add this.unblock() in case that comes up.
var result = HTTP.call("GET", urlBase); var result = HTTP.call("GET", urlBase);
//Get current time //Get current time
var date = new Date(); var date = new Date();
var linuxTime = date.getTime(); //convert millis to seconds var linuxTime = date.getTime(); //convert millis to seconds
//Store the retrieval time with the data. We shouldn't get the same data every time someone goes to the page. // Store the retrieval time with the data. We shouldn't get the same data every time someone goes to the page.
var weatherData = { var weatherData = {
retrievalTime: linuxTime, retrievalTime: linuxTime,
data: result.data, data: result.data,
error: false error: false
}; };
//Return it // Return it
return weatherData; return weatherData;
} catch (e) { }
catch (e) {
// Got a network error, time-out or HTTP error in the 400 or 500 range. // Got a network error, time-out or HTTP error in the 400 or 500 range.
console.log(e); console.log(e);
return undefined; return undefined;
......
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