zensysupnpdevicescanner.cs

来自「zwave 无线通讯协议 PC controller 控制器源码」· CS 代码 · 共 191 行

CS
191
字号
//////////////////////////////////////////////////////////////////////////////////////////////// 
//
//          #######
//          #   ##    ####   #####    #####  ##  ##   #####
//             ##    ##  ##  ##  ##  ##      ##  ##  ##
//            ##  #  ######  ##  ##   ####   ##  ##   ####
//           ##  ##  ##      ##  ##      ##   #####      ##
//          #######   ####   ##  ##  #####       ##  #####
//                                           #####
//          Z-Wave, the wireless language.
//
//          Copyright Zensys A/S, 2003,2004
//
//          All Rights Reserved
//
//          Description:   This source file is the main sourcefile for the
//                         ZensysUPnPDeviceScanner class
//
//                         Source is based on source code generated by Intel DeviceBuilder Tool
//
//          Author:   Johann Sigfredsson
//
//          Last Changed By:  $Author: jch $
//          Revision:         $Revision: 1.2 $
//          Last Changed:     $Date: 2006/04/26 08:56:04 $
//
//////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections;
using System.Threading;
using Intel.UPNP;
using MicroStack;
using Zensys.ZWave.Communication;
using Zensys.ZWave.Logging;

namespace Zensys
{
	/// <summary>
	/// Summary description for ZensysUPnPAvRendererCPDevice.
	/// </summary>
  public class ZensysUPnPDeviceScanner
  {
    public enum devState
    {
      incl,
      excl,
    }

    public delegate void SubscriberHandler(UPnPDevice device, devState state);
	
    public event SubscriberHandler subscriberEvent;

    private Mutex mutexDeviceScanner = new Mutex();
    private ZensysUPnPDeviceScannerDiscovery disco;
    private ArrayList UPnPDeviceScannerList = null;
    private ZWaveLogging mLog;

    public ZensysUPnPDeviceScanner(ZWaveLogging m_log)
    {
      this.UPnPDeviceScannerList = new ArrayList();
      this.mLog = m_log;
      this.disco = new ZensysUPnPDeviceScannerDiscovery();
      this.disco.OnAddedDevice += new ZensysUPnPDeviceScannerDiscovery.DiscoveryHandler(AddSink);
      this.disco.OnRemovedDevice += new ZensysUPnPDeviceScannerDiscovery.DiscoveryHandler(RemoveSink);
      this.disco.Start();
    }


    public void ZensysUPnPDeviceScannerReScan()
    {
      this.disco.ReScan();
    }


    public void ZensysUPnPDeviceScannerRemoveDevice(UPnPDevice d)
    {
      bool breakout = false;
      if (d != null)
      {
        foreach (UPnPDevice dev in UPnPDeviceScannerList)
        {
          if (d == dev)
          {
            if (this.subscriberEvent != null)
            {
              this.subscriberEvent(d, devState.excl);
            }
            this.mLog.Write("UPnP Device " + d.FriendlyName + " manually removed");
            disco.ForceDisposeDevice(d);
            breakout = true;
            break;
          }
        }
        if (!breakout)
        {
          mLog.Write("UPnP device not registered");
        }
      }
      else
      {
        mLog.Write("No UPnP device to remove!");
      }
    }
  
  
    private void AddSink(ZensysUPnPDeviceScannerDiscovery sender, UPnPDevice d)
    {
      this.mutexDeviceScanner.WaitOne();
      try
      {
        // To interace with a service, instantiate the appropriate wrapper class on the appropriate service
        // Traverse the device heirarchy to the correct device, and invoke 'GetServices', passing in the static field 'SERVICE_NAME'
        // of the appropriate wrapper class. This method returns an array of all services with this service type. For most purposes,
        // there will only be one service, in which case you can use array index 0.
        // Save a reference to this instance of the wrapper class for later use.

        // To subscribe to Events, call the '_subscribe' method of the wrapper class. The only parameter is
        // the duration of the event. A good value is 300 seconds.

        // The wrapper class exposes all the evented state variables through events in the form 'OnStateVariable_xx', where xx is the variable name.
  			
        // The wrapper class exposes methods in two formats. Asyncronous and Syncronous. The Async method calls are exposed simply
        // by the name of the method. The Syncronous version is the same, except with the word, 'Sync_' prepended to the name.
        // Asyncronous responses to th async method calls are dispatched through the event in the format, 'OnResult_x' where x is the method name.
  			
        // Note: All arguments are automatically type checked. Allowed Values are abstracted through enumerations, that are defined in the wrapper class.
        // To access the list of allowed values or ranges for a given device, refer to the property 'Values_XXX' for a list of the allowed values for a
        // given state variable. Similarly, refer to the properties 'HasMaximum_XXX', 'HasMinimum_XXX', 'Maximum_XXX', and 'Minimum_XXX' where XXX is the variable name, for the Max/Min values.
  			
        // To determine if a given service implements a particular StateVariable or Method, use the properties, 'HasStateVariableXXX' and 'HasActionXXX' where XXX is the method/variable name.

        /* Add it to our list of devices */
        this.UPnPDeviceScannerList.Add(d);
        this.mLog.Write("UPnP Device " + d.FriendlyName + " discovered");
        /* Tell our subscribers */
        if (this.subscriberEvent != null)
        {
          this.subscriberEvent(d, devState.incl);
        }
      }
      catch (Exception e)
      {
        this.mLog.Write(e.Message);
      }
      finally
      {
        this.mutexDeviceScanner.ReleaseMutex();
      }
    }

    private void RemoveSink(ZensysUPnPDeviceScannerDiscovery sender, UPnPDevice d)
    {
      this.mutexDeviceScanner.WaitOne();
      bool breakout = false;
      try
      {
        foreach (UPnPDevice device in UPnPDeviceScannerList)
        {
          if (d == device)
          {
            /* Found */
            breakout = true;
            this.UPnPDeviceScannerList.Remove(device);
            this.mLog.Write("UPnP Device " + d.FriendlyName + " removed");
            /* Tell our subscribers */
            if (this.subscriberEvent != null)
            {
              this.subscriberEvent(d, devState.excl);
            }
            break;
          }
        }
        if (!breakout)
        {
          this.mLog.Write("UPnP Device " + d.FriendlyName + " disappeared but was not registered");
        }
      }
      catch (Exception e)
      {
        this.mLog.Write(e.Message);
      }
      finally
      {
        this.mutexDeviceScanner.ReleaseMutex();
      }
    }

  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?