comparison ServerMonitor/Objects/ServerMonitor.cs @ 29:f6235dc0a8ec

Add ability to play a sound on check failure.
author Brad Greco <brad@bgreco.net>
date Fri, 14 Jun 2019 21:01:55 -0400
parents 437442cd8090
children 2ffb0bda7705
comparison
equal deleted inserted replaced
28:437442cd8090 29:f6235dc0a8ec
1 using Microsoft.Win32; 1 using Microsoft.Win32;
2 using NAudio.Wave;
2 using Renci.SshNet; 3 using Renci.SshNet;
3 using Renci.SshNet.Common; 4 using Renci.SshNet.Common;
4 using ServerMonitorApp.Properties; 5 using ServerMonitorApp.Properties;
5 using System; 6 using System;
6 using System.Collections.Generic; 7 using System.Collections.Generic;
7 using System.IO; 8 using System.IO;
8 using System.Linq; 9 using System.Linq;
10 using System.Media;
9 using System.Net.NetworkInformation; 11 using System.Net.NetworkInformation;
10 using System.Threading; 12 using System.Threading;
11 using System.Threading.Tasks; 13 using System.Threading.Tasks;
12 using System.Windows.Forms; 14 using System.Windows.Forms;
13 using System.Xml.Serialization; 15 using System.Xml.Serialization;
32 // List of check execution tasks that have been started. 34 // List of check execution tasks that have been started.
33 // A check task begins by sleeping until the next scheduled execution time, 35 // A check task begins by sleeping until the next scheduled execution time,
34 // then executes. 36 // then executes.
35 private Dictionary<Task<CheckResult>, int> tasks; 37 private Dictionary<Task<CheckResult>, int> tasks;
36 private ServerSummaryForm mainForm; 38 private ServerSummaryForm mainForm;
39 private WaveOut waveOut = new WaveOut();
40 MediaFoundationReader mediaReader;
37 41
38 /// <summary>Fires when the status of a check changes.</summary> 42 /// <summary>Fires when the status of a check changes.</summary>
39 public event EventHandler<CheckStatusChangedEventArgs> CheckStatusChanged; 43 public event EventHandler<CheckStatusChangedEventArgs> CheckStatusChanged;
40 44
41 /// <summary>The collection of registered servers.</summary> 45 /// <summary>The collection of registered servers.</summary>
88 /// <summary>Loads all servers and checks from the config file.</summary> 92 /// <summary>Loads all servers and checks from the config file.</summary>
89 public void LoadServers() 93 public void LoadServers()
90 { 94 {
91 bool triedBackup = false; 95 bool triedBackup = false;
92 96
93 Read: 97 Read:
94 TextReader reader = null; 98 TextReader reader = null;
95 try 99 try
96 { 100 {
97 reader = new StreamReader(ConfigFile); 101 reader = new StreamReader(ConfigFile);
98 XmlSerializer serializer = CreateXmlSerializer(); 102 XmlSerializer serializer = CreateXmlSerializer();
320 { 324 {
321 if (result.FailAction == FailAction.FlashTaskbar) 325 if (result.FailAction == FailAction.FlashTaskbar)
322 mainForm.AlertServerForm(result.Check); 326 mainForm.AlertServerForm(result.Check);
323 if (result.FailAction.In(FailAction.FlashTaskbar, FailAction.NotificationBalloon)) 327 if (result.FailAction.In(FailAction.FlashTaskbar, FailAction.NotificationBalloon))
324 mainForm.ShowBalloon(result); 328 mainForm.ShowBalloon(result);
325 } 329 PlaySound(result.FailSound);
330 }
331 }
332
333 /// <summary>Plays a sound.</summary>
334 /// <param name="sound">
335 /// If null, no sound is played.
336 /// If string.Empty, the Windows default error sound is played.
337 /// Otherwise, plays the sound at the given path.
338 /// </param>
339 private void PlaySound(string sound)
340 {
341 if (sound == string.Empty)
342 SystemSounds.Asterisk.Play();
343 else if (sound != null)
344 {
345 try
346 {
347 mediaReader = new MediaFoundationReader(sound);
348 waveOut.Init(mediaReader);
349 waveOut.PlaybackStopped += WaveOut_PlaybackStopped;
350 waveOut.Play();
351 }
352 catch
353 {
354 // Play the default sound if something went wrong.
355 SystemSounds.Asterisk.Play();
356 }
357 }
358 }
359
360 /// <summary>Disposes the media reader after sound playback.</summary>
361 private void WaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
362 {
363 mediaReader.Dispose();
326 } 364 }
327 365
328 /// <summary>Reads all check results from the log for a server.</summary> 366 /// <summary>Reads all check results from the log for a server.</summary>
329 /// <param name="server">The server whose check results should be read.</param> 367 /// <param name="server">The server whose check results should be read.</param>
330 /// <returns>A list of all check results found in the log file for the given server.</returns> 368 /// <returns>A list of all check results found in the log file for the given server.</returns>