/// <summary> /// Processing of input queue /// </summary> public void OnSenderTimer(object sender, System.Timers.ElapsedEventArgs args) { SenderMonitorEvent.SendMonitorEvent(EventLog, "Monitoring the send command activity", EventLogEntryType.Information, 3); m_SenderTimer.Stop(); string lLastError = string.Empty; int CountJobsToProcess = 0; try { KEPSSenderdbData senderDbData = new KEPSSenderdbData(OdataServiceUrl); JobOrders jobsToProcess = senderDbData.GetJobsToProcess(); CountJobsToProcess = jobsToProcess.JobOrdersObj.Count; SenderMonitorEvent.SendMonitorEvent(EventLog, "Jobs to process: " + CountJobsToProcess, EventLogEntryType.Information, 3); string sendState = string.Empty; if (CountJobsToProcess > 0) { // Step 1 -- Connect to UA server SenderMonitorEvent.SendMonitorEvent(EventLog, "Step 1 -- Connect to OPC server", EventLogEntryType.Information, 4); //string discoveryUrl = "opc.tcp://127.0.0.1:49320"; using (Session AMSession = ClientUtils.CreateSession(OPCServerUrl, "ArcelorMittal.UA.SenderCommand")) { SenderMonitorEvent.SendMonitorEvent(EventLog, "Loop through jobs", EventLogEntryType.Information, 4); foreach (JobOrders.JobOrdersValue jobVal in jobsToProcess.JobOrdersObj) { try { SenderMonitorEvent.SendMonitorEvent(EventLog, "new SenderJobProps", EventLogEntryType.Information, 4); SenderJobProps job = new SenderJobProps(jobVal.ID, jobVal.Command, (string)(jobVal.CommandRule)); SenderMonitorEvent.SendMonitorEvent(EventLog, "WriteToKEPServer", EventLogEntryType.Information, 4); if (WriteToKEPServer(AMSession, job)) { sendState = "Done"; if (wmiProductInfo != null) { wmiProductInfo.LastActivityTime = DateTime.Now; } } else { sendState = "Failed"; } lLastError = string.Format("JobOrderID: {0}. Send to KEP Server element {1} = {2}. Status: {3}", job.JobOrderID, job.Command, job.CommandRule, sendState); if (sendState == "Done") { Requests.UpdateJobStatus(OdataServiceUrl, job.JobOrderID, sendState); } else if (sendState == "Failed") { if (wmiProductInfo != null) { wmiProductInfo.LastServiceError = string.Format("{0}. On {1}", lLastError, DateTime.Now); } } SenderMonitorEvent.SendMonitorEvent(EventLog, lLastError, EventLogEntryType.Information, 5); } catch (Exception ex) { lLastError = "Error sending command: " + ex.ToString(); SenderMonitorEvent.SendMonitorEvent(EventLog, lLastError, EventLogEntryType.Error, 4); lLastError = "Reconecting..."; SenderMonitorEvent.SendMonitorEvent(EventLog, lLastError, EventLogEntryType.Information, 4); AMSession.Reconnect(); } } // Step 3 -- Clean up //AMSession.Close(); AMSession.KeepAlive -= ClientUtils.Session_KeepAlive; } } } catch (Exception ex) { try { string details = string.Empty; if (ex is System.Net.WebException) { var resp = new System.IO.StreamReader((ex as System.Net.WebException).Response.GetResponseStream()).ReadToEnd(); try { dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(resp); details = obj.error.message; } catch { details = resp; } } lLastError = "Error getting jobs: " + ex.ToString() + " Details: " + details; SenderMonitorEvent.SendMonitorEvent(EventLog, lLastError, EventLogEntryType.Error, 4); if (wmiProductInfo != null) { wmiProductInfo.LastServiceError = string.Format("{0}. On {1}", lLastError, DateTime.Now); } } catch (Exception exc) { SenderMonitorEvent.SendMonitorEvent(EventLog, exc.Message, EventLogEntryType.Error, 4); } } if (wmiProductInfo != null) { wmiProductInfo.SendCommandsCount += CountJobsToProcess; wmiProductInfo.PublishInfo(); } SenderMonitorEvent.SendMonitorEvent(EventLog, string.Format("Send command is done. {0} tasks", CountJobsToProcess), EventLogEntryType.Information, 3); m_SenderTimer.Start(); }
private bool WriteToKEPServer(Session AMSession, SenderJobProps job) { // Step 2 -- Read the value of a node representing a PI Point data under the hood NodeId nodeToRead = new NodeId(job.Command /*Element*/, 2); if (AMSession.NodeCache.Find(nodeToRead) is Node node) { DataValue value = AMSession.ReadValue(nodeToRead); value.ServerTimestamp = new DateTime(0); value.SourceTimestamp = new DateTime(0); WriteValue Wvalue = new WriteValue { NodeId = nodeToRead, AttributeId = Attributes.Value }; if ((node.NodeClass & (NodeClass.Variable | NodeClass.VariableType)) == 0) { Wvalue.AttributeId = Attributes.DisplayName; } Wvalue.IndexRange = null; Wvalue.Value = value; Wvalue.Value.Value = ConvertToObject(job.CommandRule); if (Wvalue.Value.Value != null) { WriteValueCollection nodesToWrite = new WriteValueCollection { Wvalue }; ResponseHeader responseHeader = AMSession.Write( null, nodesToWrite, out StatusCodeCollection results, out DiagnosticInfoCollection diagnosticInfos); Session.ValidateResponse(results, nodesToWrite); //Session.ValidateDiagnosticInfos(diagnosticInfos, nodesToWrite); if (results[0].Code == 0) { return(true); } else { SenderMonitorEvent.SendMonitorEvent(EventLog, string.Format("Can not send command to KEP Server. ErrorCode: {0}. ErrorText: {1}. Job order ID: {2}", results[0].Code, results[0].ToString(), job.JobOrderID), EventLogEntryType.Error, 5); return(false); } } else { SenderMonitorEvent.SendMonitorEvent(EventLog, string.Format("Can not convert command value: {0}. Job order ID: {1}", job.CommandRule, job.JobOrderID), EventLogEntryType.Error, 5); return(false); } } else { SenderMonitorEvent.SendMonitorEvent(EventLog, string.Format("Command not valid: {0}. Job order ID: {1}", job.Command, job.JobOrderID), EventLogEntryType.Error, 5); return(false); } }
/// <summary> /// Processing of input queue /// </summary> public void OnSenderTimer(object sender, System.Timers.ElapsedEventArgs args) { SenderMonitorEvent.sendMonitorEvent(vpEventLog, "Monitoring the send command activity", EventLogEntryType.Information); m_SenderTimer.Stop(); string lLastError = string.Empty; int CountJobsToProcess = 0; try { KEPSSenderdbData senderDbData = new KEPSSenderdbData(OdataServiceUrl); JobOrders jobsToProcess = senderDbData.getJobsToProcess(); CountJobsToProcess = jobsToProcess.JobOrdersObj.Count; SenderMonitorEvent.sendMonitorEvent(vpEventLog, "Jobs to process: " + CountJobsToProcess, EventLogEntryType.Information); string sendState = string.Empty; if (CountJobsToProcess > 0) { // Step 1 -- Connect to UA server //string discoveryUrl = "opc.tcp://127.0.0.1:49320"; using (Session AMSession = ClientUtils.CreateSession(OPCServerUrl, "ArcelorMittal.UA.SenderCommand")) { foreach (JobOrders.JobOrdersValue jobVal in jobsToProcess.JobOrdersObj) { try { SenderJobProps job = new SenderJobProps(jobVal.ID, jobVal.Command, (string)(jobVal.CommandRule)); if (WriteToKEPServer(AMSession, job)) { sendState = "Done"; wmiProductInfo.LastActivityTime = DateTime.Now; } else { sendState = "Failed"; } lLastError = String.Format("JobOrderID: {0}. Send to KEP Server element {1} = {2}. Status: {3}", job.JobOrderID, job.Command, job.CommandRule, sendState); if (sendState == "Done") { Requests.updateJobStatus(OdataServiceUrl, job.JobOrderID, sendState); } else if (sendState == "Failed") { wmiProductInfo.LastServiceError = string.Format("{0}. On {1}", lLastError, DateTime.Now); } } catch (Exception ex) { lLastError = "Error sending command: " + ex.ToString(); SenderMonitorEvent.sendMonitorEvent(vpEventLog, lLastError, EventLogEntryType.Error); lLastError = "Reconecting..."; SenderMonitorEvent.sendMonitorEvent(vpEventLog, lLastError, EventLogEntryType.Information); AMSession.Reconnect(); } } // Step 3 -- Clean up AMSession.Close(); } } } catch (Exception ex) { lLastError = "Error getting jobs: " + ex.ToString(); SenderMonitorEvent.sendMonitorEvent(vpEventLog, lLastError, EventLogEntryType.Error); wmiProductInfo.LastServiceError = string.Format("{0}. On {1}", lLastError, DateTime.Now); } wmiProductInfo.SendCommandsCount += CountJobsToProcess; wmiProductInfo.PublishInfo(); SenderMonitorEvent.sendMonitorEvent(vpEventLog, string.Format("Send command is done. {0} tasks", CountJobsToProcess), EventLogEntryType.Information); m_SenderTimer.Start(); }