private void AcceptDialog()
        {
            if (availableRadioButton.IsChecked.Value)
            {
                if (availableListBox.SelectedItem != null)
                {
                    ListBoxItem item = availableListBox.SelectedItem as ListBoxItem;
                    if (item != null)
                        SelectedConnection = item.Tag as InsteonConnection;
                }
            }
            else if (networkRadioButton.IsChecked.Value)
            {
                if (string.IsNullOrWhiteSpace(networkTextBox.Text))
                {
                    MessageBox.Show(this, "Please specify a valid network connection.", "Mayhem", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }
                SelectedConnection = new InsteonConnection(InsteonConnectionType.Net, networkTextBox.Text);
            }
            else if (serialRadioButton.IsChecked.Value)
            {
                ComboBoxItem item = serialComboBox.SelectedItem as ComboBoxItem;
                if (item != null)
                    SelectedConnection = item.Tag as InsteonConnection;
            }

            DialogResult = SelectedConnection != null;
            Close();
        }
Example #2
0
 /// <summary>
 /// Determines whether this connection is the same as the specified connection.
 /// </summary>
 /// <remarks>
 /// Equality is determined by comparing the Type and Value properties.
 /// The Name and Address properties are not used in the comparison as they are intended for informational display purposes only.
 /// </remarks>
 /// <param name="other">The specified other connection object.</param>
 /// <returns>Returns true if this connection is the same as the other connection.</returns>
 public bool Equals(InsteonConnection other)
 {
     if (other != null)
         return Type == other.Type && string.Equals(Value, other.Value, StringComparison.InvariantCultureIgnoreCase);
     else
         return false;
 }
Example #3
0
 /// <summary>
 /// Connects to an INSTEON network using the specified connection.
 /// </summary>
 /// <param name="connection">Specifies the connection to the INSTEON powerline controller device, which can accessed serially or over the network. Examples: "serial: COM1" or "net: 192.168.2.5".</param>
 public void Connect(InsteonConnection connection)
 {
     this.Messenger.Connect(connection);
     this.Connection = connection;
     this.Controller = new InsteonController(this);
     this.OnConnected();
     this.LastConnectStatus = null;
 }
        public CommandResponse ProcessCommand(CommandRequest commandRequest)
        {
            var network = new InsteonNetwork();
            var connection = new InsteonConnection(InsteonConnectionType.Net, "");
            network.Connect(connection);
            
            
            return new CommandResponse();

        }
Example #5
0
        public void Connect(InsteonConnection connection)
        {
            lock (this.bridge)
            {
                this.ControllerProperties = this.bridge.Connect(connection);
            }
            Log.WriteLine("Connected to '{0}'", connection);

            byte[] message = { 0x6B, 0x48 }; // disable deadman
            this.Send(message);
        }
        /// <summary>
        /// Attempts to connect to an INSTEON network by trying each specified connection. The first successful connection will be returned.
        /// </summary>
        /// <param name="connections">Specifies the list of connections.</param>
        /// <returns>Returns true if a connection was successfully made, or false if unable to find a connection.</returns>
        /// <remarks>
        /// This is a blocking operation that accesses the local network.
        /// Through the progress event, connection status is reported and the operation can be cancelled.
        /// This method does not throw an exception.
        /// </remarks>
        public bool TryConnect(InsteonConnection[] connections)
        {
            if (connections != null)
            {
                var list = new List<InsteonConnection>();
                list.AddRange(connections);
                foreach (InsteonConnection connection in connections)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("Trying connection {0} of {1} on {2}", list.IndexOf(connection) + 1, list.Count, connection.Value);
                    if (connection.Name != connection.Value)
                    {
                        sb.AppendFormat(" '{0}'", connection.Name);
                    }
                    if (!connection.Address.IsEmpty)
                    {
                        sb.AppendFormat("  ({0})", connection.Address);
                    }

                    OnConnectProgress(50 * list.IndexOf(connection) / list.Count + 50, sb.ToString()); // 50% to 100% progress
                    if (LastConnectStatus.Cancel)
                    {
                        return false;
                    }

                    if (TryConnect(connection))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// Connects to an INSTEON network using the specified connection.
        /// </summary>
        /// <param name="connection">Specifies the connection to the INSTEON controller device, which can accessed serially or over the network. Examples: "serial: COM1" or "net: 192.168.2.5".</param>
        /// <returns>Returns true if a connection was successfully made, or false if unable to find a connection.</returns>
        /// <remarks>
        /// This method does not throw an exception.
        /// </remarks>
        public bool TryConnect(InsteonConnection connection)
        {
            if (!Messenger.TryConnect(connection))
            {
                return false;
            }

            Connection = connection;
            Controller = new InsteonController(this);
            OnConnected();
            LastConnectStatus = null;

            return true;
        }
Example #8
0
        public bool TryConnect(InsteonConnection connection)
        {
            if (connection != null)
            {
                try
                {
                    Log.WriteLine("Trying connection '{0}'...", connection.ToString());

                    lock (this.bridge)
                    {
                        this.ControllerProperties = this.bridge.Connect(connection);
                    }
                    Log.WriteLine("Connected to '{0}'", connection);

                    byte[] message = { 0x6B, 0x48 }; // disable deadman
                    this.TrySend(message);

                    return true;
                }
                catch (Exception ex)
                {
                    Log.WriteLine("ERROR: Could not connect to '{0}'. {1}", connection.ToString(), ex.Message);
                }
            }
            return false;
        }
 private void AddNetworkConnection(InsteonConnection connection, bool select)
 {
     ListBoxItem item = new ListBoxItem();
     item.Content = connection.Name;
     item.ToolTip = InsteonService.GetConnectionInfo(connection);
     item.Tag = connection;
     availableListBox.Items.Add(item);
     if (select)
     {
         item.IsSelected = true;
         availableListBox.Focus();
     }
 }
Example #10
0
 /// <summary>
 /// Parses a string into a connection string object.
 /// </summary>
 /// <param name="text">The specified connection string.</param>
 /// <param name="connection">The returned connection object.</param>
 /// <returns>Returns true if the string could be parsed.</returns>
 public static bool TryParse(string text, out InsteonConnection connection)
 {
     try
     {
         connection = Parse(text);
         return true;
     }
     catch (ArgumentException)
     {
         connection = null;
         return false;
     }
     catch (FormatException)
     {
         connection = null;
         return false;
     }
 }
Example #11
0
 public static string GetConnectionInfo(InsteonConnection connection)
 {
     StringBuilder sb = new StringBuilder();
     if (connection.Type == InsteonConnectionType.Net && connection.Name != connection.Value)
         sb.AppendFormat("Network Address: {0}", connection.Value);
     if (!connection.Address.IsEmpty)
     {
         if (sb.Length > 0)
             sb.Append(", ");
         sb.AppendFormat("INSTEON Address: {0}", connection.Address.ToString());
     }
     if (sb.Length > 0)
         return sb.ToString();
     else
         return null;
 }
Example #12
0
 public static void StartNetwork(InsteonConnection connection)
 {
     Connecting = true;
     ThreadPool.QueueUserWorkItem(ConnectThreadProc, connection);
 }
        public Dictionary<PropertyKey, int> Connect(InsteonConnection connection)
        {
            port?.Close();

            port = SerialPortCreator.Create(connection);
            port.Open();

            byte[] input = { Constants.MessageStartByte, (byte)InsteonModemSerialCommand.GetImInfo };
            var properties = new Dictionary<PropertyKey, int>();
            var response = new List<byte>();

            try
            {
                for (int i = 1; i <= Constants.negotiateRetries; ++i)
                {
                    logger.DebugFormat("TX: {0}", Utilities.ByteArrayToString(input));
                    port.Write(input);

                    port.Wait(Constants.openTimeout);
                    var output = port.ReadAll();
                    if (output.Length <= 0)
                    {
                        Thread.Sleep(100);
                        continue; // try again
                    }

                    response.Clear();
                    response.AddRange(output);

                    while (output.Length > 0 && response.Count < 9)
                    {
                        port.Wait(Constants.openTimeout);
                        output = port.ReadAll();
                        response.AddRange(output);
                    }

                    logger.DebugFormat("RX: {0}", Utilities.ByteArrayToString(response.ToArray()));

                    int offset = 0;

                    // determins the start location of the actual message returned
                    for (int j = 0; j < response.Count; ++j)
                    {
                        if (response[j] == Constants.MessageStartByte)
                        {
                            offset = j;
                        }
                    }

                    if (response.Count >= offset + 9 &&
                        response[offset] == Constants.MessageStartByte &&
                        response[offset + 1] == (byte)InsteonModemSerialCommand.GetImInfo &&
                        response[offset + 8] == Constants.MessageEndByte)
                    {
                        properties[PropertyKey.Address] = response[offset + 2] << 16 | response[offset + 3] << 8 | response[offset + 4];
                        properties[PropertyKey.DevCat] = response[offset + 5];
                        properties[PropertyKey.SubCat] = response[offset + 6];
                        properties[PropertyKey.FirmwareVersion] = response[offset + 7];
                        break; // found
                    }
                }
            }
            finally
            {
                if (response.Count == 0)
                {
                    throw new IOException("Failed to open port, timeout waiting for response from port.");
                }

                if (properties.Keys.Count == 0)
                {
                    port.Close();
                    port = null;
                    throw new IOException("Failed to open port, unable to negotiate with INSTEON controller.");
                }
            }

            logger.DebugFormat("Successfully negotiated with INSTEON controller on connection '{0}'...", connection);
            port.SetNotify(DataAvailable);
            return properties;
        }
Example #14
0
 internal void Disconnect()
 {
     this.Connection = null;
     this.OnDisconnected();
 }
        public Dictionary<PropertyKey, int> Connect(InsteonConnection connection)
        {
            if (this.port != null)
                this.port.Close();

            this.port = SerialPortCreator.Create(connection);
            this.port.Open();

            var input = new byte[] { 0x02, 0x60 };
            var properties = new Dictionary<PropertyKey, int>();
            var response = new List<byte>();

            try
            {
                for (var i = 1; i <= Constants.negotiateRetries; ++i)
                {
                    Log.WriteLine("TX: {0}", Utilities.ByteArrayToString(input));
                    this.port.Write(input);

                    this.port.Wait(Constants.openTimeout);
                    var output = this.port.ReadAll();
                    if (output.Length <= 0)
                    {
                        Thread.Sleep(100);
                        continue; // try again
                    }

                    response.Clear();
                    response.AddRange(output);

                    while (output.Length > 0 && response.Count < 9)
                    {
                        this.port.Wait(Constants.openTimeout);
                        output = this.port.ReadAll();
                        response.AddRange(output);
                    }

                    Log.WriteLine("RX: {0}", Utilities.ByteArrayToString(response.ToArray()));

                    var offset = 0;
                    for (var j = 0; j < response.Count; ++j)
                        if (response[j] == 0x02)
                            offset = j;

                    if (response.Count >= offset + 9 && response[offset] == 0x02 && response[offset + 1] == 0x60 && response[offset + 8] == 0x06)
                    {
                        properties[PropertyKey.Address] = response[offset + 2] << 16 | response[offset + 3] << 8 | response[offset + 4];
                        properties[PropertyKey.DevCat] = response[offset + 5];
                        properties[PropertyKey.SubCat] = response[offset + 6];
                        properties[PropertyKey.FirmwareVersion] = response[offset + 7];
                        break; // found
                    }
                }
            }
            finally
            {
                if (response.Count == 0)
                    throw new IOException("Failed to open port, timeout waiting for response from port.");

                if (properties.Keys.Count == 0)
                {
                    this.port.Close();
                    this.port = null;
                    throw new IOException("Failed to open port, unable to negotiate with INSTEON controller.");
                }
            }

            Log.WriteLine("Successfully negotiated with INSTEON controller on connection '{0}'...", connection);
            this.port.SetNotify(this.DataAvailable);
            return properties;
        }