Skip to content

Commit

Permalink
Array / Loop version of pr #2
Browse files Browse the repository at this point in the history
Signed-off-by: Michael J. Kidd <linuxkidd@gmail.com>
  • Loading branch information
linuxkidd committed Mar 20, 2024
1 parent 4ff8263 commit 985851c
Show file tree
Hide file tree
Showing 2 changed files with 364 additions and 0 deletions.
288 changes: 288 additions & 0 deletions scripts/powershell/eclipse/april-2024-solar-eclipse.ps1
@@ -0,0 +1,288 @@
#######################################################################
# Note: This script is called from within the root SharpCap directory #
#######################################################################

#################
# Configuration #
#################

. .\scripts\eclipse\april-2024-solar-eclipse-config.ps1


#############
# Functions #
#############

##
# This function returns the current time.
#
# @returns string
#
function getCurrentTime {
( Get-Date ).ToString( 'HH:mm:ss' )
}

##
# This function returns an offset between a timestamp and the current time.
#
# @param string $timestamp
#
# @returns double
#
function getTimestampFromCurrentTime {
param (
[ Parameter( Mandatory, Position = 0 ) ]
[ String ] $timestamp
)

$timestamp_offset = ( ( Get-Date $timestamp ) - ( Get-Date ) ).TotalSeconds

[ Double ] $timestamp_offset
}

##
# This function sleeps until a specific time.
#
# @param string $timestamp
#
# @returns string
#
function sleepUntil {
param (
[ Parameter( Mandatory, Position = 0 ) ]
[ String ] $timestamp
)

$timestamp_offset = getTimestampFromCurrentTime $timestamp
$current_time = getCurrentTime
$do_sleep = if ( $timestamp_offset -gt 0 ) { $true } else { $false }

if ( $do_sleep ) {
Write-Information "$current_time`: Waiting until $timestamp ($timestamp_offset seconds)..." -InformationAction Continue

Start-Sleep -Seconds $timestamp_offset
}
else {
Write-Information "$current_time`: $timestamp is before the current time, skipping sleep" -InformationAction Continue
}

return $do_sleep
}

##
# This function plays a sound.
#
# Note: Calling this function consecutively will result in
# only the last sound playing through the speakers.
#
# @param string $path
#
# @returns void
#
function playSound {
param (
[ Parameter( Mandatory, Position = 0 ) ]
[ String ] $path
)

$current_time = getCurrentTime

Write-Information "$current_time`: Play sound ($path)" -InformationAction Continue

( New-Object Media.SoundPlayer $soundBase+$path ).Play();
}

##
# This function plays a TTS (text-to-speech) sound.
#
# Note: Calling this function consecutively will result in
# only the last sound playing through the speakers.
#
# @param string $text
#
# @returns void
#
function playTTSSound {
param (
[ Parameter( Mandatory, Position = 0 ) ]
[ String ] $text
)

$current_time = getCurrentTime
Write-Information "$current_time`: Play TTS sound (""$text"")" -InformationAction Continue

Add-Type -AssemblyName System.Speech

$tts = ( New-Object System.Speech.Synthesis.SpeechSynthesizer );
$tts.Volume = 100

$tts.SpeakAsyncCancelAll();

$tts.SetOutputToDefaultAudioDevice();

$tts.SpeakAsync( $text );
}

##
# This function initializes the script.
#
# @returns string
#
function initializeScript {
$current_time = getCurrentTime
$initialized_timestamp_offset = getTimestampFromCurrentTime $script_start_time

Write-Information "$current_time`: SharpCap Powershell Eclipse Script Initialized" -InformationAction Continue
Write-Information "$current_time`: ----------------------------------------------" -InformationAction Continue
Write-Information "" -InformationAction Continue

if ( $initialized_timestamp_offset -gt 0 ) {
if ( $display_windows_notifications ) {
showNotifyIcon 'SharCap Script - Initialized' 'The SharpCap eclipse script has been initialized and is waiting to start' 'Info'
}

if ( $use_tts ) {
playTTSSound 'The SharpCap eclipse script has been initialized and is waiting to start'
}
else {
playSound '00-Initialized.wav'
}
}
else {
Write-Information "$current_time`: Skipping initialize alert" -InformationAction Continue
}
}

