/// <summary> /// Run our simulation /// </summary> /// <param name="state"></param> private void RunSimulation(object state) { DateTime CurrentTime = dtStartTime.Value; DateTime StartTime = dtStartTime.Value; DateTime EndTime = dtEndTime.Value; int CurrentRow = 0; while (CurrentRow < EMSCommands.Count) { MM_EMS_Command ThisCommand = EMSCommands[CurrentRow]; //Now, send our command IssueCommand(ThisCommand, CurrentRow); CurrentRow++; if (CurrentRow < EMSCommands.Count - 1) { MM_EMS_Command NextCommand = EMSCommands[CurrentRow + 1]; if (NextCommand.IssuedOn > EndTime) { break; } Thread.Sleep((int)((NextCommand.IssuedOn - ThisCommand.IssuedOn).TotalSeconds * 1000.0 / (double)nudSpeed.Value)); } } MessageBox.Show("Simulation completed.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); }
/// <summary> /// Issue an EMS command /// </summary> /// <param name="Command"></param> /// <param name="CurrentRow"></param> /// <param name="Server"></param> private void IssueCommand(MM_EMS_Command Command, int CurrentRow) { if (InvokeRequired) { Invoke((MethodInvoker) delegate { IssueCommand(Command, CurrentRow); }); } else { dgvCommandHistory.CurrentCell = dgvCommandHistory.Rows[CurrentRow].Cells[0]; dgvCommandHistory.Refresh(); Servers[cmbServer.SelectedItem.ToString()].SendCommand(Command.BuildOutgoingLine()); } }
/// <summary> /// When our cell changes, update our start time /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgvCommandHistory_CurrentCellChanged(object sender, EventArgs e) { if (dgvCommandHistory.SelectedCells.Count != 1) { return; } try { MM_EMS_Command Cmd = (MM_EMS_Command)dgvCommandHistory.Rows[dgvCommandHistory.SelectedCells[0].RowIndex].DataBoundItem; dtStartTime.Value = Cmd.IssuedOn; dgvCommandHistory.Invalidate(); } catch { } }
/// <summary> /// Send a command /// </summary> /// <param name="Command"></param> /// <param name="OldValue"></param> /// <returns></returns> public bool SendCommand(string Command, String OldValue) { byte[] OutBytes = new UTF8Encoding(false).GetBytes("#CMD_TYPE,APP,FAM,CMD_NAME,DB,RECORD,FIELD,TEID,VALUE" + Environment.NewLine + Command + Environment.NewLine); DateTime SimTime = MM_Server.SimulatorTimeData.Length == 0 ? DateTime.Now : MM_Server.SimulatorTimeData[0].Simulation_Time; if (!String.IsNullOrEmpty(Settings.Default.OperatorCommandTCPAddress)) { try { using (TcpClient Client = new TcpClient()) { Client.Connect(Settings.Default.OperatorCommandTCPAddress, Settings.Default.OperatorCommandTCPPort); using (NetworkStream nS = Client.GetStream()) nS.Write(OutBytes, 0, OutBytes.Length); MM_Notification.WriteLine(ConsoleColor.White, "Sent {1} command to TEDE/TCP: by {0} on {2}. SimTime {3}", User.UserName, Command, DateTime.Now, SimTime); MM_Database_Connector.LogCommand(Command, User, "TCP"); } } catch (Exception ex) { MM_Notification.WriteLine(ConsoleColor.Red, "Error sending {1} command to TEDE/TCP: by {0} on {2} SimTime {3}: {4} ", User.UserName, Command, DateTime.Now, SimTime, ex); return(false); } } if (!String.IsNullOrEmpty(Properties.Settings.Default.OperatorCommandPath)) { String[] splStr = Command.TrimEnd(',').Split(','); String TargetFileName = Path.Combine(Properties.Settings.Default.OperatorCommandPath, ((splStr[0].Equals("REFRESH")) ? "EMS_REFRESH_" : "EMS_COMMANDS_") + RandomGenerator.Next().ToString() + ".csv"); using (FileStream fS = new FileStream(TargetFileName, FileMode.CreateNew, FileAccess.Write)) fS.Write(OutBytes, 0, OutBytes.Length); MM_Notification.WriteLine(ConsoleColor.White, "Sent {1} command to TEDE/File: by {0} on {2}. SimTime {3}", User.UserName, Command, DateTime.Now, SimTime); MM_Database_Connector.LogCommand(Command, User, "File"); } //Now, write out our command MM_EMS_Command[] OutCommands = new MM_EMS_Command[] { new MM_EMS_Command(Command, User.UserName, User.MachineName, SimTime, OldValue) }; try { List <String> OutLines = new List <string>(); if (!File.Exists("EMSCommands.csv")) { OutLines.Add(MM_EMS_Command.HeaderLine()); } OutLines.Add(OutCommands[0].BuildLine()); File.AppendAllLines("EMSCommands.csv", OutLines.ToArray()); } catch (Exception ex) { MM_Notification.WriteLine(ConsoleColor.Red, "Unable to write out command to local log: {0}", ex); } //Now, send our commands to all administrators if (Properties.Settings.Default.ForwardCommandsToAdminClients) { foreach (MM_Administrator_Types Interface in MM_Server.ConnectedAdministatorList) { try { MM_Administrator_Types.SendMessageToConsole(Interface.ConversationGuid, "AddEMSCommands", new object[] { OutCommands }); } catch (Exception ex) { MM_Notification.WriteLine(ConsoleColor.Red, "Unable to send {0} to Admin: {1}", "AddEMSCommands", ex); } } } //Now, send out our commands to all clients if (Properties.Settings.Default.ForwardCommandsToERCOTClients) { foreach (MM_WCF_Interface Interface in MM_Server.ConnectedUserList) { if (Interface.User.LoggedOnTime != default(DateTime) && Interface.User.ERCOTUser) { try { MM_WCF_Interface.SendMessage(Interface.User.UserId, "AddEMSCommands", new object[] { OutCommands }); } catch (Exception ex) { MM_Notification.WriteLine(ConsoleColor.Red, "Unable to send {0} to {1}: {2}", "AddEMSCommands", Interface.User.UserName, ex); } } } } User.LastCommand = DateTime.Now; return(true); }
/// <summary> /// Process a line /// </summary> /// <param name="CurrentLine"></param> /// <param name="DisplayCommands"></param> /// <param name="UnknownTags"></param> /// <param name="SimTime"></param> private void ProcessLine(ref string CurrentLine, List <MM_EMS_Command> DisplayCommands, List <String> UnknownTags, DateTime SimTime) { String[] splStr = CurrentLine.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries); MM_EMS_Command Command = null; try { if (splStr[0] == "DTS300") { if (splStr[4] == "CB") { foreach (MM_Breaker_Switch BreakerSwitch in MM_Repository.Substations[splStr[7]].BreakerSwitches) { if (BreakerSwitch.Name.Equals(splStr[5], StringComparison.CurrentCultureIgnoreCase)) { Command = new MM_EMS_Command() { Value = splStr[2], UserName = splStr[9], SimulatorTime = SimTime, Field = splStr[4], TEID = BreakerSwitch.TEID } } } ; } else { } } else if (splStr[0] == "DY308") { MM_Substation Site = MM_Repository.Substations[splStr[5]]; Command = new MM_EMS_Command() { SimulatorTime = SimTime, TEID = Site.TEID, UserName = String.Join(" ", splStr, 9, splStr.Length - 9) }; } else if (splStr[0] == "MSC004" || splStr[0] == "DY016") { MM_Substation Sub = MM_Repository.Substations[splStr[2]]; MM_Element FoundElem = null; //Find our element foreach (MM_Element Elem in MM_Repository.TEIDs.Values) { if (Elem.Substation == Sub && Elem.ElemType != null && Elem.ElemType.Acronym.Equals(splStr[3], StringComparison.CurrentCultureIgnoreCase)) { if (Elem.Name.Equals(splStr[4], StringComparison.CurrentCultureIgnoreCase) || (Elem is MM_Transformer && ((MM_Transformer)Elem).PrimaryWinding.Name.Equals(splStr[4], StringComparison.CurrentCultureIgnoreCase))) { FoundElem = Elem; } } } if (splStr[0] == "MSC004") { if (splStr[6] == "TOGGLED") { Command = new MM_EMS_Command() { Field = splStr[5], Value = splStr[8].TrimEnd(','), UserName = splStr[10], SimulatorTime = SimTime, TEID = FoundElem.TEID } } } ; else { Command = new MM_EMS_Command() { Field = splStr[5], Value = splStr[7].TrimEnd(','), OldValue = splStr[9], UserName = splStr[11], SimulatorTime = SimTime, TEID = FoundElem.TEID } }; else { int From = Array.IndexOf(splStr, "FROM"); Command = new MM_EMS_Command() { Field = splStr[5], Value = splStr[7].TrimEnd(','), OldValue = splStr[9], UserName = (From == -1?"?": splStr[From + 1] + splStr[From + 2]), SimulatorTime = SimTime, TEID = FoundElem.TEID }; } }