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> /// 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 AddDataPoint(string type, HMDeviceDataPoint dataPoint) { dataPoints.Add(type, dataPoint); }