Esempio n. 1
0
        public void TunnelingActionFeedbackTest()
        {
            ResetEvent = new ManualResetEventSlim();

            KnxConnection connection = new KnxConnectionTunneling("127.0.0.1", 3671, "127.0.0.1", 3672) { Debug = false };

            connection.KnxEventDelegate += Event;

            connection.Connect();

            Thread.Sleep(50);

            connection.Action(LightOnOffAddress, true);

            if (!ResetEvent.Wait(Timeout))
                Assert.Fail("Didn't receive feedback from the action");
        }
        public JsonResult Ändra(string room, string lampa, string state)
        {
            var setting = GetSettings().FirstOrDefault(item => item.RoomNormalized.Equals(room, StringComparison.CurrentCultureIgnoreCase) &&
                item.LampaNormalized.Equals(lampa, StringComparison.CurrentCultureIgnoreCase));
            if (setting != null)
            {
                lock (knxLock)
                {

                    var localAddress = ConfigurationManager.AppSettings["localAddress"];
                    var localPort = GetNextRandom();
                    var remoteAddress = ConfigurationManager.AppSettings["remoteAddress"];
                    var remotePort = int.Parse(ConfigurationManager.AppSettings["remotePort"]);

                    var connection = new KnxConnectionTunneling(remoteAddress, remotePort, localAddress, localPort);
                    try
                    {
                        connection.KnxConnectedDelegate += KnxConnected;
                        connection.KnxEventDelegate += KnxEvent;
                        connection.KnxStatusDelegate += KnxStatusEvent;
                        connection.Connect();
                        Wait(100);

                        foreach (var address in setting.Addresses)
                        {
                            if (address.OnValue == 1)
                            {
                                var value = (state.ToLower() == "on" ? true : false);
                                //Only allow change if trying to turn off or the light is allowed to be turned on 
                                if (value == false || setting.CanTurnOn)
                                    connection.Action(address.Address, value);
                            }
                            else if (address.OnValue > 1)
                            {
                                var value = (state.ToLower() == "on" ? address.OnValue : 0);
                                //Only allow change if trying to turn off or the light is allowed to be turned on 
                                if (value == 0 || setting.CanTurnOn)
                                    connection.Action(address.Address, value);
                            }

                        }
                        Wait(100);
                    }
                    finally
                    {
                        if (connection != null)
                            connection.Disconnect();
                    }
                }
            }
            return Json("");
        }