zensysupnplightcpdevice.cs

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

CS
226
字号
//////////////////////////////////////////////////////////////////////////////////////////////// 
//
//          #######
//          #   ##    ####   #####    #####  ##  ##   #####
//             ##    ##  ##  ##  ##  ##      ##  ##  ##
//            ##  #  ######  ##  ##   ####   ##  ##   ####
//           ##  ##  ##      ##  ##      ##   #####      ##
//          #######   ####   ##  ##  #####       ##  #####
//                                           #####
//          Z-Wave, the wireless language.
//
//          Copyright Zensys A/S, 2003,2004
//
//          All Rights Reserved
//
//          Description:   This source file is the main sourcefile for the
//                         ZensysUPnPLightCPDevice class, which is a control point
//                         for UPnP BinaryLight devices
//
//                         Source is based on source code generated by Intel DeviceBuilder Tool
//
//          Author:   Johann Sigfredsson
//
//          Last Changed By:  $Author: jch $
//          Revision:         $Revision: 1.1 $
//          Last Changed:     $Date: 2006/03/31 14:30:46 $
//
//////////////////////////////////////////////////////////////////////////////////////////////

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

namespace Zensys
{
	/// <summary>
	/// Summary description for ZensysUPnPLightCPDevice.
	/// </summary>
	public class ZensysUPnPLightCPDevice : ZensysUPnPCPDevice
	{
    public enum lightEventID : byte
    {
      None,
      Status,
    }

    public delegate void UPnPLightCP_SubscriberHandler(ZensysCPUPnPLightDeviceStorage lightDevice, eventID state, lightEventID eventState);
	
    public event UPnPLightCP_SubscriberHandler UPnPLightCP_subscriberEvent;

    [Serializable]
    public struct ZensysCPUPnPLightDeviceStorage
    {
      public byte nodeID;
      public UPnPDevice device;
      public CpSwitchPower SwitchPower;
      public CpDimmingService DimmingService;
    }

    private BinaryLightDiscovery disco;
    private ArrayList CPUPnPLightDeviceList = null;


    /// <summary>
    /// 
    /// </summary>
    /// <param name="log"></param>
    public ZensysUPnPLightCPDevice(ZWaveLogging log)
		{
      m_log = log;
      CPUPnPLightDeviceList = new ArrayList();
      disco = new BinaryLightDiscovery();
      disco.OnAddedDevice += new BinaryLightDiscovery.DiscoveryHandler(AddSink);
      disco.OnRemovedDevice += new BinaryLightDiscovery.DiscoveryHandler(RemoveSink);
			
      disco.Start();
    }
		
    
    /// <summary>
    /// 
    /// </summary>
    /// <param name="devStore"></param>
    public void ZensysLightToggle(ZensysCPUPnPLightDeviceStorage devStore)
    {
      bool currentStatus;
      try
      {
        currentStatus = devStore.SwitchPower.Status;
      }
      catch (Exception e)
      {
        currentStatus = false;
        m_log.Write("ZensysLightToggle: Exception - currentStatus set to " + currentStatus.ToString() + " [" + e.Message + "]");
      }
      try
      {
        devStore.SwitchPower.SetTarget(!currentStatus);
      }
      catch (Exception e)
      {
        m_log.Write("ZensysLightToggle: Exception - SwitchPower " + e.Message);
        try
        {
          devStore.DimmingService.SetLoadLevelTarget((devStore.DimmingService.LoadLevelStatus > 0) ? (byte)0 : (byte)75);
        }
        catch (Exception excp)
        {
          m_log.Write("ZensysLightToggle: Exception - DimmingService " + excp.Message);
        }
      }
    }


    private void SwitchPower_OnStateVariable_Status(CpSwitchPower sender, Boolean NewValue)
    {
      m_log.Write("Binary light State variable Status : " + NewValue.ToString());
      foreach (ZensysCPUPnPLightDeviceStorage dev in CPUPnPLightDeviceList)
      {
        if (dev.SwitchPower == sender)
        {
          if (UPnPLightCP_subscriberEvent != null)
          {
            UPnPLightCP_subscriberEvent(dev, eventID.state, lightEventID.Status);
          }
          // TODO - Here we tell Application the SetTarget Status
          break;
        }
      }
    }

    

    private void DimmingService_OnStateVariable_LoadLevelStatus(CpDimmingService sender, Byte NewValue)
    {
      m_log.Write("Binary light State variable LoadLevelStatus : " + NewValue.ToString());
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="d"></param>
    private void AddSink(BinaryLightDiscovery sender, UPnPDevice d)
    {
      ZensysCPUPnPLightDeviceStorage devstore = new ZensysCPUPnPLightDeviceStorage();
      devstore.nodeID = 0xff;
      devstore.device = d;
			
      // 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.
      devstore.DimmingService = new CpDimmingService(d.GetServices(CpDimmingService.SERVICE_NAME)[0]);
      devstore.SwitchPower = new CpSwitchPower(d.GetServices(CpSwitchPower.SERVICE_NAME)[0]);

      // 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.
      devstore.DimmingService._subscribe(300);
      devstore.DimmingService.OnStateVariable_LoadLevelStatus +=new MicroStack.CpDimmingService.StateVariableModifiedHandler_LoadLevelStatus(DimmingService_OnStateVariable_LoadLevelStatus);
      devstore.SwitchPower._subscribe(300);
      devstore.SwitchPower.OnStateVariable_Status +=new MicroStack.CpSwitchPower.StateVariableModifiedHandler_Status(SwitchPower_OnStateVariable_Status);
      // 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 */
      CPUPnPLightDeviceList.Add(devstore);
      m_log.Write("BinaryLight " + d.FriendlyName + " found");
      /* Tell our subscribers */
      if (UPnPLightCP_subscriberEvent != null)
      {
        UPnPLightCP_subscriberEvent(devstore, eventID.include, lightEventID.None);
      }
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="d"></param>
    private void RemoveSink(BinaryLightDiscovery sender, UPnPDevice d)
    {
      foreach (ZensysCPUPnPLightDeviceStorage devstore in CPUPnPLightDeviceList)
      {
        if (d == devstore.device)
        {
          /* Tell our subscribers */
          CPUPnPLightDeviceList.Remove(devstore);
          m_log.Write("BinaryLight " + d.FriendlyName + " removed");
          if (UPnPLightCP_subscriberEvent != null)
          {
            UPnPLightCP_subscriberEvent(devstore, eventID.exclude, lightEventID.None);
          }
          return;
        }
      }
      m_log.Write("BinaryLight " + d.FriendlyName + " disappeared but was not registered");
    }


    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public override string ToString()
    {
      return "UPnPBinaryLightCP";
    }
  }
}

⌨️ 快捷键说明

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