Skip to content

Commit

Permalink
samedec: update for breaking EventCode API
Browse files Browse the repository at this point in the history
The new API is simpler and doesn't require matches for errors.

The integration tests are also updated.
  • Loading branch information
cbs228 committed Feb 22, 2024
1 parent 3138f73 commit 5007830
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
9 changes: 5 additions & 4 deletions crates/samedec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,16 @@ The child process receives the following additional environment variables:

* `SAMEDEC_EVT`: the three-character SAME event code, like "`RWT`"

* `SAMEDEC_EVENT`: human-readable event name: "`Required Weekly Test`." If the
event code is not known, and it its significance level is also unknown, then
this string will be "`Unrecognized`."
* `SAMEDEC_EVENT`: human-readable event description, including its significance
level: "`Required Weekly Test`." If the event code is not known, and it its
significance level is also unknown, then this string will be
"`Unrecognized Warning`."

* `SAMEDEC_SIGNIFICANCE`: one-character significance level. This variable will
be empty if the significance level could not be determined (i.e., because
the event code is unknown).

* `T`: Test
* `M`: Message
* `S`: Statement
* `E`: Emergency
* `A`: Watch
Expand Down
4 changes: 2 additions & 2 deletions crates/samedec/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ mod tests {
use super::*;

use chrono::{Duration, TimeZone, Utc};
use sameold::EventCode;
use sameold::Phenomenon;

#[test]
fn test_make_demo_message() {
Expand All @@ -270,7 +270,7 @@ mod tests {
Message::StartOfMessage(hdr) => hdr,
_ => unreachable!(),
};
assert_eq!(msg.event().unwrap(), EventCode::PracticeDemoWarning);
assert_eq!(msg.event().phenomenon(), Phenomenon::PracticeDemoWarning);
assert_eq!(msg.issue_datetime(&tm).unwrap(), tm);
assert_eq!(msg.valid_duration(), Duration::minutes(15));
}
Expand Down
35 changes: 6 additions & 29 deletions crates/samedec/src/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io;
use std::process::{Child, Command, Stdio};

use chrono::{DateTime, Utc};
use sameold::{MessageHeader, UnrecognizedEventCode};
use sameold::MessageHeader;

/// Spawn a child process to handle the given message
///
Expand Down Expand Up @@ -54,8 +54,11 @@ where
header.originator().as_display_str(),
)
.env(childenv::SAMEDEC_EVT, header.event_str())
.env(childenv::SAMEDEC_EVENT, msg_to_event(&header))
.env(childenv::SAMEDEC_SIGNIFICANCE, msg_to_significance(header))
.env(childenv::SAMEDEC_EVENT, header.event().to_string())
.env(
childenv::SAMEDEC_SIGNIFICANCE,
header.event().significance().as_code_str(),
)
.env(childenv::SAMEDEC_LOCATIONS, locations.join(" "))
.env(childenv::SAMEDEC_ISSUETIME, issue_ts)
.env(childenv::SAMEDEC_PURGETIME, purge_ts)
Expand Down Expand Up @@ -143,23 +146,6 @@ mod childenv {
pub const SAMEDEC_PURGETIME: &str = "SAMEDEC_PURGETIME";
}

// convert message event code to string
fn msg_to_event(msg: &MessageHeader) -> String {
match msg.event() {
Ok(evt) => evt.as_display_str().to_owned(),
Err(err) => format!("{}", err),
}
}

// convert message event code to significance
fn msg_to_significance(msg: &MessageHeader) -> &'static str {
match msg.event() {
Ok(evt) => evt.to_significance_level().as_str(),
Err(UnrecognizedEventCode::WithSignificance(sl)) => sl.as_str(),
Err(UnrecognizedEventCode::Unrecognized) => "",
}
}

// convert DateTime to UTC unix timestamp in seconds, as string
fn time_to_unix_str(tm: DateTime<Utc>) -> String {
format!("{}", tm.format("%s"))
Expand All @@ -169,15 +155,6 @@ fn time_to_unix_str(tm: DateTime<Utc>) -> String {
mod tests {
use super::*;

use sameold::MessageHeader;

#[test]
fn test_msg_to_significance() {
const MSG: &str = "ZCZC-WXR-RWT-012345-567890-888990+0351-3662322-NOCALL-";
let msg = MessageHeader::new(MSG).unwrap();
assert_eq!("T", msg_to_significance(&msg));
}

#[test]
fn test_time_to_unix_str() {
let dt: DateTime<Utc> = DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:16:09 GMT")
Expand Down
2 changes: 1 addition & 1 deletion sample/two_and_two.22050.s16le.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set -e
exec 0>/dev/null

[ "$SAMEDEC_EVENT" = "Severe Thunderstorm Warning" ]
[ "$SAMEDEC_ORIGINATOR" = "Weather Service" ]
[ "$SAMEDEC_ORIGINATOR" = "National Weather Service" ]
[ "$SAMEDEC_SIGNIFICANCE" = "W" ]

lifetime=$(( SAMEDEC_PURGETIME - SAMEDEC_ISSUETIME))
Expand Down

0 comments on commit 5007830

Please sign in to comment.