9 Commits

Author SHA1 Message Date
mattbk
f1d0f6dc31 Clean up APRS location logic and add spotter location. 2025-10-25 22:46:43 -05:00
mattbk
d353b1b055 Parse APRS location from packets that have that information. 2025-10-24 23:16:19 -05:00
f452e72ef9 Merge pull request 'Spot UI frames to spothole.app' (#13) from spothole into dev
Reviewed-on: #13
2025-10-22 02:17:12 +00:00
mattbk
796035ac59 Fix command. 2025-10-21 21:13:48 -05:00
mattbk
b14f4eee6a Add detail. 2025-10-21 21:12:51 -05:00
mattbk
5cd273a476 Update readme. 2025-10-21 21:10:43 -05:00
mattbk
a62cb8630b Use alternative spothole URL if desired. 2025-10-19 21:57:14 -05:00
mattbk
c9f6a4a6c5 Spot packets to spothole.app if desired. 2025-10-18 11:33:38 -05:00
mattbk
369de6f873 Make simple GET request from Spothole. 2025-10-18 10:43:56 -05:00
3 changed files with 156 additions and 6 deletions

View File

@@ -8,6 +8,9 @@ authors = ["Chris, N6CTA <mail@n6cta.com>"]
anyhow = "1.0"
chrono = "0.4"
clap = { version = "4", features = ["derive"] }
rand = "0.9.2"
regex = "1.12.2"
reqwest = { version = "0.12.24", features = ["json", "blocking"] }
serde_json = "1.0.145"
socket2 = "0.5"

View File

@@ -57,6 +57,30 @@ mwtchahrd -i <IP> -p <PORT> [-c <CHANNEL>] [-d] [-u]
- `-u, --ui_only`
Only display frames with a UI payload. When this flag is set, frames that are not UI are skipped.
- `-b, --uip <UIP>`
Send UDP to what IP address (e.g. 127.0.0.1) [default: 127.0.0.1].
- `-k, --uport <UPORT>`
Send UDP to what port (e.g. 8000; 55555 disables UDP) [default: 55555].
- `-s, --spotter <SPOTTER>`
Spotter callsign (e.g. W1CDN).
- `-o, --spothole`
Spot UI frames to a Spothole server.
- `-O, --spothole-alt <SPOTHOLE_ALT>`
Provide a different URL than https://spothole.app/api/v1/spot.
- `-f, --freq <FREQ>`
Spotting frequency [default: 14105000].
- `-h, --help`
Print help.
- `-V, --version`
Print version.
## Examples
### Monitoring All Frames
@@ -83,6 +107,14 @@ Monitor only UI frames:
mwtchahrd -i 127.0.0.1 -p 8000 -u
```
### Spotting to [Spothole.app](https://spothole.app) API
Sends only UI frames and mode is hardcoded as `PKT`. Spot from callsign `W1CDN` and report fixed frequency 14.105 MHz:
```bash
mwtchahrd --ip 192.168.0.6 --port 8000 --spotter W1CDN --spothole --freq 14105000
```
## Code Overview
- **Validation Functions:**

View File

@@ -11,6 +11,10 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::net::UdpSocket;
use std::net::Ipv4Addr;
use serde_json::json;
use reqwest;
use regex::Regex;
use rand::Rng;
/// Validate that the provided port string can be parsed into a u16 and is nonzero.
fn validate_port(port: &str) -> Result<u16, String> {
@@ -61,7 +65,28 @@ struct Cli {
/// Spotter callsign (e.g. W1CDN)
#[arg(short = 's', long)]
spotter: Option<String>,
my_call: Option<String>,
/// Spot UI frames to a Spothole server
#[arg(short = 'o', long, default_value_t = false)]
spothole: bool,
/// Provide a different URL than https://spothole.app/api/v1/spot
#[arg(short = 'O', long)]
spothole_alt: Option<String>,
/// Spotting frequency
#[arg(short = 'f', long, default_value_t = 14105000)]
freq: u32,
/// Spotter latitude DD.dddd
#[arg(short = 'y', long, default_value_t = -9999.0_f64)]
my_lat: f64,
/// Spotter longitude DD.dddd; to send negaitve (W or S), use = e.g. --my-lon=-97.1
#[arg(short = 'x', long, default_value_t = -9999.0_f64)]
my_lon: f64,
}
/// Convert a byte slice into a hex-dump string for debugging purposes.
@@ -362,7 +387,16 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
let source = hdr.callfrom_str();
let basic_destination = hdr.callto_str();
let timestamp = Local::now().format("%H:%M:%S").to_string();
let spotter = &cli.spotter;
let my_call = &cli.my_call;
let freq = &cli.freq;
let my_lat = &cli.my_lat;
let my_lon = &cli.my_lon;
// If user provides an alternate Spothole URL, use that one
let spothole_url = match &cli.spothole_alt {
Some(spothole_alt) => spothole_alt,
None => &"https://spothole.app/api/v1/spot".to_string(),
};
// Filter and compute the text from the payload only once.
let text = filter_text(&frame.payload);
@@ -375,6 +409,12 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
basic_destination.clone()
};
// Extract location from APRS format
let (lat, lon) = aprs_loc(&text);
// Only send good locations on
let json_lat = if lat > -9999.0_f64 && lon > -9999.0_f64 { lat.to_string() } else { "".to_string() }; // send nothing
let json_lon = if lat > -9999.0_f64 && lon > -9999.0_f64 { lon.to_string() } else { "".to_string() }; // send nothing
// Ignore frames where the basic destination contains "NODES" (caseinsensitive).
if basic_destination.to_uppercase().contains("NODES") {
return;
@@ -400,6 +440,31 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
return;
}
// If Spothole is enabled
if summary == "UI" && cli.spothole {
// POST JSON
let packet = json!({
"dx_call": &source,
"de_call": &my_call,
"de_latitude": &my_lat,
"de_longitude": &my_lon,
"freq": &freq,
"comment": &text,
"dx_latitude": &json_lat,
"dx_longitude": &json_lon,
"mode": "PKT",
"mode_type": "DATA",
"mode_source": "SPOT",
"time": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
});
let client = reqwest::blocking::Client::new();
let res = client.post(spothole_url)
.json(&packet)
.send();
println!("sent to {} = {}", spothole_url, packet);
println!("res = {res:?}");
}
// Send UDP
if cli.uport != 55555 {
let uaddr = format!("{}:{}", cli.uip, cli.uport);
@@ -408,9 +473,14 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
let packet = json!({
"final_destination": &final_destination,
"source": &source,
"spotter": &spotter,
"spotter": &my_call,
"spotter_latitude": &my_lat,
"spotter_longitude": &my_lon,
"summary": &summary,
"text": &text,
"dx_latitude": &json_lat,
"dx_longitude": &json_lon,
"freq": &freq,
"timestamp": SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
"type": "data"
});
@@ -428,6 +498,51 @@ fn handle_frame(frame: &AgwFrame, cli: &Cli, buffers: &mut BufferManager) {
}
}
fn aprs_loc(packet: &str) -> (f64, f64) {
// Capture different pieces of the location stringf
let re_loc = Regex::new(r"(?P<latd>\d{2})(?P<latm>[\d ]{2}\.[\d ]{2})(?P<ns>[nsNS])/(?P<lond>\d{3})(?P<lonm>[\d ]{2}\.[\d ]{2})(?P<ew>[ewEW])").unwrap();
// Only proceed if there were captures
match re_loc.captures(&packet) {
Some(_caps) => {
// Break captures into named values
let loc = re_loc.captures(&packet).unwrap();
// Initiate randomness for ambiguity..
let mut rng = rand::rng();
// Convert to decimal degrees. If ambiguity spaces are included (see APRS spec), replace them with random digits.
let mut lat_dec: f64 = &loc["latd"].trim().parse().expect("Expects a number!") + (&loc["latm"].replace(" ", &rng.random_range(0..9).to_string()).trim().parse().expect("Expects a number!") / 60.0);
// If south, make negative
if &loc["ns"] == "S" {
lat_dec = lat_dec * -1.0
}
// TODO if there are spaces in loc["latm"], the same spaces need to be in loc["lonm"] for proper ambiguity according to APRS spec
let mut lon_dec: f64 = &loc["lond"].trim().parse().expect("Expects a number!") + (&loc["lonm"].replace(" ", &rng.random_range(0..9).to_string()).trim().parse().expect("Expects a number!") / 60.0);
// If west, make negative
if &loc["ew"] == "W" {
lon_dec = lon_dec * -1.0
}
// String to paste into map for testing
//println!("{}, {}", lat_dec, lon_dec);
// Return
(lat_dec, lon_dec)
}
// Otherwise if there were no captures, return bad data
None => {
// The regex did not match. Deal with it here!
(-9999.0_f64, -9999.0_f64)
}
}
}
/// Main entry point:
/// - Parses CLI options.
/// - Connects to the AGWPE server and sends the monitor command.