Skip to content

Commit 31ef67e

Browse files
simont77naofireblade
authored andcommitted
Various small improvements (#49)
Added option to set serial number Changed history timer to make use of advantages in newer fakegato version Fixed precipitation calculation in dark sky api Code cleaned up
1 parent 722dfda commit 31ef67e

File tree

8 files changed

+161
-166
lines changed

8 files changed

+161
-166
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,9 @@
117117
* Added option to rename forecastAccessory
118118
* Added plugin version to Accessory Info
119119
* Updated debug dependency
120+
121+
## 2.6.0
122+
123+
* Added option to set serial number
124+
* Changed history timer to make use of advantages in newer fakegato version
125+
* Fixed precipitation calculation in dark sky api

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ The **currentObservations** parameter is *optional* and sets how the 3 current o
116116

117117
The **fakegatoParameters** parameter is *optional*. By default, history is persisted on filesystem. You can pass your own parameters to *fakegato-history* module using this paramter, in order to change the location of the persisted file or use GoogleDrive persistance. See https://github.com/simont77/fakegato-history#file-system and https://github.com/simont77/fakegato-history#google-drive for more info. **IMPORTANT NOTE:** Do not modify the parameter for the fakegato internal timer.
118118

119+
The **serial** parameter is *optional* and sets the Serial Number of the accessory. If it's not provided the serial number will be set to the **location** if present, or to 999 if not. Note that for proper operation of fakegato when multiple fakegato-enabled weather accessories are present in your system, the serial number must be unique.
120+
119121

120122
### Dark Sky
121123

api/darksky.js

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,24 @@ var init = function (apiKey, language, locationGeo, l, d) {
8585
});
8686
};
8787