##
# This function displays a Windows notification.
#
# @param string $title
# @param string $text
# @param string $icon_type
# @param string $timeout
#
# @returns void
#
function showNotifyIcon {
[CmdletBinding()]

param (
[ Parameter( Mandatory, Position = 0 ) ]
[ String ] $title,

[ Parameter( Mandatory, Position = 1 ) ]
[ ValidateNotNullOrEmpty() ]
[ String ] $text,

[ Parameter( Position = 2 ) ]
[ ValidateSet( "Error", "Info", "None", "Warning" ) ]
[ String ] $icon_type = 'Info',

[ Parameter( Position = 3 ) ]
[ Int ] $timeout = 10000
)

$current_time = getCurrentTime
Write-Information "$current_time`: Showing notification ($title)" -InformationAction Continue

Add-Type -AssemblyName "System.Windows.Forms"

if ( ! ( Test-Path -Path Variable:\Script:icon ) ) {
$ps_exe = Join-Path $PSHOME "powershell.exe" -Resolve
$Script:icon = [ Drawing.Icon ]::ExtractAssociatedIcon( $ps_exe )
}

$NotifyIcon = ( New-Object System.Windows.Forms.NotifyIcon )
$NotifyIcon.Icon = $Script:icon;
$NotifyIcon.Visible = $true;

$NotifyIcon.ShowBalloonTip( $timeout, $title, $text, [ Windows.Forms.ToolTipIcon ] $icon_type )

# Note: On Windows 11, calling $NotifyIcon.Dispose(); right after $NotifyIcon.ShowBalloonTip()
# helps to ensure the notification is removed from the system tray and also the notifications center
# automatically after some time. It also helps to ensure the notifications are removed from the system
# tray when the script exits. However, this does affect the notification window title (the Powershell
# application is not named).
$NotifyIcon.Dispose();
}

##
# This function sleeps until a specified timestamp and then triggers an audible alert. It may also trigger a Windows Notification.
#
# @param string $timestamp
# @param string $sound_path
# @param string $tts_text
# @param string $notification_title
# @param string $notification_text
# @param string $notification_icon_type
# @param int $notification_timeout
#
# @uses $display_windows_notifications
# @uses $use_tts
#
# @returns void
#
function sleepUntilAndThenTriggerAlert {
param (
[ Parameter( Mandatory, Position = 0 ) ]
[ String ] $timestamp,

[ Parameter( Mandatory, Position = 1 ) ]
[ String ] $sound_path,

[ Parameter( Mandatory, Position = 2 ) ]
[ String ] $tts_text,

[ Parameter( Mandatory, Position = 3 ) ]
[ string ] $notification_title,

[ Parameter( Position = 4 ) ]
[ ValidateSet( "Error", "Info", "None", "Warning" ) ]
[ String ] $notification_icon_type = 'Info',

[ Parameter( Position = 5 ) ]
[ Int ] $notification_timeout = 10000
)

$current_time = getCurrentTime
$did_sleep = sleepUntil( $timestamp )

if ( $did_sleep ) {
if ( $display_windows_notifications ) {
showNotifyIcon $notification_title $tts_text $notification_icon_type $notification_timeout
}

if ( $use_tts ) {
playTTSSound $tts_text
}
else {
playSound $sound_path
}
}
else {
Write-Information "$current_time`: Skipping alert ($notification_title)" -InformationAction Continue
}

Write-Information "" -InformationAction Continue
}


##########
# Script #
##########

# Initialize
initializeScript

ForEach($alert in $alerts) {
$type = 'Info'
if ( $alert.ContainsKey('type') ) {
$type = $alert['type']
}
$startTime = ( Get-Date $times[$alert['base']] ).AddSeconds( $alert['offset'] ).ToString( 'HH:mm:ss' )
sleepUntilAndThenTriggerAlert $startTime $alert['sound'] $alert['tts'] $alert['title'] $type
}

# Exit (5 seconds after the script end alert)
Write-Information "Exiting..." -InformationAction Continue
sleepUntil ( Get-Date $script_end_time ).AddSeconds(5).ToString('HH:mm:ss')
@@ -0,0 +1,76 @@
#################
# Configuration #
#################

##
# Global
#

# Windows Notifications
$display_windows_notifications = $true

# TTS
$use_tts = $true

# Where the pre-generated sounds live - if $use_tts = $false
$soundBase = '.\scripts\eclipse\sounds\'


# Eclipse Times
$times=@{'c1'='13:06:13','c2'='14:20:32','c3'='14:23:05','c4'='15:09:26'}

