Ejemplo n.º 1
0
 internal InsteonDevice(InsteonNetwork network, InsteonAddress address, InsteonIdentity identity)
 {
     this.network = network;
     this.Address = address;
     this.Identity = identity;
     this.ackTimer = new Timer(new TimerCallback(this.PendingCommandTimerCallback), null, Timeout.Infinite, Constants.deviceAckTimeout);
 }
Ejemplo n.º 2
0
        private bool GetLinkIdentity(InsteonAddress address, out InsteonIdentity?identity)
        {
            Dictionary <PropertyKey, int> properties;

            logger.DebugFormat("Device {0} GetLinkIdentity", Address.ToString());
            byte[] message = { (byte)InsteonModemSerialCommand.StandardOrExtendedMessage, address[2],                            address[1], address[0],
                               (byte)MessageFlagsStandard.ThreeHopsThreeRemaining,        (byte)InsteonDirectCommands.IdRequest, Byte.MinValue };

            var status = network.Messenger.TrySendReceive(message, false, (byte)InsteonModemSerialCommand.StandardMessage, InsteonMessageType.SetButtonPressed, out properties);

            if (status == EchoStatus.NAK)
            {
                logger.ErrorFormat("received NAK trying to get idendity information");
                identity = null;
                return(false);
            }
            if (status == EchoStatus.ACK)
            {
                if (properties == null)
                {
                    logger.ErrorFormat("Device Id {0} has null properties object", Address.ToString());
                    identity = null;
                    return(false);
                }
                identity = new InsteonIdentity((byte)properties[PropertyKey.DevCat], (byte)properties[PropertyKey.SubCat], (byte)properties[PropertyKey.FirmwareVersion]);
                return(true);
            }

            logger.ErrorFormat("received unknown status trying to get idendity information");
            identity = null;
            return(false); // echo was not ACK or NAK
        }
        /// <summary>
        /// Parses a string into a connection object.
        /// </summary>
        /// <param name="text">The specified connection string.</param>
        /// <returns>Returns the connection object.</returns>
        public static InsteonConnection Parse(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentNullException();
            }

            Regex connectionPattern = new Regex(@"\s*(?<type>[^: ]+)\s*:\s*(?<value>[^, ]+)\s*(,\s*(?<name>[^,]+)\s*)?(,\s*(?<address>[^, ]+))?");
            Match m = connectionPattern.Match(text);

            if (!m.Success)
            {
                throw new FormatException();
            }

            string type    = m.Groups["type"].ToString().Trim();
            string value   = m.Groups["value"].ToString().Trim();
            string name    = m.Groups["name"].ToString().Trim();
            string address = m.Groups["address"].ToString().Trim();

            if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(value))
            {
                throw new FormatException();
            }

            type = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(type);

            InsteonConnectionType rtype    = (InsteonConnectionType)System.Enum.Parse(typeof(InsteonConnectionType), type);
            InsteonAddress        raddress = !string.IsNullOrEmpty(address) ? InsteonAddress.Parse(address) : new InsteonAddress();

            return(new InsteonConnection(rtype, value, name, raddress));
        }
 internal void OnMessage(InsteonMessage message)
 {
     if (message.MessageType == InsteonMessageType.DeviceLink)
     {
         InsteonAddress  address  = new InsteonAddress(message.Properties[PropertyKey.Address]);
         InsteonIdentity identity = new InsteonIdentity((byte)message.Properties[PropertyKey.DevCat], (byte)message.Properties[PropertyKey.SubCat], (byte)message.Properties[PropertyKey.FirmwareVersion]);
         InsteonDevice   device   = network.Devices.Add(address, identity);
         timer.Stop();
         IsInLinkingMode = false;
         if (linkingMode.HasValue)
         {
             if (linkingMode != InsteonLinkMode.Delete)
             {
                 OnDeviceLinked(device);
             }
             else
             {
                 OnDeviceUnlinked(device);
             }
         }
         else
         {
             OnDeviceLinked(device);
         }
     }
 }
 internal InsteonDevice(InsteonNetwork network, InsteonAddress address, InsteonIdentity identity)
 {
     this.network  = network;
     this.Address  = address;
     this.Identity = identity;
     this.ackTimer = new Timer(new TimerCallback(PendingCommandTimerCallback), null, Timeout.Infinite, Constants.deviceAckTimeout);
 }
 private void OnMessage(InsteonMessage message)
 {
     if (message.Properties.ContainsKey(PropertyKey.FromAddress))
     {
         int address = message.Properties[PropertyKey.FromAddress];
         if (network.Devices.ContainsKey(address))
         {
             Log.WriteLine("Device {0} received message {1}", InsteonAddress.Format(address), message.ToString());
             InsteonDevice device = network.Devices.Find(address);
             device.OnMessage(message);
         }
         else if (message.MessageType == InsteonMessageType.SetButtonPressed)
         {
             // don't warn about SetButtonPressed message from unknown devices, because it may be from a device about to be added
         }
         else if (network.AutoAdd)
         {
             Log.WriteLine("Unknown device {0} received message {1}, adding device", InsteonAddress.Format(address), message.ToString());
             InsteonDevice device = network.Devices.Add(new InsteonAddress(address), new InsteonIdentity());
             device.OnMessage(message);
         }
         else
         {
             Log.WriteLine("WARNING: Unknown device {0} received message {1}", InsteonAddress.Format(address), message.ToString());
         }
     }
     else
     {
         Log.WriteLine("Controller received message {0}", message.ToString());
         network.Controller.OnMessage(message);
     }
 }
        private InsteonController(InsteonNetwork network, InsteonAddress address, InsteonIdentity identity)
        {
            this.network  = network;
            this.Address  = address;
            this.Identity = identity;

            this.timer.Interval  = 4 * 60 * 1000; // 4 minutes
            this.timer.AutoReset = false;
            this.timer.Elapsed  += new System.Timers.ElapsedEventHandler(timer_Elapsed);
        }
