Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter Name Fixes #4

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions send-cwop-rest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { connect } from 'cloudflare:sockets';
const cache = caches.default;
const host_domain_name = 'send.cwop.rest'
const host_url = `https://${host_domain_name}`;
const packet_sender_name = 'cwop.rest';

export default {
async fetch(request) {
Expand Down Expand Up @@ -47,7 +50,7 @@ export async function handleRequest(request) {

if (body.packet) { // default to provided packet, if any
packet = body.packet + '—viacwop.rest';
} else if (body.time && body.id && body.lat && body.long && body.tempf && body.windspeed && body.windgust && body.winddir) { // otherwise, check for required params to build our own
} else if (body.time && body.id && body.lat && body.long && body.tempf != null && body.windspeedmph != null && body.windgustmph != null && body.winddir != null) { // otherwise, check for required params to build our own
packet = buildPacket(body);
} else {
return new Response('Missing required packet or readings parameters in payload', { "status": 422 });
Expand All @@ -70,7 +73,7 @@ export async function handleRequest(request) {
// possibly valid! has this id sent recently?

const id = packet.split('>')[0];
const cacheKey = 'https://send.cwop.rest/id=' + id;
const cacheKey = `${host_url}/id=${id}`;
const lastSentTimeResponse = await cache.match(cacheKey);
if (lastSentTimeResponse) {
const lastSentTime = await lastSentTimeResponse.text();
Expand All @@ -84,7 +87,7 @@ export async function handleRequest(request) {
if (validationCode) server = 'rotate.aprs.net'; // http://www.wxqa.com/servers2use.html
if (manuallySpecifiedServer) server = manuallySpecifiedServer;

if (url.host !== 'send.cwop.rest') { // for testing
if (url.host !== host_domain_name) { // for testing
return new Response('APRS packet "' + packet + '" would have been sent to ' + server, { "status": 200 });
}

Expand All @@ -97,7 +100,10 @@ export async function handleRequest(request) {

await cache.put(cacheKey, new Response(Date.now().toString()));

return new Response('APRS packet "' + packet + '" sent to ' + server, { "status": 200 });
let msg = `APRS packet '${packet}' sent to '${server}'`;
console.log(msg);

return new Response(msg, { "status": 200 });

}

Expand Down Expand Up @@ -199,7 +205,7 @@ function buildPacket(observation) {
}
}

packet += 'cwop.rest';
packet += packet_sender_name;

return packet;

Expand Down Expand Up @@ -305,6 +311,5 @@ export async function sendPacket(packet, server, port, validationCode = '-1') {

console.log('Closing connection to ' + server + ':' + port);

return new Response(serverResponse, { "headers": { "Content-Type": "text/plain" } });

return new Response(serverResponse, { "headers": { "Content-Type": "text/plain" } });
}