public void Add(ComponentItem component) { if (Components == null) { Components = new List <ComponentItem>(); } Components.Add(component); }
public bool GetResponse(Uri requestUri, Stream stream) { var query = new RequestQuery(requestUri); if (query.IsValid) { try { DeviceDefinition device = null; List <ComponentDefinition> components = null; List <DataItemDefinition> dataItems = null; var stpw = new System.Diagnostics.Stopwatch(); stpw.Start(); // Get Current Agent var connection = Database.ReadConnection(query.DeviceId); if (connection != null) { var agent = Database.ReadAgent(query.DeviceId); if (agent != null) { // Get Device device = Database.ReadDevice(query.DeviceId, agent.InstanceId); if (!query.DeviceOnly) { // Get Components components = Database.ReadComponents(query.DeviceId, agent.InstanceId); // Get Data Items dataItems = Database.ReadDataItems(query.DeviceId, agent.InstanceId); } } stpw.Stop(); Console.WriteLine(stpw.ElapsedMilliseconds + "ms"); if (device != null) { // Use a converter to actually convert Defintions to DataItem objects. // This is because we don't want to output the Defintion properties to JSON (as they are redundant) var converter = new Converter <DataItemDefinition, DataItem>(ConvertDataItem); // Create new Item var deviceItem = new DeviceItem(device, connection, agent); if (!components.IsNullOrEmpty() && !dataItems.IsNullOrEmpty()) { deviceItem.Add(dataItems.FindAll(o => o.ParentId == deviceItem.Id).ConvertAll(converter)); var componentItems = new List <ComponentItem>(); foreach (var component in components) { var item = new ComponentItem(component); item.Add(dataItems.FindAll(o => o.ParentId == component.Id).ConvertAll(converter)); // Add any child components foreach (var child in componentItems.FindAll(o => o.ParentId == component.Id)) { componentItems.Remove(child); item.Add(child); } // Add to parent component var parent = componentItems.Find(o => o.Id == component.ParentId); if (parent != null) { parent.Add(item); } else { componentItems.Add(item); } } deviceItem.Add(componentItems); } // Write DeviceItem JSON to stream string json = Json.Convert.ToJson(deviceItem, true); var bytes = Encoding.UTF8.GetBytes(json); stream.Write(bytes, 0, bytes.Length); } } return(true); } catch (Exception ex) { log.Trace(ex); } } return(false); }