/// <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); }