-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenWeather.js
More file actions
223 lines (161 loc) · 6.51 KB
/
OpenWeather.js
File metadata and controls
223 lines (161 loc) · 6.51 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: deep-green; icon-glyph: leaf;
const MAX = 999
async function createWidget(tNow)
{
let wg = new ListWidget()
const wd = await requestData(tNow)
sTemp = Math.round(wd.w.current.temp).toString() + "°"
sFeel = Math.round(wd.w.current.feels_like).toString() + "°"
sUVI = Math.round(wd.w.current.uvi).toString()
sAQI = Math.round(wd.p.list[0].main.aqi).toString()
sPM25 = Math.round(wd.p.list[0].components.pm2_5).toString()
sPM10 = Math.round(wd.p.list[0].components.pm10).toString()
sCO = Math.round(wd.p.list[0].components.co).toString()
sSO2 = Math.round(wd.p.list[0].components.so2.toString()).toString()
sHum = wd.w.current.humidity.toString() + "%"
sLocation = checkKnownLocations(wd.l, 0.3, wd.w.timezone)
widgetAddText(wg, sLocation, 12)
widgetAddText(wg, wd.w.current.weather[0].main, 16)
widgetAddText(wg, sTemp + " (" + sFeel + ")" + " " + sHum, 16)
widgetAddText(wg, "uvi: " + sUVI + ", aqi: " + sAQI, 12)
widgetAddText(wg, "pm25: " + sPM25 + ", pm10: " + sPM10, 12)
widgetAddText(wg, "co: " + sCO + ", so2: " + sSO2, 12)
wg.addSpacer()
let df = new DateFormatter()
df.dateFormat = "HH:mm:ss"
const tDate = wg.addText(df.string(tNow) + " - " + wd.m + " mins")
tDate.font = Font.lightRoundedSystemFont(10)
return wg
}
async function run()
{
// get information to display
let gDate = new Date()
// create the widget and associate with script
let widget = await createWidget(gDate)
Script.setWidget(widget)
// finish execution and display preview
if (config.runsInApp) widget.presentSmall()
Script.complete()
}
function checkKnownLocations(loc, maxDist, LCurrent)
{
let dToHome = getDistanceToStoredLocation(loc, "home.txt")
console.log("Home " + dToHome.toString())
if(dToHome < maxDist) return "Home"
let dToWork = getDistanceToStoredLocation(loc, "work.txt")
console.log("Work " + dToWork.toString())
if(dToWork < maxDist) return "Work"
return LCurrent
}
function getDistanceToStoredLocation(locNow, fileName)
{
const fm = FileManager.iCloud()
let docpath = fm.documentsDirectory() + "/storage" + "/" + fileName
fm.downloadFileFromiCloud(docpath)
let docContent = fm.readString(docpath)
if(docContent.length > 0) {
var aContent = docContent.split(",")
//console.log(aContent[0].toString() + ", " + aContent[1].toString() + "; " + locNow.lat.toString() + ", " + locNow.lon.toString())
return getDistance(aContent[0], aContent[1], locNow.latitude, locNow.longitude)
}
return MAX
}
function getDistance(lat1, lon1, lat2, lon2)
{
//console.log(lat1.toString() + ", " + lon1.toString() + "; " + lat2.toString() + ", " + lon2.toString())
var R = 6371 // Radius of the earth in km
var dLat = deg2rad(lat2-lat1) // deg2rad below
var dLon = deg2rad(lon2-lon1)
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon/2) * Math.sin(dLon/2)
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))
var d = R * c // Distance in km
return d
}
function deg2rad(deg)
{
return deg * (Math.PI/180)
}
// Ex: api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=faca05867d84ee306effa2c224819d0e
// api.openweathermap.org/data/2.5/air_pollution?lat={lat}&lon={lon}&appid=faca05867d84ee306effa2c224819d0e
// AQI: Possible values: 1, 2, 3, 4, 5. Where 1 = Good, 2 = Fair, 3 = Moderate, 4 = Poor, 5 = Very Poor
// tile.openweathermap.org/map/{layer}/{z}/{x}/{y}.png?appid=faca05867d84ee306effa2c224819d0e
// layer: temp_new
// z: zoom
async function requestData(tNow)
{
var jWeather = await JSON.parse(readCache("owlastupdate.txt"))
var jPolution = await JSON.parse(readCache("oplastupdate.txt"))
var minPassed = Math.trunc(((Date.now() / 1000) - jWeather.current.dt) / 60)
console.log("minutes passed " + minPassed.toString())
var oldLoc = {latitude: jWeather.lat, longitude: jWeather.lon}
var loc = oldLoc
try { // if location request fails use the last known location
loc = await Location.current()
await setLocation(loc)
} catch (error) {
console.log(error)
writeCache(error, "openweather_locerror.txt")
loc = oldLoc
}
if(minPassed >= 15) {
let url = "https://api.openweathermap.org/data/2.5/onecall?lat=" + loc.latitude + "&lon=" + loc.longitude + "&exclude=minutely,hourly,daily" + "&units=metric" + "&appid=faca05867d84ee306effa2c224819d0e"
jWeather = await sendRequest(url)
writeCache(jWeather, "owlastupdate.txt")
url = "https://api.openweathermap.org/data/2.5/air_pollution?lat=" + loc.latitude + "&lon=" + loc.longitude + "&appid=faca05867d84ee306effa2c224819d0e"
jPolution = await sendRequest(url)
writeCache(jPolution, "oplastupdate.txt")
}
return {w: jWeather, p: jPolution, l: loc, m: minPassed}
}
async function sendRequest(url)
{
var jInfo
var req
try {
req = new Request(url)
} catch (error) {
console.log(error)
writeCache(error, "openweather_reqerror.txt")
return
}
jInfo = await req.loadJSON()
return jInfo
}
function widgetAddText(wg, sText, iSize)
{
let tInfo = wg.addText(sText)
tInfo.font = Font.mediumRoundedSystemFont(iSize)
}
function readCache(fileName)
{
const fm = FileManager.iCloud()
const BASEPATH = fm.documentsDirectory() + "/storage"
let docpath = BASEPATH + "/" + fileName
if(!fm.fileExists(BASEPATH) || !fm.fileExists(docpath)) {
console.log("File not found " + docpath)
return {error: 404}
}
//return await JSON.parse(fm.readString(docpath))
return fm.readString(docpath)
}
function writeCache(wd, fileName)
{
const fm = FileManager.iCloud()
const BASEPATH = fm.documentsDirectory() + "/storage"
if(!fm.fileExists(BASEPATH)) fm.createDirectory(BASEPATH)
let docpath = BASEPATH + "/" + fileName
fm.writeString(docpath, JSON.stringify(wd))
}
async function setLocation(loc) // store current location on a file named after the date
{
const fm = FileManager.iCloud()
let filePath = fm.documentsDirectory() + "/storage" + "/" + "curr.txt"
fm.downloadFileFromiCloud(filePath)
console.log("location updated: " + filePath)
let docContent = loc.latitude + "," + loc.longitude
fm.writeString(filePath, docContent)
}
await run()