/// <summary> /// Gets the internal ID for devices or channels by given HomeMatic device or channel address /// </summary> /// <param name="address">HomeMatic device or channel address</param> /// <returns>Internal ID (iseId); if it is -1 we wasn't able to find a matching device or channel</returns> private int GetInternalIdByAddress(string address) { int result = -1; try { bool searchChannels = address.Contains(":"); HMDevice device = null; HMDeviceChannel channel = null; device = devices.First(d => address.StartsWith(d.Address)); if (searchChannels && device != null) { channel = device.Channels.First(c => c.Address == address); } if (device != null && !String.IsNullOrEmpty(device.Address)) { result = device.InternalId; if (channel != null && !String.IsNullOrEmpty(channel.Address)) { result = channel.InternalId; } } } catch (Exception) { // Seems that we have a problem finding this id in internal data so we let it go and pass a minus-one to indicate our disability } return(result); }
public DialogHMData(HMBase hmElement) { HMElement = hmElement; InitializeComponent(); try { Text += HMElement.GetType().Name; lblDataPointName.Text = hmElement.ToString(); switch (Text) { case "HMDevice": txtValue.ReadOnly = true; txtValue.Text = "A device can not be set directly; choose a channel or data point!"; break; case "HMDeviceChannel": HMDeviceChannel chan = (HMDeviceChannel)HMElement; txtValue.Text = String.Concat(chan.PrimaryValue); btnSet.Enabled = true; break; case "HMDeviceDataPoint": HMDeviceDataPoint dp = (HMDeviceDataPoint)HMElement; txtValue.Text = String.Concat(dp.ValueString); btnSet.Enabled = true; break; case "HMSystemMessage": txtValue.ReadOnly = true; txtValue.Text = "A message can not be set directly; choose a channel or data point!"; break; case "HMSystemVariable": HMSystemVariable svar = (HMSystemVariable)HMElement; txtValue.Text = String.Concat(svar.Value); btnSet.Enabled = true; break; default: txtValue.ReadOnly = true; txtValue.Text = "Don't know what it is; choose a channel or data point!"; break; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Identifing Homematic element failed...\n" + ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Gets the device channels datapoint by given HomeMatic device or channel address and data value type name /// </summary> /// <param name="address"></param> /// <param name="valueType">The name of the value type (STATE, LOWBAT, MOTION, etc.)</param> /// <returns>Data point</returns> public HMDeviceDataPoint GetDataByAddress(string address, string valueTypeName) { try { HMDeviceChannel channel = GetChannelByAddress(address); if (channel != null && channel.DataPoints.ContainsKey(valueTypeName)) { return(channel.DataPoints[valueTypeName]); } return(null); } catch (Exception) { return(null); } }
/// <summary> /// Gets all devices including their channels but without any data point or state data /// </summary> /// <returns>List containing devices with channels</returns> private List <HMDevice> GetDevices() { List <HMDevice> result = new List <HMDevice>(); // requesting devices list from HomeMatic XmlApi XmlDocument xmlDevices = GetApiData(xmlApiMethodDevice); // iterating devices foreach (XmlElement devElement in xmlDevices.DocumentElement.ChildNodes) { HMDevice device = new HMDevice() { Name = devElement.GetAttribute("name"), Address = devElement.GetAttribute("address"), InternalId = int.Parse(devElement.GetAttribute("ise_id")), DeviceType = devElement.GetAttribute("device_type") }; // iterating channels foreach (XmlElement chanElement in devElement.ChildNodes) { HMDeviceChannel channel = new HMDeviceChannel() { Name = chanElement.GetAttribute("name"), Address = chanElement.GetAttribute("address"), InternalId = int.Parse(chanElement.GetAttribute("ise_id")), }; device.AddChannel(channel); } result.Add(device); } return(result); }
/// <summary> /// Updates the global device list including their channels and data point or state data based on XML input /// </summary> /// <param name="xmlStates">XML data for states</param> private void UpdateStates(XmlDocument xmlStates) { string currentElementPlain = String.Empty; // iterating devices foreach (XmlElement devElement in xmlStates.DocumentElement.ChildNodes) { try { currentElementPlain = devElement.InnerXml.Length >= 100 ? devElement.InnerXml.Substring(0, 100) : devElement.InnerXml; int devIseId = int.Parse(devElement.GetAttribute("ise_id")); // looking for existing device HMDevice device; try { device = devices.First(d => devIseId == d.InternalId); } catch { device = null; } if (device != null) { // iterating channels foreach (XmlElement chanElement in devElement.ChildNodes) { try { currentElementPlain = chanElement.InnerXml.Length >= 100 ? chanElement.InnerXml.Substring(0, 100) : chanElement.InnerXml; int chanIseId = int.Parse(chanElement.GetAttribute("ise_id")); // looking for existing channel HMDeviceChannel channel; try { channel = device.Channels.First(c => chanIseId == c.InternalId); } catch { channel = null; } if (channel == null && String.Concat(chanElement.GetAttribute("name")).Contains(device.Name + ":0")) { // create new channel and add to device channel = new HMDeviceChannel() { Name = "DeviceRoot", Address = String.Concat(device.Address, ":0"), InternalId = int.Parse(chanElement.GetAttribute("ise_id")), }; device.AddChannel(channel); } else if (channel == null) { // create new channel and add to device channel = new HMDeviceChannel() { Name = chanElement.GetAttribute("name"), Address = chanElement.GetAttribute("address"), InternalId = int.Parse(chanElement.GetAttribute("ise_id")), }; device.AddChannel(channel); } else { // clear all data points to create new channel.DataPoints.Clear(); } // iterating data points if (channel != null) { foreach (XmlElement pointElement in chanElement.ChildNodes) { try { HMDeviceDataPoint dataPoint = new HMDeviceDataPoint() { InternalId = int.Parse(pointElement.GetAttribute("ise_id")), InternalIdParent = chanIseId, Type = pointElement.GetAttribute("type"), LastUpdateTimeStamp = long.Parse(pointElement.GetAttribute("timestamp")), ValueString = pointElement.GetAttribute("value"), ValueType = pointElement.GetAttribute("valuetype"), ValueUnit = pointElement.GetAttribute("valueunit") }; channel.AddDataPoint(dataPoint.Type, dataPoint); } catch (Exception ex) { WriteInternalLog("DataPoint failed: " + ex.Message, true); // well, maybe there was an datapoint that could not be created // due to missing information } } } } catch (Exception ex) { WriteInternalLog("Channel failed: " + ex.Message + "\n --- " + currentElementPlain, true); // well, maybe there was an channel that is not listed in device list // no problem, we'll just ignore it at this point } } } } catch (Exception ex) { WriteInternalLog("Device failed: " + ex.Message + "\n --- " + currentElementPlain, true); // well, maybe there was an device that is not listed in device list // no problem, we'll just ignore it at this point } } }
public void AddChannel(HMDeviceChannel channel) { channels.Add(channel); }