-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_extension.js
130 lines (114 loc) · 3.61 KB
/
weather_extension.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
/* Extension demonstrating a blocking reporter block */
/* Sayamindu Dasgupta <[email protected]>, May 2014 */
/* Kreg Hanning <[email protected]>, July 2016 */
(function(ext) {
// You will need to obtain an API key to query
// the OpenWeatherMap.org server
// https://openweathermap.org/api
var APPID = 'INSERT_API_KEY_HERE';
var cacheDuration = 1800000 //ms, 30 minutes
var cachedTemps = {};
var units = 'imperial';
function getWeatherData(weatherData, type) {
var val = null;
switch (type) {
case 'temperature':
val = weatherData.main.temp;
if (units === 'metric')
val = (val - 32) * (5/9)
val = Math.round(val);
break;
case 'weather':
val = weatherData.weather[0].description;
break;
case 'humidity':
val = weatherData.main.humidity;
break;
case 'wind speed':
val = weatherData.wind.speed;
if (units === 'imperial')
val *= 2.23694;
if (Math.round(val) !== val)
val = val.toFixed(1);
break;
case 'cloudiness':
val = weatherData.clouds.all;
break;
}
return(val);
}
function fetchWeatherData(location, callback) {
if (location in cachedTemps &&
Date.now() - cachedTemps[location].time < cacheDuration) {
//Weather data is cached
callback(cachedTemps[location].data);
return;
}
// Make an AJAX call to the Open Weather Maps API
$.ajax({
url: 'http://api.openweathermap.org/data/2.5/weather',
data: {q: location, units: 'imperial', appid: APPID},
dataType: 'jsonp',
success: function(weatherData) {
//Received the weather data. Cache and return the data.
cachedTemps[location] = {data: weatherData, time: Date.now()};
callback(weatherData);
}
});
}
// Cleanup function when the extension is unloaded
ext._shutdown = function() {};
// Status reporting code
// Use this to report missing hardware, plugin or unsupported browser
ext._getStatus = function() {
return {status: 2, msg: 'Ready'};
};
ext.getWeather = function(type, location, callback) {
fetchWeatherData(location, function(data) {
var val = getWeatherData(data, type);
callback(val);
});
};
ext.whenWeather = function(type, location, op, val) {
if (!cachedTemps[location]) {
//Weather data not cached
//Fetch it and return false for now
fetchWeatherData(location, function(){});
return false;
}
//Weather data is cached, no risk of blocking
var data = getWeatherData(cachedTemps[location].data, type);
switch (op) {
case '<':
return (data < val);
case '=':
return (data == val);
case '>':
return (data > val);
}
};
ext.setUnits = function(format) {
units = format;
return;
};
ext.getUnits = function() {
return units;
};
// Block and block menu descriptions
var descriptor = {
blocks: [
['R', '%m.reporterData in %s', 'getWeather', 'temperature', 'Boston, MA'],
['h', 'when %m.eventData in %s is %m.ops %n', 'whenWeather', 'temperature', 'Boston, MA', '>', 80],
[' ', 'set units to %m.units', 'setUnits', 'imperial'],
['r', 'unit format', 'getUnits']
],
menus: {
reporterData: ['temperature', 'weather', 'humidity', 'wind speed', 'cloudiness'],
eventData: ['temperature', 'humidity', 'wind speed', 'cloudiness'],
ops: ['>','=', '<'],
units: ['imperial', 'metric']
}
};
// Register the extension
ScratchExtensions.register('Weather extension', descriptor, ext);
})({});