Пример #1
0
        /// <summary>
        /// Set zone status, connects to the end point address and sets a zone status.
        /// </summary>
        /// <param name="endPointAddress">Server end point address.</param>
        /// <param name="adr">Zone Address to set the status.</param>
        /// <param name="stateCommand">Command to send to zone.</param>
        private static void SetZoneState(EndpointAddress endPointAddress, Address adr, ZoneState stateCommand)
        {
            Console.WriteLine(">>> Setup M&C server ...");

            ILog _log = LogManager.GetCurrentClassLogger();
            IMonitorAndControl             pipeProxy      = null;
            IMonitorAndControlNotification serverCallback = new ServerCallback();

            try
            {
                int port    = FindPort();
                var binding = new WSDualHttpBinding("WSDualHttpBinding_IMonitorAndControl");
                binding.ClientBaseAddress = new Uri("http://" + NetworkHelper.getHostName() + ":" + port + "/");

                /*note the "DuplexChannelFactory".  This is necessary for Callbacks.
                 * A regular "ChannelFactory" won't work with callbacks.*/
                DuplexChannelFactory <IMonitorAndControl> pipeFactory =
                    new DuplexChannelFactory <IMonitorAndControl>(
                        new InstanceContext(serverCallback), binding, endPointAddress);
                try
                {
                    Console.WriteLine(">>>   creating channel to {0} with callback address {1}", endPointAddress.Uri.ToString(), binding.ClientBaseAddress.ToString());

                    //Open the channel to the server
                    pipeProxy = pipeFactory.CreateChannel();
                    pipeProxy.Connect();

                    // Get zone status
                    ZoneState state = pipeProxy.GetZoneState(adr);
                    Console.WriteLine(">>>   zone state: {0}", state.ToString());

                    // Get zone status
                    pipeProxy.SetZoneState(adr, stateCommand);
                }
                catch (Exception e)
                {
                    Console.WriteLine("SetZoneState - Exception: {0}", e.Message);
                    _log.Fatal(m => m("SetZoneState - Exception: {0}", e.Message));
                }
            }
            catch (FaultException <ArgumentException> exc)
            {
                Console.WriteLine("SetZoneState - FaultException: {0}", exc);
                _log.Fatal(m => m("SetZoneState - FaultException: {0}", exc));
            }
            catch (Exception exc)
            {
                Console.WriteLine("SetZoneState - Exception: {0}", exc);
                _log.Fatal(m => m("SetZoneState - Exception: {0}", exc));
            }
        }
        /// <summary>
        /// Commands the zone state.
        /// </summary>
        /// <param name="zoneId">Address of the zone.</param>
        /// <param name="command">The new zone state.</param>
        public void SetZoneState(Address zoneId, ZoneState command)
        {
            _log.Trace(m => m(String.Format("M&C Proxy; SetZoneState(); Address: {0}, Command: {1}", zoneId, command)));

            _mcServiceProxy.SetZoneState(zoneId, command);
        }