| 1321 | FUNCTION checkUpdates |
| 1322 | ------------------------------------------------------------------*/ |
| 1323 | async function checkUpdates(){ |
| 1324 | // Version info |
| 1325 | const VERSION = "2.1.2"; // DO NOT CHANGE THIS VALUE |
| 1326 | if (UPDATE_CHECK_DAYS == 0) return false; |
| 1327 | versionURL = 'https://raw.githubusercontent.com/ajatkj/scriptable/master/lib/LSWeatherVersionInfo.json'; |
| 1328 | let updateRequired = false; |
| 1329 | let t = new Date() |
| 1330 | let today = t.getDate().toString().padStart(2,'0') + "/" + (t.getMonth() + 1).toString().padStart(2,'0') + "/" + t.getFullYear(); |
| 1331 | let fm = FileManager.iCloud(); |
| 1332 | let versionPath = fm.joinPath(fm.documentsDirectory(), "LSWeather"); |
| 1333 | if (!fm.fileExists(versionPath)) fm.createDirectory(versionPath); |
| 1334 | let versionFile = fm.joinPath(versionPath,'versioncheck.txt'); |
| 1335 | if (fm.fileExists(versionFile)) { |
| 1336 | vers = JSON.parse(fm.readString(versionFile)); |
| 1337 | oldVersion = vers.version; |
| 1338 | lastChecked = vers.lastChecked; |
| 1339 | } else { |
| 1340 | oldVersion = 0; |
| 1341 | lastChecked = 0; |
| 1342 | } |
| 1343 | writeLOG("Updates were last checked at " + lastChecked + " with version " + oldVersion); |
| 1344 | if (oldVersion != VERSION && oldVersion != 0) { |
| 1345 | updateRequired = true; |
| 1346 | writeLOG("Update available, version: " + oldVersion + ", current version is " + VERSION); |
| 1347 | } |
| 1348 | // Find when was last check done |
| 1349 | if (lastChecked != 0) { |
| 1350 | lc = lastChecked.split("/"); |
| 1351 | td = today.split("/"); |
| 1352 | daysDiff = ((new Date(+td[2], td[1] - 1, +td[0])) - (new Date(+lc[2], lc[1] - 1, +lc[0]))) / (1000 * 3600 * 24); |
| 1353 | } else { |
| 1354 | daysDiff = 999; |
| 1355 | } |
| 1356 | if (daysDiff >= UPDATE_CHECK_DAYS || lastChecked == 0) { |
| 1357 | //Check for updates |
| 1358 | try { |
| 1359 | let updateCheck = new Request(versionURL); |
| 1360 | updateCheck.timeoutInterval = 10; |
| 1361 | writeLOG(`Going to check current version at ${versionURL}`) |
| 1362 | uC = await updateCheck.loadJSON() |
| 1363 | writeLOG("Found version: " + uC.version); |
| 1364 | if (uC.version != VERSION){ |
| 1365 | writeLOG("Update available, version: " + uC.version + ", current version is " + VERSION); |
| 1366 | lastChecked = today; |
| 1367 | fm.writeString(versionFile, JSON.stringify({"version": uC.version, "lastChecked": lastChecked})); |
| 1368 | updateRequired = true; |
| 1369 | } else if (lastChecked == 0) { |
| 1370 | fm.writeString(versionFile, JSON.stringify({"version": VERSION, "lastChecked": today})); |
| 1371 | } |
| 1372 | } catch (error) { |
| 1373 | errMsg = "updateCheck_" + error.message.replace(/\s/g,"_") + ".Proceeding_without_checks."; |
| 1374 | writeLOG(errMsg); |
| 1375 | updateRequired = false; |
| 1376 | } |
| 1377 | } |
| 1378 | return updateRequired; |
| 1379 | } |