static void FillInstanceDataWithCurrentData(List <string> usedVariables, Instance data, List <MTConnectStreams.DataItem> dataItems)
        {
            foreach (var item in dataItems)
            {
                if (!usedVariables.Contains(item.DataItemId))
                {
                    var value = new Instance.DataItemValue();
                    value.Id = item.DataItemId;

                    value.Type            = item.Type;
                    value.SubType         = item.SubType;
                    value.ChangedSequence = item.Sequence;

                    if (item.Category == MTConnect.DataItemCategory.CONDITION)
                    {
                        value.Value = ((MTConnectStreams.Condition)item).ConditionValue.ToString();
                    }
                    else
                    {
                        value.Value = item.CDATA;
                    }

                    data.Values.Add(value);
                    usedVariables.Add(value.Id);
                }
            }
        }
        // Process instance table after receiving Sample Data
        private List <Instance> ProcessInstances(MTConnectStreams.Document currentData, MTConnectStreams.Document sampleData)
        {
            var stpw = new System.Diagnostics.Stopwatch();

            stpw.Start();

            var result = new List <Instance>();

            Instance previousData = previousInstanceDataOld;

            if (currentData != null && sampleData != null)
            {
                if (sampleData.DeviceStreams != null && currentData.DeviceStreams != null)
                {
                    // Get all of the DataItems from the DeviceStream object
                    var dataItems = sampleData.DeviceStreams[0].DataItems;

                    // Convert the DataItems to a List of VariableData objects
                    List <VariableData> values = VariableData.Get(dataItems);

                    // Get List of Distinct Timestamps
                    IEnumerable <DateTime> timestamps = values.Select(x => x.Timestamp).Distinct();

                    // Sort timestamps by DateTime ASC
                    List <DateTime> sortedTimestamps = timestamps.ToList();
                    sortedTimestamps.Sort();

                    // Get List of Variables used
                    IEnumerable <string> usedVariables = values.Select(x => x.Id).Distinct();

                    var currentDataItems = currentData.DeviceStreams[0].DataItems;

                    foreach (DateTime timestamp in sortedTimestamps.ToList())
                    {
                        if (previousData == null || timestamp > previousData.Timestamp)
                        {
                            var data = new Instance();

                            // Preset previous values into new InstanceData object
                            if (previousData != null)
                            {
                                data = previousData;
                            }
                            // Fill unused variables with the values from the CurrentData object
                            else
                            {
                                FillInstanceDataWithCurrentData(usedVariables.ToList(), data, currentDataItems);
                            }

                            // Set timestamp for InstanceData object
                            data.Timestamp = timestamp;

                            data.AgentInstanceId = currentData.Header.InstanceId;

                            // Get List of Values at this timestamp
                            var valuesAtTimestamp = values.FindAll(x => x.Timestamp == timestamp);

                            foreach (var ivd in valuesAtTimestamp)
                            {
                                Instance.DataItemValue oldval = data.Values.Find(x => x.Id == ivd.Id);
                                // if value with id is already in data.values then overwrite the value
                                if (oldval != null)
                                {
                                    string s = null;
                                    if (ivd.Value != null)
                                    {
                                        s = ivd.Value.ToString();
                                    }

                                    oldval.Value           = s;
                                    oldval.ChangedSequence = ivd.Sequence;
                                }
                                // if not already in data.values then create new InstanceData.Value object and add it
                                else
                                {
                                    var newval = new Instance.DataItemValue();
                                    newval.Id              = ivd.Id;
                                    newval.Type            = ivd.Type;
                                    newval.SubType         = ivd.SubType;
                                    newval.ChangedSequence = ivd.Sequence;

                                    if (ivd.Value != null)
                                    {
                                        newval.Value = ivd.Value.ToString();
                                    }
                                    data.Values.Add(newval);
                                }

                                data.Sequence = ivd.Sequence;
                            }

                            previousData = data.Copy();

                            result.Add(data);
                        }
                    }
                }
            }
            else if (currentData != null)
            {
                Instance instanceData = ProcessInstance(currentData);

                if (previousData == null || instanceData.Timestamp > previousData.Timestamp)
                {
                    result.Add(instanceData);

                    previousData = instanceData.Copy();
                }
            }

            previousInstanceDataNew = previousData;

            return(result);
        }