88-
var update = function (callback) {
88+
var update = function (callback, requestedForecastDays) {
8989
let weather = {};
9090
weather.forecasts = [];
9191
let that = this;
9292

93-
updateCache(this.darkskyTimeMachine, function () {
93+
updateCache(this.darkskyTimeMachine, requestedForecastDays, function () {
9494
debug("Updating weather with dark sky");
9595
that.darksky.get()
9696
.then(function (response) {
9797

9898
// Current weather report
99-
response['currently']['rainDay'] = cache.report.rainDay; // Load rainDay from cache
100-
weather.report = parseReport(response['currently'], response['timezone']);
99+
response.currently.rainDay = cache.report.rainDay; // Load rainDay from cache
100+
weather.report = parseReport(response.currently, response.timezone);
101101

102102
// Forecasts for today and next 6 days
103103
for (let i = 0; i <= 6; i++) {
104-
response['daily']['data'][i]['rainDay'] = cache.forecast['day' + i].rainDay; // Load rainDay from cache
105-
weather.forecasts.push(parseForecast(response['daily']['data'][i], response['timezone']));
104+
response.daily.data[i].rainDay = cache.forecast['day' + i].rainDay; // Load rainDay from cache
105+
weather.forecasts.push(parseForecast(response.daily.data[i], response.timezone));
106106
}
107107

108108
callback(null, weather);
@@ -118,57 +118,57 @@ var update = function (callback) {
118118
var parseReport = function (values, timezone) {
119119
let report = {};
120120

121-
report.AirPressure = parseInt(values['pressure']);
122-
report.CloudCover = parseInt(values['cloudCover'] * 100);
123-
report.Condition = values['summary'];
124-
report.ConditionCategory = converter.getConditionCategory(values['icon']);
125-
report.DewPoint = parseInt(values['dewPoint']);
126-
report.Humidity = parseInt(values['humidity'] * 100);
127-
report.ObservationTime = moment.unix(values['time']).tz(timezone).format('HH:mm:ss');
128-
report.Ozone = parseInt(values['ozone']);
129-
report.Rain1h = isNaN(parseInt(values['precipIntensity'])) ? 0 : parseInt(values['precipIntensity']);
130-
report.RainDay = values['rainDay']
131-
report.Temperature = values['temperature'];
132-
report.UVIndex = isNaN(parseInt(values['uvIndex'])) ? 0 : parseInt(values['uvIndex']);
133-
report.Visibility = isNaN(parseInt(values['visibility'])) ? 0 : parseInt(values['visibility']);
134-
report.WindDirection = converter.getWindDirection(values['windBearing']);
135-
report.WindSpeed = parseFloat(values['windSpeed']);
136-
report.WindSpeedMax = parseFloat(values['windGust']);
121+
report.AirPressure = parseInt(values.pressure);
122+
report.CloudCover = parseInt(values.cloudCover * 100);
123+
report.Condition = values.summary;
124+
report.ConditionCategory = converter.getConditionCategory(values.icon);
125+
report.DewPoint = parseInt(values.dewPoint);
126+
report.Humidity = parseInt(values.humidity * 100);
127+
report.ObservationTime = moment.unix(values.time).tz(timezone).format('HH:mm:ss');
128+
report.Ozone = parseInt(values.ozone);
129+
report.Rain1h = isNaN(parseInt(values.precipIntensity)) ? 0 : parseInt(values.precipIntensity);
130+
report.RainDay = values.rainDay;
131+
report.Temperature = values.temperature;
132+
report.UVIndex = isNaN(parseInt(values.uvIndex)) ? 0 : parseInt(values.uvIndex);
133+
report.Visibility = isNaN(parseInt(values.visibility)) ? 0 : parseInt(values.visibility);
134+
report.WindDirection = converter.getWindDirection(values.windBearing);
135+
report.WindSpeed = parseFloat(values.windSpeed);
136+
report.WindSpeedMax = parseFloat(values.windGust);
137137

138138
return report;
139139
};
140140

141141
var parseForecast = function (values, timezone, i) {
142142
let forecast = {};
143143

144-
forecast.AirPressure = parseInt(values['pressure']);
145-
forecast.CloudCover = parseInt(values['cloudCover'] * 100);
146-
forecast.Condition = values['summary'];
147-
forecast.ConditionCategory = converter.getConditionCategory(values['icon']);
148-
forecast.DewPoint = parseInt(values['dewPoint']);
149-
forecast.ForecastDay = moment.unix(values['time']).tz(timezone).format('dddd');
150-
forecast.Humidity = parseInt(values['humidity'] * 100);
151-
forecast.Ozone = parseInt(values['ozone']);
152-
forecast.RainChance = parseInt(values['precipProbability'] * 100);
153-
forecast.RainDay = values['rainDay']
154-
forecast.Temperature = values['temperatureHigh'];
155-
forecast.TemperatureMin = values['temperatureLow'];
156-
forecast.UVIndex = isNaN(parseInt(values['uvIndex'])) ? 0 : parseInt(values['uvIndex']);
157-
forecast.Visibility = isNaN(parseInt(values['visibility'])) ? 0 : parseInt(values['visibility']);
158-
forecast.WindDirection = converter.getWindDirection(values['windBearing']);
159-
forecast.WindSpeed = parseFloat(values['windSpeed']);
160-
forecast.WindSpeedMax = parseFloat(values['windGust']);
144+
forecast.AirPressure = parseInt(values.pressure);
145+
forecast.CloudCover = parseInt(values.cloudCover * 100);
146+
forecast.Condition = values.summary;
147+
forecast.ConditionCategory = converter.getConditionCategory(values.icon);
148+
forecast.DewPoint = parseInt(values.dewPoint);
149+
forecast.ForecastDay = moment.unix(values.time).tz(timezone).format('dddd');
150+
forecast.Humidity = parseInt(values.humidity * 100);
151+
forecast.Ozone = parseInt(values.ozone);
152+
forecast.RainChance = parseInt(values.precipProbability * 100);
153+
forecast.RainDay = values.rainDay;
154+
forecast.Temperature = values.temperatureHigh;
155+
forecast.TemperatureMin = values.temperatureLow;
156+
forecast.UVIndex = isNaN(parseInt(values.uvIndex)) ? 0 : parseInt(values.uvIndex);
157+
forecast.Visibility = isNaN(parseInt(values.visibility)) ? 0 : parseInt(values.visibility);
158+
forecast.WindDirection = converter.getWindDirection(values.windBearing);
159+
forecast.WindSpeed = parseFloat(values.windSpeed);
160+
forecast.WindSpeedMax = parseFloat(values.windGust);
161161

162162
return forecast;
163163
};
164164

165-
var updateCache = function (api, callback) {
165+
var updateCache = function (api, requestedForecastDays, callback) {
166166
if (typeof cache.lastUpdate === 'undefined' || new Date() - cache.lastUpdate > 3600000) {
167167
debug("Called hourly update of rain data");
168168
cache.lastUpdate = new Date();
169169

170170
let now = moment();
171-
let callbacks = 8;
171+
let callbacks = requestedForecastDays + 1;
172172

173173
doTimeMachineRequest(api, now, function (result) {
174174
cache.report.rainDay = result;
@@ -178,7 +178,7 @@ var updateCache = function (api, callback) {
178178
}
179179
}, true);
180180

181-
for (let i = 0; i <= 6; i++) {
181+
for (let i = 0; i < requestedForecastDays; i++) {
182182
doTimeMachineRequest(api, now.clone().add(i, 'd'), function (result) {
183183
cache.forecast['day' + i].rainDay = result;
184184
callbacks--;
@@ -201,12 +201,12 @@ var doTimeMachineRequest = function (api, now, callback, limit = false) {
201201
// Current hour in time zone of weather location
202202
let hour = 24;
203203
if (limit) {
204-
hour = now.tz(response['timezone']).format('H');
204+
hour = now.tz(response.timezone).format('H');
205205
}
206206

207207
// Sum all values for the requested day
208-
debug("Accumulate rain for " + now.tz(response['timezone']).format('dddd') + (limit ? (' until ' + hour + ':00') : ''));
209-
let result = converter.getRainAccumulated(response['hourly']['data'].slice(0, hour), 'precipIntensity');
208+
debug("Accumulate rain for " + now.tz(response.timezone).format('dddd') + (limit ? (' until ' + hour + ':00') : ''));
209+
let result = converter.getRainAccumulated(response.hourly.data.slice(0, hour), 'precipIntensity');
210210
debug("Accumulated rain: " + result);
211211
callback(result);
212212
})

api/openweathermap.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var update = function (callback) {
5757

5858
let weather = {};
5959
Openweathermap.getAllWeather(function (err, jsonObj) {
60-
if (!err && jsonObj['coord']) {
60+
if (!err && jsonObj.coord) {
6161
// Current weather report
6262
parseReport(weather, jsonObj, callback);
6363
}
@@ -69,7 +69,7 @@ var update = function (callback) {
6969
});
7070

7171
Openweathermap.getWeatherForecast(function (err, jsonObj) {
72-
if (!err && jsonObj['city']) {
72+
if (!err && jsonObj.city) {
7373
parseForecasts(weather, jsonObj, callback);
7474
}
7575
else {
@@ -82,17 +82,17 @@ var update = function (callback) {
8282

8383
var parseReport = function (weather, values, callback) {
8484
let report = weather.report || {};
85-
const timezone = geoTz(values['coord']['lat'], values['coord']['lon']);
86-
87-
report.AirPressure = parseInt(values['main']['pressure']);
88-
report.CloudCover = parseInt(values['clouds']['all']);
89-
report.Condition = values['weather'][0]['description'];
90-
report.ConditionCategory = converter.getConditionCategoryOwm(values['weather'][0]['id']);
91-
report.Humidity = parseInt(values['main']['humidity']);
92-
report.ObservationTime = moment.unix(values['dt']).tz(timezone).format('HH:mm:ss');
93-
report.Temperature = values['main']['temp'];
94-
report.WindDirection = converter.getWindDirection(values['wind']['deg']);
95-
report.WindSpeed = values['wind']['speed'];
85+
const timezone = geoTz(values.coord.lat, values.coord.lon);
86+
87+
report.AirPressure = parseInt(values.main.pressure);
88+
report.CloudCover = parseInt(values.clouds.all);
89+
report.Condition = values.weather[0].description;
90+
report.ConditionCategory = converter.getConditionCategoryOwm(values.weather[0].id);
91+
report.Humidity = parseInt(values.main.humidity);
92+
report.ObservationTime = moment.unix(values.dt).tz(timezone).format('HH:mm:ss');
93+
report.Temperature = values.main.temp;
94+
report.WindDirection = converter.getWindDirection(values.wind.deg);
95+
report.WindSpeed = values.wind.speed;
9696

9797
weather.report = report;
9898
if (weather.forecasts) {
@@ -101,11 +101,11 @@ var parseReport = function (weather, values, callback) {
101101
};
102102

103103
var parseForecasts = function (weather, values, callback) {
104-
const timezone = geoTz(values['city']['coord']['lat'], values['city']['coord']['lon']);
104+
const timezone = geoTz(values.city.coord.lat, values.city.coord.lon);
105105

106106
let forecasts = [];
107107
// We get a forecast for 5 days with values each 3 hours.
108-
const itemsFiltered = prepareForecasts(values['list'], timezone);
108+
const itemsFiltered = prepareForecasts(values.list, timezone);
109109
for (let i = 0; i < itemsFiltered.length; i++) {
110110
forecasts[forecasts.length] = parseForecast(itemsFiltered[i], timezone);
111111
}
@@ -119,16 +119,16 @@ var parseForecasts = function (weather, values, callback) {
119119
var parseForecast = function (values, timezone) {
120120
let forecast = {};
121121

122-
forecast.AirPressure = parseInt(values['main']['pressure']);
123-
forecast.CloudCover = parseInt(values['clouds']['all']);
124-
forecast.Condition = values['weather'][0]['description'];
125-
forecast.ConditionCategory = converter.getConditionCategoryOwm(values['weather'][0]['id']);
126-
forecast.ForecastDay = moment.unix(values['dt']).tz(timezone).format('dddd');
127-
forecast.Humidity = parseInt(values['main']['humidity']);
128-
forecast.Temperature = values['main']['temp_max'];
129-
forecast.TemperatureMin = values['main']['temp_min'];
130-
forecast.WindDirection = converter.getWindDirection(values['wind']['deg']);
131-
forecast.WindSpeed = values['wind']['speed'];
122+
forecast.AirPressure = parseInt(values.main.pressure);
123+
forecast.CloudCover = parseInt(values.clouds.all);
124+
forecast.Condition = values.weather[0].description;
125+
forecast.ConditionCategory = converter.getConditionCategoryOwm(values.weather[0].id);
126+
forecast.ForecastDay = moment.unix(values.dt).tz(timezone).format('dddd');
127+
forecast.Humidity = parseInt(values.main.humidity);
128+
forecast.Temperature = values.main.temp_max;
129+
forecast.TemperatureMin = values.main.temp_min;
130+
forecast.WindDirection = converter.getWindDirection(values.wind.deg);
131+
forecast.WindSpeed = values.wind.speed;
132132

133133
return forecast;
134134
};
@@ -150,8 +150,8 @@ var prepareForecasts = function (forecasts, timezone) {
150150
// Find one for today
151151
for (let i = 0; i < forecasts.length; i++) {
152152
const listItem = forecasts[i];
153-
if (listItem['dt'] <= endOfToday) {
154-
const hourOfDay = moment.unix(listItem['dt']).tz(timezone).format("H");
153+
if (listItem.dt <= endOfToday) {
154+
const hourOfDay = moment.unix(listItem.dt).tz(timezone).format("H");
155155
if (parseInt(hourOfDay) >= 10 && parseInt(hourOfDay) <= 13) {
156156
forecastsFiltered[forecastsFiltered.length] = forecasts[i];
157157
}
@@ -165,8 +165,8 @@ var prepareForecasts = function (forecasts, timezone) {
165165
// For all following days find exactly one value (best: noon)
166166
for (let i = 0; i < forecasts.length; i++) {
167167
const listItem = forecasts[i];
168-
if (listItem['dt'] > endOfToday) {
169-
const hourOfDay = moment.unix(listItem['dt']).tz(timezone).format("H");
168+
if (listItem.dt > endOfToday) {
169+
const hourOfDay = moment.unix(listItem.dt).tz(timezone).format("H");
170170
if (parseInt(hourOfDay) >= 10 && parseInt(hourOfDay) <= 13) {
171171
forecastsFiltered[forecastsFiltered.length] = forecasts[i];
172172
}

api/weatherunderground.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ var update = function (callback) {
5656
this.wunderground.conditions().forecast().request(this.location, function (error, response) {
5757
if (!error) {
5858
// Current weather report
59-
weather.report = parseReport(response['current_observation']);
59+
weather.report = parseReport(response.current_observation);
6060

6161
// Forecasts for today and next 3 days
62-
weather.forecasts.push(parseForecast(response['forecast']['simpleforecast']['forecastday'][0]));
63-
weather.forecasts.push(parseForecast(response['forecast']['simpleforecast']['forecastday'][1]));
64-
weather.forecasts.push(parseForecast(response['forecast']['simpleforecast']['forecastday'][2]));
65-
weather.forecasts.push(parseForecast(response['forecast']['simpleforecast']['forecastday'][3]));
62+
weather.forecasts.push(parseForecast(response.forecast.simpleforecast.forecastday[0]));
63+
weather.forecasts.push(parseForecast(response.forecast.simpleforecast.forecastday[1]));
64+
weather.forecasts.push(parseForecast(response.forecast.simpleforecast.forecastday[2]));
65+
weather.forecasts.push(parseForecast(response.forecast.simpleforecast.forecastday[3]));
6666
callback(null, weather);
6767
}
6868
else {
@@ -76,39 +76,39 @@ var update = function (callback) {
7676
var parseReport = function (values) {
7777
let report = {};
7878

79-
report.AirPressure = parseInt(values['pressure_mb']);
80-
report.Condition = values['weather'];
81-
report.ConditionCategory = converter.getConditionCategory(values['icon']);
82-
report.Humidity = parseInt(values['relative_humidity'].substr(0, values['relative_humidity'].length - 1));
83-
report.ObservationStation = values['observation_location']['full'];
84-
report.ObservationTime = values['observation_time_rfc822'].split(' ')[4];
85-
report.Rain1h = isNaN(parseInt(values['precip_1hr_metric'])) ? 0 : parseInt(values['precip_1hr_metric']);
86-
report.RainDay = isNaN(parseInt(values['precip_today_metric'])) ? 0 : parseInt(values['precip_today_metric']);
87-
report.SolarRadiation = isNaN(parseInt(values['solarradiation'])) ? 0 : parseInt(values['solarradiation']);
88-
report.Temperature = values['temp_c'];
89-
report.UVIndex = isNaN(parseInt(values['UV'])) ? 0 : parseInt(values['UV']);
90-
report.Visibility = isNaN(parseInt(values['visibility_km'])) ? 0 : parseInt(values['visibility_km']);
91-
report.WindDirection = values['wind_dir'];
92-
report.WindSpeed = parseFloat(values['wind_kph']);
93-
report.WindSpeedMax = parseFloat(values['wind_gust_kph']);
79+
report.AirPressure = parseInt(values.pressure_mb);
80+
report.Condition = values.weather;
81+
report.ConditionCategory = converter.getConditionCategory(values.icon);
82+
report.Humidity = parseInt(values.relative_humidity.substr(0, values.relative_humidity.length - 1));
83+
report.ObservationStation = values.observation_location.full;
84+
report.ObservationTime = values.observation_time_rfc822.split(' ')[4];
85+
report.Rain1h = isNaN(parseInt(values.precip_1hr_metric)) ? 0 : parseInt(values.precip_1hr_metric);
86+
report.RainDay = isNaN(parseInt(values.precip_today_metric)) ? 0 : parseInt(values.precip_today_metric);
87+
report.SolarRadiation = isNaN(parseInt(values.solarradiation)) ? 0 : parseInt(values.solarradiation);
88+
report.Temperature = values.temp_c;
89+
report.UVIndex = isNaN(parseInt(values.UV)) ? 0 : parseInt(values.UV);
90+
report.Visibility = isNaN(parseInt(values.visibility_km)) ? 0 : parseInt(values.visibility_km);
91+
report.WindDirection = values.wind_dir;
92+
report.WindSpeed = parseFloat(values.wind_kph);
93+
report.WindSpeedMax = parseFloat(values.wind_gust_kph);
9494

9595
return report;
9696
};
9797

9898
var parseForecast = function (values) {
9999
let forecast = {};
100100

101-
forecast.Condition = values['conditions'];
102-
forecast.ConditionCategory = converter.getConditionCategory(values['icon']);
103-
forecast.ForecastDay = values['date']['weekday'];
104-
forecast.Humidity = parseInt(values['avehumidity'])
105-
forecast.RainChance = values['pop'];
106-
forecast.RainDay = isNaN(parseInt(values['qpf_allday']['mm'])) ? 0 : parseInt(values['qpf_allday']['mm']);
107-
forecast.Temperature = values['high']['celsius'];
108-
forecast.TemperatureMin = values['low']['celsius'];
109-
forecast.WindDirection = values['avewind']['dir'];
110-
forecast.WindSpeed = parseFloat(values['avewind']['kph']);
111-
forecast.WindSpeedMax = parseFloat(values['maxwind']['kph']);
101+
forecast.Condition = values.conditions;
102+
forecast.ConditionCategory = converter.getConditionCategory(values.icon);
103+
forecast.ForecastDay = values.date.weekday;
104+
forecast.Humidity = parseInt(values.avehumidity);
105+
forecast.RainChance = values.pop;
106+
forecast.RainDay = isNaN(parseInt(values.qpf_allday.mm)) ? 0 : parseInt(values.qpf_allday.mm);
107+
forecast.Temperature = values.high.celsius;
108+
forecast.TemperatureMin = values.low.celsius;
109+
forecast.WindDirection = values.avewind.dir;
110+
forecast.WindSpeed = parseFloat(values.avewind.kph);
111+
forecast.WindSpeedMax = parseFloat(values.maxwind.kph);
112112

113113
return forecast;
114114
};

0 commit comments

Comments
 (0)