Ejemplo n.º 8
0
 private InsteonController(InsteonNetwork network, InsteonAddress address, InsteonIdentity identity)
 {
     this.network = network;
     this.Address = address;
     this.Identity = identity;
     
     this.timer.Interval = 4 * 60* 1000; // 4 minutes
     this.timer.AutoReset = false;
     this.timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Initializes a new connection instance.
        /// </summary>
        /// <param name="type">Type type of connection.</param>
        /// <param name="value">The connection value.</param>
        /// <param name="name">The display name for the connection.</param>
        /// <param name="address">The INSTEON address of the controller device.</param>
        public InsteonConnection(InsteonConnectionType type, string value, string name, InsteonAddress address)
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentNullException();

            this.Type = type;
            this.Value = value.Trim();
            if (!string.IsNullOrEmpty(name) && name.Trim().Length > 0)
                this.Name = name.Trim();
            else
                this.Name = value;
            this.Address = address;
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Adds an INSTEON device to the list of known devices.
        /// </summary>
        /// <param name="address">The INSTEON address of the device to add.</param>
        /// <param name="identity">The INSTEON identity of the device to add.</param>
        /// <returns>Returns an object representing the specified device.</returns>
        /// <remarks>This method does not perform any INSTEON messaging, it only adds the specified device to a list of known devices.</remarks>
        public InsteonDevice Add(InsteonAddress address, InsteonIdentity identity)
        {
            if (devices.ContainsKey(address.Value))
            {
                return(devices[address.Value]);
            }

            InsteonDevice device = new InsteonDevice(network, address, identity);

            devices.Add(address.Value, device);
            OnDeviceAdded(device);
            return(device);
        }
Ejemplo n.º 11
0
        private InsteonController(InsteonNetwork network, InsteonAddress address, InsteonIdentity identity)
        {
            this.network = network;
            Address      = address;
            Identity     = identity;

            timer.Interval  = 4 * 60 * 1000; // 4 minutes
            timer.AutoReset = false;
            timer.Elapsed  += (sender, args) =>
            {
                IsInLinkingMode = false;
                OnDeviceLinkTimeout();
            };
        }
        /// <summary>
        /// Initializes a new connection instance.
        /// </summary>
        /// <param name="type">Type type of connection.</param>
        /// <param name="value">The connection value.</param>
        /// <param name="name">The display name for the connection.</param>
        /// <param name="address">The INSTEON address of the controller device.</param>
        public InsteonConnection(InsteonConnectionType type, string value, string name, InsteonAddress address)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentNullException();
            }

            Type  = type;
            Value = value.Trim();
            if (!string.IsNullOrEmpty(name) && name.Trim().Length > 0)
            {
                Name = name.Trim();
            }
            else
            {
                Name = value;
            }
            Address = address;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Converts the string representation of an INSTEON address to its numeric equivalent.
        /// </summary>
        /// <param name="value">A string specifying the INSTEON address, example: "19.9E.4E".</param>
        /// <param name="address">An object representing the resulting INSTEON address.</param>
        /// <returns>Returns true if the string was successfully parsed.</returns>
        /// <remarks>
        /// This method does not throw an exception.
        /// </remarks>
        public static bool TryParse(string value, out InsteonAddress address)
        {
            address = new InsteonAddress();
            if (string.IsNullOrEmpty(value))
            {
                return(false);
            }
            value = value.Trim();
            if (value.Length != 8)
            {
                return(false);
            }
            if (value[2] != '.' || value[5] != '.')
            {
                return(false);
            }
            byte a0 = byte.Parse(value.Substring(6, 2), NumberStyles.HexNumber);
            byte a1 = byte.Parse(value.Substring(3, 2), NumberStyles.HexNumber);
            byte a2 = byte.Parse(value.Substring(0, 2), NumberStyles.HexNumber);

            address = new InsteonAddress(a2, a1, a0);
            return(true);
        }
Ejemplo n.º 14
0
 internal SmartLincInfo(string url, string address)
 {
     this.Uri            = new Uri(url);
     this.InsteonAddress = InsteonAddress.Parse(address);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Finds the object representation of the specified device within the list of known devices.
 /// </summary>
 /// <param name="address">The specified INSTEON address.</param>
 /// <returns>Returns an object representing the specified INSTEON device.</returns>
 public InsteonDevice Find(InsteonAddress address)
 {
     return(devices[address.Value]);
 }
Ejemplo n.º 16
0
 public bool TryGetLinkIdentity(InsteonAddress address, out InsteonIdentity?identity)
 {
     return(GetLinkIdentity(address, out identity));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Determiens whether the specified INSTEON device address is contained within the list of known devices.
 /// </summary>
 /// <param name="address">The specified INSTEON address.</param>
 /// <returns>Returns true if the list contains the specified INSTEON device.</returns>
 public bool ContainsKey(InsteonAddress address)
 {
     return(devices.ContainsKey(address.Value));
 }
Ejemplo n.º 18
0
 internal void OnMessage(InsteonMessage message)
 {
     if (message.MessageType == InsteonMessageType.DeviceLink)
     {
         InsteonAddress address = new InsteonAddress(message.Properties[PropertyKey.Address]);
         InsteonIdentity identity = new InsteonIdentity((byte)message.Properties[PropertyKey.DevCat], (byte)message.Properties[PropertyKey.SubCat], (byte)message.Properties[PropertyKey.FirmwareVersion]);
         InsteonDevice device = network.Devices.Add(address, identity);
         timer.Stop();
         IsInLinkingMode = false;
         if (linkingMode.HasValue)
         {
             if (linkingMode != InsteonLinkMode.Delete)
                 OnDeviceLinked(device);
             else
                 OnDeviceUnlinked(device);
         }
         else
         {
             OnDeviceLinked(device);
         }
     }
 }
 private static byte[] GetStandardMessage(InsteonAddress address, byte cmd1, byte cmd2)
 {
     byte[] message = { 0x62, address[2], address[1], address[0], 0x0F, cmd1, cmd2 };
     return(message);
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Converts the string representation of an INSTEON address to its numeric equivalent.
 /// </summary>
 /// <param name="value">A string specifying the INSTEON address, example: "19.9E.4E".</param>
 /// <param name="address">An object representing the resulting INSTEON address.</param>
 /// <returns>Returns true if the string was successfully parsed.</returns>
 /// <remarks>
 /// This method does not throw an exception.
 /// </remarks>
 public static bool TryParse(string value, out InsteonAddress address)
 {
     address = new InsteonAddress();
     if (string.IsNullOrEmpty(value))
         return false;
     value = value.Trim();
     if (value.Length != 8)
         return false;
     if (value[2] != '.' || value[5] != '.')
         return false;
     byte a0 = byte.Parse(value.Substring(6, 2), NumberStyles.HexNumber);
     byte a1 = byte.Parse(value.Substring(3, 2), NumberStyles.HexNumber);
     byte a2 = byte.Parse(value.Substring(0, 2), NumberStyles.HexNumber);
     address = new InsteonAddress(a2, a1, a0);
     return true;
 }
Ejemplo n.º 21
0
 private static byte[] GetStandardMessage(InsteonAddress address, byte cmd1, byte cmd2)
 {
     byte[] message = { 0x62, address[2], address[1], address[0], 0x0F, cmd1, cmd2 };
     return message;
 }