|
@ -20,12 +20,15 @@ const CREDIT_MSG = "darksky.net/poweredby • smol.gq/wthr-src\n\n" |
|
|
const jsonReader = (filePath, cb) => { |
|
|
const jsonReader = (filePath, cb) => { |
|
|
fs.readFile(filePath, (err, fileData) => { |
|
|
fs.readFile(filePath, (err, fileData) => { |
|
|
if (err) { |
|
|
if (err) { |
|
|
|
|
|
log(err) |
|
|
return cb && cb(err) |
|
|
return cb && cb(err) |
|
|
} |
|
|
} |
|
|
try { |
|
|
try { |
|
|
const object = JSON.parse(fileData) |
|
|
const object = JSON.parse(fileData) |
|
|
|
|
|
// log(filePath, object['latitude'])
|
|
|
return cb && cb(null, object) |
|
|
return cb && cb(null, object) |
|
|
} catch(err) { |
|
|
} catch(err) { |
|
|
|
|
|
log(err) |
|
|
return cb && cb(err) |
|
|
return cb && cb(err) |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
@ -33,6 +36,15 @@ const jsonReader = (filePath, cb) => { |
|
|
|
|
|
|
|
|
const round5 = x => { return Math.ceil(x / 5) * 5 } |
|
|
const round5 = x => { return Math.ceil(x / 5) * 5 } |
|
|
|
|
|
|
|
|
|
|
|
const updateTempHigh = (temp, index, tempHigh) => { |
|
|
|
|
|
// +1 to offset header
|
|
|
|
|
|
return (temp > tempHigh[0]) ? [temp, index + 1] : tempHigh |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const updateTempLow = (temp, index, tempLow) => { |
|
|
|
|
|
return (temp < tempLow[0]) ? [temp, index + 1] : tempLow |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const getWeatherTable = (jsonFile, loc, orientation) => { |
|
|
const getWeatherTable = (jsonFile, loc, orientation) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
jsonReader(jsonFile, (err, weatherInfo) => { |
|
|
jsonReader(jsonFile, (err, weatherInfo) => { |
|
@ -41,7 +53,9 @@ const getWeatherTable = (jsonFile, loc, orientation) => { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
let output = loc + '\n' |
|
|
let output = loc + '\n' |
|
|
// log(loc)
|
|
|
|
|
|
|
|
|
// value, index
|
|
|
|
|
|
let tempHigh = [-100, -1] |
|
|
|
|
|
let tempLow = [100, -1] |
|
|
|
|
|
|
|
|
weatherInfo['hourly']['data'][0] = weatherInfo['currently'] |
|
|
weatherInfo['hourly']['data'][0] = weatherInfo['currently'] |
|
|
if (orientation == 'v') { |
|
|
if (orientation == 'v') { |
|
@ -49,18 +63,22 @@ const getWeatherTable = (jsonFile, loc, orientation) => { |
|
|
for (let i = 0; i < 12; i++) { |
|
|
for (let i = 0; i < 12; i++) { |
|
|
const hourInfo = weatherInfo['hourly']['data'][i] |
|
|
const hourInfo = weatherInfo['hourly']['data'][i] |
|
|
const date = new Date(hourInfo['time'] * 1000) |
|
|
const date = new Date(hourInfo['time'] * 1000) |
|
|
|
|
|
const temp = Math.floor(hourInfo['apparentTemperature']) |
|
|
|
|
|
tempHigh = updateTempHigh(temp, i, tempHigh) |
|
|
|
|
|
tempLow = updateTempLow(temp, i, tempHigh) |
|
|
const precipProbability = round5(hourInfo['precipProbability'] * 100) |
|
|
const precipProbability = round5(hourInfo['precipProbability'] * 100) |
|
|
const precipText = ((precipProbability < HIDE_PRECIP_LESS_THAN) |
|
|
const precipText = ((precipProbability < HIDE_PRECIP_LESS_THAN) |
|
|
? '' : precipProbability) |
|
|
? '' : precipProbability) |
|
|
infoList.push([ |
|
|
infoList.push([ |
|
|
date.getHours(), |
|
|
date.getHours(), |
|
|
Math.floor(hourInfo['apparentTemperature']), |
|
|
|
|
|
|
|
|
temp, |
|
|
((hourInfo['summary'].includes('Rain')) ? 'Y' : ''), |
|
|
((hourInfo['summary'].includes('Rain')) ? 'Y' : ''), |
|
|
precipText, |
|
|
precipText, |
|
|
]) |
|
|
]) |
|
|
} |
|
|
} |
|
|
|
|
|
infoList[tempHigh[1]][1] = `${infoList[tempHigh[1]][1]}+` |
|
|
|
|
|
infoList[tempLow[1]][1] = `${infoList[tempLow[1]][1]}-` |
|
|
output += table(infoList, TABLE_CONFIG) |
|
|
output += table(infoList, TABLE_CONFIG) |
|
|
// log(table(infoList, TABLE_CONFIG))
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
// horizontal
|
|
|
// horizontal
|
|
|
const hoursList = ['H'] |
|
|
const hoursList = ['H'] |
|
@ -70,20 +88,20 @@ const getWeatherTable = (jsonFile, loc, orientation) => { |
|
|
for (let i = 0; i < 16; i += 2) { |
|
|
for (let i = 0; i < 16; i += 2) { |
|
|
const hourInfo = weatherInfo['hourly']['data'][i] |
|
|
const hourInfo = weatherInfo['hourly']['data'][i] |
|
|
const date = new Date(hourInfo['time'] * 1000) |
|
|
const date = new Date(hourInfo['time'] * 1000) |
|
|
|
|
|
const temp = Math.floor(hourInfo['apparentTemperature']) |
|
|
|
|
|
tempHigh = updateTempHigh(temp, i/2, tempHigh) |
|
|
|
|
|
tempLow = updateTempLow(temp, i/2, tempHigh) |
|
|
hoursList.push(date.getHours()) |
|
|
hoursList.push(date.getHours()) |
|
|
tempsList.push(Math.floor(hourInfo['apparentTemperature'])) |
|
|
|
|
|
|
|
|
tempsList.push(temp) |
|
|
rainList.push((hourInfo['summary'].includes('Rain')) ? 'Y' : '') |
|
|
rainList.push((hourInfo['summary'].includes('Rain')) ? 'Y' : '') |
|
|
const precipProbability = round5(hourInfo['precipProbability'] * 100) |
|
|
const precipProbability = round5(hourInfo['precipProbability'] * 100) |
|
|
precipList.push((precipProbability < HIDE_PRECIP_LESS_THAN) |
|
|
precipList.push((precipProbability < HIDE_PRECIP_LESS_THAN) |
|
|
? '' : precipProbability) |
|
|
? '' : precipProbability) |
|
|
} |
|
|
} |
|
|
const maxTempIndex = tempsList.indexOf(Math.max(...tempsList.slice(1))) |
|
|
|
|
|
const minTempIndex = tempsList.indexOf(Math.min(...tempsList.slice(1))) |
|
|
|
|
|
tempsList[maxTempIndex] = `${tempsList[maxTempIndex]}+` |
|
|
|
|
|
tempsList[minTempIndex] = `${tempsList[minTempIndex]}-` |
|
|
|
|
|
|
|
|
tempsList[tempHigh[1]] = `${tempsList[tempHigh[1]]}+` |
|
|
|
|
|
tempsList[tempLow[1]] = `${tempsList[tempLow[1]]}-` |
|
|
output += table([hoursList, tempsList, rainList, precipList], |
|
|
output += table([hoursList, tempsList, rainList, precipList], |
|
|
TABLE_CONFIG) |
|
|
TABLE_CONFIG) |
|
|
// log(table([hoursList, tempsList, rainList, precipList], TABLE_CONFIG))
|
|
|
|
|
|
} |
|
|
} |
|
|
resolve(output) |
|
|
resolve(output) |
|
|
}) |
|
|
}) |
|
|