1
0
Fork 0

Merge pull request #1969 from halemmerich/owmweather

Pull weather from OpenWeatherMap via http
master
Gordon Williams 2022-07-04 14:54:54 +01:00 committed by GitHub
commit 427cfb593e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 250 additions and 0 deletions

View File

@ -0,0 +1 @@
0.01: New App!

13
apps/owmweather/README.md Normal file
View File

@ -0,0 +1,13 @@
# Open Weather Map weather provider
This updates [Weather](https://banglejs.com/apps/#weather) with data from the Open Weather Map API
## Usage
Just install and configure the app. This needs an internet-enabled Gadgetbridge version.
Install [My Location](https://banglejs.com/apps/#mylocation) to change the location for the weather requests.
Install one of the text input libraries to modify the API key in the settings
## Creator
[halemmerich](https://github.com/halemmerich)

BIN
apps/owmweather/app.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

28
apps/owmweather/boot.js Normal file
View File

@ -0,0 +1,28 @@
(function() {
let waiting = false;
let settings = require("Storage").readJSON("owmweather.json", 1) || {
enabled: false
};
function completion(){
waiting = false;
}
if (settings.enabled) {
let weather = require("Storage").readJSON('weather.json') || {};
let lastUpdate;
if (weather && weather.weather && weather.weather.time) lastUpdate = weather.weather.time;
if (!lastUpdate || lastUpdate + settings.refresh * 1000 * 60 < Date.now()){
if (!waiting){
waiting = true;
require("owmweather").pull(completion);
}
}
setInterval(() => {
if (!waiting && NRF.getSecurityStatus().connected){
waiting = true;
require("owmweather").pull(completion);
}
}, settings.refresh * 1000 * 60);
}
})();

View File

@ -0,0 +1 @@
{"enabled":false,"refresh":180}

View File

@ -0,0 +1,48 @@
<html>
<head>
<link rel="stylesheet" href="../../css/spectre.min.css">
</head>
<body>
<h3>Set OWM API key</h3>
<p><input id="apikey" style="width:90%;"></input></p>
<p><button id="upload" class="btn btn-primary">Set</button></p>
<script src="../../core/lib/interface.js"></script>
<script>
var settings = {};
function onInit(){
console.log("Loading settings from BangleJs...");
try {
Util.readStorage("owmweather.json", data=>{
if(data.length > 0){
settings = JSON.parse(data);
console.log("Got settings", settings);
document.getElementById("apikey").value = settings.apikey;
console.log("Loaded apikey from BangleJs.");
}
});
} catch(ex) {
console.log("(Warning) Could not load apikey from BangleJs.");
console.log(ex);
}
}
document.getElementById("upload").addEventListener("click", function() {
try {
settings.apikey = document.getElementById("apikey").value;
Util.showModal("Saving...");
Util.writeStorage("owmweather.json", JSON.stringify(settings), ()=>{
Util.hideModal();
});
console.log("Sent settings!");
} catch(ex) {
console.log("(Warning) Could not write settings to BangleJs.");
console.log(ex);
}
});
</script>
</body>
</html>

53
apps/owmweather/lib.js Normal file
View File

@ -0,0 +1,53 @@
function parseWeather(response) {
let owmData = JSON.parse(response);
let isOwmData = owmData.coord && owmData.weather && owmData.main;
if (isOwmData) {
let json = require("Storage").readJSON('weather.json') || {};
let weather = {};
weather.time = Date.now();
weather.hum = owmData.main.humidity;
weather.temp = owmData.main.temp;
weather.code = owmData.weather[0].id;
weather.wdir = owmData.wind.deg;
weather.wind = owmData.wind.speed;
weather.loc = owmData.name;
weather.txt = owmData.weather[0].main;
if (weather.wdir != null) {
let deg = weather.wdir;
while (deg < 0 || deg > 360) {
deg = (deg + 360) % 360;
}
weather.wrose = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw', 'n'][Math.floor((deg + 22.5) / 45)];
}
json.weather = weather;
require("Storage").writeJSON('weather.json', json);
require("weather").emit("update", json.weather);
return undefined;
} else {
return /*LANG*/"Not OWM data";
}
}
exports.pull = function(completionCallback) {
let location = require("Storage").readJSON("mylocation.json", 1) || {
"lat": 51.50,
"lon": 0.12,
"location": "London"
};
let settings = require("Storage").readJSON("owmweather.json", 1);
let uri = "https://api.openweathermap.org/data/2.5/weather?lat=" + location.lat.toFixed(2) + "&lon=" + location.lon.toFixed(2) + "&exclude=hourly,daily&appid=" + settings.apikey;
if (Bangle.http){
Bangle.http(uri, {timeout:10000}).then(event => {
let result = parseWeather(event.resp);
if (completionCallback) completionCallback(result);
}).catch((e)=>{
if (completionCallback) completionCallback(e);
});
} else {
if (completionCallback) completionCallback(/*LANG*/"No http method found");
}
};

View File

@ -0,0 +1,22 @@
{ "id": "owmweather",
"name": "Open Weather Map weather provider",
"shortName":"OWM Weather",
"version":"0.01",
"description": "Pulls weather from OWM API",
"icon": "app.png",
"type": "bootloader",
"tags": "boot,tool,weather",
"supports" : ["BANGLEJS2"],
"interface": "interface.html",
"readme": "README.md",
"data": [
{"name":"owmweather.json"},
{"name":"weather.json"}
],
"storage": [
{"name":"owmweather.default.json","url":"default.json"},
{"name":"owmweather.boot.js","url":"boot.js"},
{"name":"owmweather","url":"lib.js"},
{"name":"owmweather.settings.js","url":"settings.js"}
]
}

View File

@ -0,0 +1,84 @@
(function(back) {
function writeSettings(key, value) {
var s = require('Storage').readJSON(FILE, true) || {};
s[key] = value;
require('Storage').writeJSON(FILE, s);
readSettings();
}
function readSettings(){
settings = Object.assign(
require('Storage').readJSON("owmweather.default.json", true) || {},
require('Storage').readJSON(FILE, true) || {}
);
}
var FILE="owmweather.json";
var settings;
readSettings();
function buildMainMenu(){
var mainmenu = {
'': { 'title': 'OWM weather' },
'< Back': back,
"Enabled": {
value: !!settings.enabled,
onchange: v => {
writeSettings("enabled", v);
}
},
"Refresh every": {
value: settings.refresh / 60,
min: 1,
max: 48,
step: 1,
format: v=>v+"h",
onchange: v => {
writeSettings("refresh",Math.round(v * 60));
}
},
"Force refresh": ()=>{
if (!settings.apikey){
E.showAlert("API key is needed","Hint").then(()=>{
E.showMenu(buildMainMenu());
});
} else {
E.showMessage("Reloading weather");
require("owmweather").pull((e)=>{
if (e) {
E.showAlert(e,"Error").then(()=>{
E.showMenu(buildMainMenu());
});
} else {
E.showAlert("Success").then(()=>{
E.showMenu(buildMainMenu());
});
}
});
}
}
};
mainmenu["API key"] = function (){
if (require("textinput")){
require("textinput").input({text:settings.apikey}).then(result => {
if (result != "") {
print("Result is", result);
settings.apikey = result;
writeSettings("apikey",result);
}
E.showMenu(buildMainMenu());
});
} else {
E.showPrompt("Install a text input lib"),then(()=>{
E.showMenu(buildMainMenu());
});
}
};
return mainmenu;
}
E.showMenu(buildMainMenu());
});