##
# Sounds, Text-to-Speech (TTS), and Notifications
#
$alerts = @(
@{'base'='c1', 'offset'=-150, 'sound'='00-Initialized.wav', 'tts'='The SharpCap eclipse script has been initialized and is waiting to start', 'title'='SharCap Script - Initialized'},

@{'base'='c1', 'offset'=-120, 'sound'='01-Eclipse-Start.wav', 'tts'='The SharpCap eclipse script has started', 'title'='SharpCap Script - Started'},
@{'base'='c1', 'offset'=-60, 'sound'='02-First-Contact-60-Seconds.wav', 'tts'='First contact is in 60 seconds', 'title'='First Contact In 60 Seconds'},
@{'base'='c1', 'offset'=-30, 'sound'='00-30-Seconds.wav', 'tts'='30 seconds', 'title'='First Contact In 30 Seconds'},
@{'base'='c1', 'offset'=-20, 'sound'='00-20-Seconds.wav', 'tts'='20 seconds', 'title'='First Contact In 20 Seconds'},
@{'base'='c1', 'offset'=-10, 'sound'='00-10-Seconds.wav', 'tts'='10 seconds', 'title'='First Contact In 10 Seconds'},
@{'base'='c1', 'offset'=0, 'sound'='03-First-Contact-Now.wav', 'tts'='First contact is happening now', 'title'='First contact Now', 'type'='Warning'},

@{'base'='c2', 'offset'=-1500, 'sound'='00-Refocus-Telescope.wav', 'tts'='Refocus telescope if necessary', 'title'='Refocus Telescope'},
@{'base'='c2', 'offset'=-1200, 'sound'='00-Venus.wav', 'tts'='Look for Venus at about 5 o''clock in relation to the sun', 'title'='Look for Venus'},
@{'base'='c2', 'offset'=-600, 'sound'='00-Venus.wav', 'tts'='Look for Venus at about 5 o''clock in relation to the sun', 'title'='Look for Venus'},
@{'base'='c2', 'offset'=-300, 'sound'='00-Venus.wav', 'tts'='Look for Venus at about 5 o''clock in relation to the sun', 'title'='Look for Venus'},
@{'base'='c2', 'offset'=-180, 'sound'='04-Second-Contact-3-Minutes.wav', 'tts'='Totality is in 3 minutes', 'title'='Totality in 3 Minutes'},
@{'base'='c2', 'offset'=-120, 'sound'='05-Second-Contact-2-Minutes.wav', 'tts'='Totality is in 2 minutes', 'title'='Totality in 2 Minutes'},
@{'base'='c2', 'offset'=-90, 'sound'='00-Jupiter.wav', 'tts'='Look for Jupiter at about 11 o''clock in relation to the sun', 'title'='Look for Jupiter'},
@{'base'='c2', 'offset'=-60, 'sound'='06-Second-Contact-60-Seconds.wav', 'tts'='Totality is in 1 minute', 'title'='Totality in 1 Minute'},
@{'base'='c2', 'offset'=-33, 'sound'='07-Remove-Solar-Filter.wav', 'tts'='Remove solar filter', 'title'='Remove Solar Filter', 'type'='Warning'},
@{'base'='c2', 'offset'=-30, 'sound'='00-30-Seconds.wav', 'tts'='30 seconds', 'title'='Totality in 30 Seconds'},
@{'base'='c2', 'offset'=-20, 'sound'='00-20-Seconds.wav', 'tts'='20 seconds', 'title'='Totality in 20 Seconds'},
@{'base'='c2', 'offset'=-10, 'sound'='00-10-Seconds.wav', 'tts'='10 seconds', 'title'='Totality in 10 Seconds'},
@{'base'='c2', 'offset'=-8, 'sound'='00-Diamond-Ring.wav', 'tts'='Look for diamond ring', 'title'='Diamond Ring', 'type'='Warning'},
@{'base'='c2', 'offset'=-5, 'sound'='00-Bailys-Beads.wav', 'tts'='Look for Baily''s Beads', 'title'='Baily''s Beads', 'type'='Warning'},
@{'base'='c2', 'offset'=0, 'sound'='09-Totality-Now.wav', 'tts'='Totality starts now', 'title'='Totality Starts', 'type'='Warning'},

@{'base'='c2', 'offset'=20, 'sound'='00-Observe-Horizon.wav', 'tts'='Observe the horizon', 'title'='Observe the Horizon'},
@{'base'='c2', 'offset'=50, 'sound'='10-Pons-Brooks.wav', 'tts'='Look for comet 12P (Pons-Brooks) near Jupiter at about 4 o''clock in relation to Jupiter. This comet will appear between Jupiter and the Sun if visible.', 'title'='Comet 12P (Pons-Brooks)'},
@{'base'='c2', 'offset'=76, 'sound'='11-Maximum-Eclipse-Now.wav', 'tts'='Maximum eclipse is happening now', 'title'='Maximum Eclipse Now','type'='Warning'},
@{'base'='c2', 'offset'=80, 'sound'='00-Observe-Horizon.wav', 'tts'='Observe the horizon', 'title'='Observe the Horizon'},

@{'base'='c3', 'offset'=-60, 'sound'='12-Third-Contact-60-Seconds.wav', 'tts'='Totality ends in 60 seconds', 'title'='Totality Ends in 60 seconds'},
@{'base'='c3', 'offset'=-30, 'sound'='00-30-Seconds.wav', 'tts'='30 seconds', 'title'='Totality ends in 30 seconds'},
@{'base'='c3', 'offset'=-20, 'sound'='00-20-Seconds.wav', 'tts'='20 seconds', 'title'='Totality ends in 30 seconds'},
@{'base'='c3', 'offset'=-10, 'sound'='00-10-Seconds.wav', 'tts'='10 seconds', 'title'='Totality ends in 10 seconds'},
@{'base'='c3', 'offset'=0, 'sound'='13-Totality-Ending-Now.wav', 'tts'='Totality ending now', 'title'='Totality Ending Now','type'='Warning'},
@{'base'='c3', 'offset'=3, 'sound'='00-Bailys-Beads.wav', 'tts'='Look for Baily''s Beads', 'title'='Baily''s Beads','type'='Warning'},
@{'base'='c3', 'offset'=6, 'sound'='00-Diamond-Ring.wav', 'tts'='Look for diamond ring', 'title'='Diamond Ring'},
@{'base'='c3', 'offset'=32, 'sound'='15-Replace-Solar-Filter.wav', 'tts'='Warning, Replace Solar Filter', 'title'='Replace Solar Filter','type'='Warning'},
@{'base'='c3', 'offset'=60, 'sound'='00-Jupiter-Disappear.wav', 'tts'='Jumpiter should disappear shortly', 'title'='Jupiter Disappearing'},
@{'base'='c3', 'offset'=300, 'sound'='00-Venus-Disappear.wav', 'tts'='Venus should disappear shortly', 'title'='Venus Disappearing'},
@{'base'='c3', 'offset'=600, 'sound'='00-Venus-Disappear.wav', 'tts'='Venus should disappear shortly', 'title'='Venus Disappearing'},
@{'base'='c3', 'offset'=1200, 'sound'='00-Venus-Disappear.wav', 'tts'='Venus should disappear shortly', 'title'='Venus Disappearing'},
@{'base'='c3', 'offset'=1500, 'sound'='00-Refocus-Telescope.wav', 'tts'='Refocus telescope if necessary', 'title'='Refocus Telescope'},

@{'base'='c4', 'offset'=-60, 'sound'='16-Fourth-Contact-60-Seconds.wav', 'tts'='Last contact in 60 seconds', 'title'='Last Contact'},
@{'base'='c4', 'offset'=-30, 'sound'='00-30-Seconds.wav', 'tts'='30 seconds', 'title'='Last Contact in 30 seconds'},
@{'base'='c4', 'offset'=-20, 'sound'='00-20-Seconds.wav', 'tts'='20 seconds', 'title'='Last Contact in 20 seconds'},
@{'base'='c4', 'offset'=-10, 'sound'='00-10-Seconds.wav', 'tts'='10 seconds', 'title'='Last Contact in 10 seconds'},
@{'base'='c4', 'offset'=0, 'sound'='17-Fourth-Contact-Now.wav', 'tts'='Last contact is happening now', 'title'='Last Contact', 'type'='Warning'},
@{'base'='c4', 'offset'=30, 'sound'='18-Eclipse-End.wav', 'tts'='THe SharpCap eclipse script has ended', 'title'='SharpCap Script - Ended'},
)

0 comments on commit 985851c

Please sign in to comment.