Skip to content
azrellie edited this page Dec 26, 2024 · 11 revisions

Events in this API allow you to listen for newly issued data right as its available. Setting up these events is straightforward.

Before we start

Before we start listening for events, you must call this method:

events.enableEvents(); // tell the class to listen for events

This tells the class to begin listening for events.

You can also disable events by calling this method:

events.disableEvents(); // stop listening for events

This will make the class no longer listen for events until events.enableEvents() is called again.

Connecting the events

Just like with enabling/disabling events, this part is pretty simple.

static StormPredictionCenter spc = new();

static void Main(string[] args)
{
    spc.events.enableEvents();
    spc.events.watchIssued += Events_watchIssued;
}

static void Events_watchIssued(object sender, StormPredictionCenterWatch[] watches, StormPredictionCenterWatchBox[] watchBoxes)
{
    for (int i = 0; i < watches.Length; i++) // or you can use a foreach loop, but this works just as well
    {
        StormPredictionCenterWatch w = watches[i];
        Console.WriteLine(w.ToString());
    }
}
As of 1.1.0, only one event exists, with that being spc.events.watchIssued. More events will be added in future versions.

Example code to display newly issued watches from the SPC/NWS

static StormPredictionCenter spc = new();

static void Main(string[] args)
{
    spc.events.enableEvents();
    spc.events.watchIssued += Events_watchIssued;
    string? cmd = Console.ReadLine();
    if (cmd == "exit")
        Environment.Exit(0);
}

static void Events_watchIssued(object sender, StormPredictionCenterWatch watch, StormPredictionCenterWatchBox watchBox)
{
    Console.WriteLine($"{watch.watchType} {watch.watchNumber} watch has just been issued\n\ndetails:");
    Console.WriteLine(watch.headline);
    Console.WriteLine(watch.description);
    Console.WriteLine("\ncounties affected:");
    foreach (var county in watch.counties)
	Console.WriteLine(county.name + ", " + county.state);
}
As of 1.1.0, testing has been done and indeed works, but more testing must be done to ensure no bugs or other issues.