From 8e788a608a69c02e729f78cf4c0e2d3961badfc0 Mon Sep 17 00:00:00 2001 From: Martin Boonk Date: Thu, 16 Jun 2022 17:23:02 +0200 Subject: [PATCH] Initial proof of concept --- apps/owmweather/ChangeLog | 1 + apps/owmweather/README.md | 25 ++++++++++++++ apps/owmweather/app.png | Bin 0 -> 1173 bytes apps/owmweather/boot.js | 60 ++++++++++++++++++++++++++++++++++ apps/owmweather/metadata.json | 14 ++++++++ 5 files changed, 100 insertions(+) create mode 100644 apps/owmweather/ChangeLog create mode 100644 apps/owmweather/README.md create mode 100644 apps/owmweather/app.png create mode 100644 apps/owmweather/boot.js create mode 100644 apps/owmweather/metadata.json diff --git a/apps/owmweather/ChangeLog b/apps/owmweather/ChangeLog new file mode 100644 index 000000000..5560f00bc --- /dev/null +++ b/apps/owmweather/ChangeLog @@ -0,0 +1 @@ +0.01: New App! diff --git a/apps/owmweather/README.md b/apps/owmweather/README.md new file mode 100644 index 000000000..dc139bc9a --- /dev/null +++ b/apps/owmweather/README.md @@ -0,0 +1,25 @@ +# App Name + +Describe the app... + +Add screen shots (if possible) to the app folder and link then into this file with ![](.png) + +## Usage + +Describe how to use it + +## Features + +Name the function + +## Controls + +Name the buttons and what they are used for + +## Requests + +Name who should be contacted for support/update requests + +## Creator + +Your name diff --git a/apps/owmweather/app.png b/apps/owmweather/app.png new file mode 100644 index 0000000000000000000000000000000000000000..bbfc0ace0b8d25c4afefcb336ea04df799a70f22 GIT binary patch literal 1173 zcmV;G1Zw+fgKelF z+6!B~5F)8y)e1%MqEWnPiWg$RgkaGND+=ED(@M!jy(#RCC2ci@HZ&m;H8wSEeq1-3 zna_*eO*h%I$(hL}OJN@v*!|9#@4WBKnQzV+C{Un)Z={DV>E%eib@(DFUXn-NOejAm zNe^8p4lfCJt4`j{fhJF^Xh3?ARSnST?;@FEt!47{!1jEGWkFpPY~BdvcO+sr|AoPp7;j(Z^c9nYrv8 zkRG}g{?t`1FFr+i3Dj4?(4UDMy&OqZr_)1HX<73JfL%aC@{G&CN!PWHJ+npq?bSfm z3XxGNw?6LPs7g_(j(pdPtX6NkmEpEmb0c6>eVOf?QhKnNuV{Lt@F=40(L-~ zBr4U6#P%`av@$0Zg!qTMN57+X49^FVjkbLQP)KEm&RPQbv6&-%R5y%N$)W|^G z3HAX4eumxfNt%QZOugspBgMdEG}xzWFfy>SLB*_~-Ht~Sp@@Yn(5USLDV>K=tk6T&Gr@s zpI_!oxJY!Hg>_~@60*CA@DI5jXU)oJfI#||^z>SYhgIBx&7a<=4-nV|vaT9J`>ZY?00000NkvXXu0mjfi9tF% literal 0 HcmV?d00001 diff --git a/apps/owmweather/boot.js b/apps/owmweather/boot.js new file mode 100644 index 000000000..525500e80 --- /dev/null +++ b/apps/owmweather/boot.js @@ -0,0 +1,60 @@ +(function() { + let settings = require("Storage").readJSON("owmweather.json",1)||{enabled:false}; + console.log("Settings", settings); + if (settings.enabled && settings.apikey){ + let location = require("Storage").readJSON("mylocation.json",1)||{"lat":51.50,"lon":0.12,"location":"London"}; + + function pullWeather() { + console.log("pull weather"); + Bluetooth.println(JSON.stringify({ + t: "http", + url: "https://api.openweathermap.org/data/2.5/weather?lat=" + location.lat.toFixed(2) + "&lon=" + location.lon.toFixed(2) + "&exclude=hourly,daily&appid=" + settings.apikey + })); + } + + function parseWeather(event) { + let owmData = JSON.parse(event.resp); + console.log("OWM Data", owmData); + + let isOwmData = owmData.coord && owmData.weather && owmData.main; + + if (isOwmData) { + console.log("Converting data"); + 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); + } + } + + const _GB = global.GB; + global.GB = (event) => { + if (event.t==="http") parseWeather(event); + if (_GB) setTimeout(_GB, 0, event); + }; + + console.log("Setting interval"); + setInterval(()=>{ + pullWeather(); + }, settings.refresh * 1000 * 60); + pullWeather(); + } +})(); diff --git a/apps/owmweather/metadata.json b/apps/owmweather/metadata.json new file mode 100644 index 000000000..a271f9eaa --- /dev/null +++ b/apps/owmweather/metadata.json @@ -0,0 +1,14 @@ +{ "id": "owmweather", + "name": "openweathermap weather provider", + "shortName":"OWM Weather", + "version":"0.01", + "description": "A detailed description of my great app", + "icon": "app.png", + "type": "bootloader", + "tags": "boot,tool,weather", + "supports" : ["BANGLEJS2"], + "readme": "README.md", + "storage": [ + {"name":"owmweather.boot.js","url":"boot.js"} + ] +}