private MTConnect.Application.Streams.ReturnData GetCurrent(Data.AgentInfo config) { MTConnect.Application.Streams.ReturnData result = null; string address = config.Address; int port = config.Port; string deviceName = config.DeviceName; // Set Proxy Settings var proxy = new HTTP.ProxySettings(); proxy.Address = config.ProxyAddress; proxy.Port = config.ProxyPort; DateTime requestTimestamp = DateTime.Now; string url = HTTP.GetUrl(address, port, deviceName) + "current"; result = MTConnect.Application.Streams.Requests.Get(url, proxy, 2000, 1); if (result != null) { if (verbose) { Logger.Log("Current Successful : " + url + " @ " + requestTimestamp.ToString("o") + " : " + result.Header.LastSequence, LogLineType.Console); } } else { Logger.Log("Current Error : " + url + " @ " + requestTimestamp.ToString("o") + " : Retrying in " + (config.Heartbeat / 1000) + " sec.."); } return(result); }
public void Update_Sample(MTConnect.Application.Streams.ReturnData returnData) { List <Instance> instances = ProcessInstances(currentData, returnData); bufferedInstances.AddRange(instances); previousInstanceDataOld = previousInstanceDataNew; }
private void SendSampleData(MTConnect.Application.Streams.ReturnData returnData, DeviceConfiguration config) { var data = new EventData(this); data.Id = "MTCONNECT_SAMPLE"; data.Data01 = config; data.Data02 = returnData; SendData?.Invoke(data); }
// Process InstanceData after receiving Current Data public static Instance ProcessInstance(MTConnect.Application.Streams.ReturnData currentData) { var result = new Instance();; result.Timestamp = currentData.Header.CreationTime; // Agent.MTConnect.org only outputs to the nearest second (not fractional seconds), check if issue with Open Source Agent result.AgentInstanceId = currentData.Header.InstanceId; result.Sequence = currentData.Header.LastSequence; var dataItems = currentData.DeviceStreams[0].GetAllDataItems(); FillInstanceDataWithCurrentData(new List <string>(), result, dataItems); return(result); }
private void RunRequests(DeviceConfiguration config) { var ac = config.Agent; if (ac != null) { // Run a Probe request and get the returned data if (probeData == null) { probeData = GetProbe(ac); // Send the Probe data to other plugins SendProbeData(probeData, config); } if (probeData != null) { // Run a Current request and get the returned data currentData = GetCurrent(ac); if (currentData != null && currentData.Header != null) { // Send the Current data to other plugins SendCurrentData(currentData, config); //Run a Sample request and get the returned data var sampleData = GetSample(currentData.Header, ac, config); //Send the Sample data to other plugins if (sampleData != null) { SendSampleData(sampleData, config); } UpdateAgentData(currentData.Header.InstanceId, currentData.Header.LastSequence); } else { probeData = null; SendAvailability(false, ac.Heartbeat, config); } } else { SendAvailability(false, ac.Heartbeat, config); } } }
private MTConnect.Application.Streams.ReturnData GetSample(Streams header, Data.AgentInfo ac, DeviceConfiguration config) { MTConnect.Application.Streams.ReturnData result = null; string address = ac.Address; int port = ac.Port; string deviceName = ac.DeviceName; // Set Proxy Settings var proxy = new HTTP.ProxySettings(); proxy.Address = ac.ProxyAddress; proxy.Port = ac.ProxyPort; SampleInfo info = GetSampleInfo(header, config); if (info != null) { if (info.Count > 0) { DateTime requestTimestamp = DateTime.Now; string url = HTTP.GetUrl(address, port, deviceName) + "sample?from=" + info.From.ToString() + "&count=" + info.Count.ToString(); result = MTConnect.Application.Streams.Requests.Get(url, proxy, ac.Heartbeat / 2, 2); if (result != null) { UpdateAgentData(header.InstanceId, info.From + info.Count); if (verbose) { Logger.Log("Sample Successful : " + url + " @ " + requestTimestamp.ToString("o"), LogLineType.Console); } } else { Logger.Log("Sample Error : " + url + " @ " + requestTimestamp.ToString("o")); } } else { UpdateAgentData(header.InstanceId, header.LastSequence); } } return(result); }
public void Update_Current(MTConnect.Application.Streams.ReturnData returnData) { currentData = returnData; Instance instance = ProcessInstance(returnData); previousInstanceDataOld = previousInstanceDataNew; var cid = new CurrentInstance(); cid.CurrentData = returnData; cid.Instance = instance; var instances = new List <Instance>(); if (bufferedInstances != null && bufferedInstances.Count > 0) { instances.AddRange(bufferedInstances); } // Only return new instances instances = instances.FindAll(o => o.Timestamp > lastTimestamp); // Sort instances ASC by timestamp instances = instances.OrderBy(o => o.Timestamp).ToList(); // update lastTimestamp if (instances.Count > 0) { lastTimestamp = instances[instances.Count - 1].Timestamp; } // Clear the send buffer bufferedInstances.Clear(); bufferedInstances.Add(instance); SendCurrentInstanceData(configuration, cid); SendInstanceData(configuration, instances); }
private void RunRequests(DeviceConfiguration config) { var ac = config.Agent; if (ac != null) { // Run a Probe request and get the returned data if (probeData == null) { probeData = GetProbe(ac); // Send the Probe data to other plugins SendProbeData(probeData, config); } if (probeData != null) { // Run a Current request and get the returned data currentData = GetCurrent(ac); if (currentData != null && currentData.Header != null) { // Send the Current data to other plugins SendCurrentData(currentData, config); //Run a Sample request and get the returned data var sampleData = GetSample(currentData.Header, ac, config); //Send the Sample data to other plugins if (sampleData != null) SendSampleData(sampleData, config); UpdateAgentData(currentData.Header.InstanceId, currentData.Header.LastSequence); } else { probeData = null; SendAvailability(false, ac.Heartbeat, config); } } else SendAvailability(false, ac.Heartbeat, config); } }
// Process instance table after receiving Sample Data private List <Instance> ProcessInstances(MTConnect.Application.Streams.ReturnData currentData, MTConnect.Application.Streams.ReturnData 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].GetAllDataItems(); // 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].GetAllDataItems(); 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); }