112 lines
4.0 KiB
JavaScript
112 lines
4.0 KiB
JavaScript
const fetch = require("node-fetch")
|
|
const moment = require("moment-timezone")
|
|
const puppeteer = require('puppeteer');
|
|
const fs = require("fs")
|
|
let settings = {
|
|
method: "Get"
|
|
};
|
|
let days = "2"
|
|
//var today = new Date();
|
|
//var date = (today.getMonth() + 1) + '/' + today.getDate() + '/' + today.getFullYear();
|
|
var headerhtml = "<h1>Pass Predictions for Palmyra, ME</h1><h3>(44.8453175, -69.3449081)</h3><h5>Information provided by n2yo.com</h5><br>"
|
|
var footerhtml = "<br><h5>©2022 Nicholas Pease<br>Generated on "+new Date()+"</h5>"
|
|
// Use n2yo API and grab Satellite Data
|
|
const getSatelliteInfo = async id => {
|
|
try {
|
|
Sat = await fetch("https://api.n2yo.com/rest/v1/satellite/radiopasses/" + id + "/44.8453175/-69.3449081/90/" + days + "/20&apiKey=LEVKTQ-JNT2BE-3G49YV-4BZX", settings)
|
|
json = await Sat.json();
|
|
return json
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
};
|
|
|
|
// Convert Given Timestamp to Easy Read format
|
|
function convertTimestamp(timestamp) {
|
|
var date = moment(timestamp * 1000)
|
|
date.tz('America/New_York')
|
|
formattedTime = date.format("LLLL")
|
|
return formattedTime
|
|
}
|
|
|
|
// Take Data Given and compose sectional data with each satellite
|
|
function createSection(json) {
|
|
headerinfo = "<h3>" + json.info.satname + " (" + json.info.satid + ")</h3><h4>" + days + " Day Predictions</h4>"
|
|
header = "<table><tbody><tr><th style='width: 15%;'>Start Time:</th><th style='width: 5%'>Max Elevation:</th><th style='width: 15%;'>End Time:</th></tr>"
|
|
body = null
|
|
end = "</table><br>"
|
|
json.passes.forEach(function (element, key) {
|
|
json.passes[key]
|
|
if (body) {
|
|
body = body + "<tr><td style='text-align: center'>" + convertTimestamp(json.passes[key].startUTC) + "</td><td style='text-align: center'>" + json.passes[key].maxEl + "°</td><td style='text-align: center'>" + convertTimestamp(json.passes[key].endUTC) + "</td></tr>"
|
|
} else {
|
|
body = "<tr><td style='text-align: center'>" + convertTimestamp(json.passes[key].startUTC) + "</td><td style='text-align: center'>" + json.passes[key].maxEl + "°</td><td style='text-align: center'>" + convertTimestamp(json.passes[key].endUTC) + "</td></tr>"
|
|
}
|
|
})
|
|
var html = headerinfo + header + body + end
|
|
return html
|
|
}
|
|
|
|
// Main function, handles all the data gathering and then email composition
|
|
(async () => {
|
|
const response = await Promise.all([
|
|
getSatelliteInfo("25338"),
|
|
getSatelliteInfo("28654"),
|
|
getSatelliteInfo("33591"),
|
|
getSatelliteInfo("40069"),
|
|
getSatelliteInfo("25544"),
|
|
getSatelliteInfo("57166")
|
|
]);
|
|
let NOAA15 = response[0]
|
|
let NOAA18 = response[1]
|
|
let NOAA19 = response[2]
|
|
let METEORM2 = response[3]
|
|
let ISS = response[4]
|
|
let METEORM23 = response[5]
|
|
|
|
N15HTML = createSection(NOAA15)
|
|
N18HTML = createSection(NOAA18)
|
|
N19HTML = createSection(NOAA19)
|
|
METHTML = createSection(METEORM2)
|
|
ISSHTML = createSection(ISS)
|
|
MET23HTML = createSection(METEORM23)
|
|
var messagehtml = headerhtml + N15HTML + N18HTML + N19HTML + METHTML + MET23HTML + ISSHTML + footerhtml
|
|
var margins = {
|
|
top: "50",
|
|
bottom: "50",
|
|
left: "50",
|
|
right: "50"
|
|
}
|
|
var options = {
|
|
format: 'letter',
|
|
printBackground: true,
|
|
margin: margins
|
|
};
|
|
let file = {
|
|
content: messagehtml
|
|
};
|
|
fs.writeFileSync("/var/www/html/reports/satellitepasspredictions.html", messagehtml)
|
|
savetoPDF()
|
|
|
|
|
|
})();
|
|
|
|
async function savetoPDF() {
|
|
var margins = {
|
|
top: "42",
|
|
bottom: "46",
|
|
left: "10"
|
|
}
|
|
const browser = await puppeteer.launch({
|
|
headless: true,
|
|
args: ['--no-sandbox']});
|
|
const page = await browser.newPage();
|
|
await page.goto('http://192.168.0.201/reports/satellitepasspredictions.html');
|
|
await page.pdf({
|
|
path: '/var/www/html/reports/satellitepasspredictions.pdf',
|
|
printBackground: true,
|
|
margin: margins
|
|
});
|
|
await browser.close();
|
|
}
|