Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Corey Sheldon
weather
Commits
dfe51d31
Unverified
Commit
dfe51d31
authored
Jan 24, 2017
by
David Haynes
Browse files
Update some comments, fix due to meteor update, npm init
- meteor npm init needed to install package
parent
2098cf60
Changes
4
Hide whitespace changes
Inline
Side-by-side
.gitignore
View file @
dfe51d31
...
...
@@ -18,3 +18,5 @@ Session.vim
# auto-generated tag files
tags
weather/node_modules
weather/imports/api/weather-data.js
View file @
dfe51d31
...
...
@@ -24,47 +24,3 @@ WeatherData.schema = new SimpleSchema({
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);
}
}
*/
weather/package.json
0 → 100644
View file @
dfe51d31
{
"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"
}
}
weather/server/methods.js
View file @
dfe51d31
//
Meteor
.
methods
({
getWeather
:
function
(
latitude
,
longitude
)
{
return
getWeather
(
latitude
,
longitude
);
}
});
/**
* [getWeather description]
* @param {[type]} latitude [description]
* @param {[type]} longitude [description]
* @return {[type]} [description]
*/
function
getWeather
(
latitude
,
longitude
)
{
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
)
{
console
.
log
(
"
Getting first time data.
"
)
var
weatherData
=
getWeatherFromAPI
(
latitude
,
longitude
);
...
...
@@ -17,54 +24,63 @@ function getWeather(latitude, longitude) {
var
date
=
new
Date
();
var
timeDiff
=
(
date
.
getTime
())
-
curWeatherData
.
retrievalTime
;
//Subtract the data timestamp form cur time to get age
if
(
timeDiff
>
(
Meteor
.
settings
.
weatherCacheExpireTime
*
10
))
{
//Data is expired, retrieve again.
//
Data is expired, retrieve again.
console
.
log
(
"
Data Age:
"
+
timeDiff
)
console
.
log
(
"
Cache expired. Retrieving...
"
)
var
weatherData
=
getWeatherFromAPI
(
latitude
,
longitude
);
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
;
//Return it
//
Return it
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
);
return
curWeatherData
;
}
}
else
{
console
.
log
(
"
Returning cached data.
"
)
//The data is still valid, return it
//
The data is still valid, return it
return
curWeatherData
;
}
}
/**
* [getWeatherFromAPI description]
* @param {[type]} latitude [description]
* @param {[type]} longitude [description]
* @return {[type]} [description]
*/
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
urlBase
=
"
https://api.darksky.net/forecast/
"
+
darkskyAPIKey
+
"
/
"
+
latitude
+
"
,
"
+
longitude
console
.
log
(
urlBase
);
try
{
//Make a blocking call
//I don't think we should add this.unblock() in case that comes up.
//
Make a blocking call
//
I don't think we should add this.unblock() in case that comes up.
var
result
=
HTTP
.
call
(
"
GET
"
,
urlBase
);
//Get current time
var
date
=
new
Date
();
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
=
{
retrievalTime
:
linuxTime
,
data
:
result
.
data
,
error
:
false
};
//Return it
//
Return it
return
weatherData
;
}
catch
(
e
)
{
}
catch
(
e
)
{
// Got a network error, time-out or HTTP error in the 400 or 500 range.
console
.
log
(
e
);
return
undefined
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment