public bool TryGetOnLevel(InsteonAddress address, out byte level) { level = 0; var device = Network.Devices.FirstOrDefault(a => a.Address.Equals(address)); return(device?.TryGetOnLevel(out level) ?? false); }
public void SimpleConnectAnySerialPortTest() { network = new InsteonNetwork(); var connected = network.TryConnect(); Assert.IsTrue(connected); Assert.AreEqual(network.Connection.Address.Value, InsteonAddress.Parse(ConfigurationManager.AppSettings["plmIdentityTest"]).Value); network.Close(); }
public void SimpleConnectKnownSerialPortTest() { InsteonConnection connection; Assert.IsTrue(InsteonConnection.TryParse(insteonSource, out connection)); network = new InsteonNetwork(); var connected = network.TryConnect(connection); Assert.IsTrue(connected); Assert.AreEqual(connection.Address.Value, InsteonAddress.Parse(ConfigurationManager.AppSettings["plmIdentityTest"]).Value); network.Close(); }
public IObservable <bool> GetLightFastOnOffObservable(InsteonAddress address, byte group = 1) { var device = DeviceFromAddress(address); return(device.InsteonMessageObservable .Where( m => (m.MessageType == InsteonMessageType.FastOnCleanup || m.MessageType == InsteonMessageType.FastOffCleanup) && m.Properties[PropertyKey.Group] == group) .Select(m => m.MessageType == InsteonMessageType.FastOffBroadcast)); }
private void LoadDevicesFromDatabase() { var dataManager = new InsteonDataManager(false); var devices = dataManager.GetAllDevices(); foreach (var device in devices) { var id = new InsteonIdentity(device.Category, device.SubCategory, device.Firmware); if (!id.IsEmpty) { Network.Devices.Add(InsteonAddress.Parse(device.Address), id); } } }
protected InsteonDevice FindDevice(string deviceId) { InsteonAddress address; if (InsteonAddress.TryParse(deviceId, out address)) { if (Manager.Network.Devices.ContainsKey(address)) { var device = Manager.Network.Devices.Find(address); return(device); } throw HttpError.NotFound("Device does not exist or is not linked."); } throw HttpError.NotFound("Device does not exist or is not linked."); }
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(); }; }
private void OnMessage(InsteonMessage message) { if (message.Properties.ContainsKey(PropertyKey.FromAddress)) { int address = message.Properties[PropertyKey.FromAddress]; if (network.Devices.ContainsKey(address)) { logger.DebugFormat("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) { logger.DebugFormat("Unknown device {0} received message {1}, adding device", InsteonAddress.Format(address), message.ToString()); //note: due to how messages are handled and how devices cannot receive new messages while pending sends (I think) we should only add on certain message types. // right now I've only tested devices where we get broadcast messages. Thus, we wait until the last message received. if (message.MessageType == InsteonMessageType.SuccessBroadcast) { InsteonIdentity?id; // TODO: probably shouldn't be in a while loop. Need a better way to address this while (!network.Controller.TryGetLinkIdentity(new InsteonAddress(address), out id)) { if (id != null) { InsteonDevice device = network.Devices.Add(new InsteonAddress(address), id.Value); device.OnMessage(message); } } } } else { logger.WarnFormat("Unknown device {0} received message {1}. Could be Identification process.", InsteonAddress.Format(address), message.ToString()); } } else { logger.DebugFormat("Controller received message {0}", message.ToString()); network.Controller.OnMessage(message); } }
/// <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; }
public void Connect(string comPort = null) { if (comPort == null) { comPort = "COM5"; } var addr = new InsteonAddress(0x49, 0xFA, 0xED); var plm = new InsteonConnection(InsteonConnectionType.Serial, comPort, "POWDERHORN", addr); Network.AutoAdd = true; bool ok = Network.TryConnect(plm); Links = Network.Controller.GetLinks(); // add devices Links.ToList().ForEach(link => { if (!Network.Devices.ContainsKey(link.Address)) { Network.Devices.Add(new InsteonAddress(link.Address.Value), new InsteonIdentity()); } }); // fire connection status var status = new ConnectionStatus() { Connected = ok, Links = Links }; _connectionStatusSubject.OnNext(status); // fire device list _devicesSubject.OnNext(Network.Devices); // monitor for changes to device list Network.Devices.DeviceAdded += Devices_DeviceAdded; }
public bool TryCommand(InsteonAddress address, InsteonDeviceCommands command, byte data) { var device = Network.Devices.FirstOrDefault(a => a.Address.Equals(address)); return(device?.TryCommand(command, data) ?? false); }
internal IoLinc(InsteonNetwork network, InsteonAddress address, InsteonIdentity identity) : base(network, address, identity) { }
internal MiniRemote(InsteonNetwork network, InsteonAddress address, InsteonIdentity identity, int buttonCount) : base(network, address, identity) { NumberOfButtons = buttonCount; }
private bool TryParserAddress(string addressText, out InsteonAddress address) { return(InsteonAddress.TryParse(addressText, out address)); }
public void Initalize() { Trace.TraceInformation("Connecting to Insteon..."); this.insteonNetwork = new InsteonNetwork(); this.insteonNetwork.Close(); this.insteonNetwork.AutoAdd = true; this.insteonNetwork.Connect(new InsteonConnection(InsteonConnectionType.Serial, this.usbSerial)); if (!this.insteonNetwork.IsConnected) { Trace.TraceError("Cannot connect to Insteon."); } this.insteonNetwork.Controller.DeviceLinked += (o, d) => { Trace.TraceInformation("DeviceLinked: {0}", d.Device.Address.ToString()); this.insteonNetwork.Controller.CancelLinkMode(); }; this.insteonNetwork.Controller.DeviceLinkTimeout += (o, d) => { Trace.TraceWarning("DeviceLinkTimeout"); this.insteonNetwork.Controller.CancelLinkMode(); }; this.insteonNetwork.Controller.DeviceUnlinked += (o, d) => { Trace.TraceInformation("DeviceUnlinked: {0}", d.Device.Address.ToString()); this.insteonNetwork.Controller.CancelLinkMode(); }; //InsteonDeviceLinkRecord[] knownLinks; //this.insteonNetwork.Controller.TryGetLinks(out knownLinks); foreach (var device in this.settings.Devices) { var d = this.insteonNetwork.Devices.Add(InsteonAddress.Parse(device.ID), new InsteonIdentity()); d.DeviceCommandTimeout += (o, data) => { Trace.TraceWarning("Device command timeout. Device: " + data.Device.Address); }; d.DeviceIdentified += (o, data) => { Trace.TraceInformation("Device identified. Device: " + data.Device.Address); }; d.DeviceStatusChanged += (o, data) => { Trace.TraceInformation("Device status changed. Device: " + data.Device.Address + ", Status: " + data.DeviceStatus); foreach (var dev in this.settings.Devices) { if (data.Device.Address.Value == InsteonAddress.Parse(dev.ID).Value) { switch (data.DeviceStatus) { case InsteonDeviceStatus.Brighten: case InsteonDeviceStatus.Dim: case InsteonDeviceStatus.FastOn: case InsteonDeviceStatus.On: dev.State = "On"; dev.Level = 255; break; case InsteonDeviceStatus.FastOff: case InsteonDeviceStatus.Off: dev.State = "Off"; dev.Level = 0; break; default: dev.State = "Unknown"; dev.Level = 0; break; } break; } } }; deviceList.Add(d); //d.Command(InsteonDeviceCommands.StatusRequest); } //if (this.insteonNetwork.Controller.TryGetLinks(out knownLinks)) //{ // foreach (var link in knownLinks) // { // this.debugOutput.DebugInformation("Adding device id {0}", link.Address); // var d = this.insteonNetwork.Devices.Add(link.Address, new InsteonIdentity()); // d.DeviceCommandTimeout += // (o, data) => // { // this.debugOutput.DebugInformation("Device command timeout. Device: " + data.Device.Address); // }; // d.DeviceIdentified += // (o, data) => // { // this.debugOutput.DebugInformation("Device identified. Device: " + data.Device.Address); // }; // d.DeviceStatusChanged += // (o, data) => // { // this.debugOutput.DebugInformation("Device status changed. Device: " + data.Device.Address + ", Status: " + data.DeviceStatus); // foreach (var de in this.settings.Devices) // if (GetInsteonDevice(de.ID) != null) // { // de.State = data.DeviceStatus == InsteonDeviceStatus.On ? "On" : "Off"; // de.Level = (byte)(data.DeviceStatus == InsteonDeviceStatus.On ? 255 : 0); // break; // } // }; // //d.Command(InsteonDeviceCommands.StatusRequest); // } //} //else //{ // this.debugOutput.DebugError("Cannot get links"); //} }
internal SmartLincInfo(string url, string address) { Uri = new Uri(url); InsteonAddress = InsteonAddress.Parse(address); }
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 }
internal PowerLincUsbModem(InsteonNetwork network, InsteonAddress address, InsteonIdentity identity) : base(network, address, identity) { }
internal DimmableLighting(InsteonNetwork network, InsteonAddress address, InsteonIdentity identity) : base(network, address, identity) { }
public InsteonDevice DeviceFromAddress(InsteonAddress address) { return(Network.Devices.Find(address)); }
internal SwitchedLighting(InsteonNetwork network, InsteonAddress address, InsteonIdentity identity) : base(network, address, identity) { }
internal void OnMessage(InsteonMessage message) { if (message.MessageType == InsteonMessageType.DeviceLink) { var address = new InsteonAddress(message.Properties[PropertyKey.Address]); var identity = new InsteonIdentity((byte)message.Properties[PropertyKey.DevCat], (byte)message.Properties[PropertyKey.SubCat], (byte)message.Properties[PropertyKey.FirmwareVersion]); var 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 MotionSensor(InsteonNetwork network, InsteonAddress address, InsteonIdentity identity) : base(network, address, identity) { }
public bool TryGetLinkIdentity(InsteonAddress address, out InsteonIdentity? identity) { return GetLinkIdentity(address, out identity); }