Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Sample Code

Windows Server 2008

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.HomeServer.SDK.Interop.v1;

namespace Health_Notification_Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            IWHSInfo info = new WHSInfoClass();
            //Register application name
            info.Init("Sample App");

            NotificationCallbackClass notificationClass = new NotificationCallbackClass();
            //Register notification callback class
            info.RegisterForNotifications(notificationClass);

            //Check current state
            Console.WriteLine("Current State: " + notificationClass.GetHealthState().ToString());

            Console.WriteLine("Done");
            Console.ReadLine();
        }
    }

    public enum HealthState
    {
        Healthy, //Green
        Warning, //Yellow
        Error    //Red
    }

    //Storage container for individual notifications
    class NotificationItem
    {
        public string UniqueID { get; set; }
        public WHS_Notification_Severity Severity { get; set; }
        public bool IsSuppressed { get; set; }
        public string textHeader { get; set; }
        public string textDescription { get; set; }
        public string helpFilename { get; set; }
        public string helpSection { get; set; }
        public string helpLinkText { get; set; }

        public NotificationItem(string UniqueID, WHS_Notification_Severity Severity, int IsSuppressed, string textHeader, string textDescription, string helpFilename, string helpSection, string helpLinkText)
        {
            this.UniqueID = UniqueID;
            this.Severity = Severity;
            this.IsSuppressed = IsSuppressed == 1;
            this.textHeader = textHeader;
            this.textDescription = textDescription;
            this.helpFilename = helpFilename;
            this.helpSection = helpSection;
            this.helpLinkText = helpLinkText;

        }
    }

    public class NotificationCallbackClass : INotificationCallback
    {
        Dictionary<string, NotificationItem> notificationItems = new Dictionary<string, NotificationItem>();
        
        //Custom method to count through outstanding notifications and determine
        //how many of which kinds are oustanding and not suppressed
        public HealthState GetHealthState()
        {
            int warnings = 0;
            int errors = 0;

            foreach (NotificationItem item in notificationItems.Values)
            {
                //Go through each notification and count non-suppressed ones of each type
                if (item.Severity == WHS_Notification_Severity.WHS_WARNING && !item.IsSuppressed)
                {
                    warnings++;
                }
                else if (item.Severity == WHS_Notification_Severity.WHS_ERROR && !item.IsSuppressed)
                {
                    errors++;
                }
            }

            if (errors > 0)
                return HealthState.Error;
            else if (warnings > 0)
                return HealthState.Warning;
            else
                return HealthState.Healthy;
        }

        public void NotificationChanged(string UniqueID, WHS_Notification_Type Type, WHS_Notification_Severity Severity, int IsSuppressed, string textHeader, string textDescription, string helpFilename, string helpSection, string helpLinkText)
        {
            NotificationItem item = new NotificationItem(UniqueID, Severity, IsSuppressed, textHeader, textDescription, helpFilename, helpSection, helpLinkText);
            
            //if an add, add/overwrite item to existing list
            if (Type == WHS_Notification_Type.WHS_NOTIFICATION_ADD)
            {
                notificationItems[item.UniqueID] = item;
            }
            else if (Type == WHS_Notification_Type.WHS_NOTIFICATION_REMOVE)
            {
                //If remove, if item exists remove it
                if (notificationItems.ContainsKey(item.UniqueID))
                {
                    notificationItems.Remove(item.UniqueID);
                }
            }
        }

        //Methods that are unused in this example but required as part of implementing INotificationCallback
        public void BackupStateChanged(WHSBackupState State)
        {

        }

        public void Disconnected()
        {

        }
        
        public void PhysicalDiskChanged(IDiskInfo pDiskInfo)
        {

        }

        public void ReConnected()
        {

        }

        public void UserInfoChanged()
        {

        }
    }
}


Show:
© 2015 Microsoft