public static void Save() { ConsoleHandler.AddMessage(MessageType.REGULAR, "Saving database..."); Database.Instance.Save(); PrintSuccess("Saved database"); }
public static void Execute() { while (true) { Execute(Console.ReadLine()); ConsoleHandler.ClearCommand(); Thread.Yield(); // Other threads should be able to run after each command } }
public static void Reload() { ConsoleHandler.AddMessage(MessageType.REGULAR, "Reloading database..."); Database.Instance.Load(); PrintSuccess("Database loaded"); Program.UpdatePositionInformation(); }
public static void CurrentJob() { if (BluetoothConnection.CurrentJob != null) { ConsoleHandler.AddMessage(MessageType.REGULAR, "Current job: '" + BluetoothConnection.CurrentJob.ToString() + "'"); } else { ConsoleHandler.AddMessage(MessageType.REGULAR, "No current job"); } }
public static void Payload() { Forklift f = Database.Instance.Data.Forklifts.FirstOrDefault().Value; if (f.HasPallet) { ConsoleHandler.AddMessage(MessageType.REGULAR, "Payload: '" + f.Payload.Name + "'"); } else { ConsoleHandler.AddMessage(MessageType.REGULAR, "No payload"); } }
public static void Position() { Forklift f = Database.Instance.Data.Forklifts.FirstOrDefault().Value; if (f.FrontNode == null || f.RearNode == null) { ConsoleHandler.AddMessage(MessageType.REGULAR, "Position: Unknown"); } else { ConsoleHandler.AddMessage(MessageType.REGULAR, "Front-node: " + f.FrontNode.Name); ConsoleHandler.AddMessage(MessageType.REGULAR, "Rear-node: " + f.RearNode.Name); } }
public static void Palletlist() { Dictionary <int, Pallet> pallets = Database.Instance.Data.Pallets; if (pallets.Count == 0) { ConsoleHandler.AddMessage(MessageType.REGULAR, "No pallets"); return; } foreach (KeyValuePair <int, Pallet> pallet in pallets) { ConsoleHandler.AddMessage(MessageType.REGULAR, "#" + pallet.Key + " - " + pallet.Value.ToString()); } }
public static void Unblock(String nodeOne, String nodeTwo) { Graph g = Database.Instance.Data.Graphs.FirstOrDefault().Value; try { Node n1 = g.getNode(nodeOne); Node n2 = g.getNode(nodeTwo); g.UnblockEdge(n1, n2); ConsoleHandler.AddMessage(MessageType.SUCCESS, "Unblocked edge |" + n1.Name + " " + n2.Name + "|"); } catch (NodeException e) { ConsoleHandler.AddMessage(MessageType.ERROR, e.Message); } }
public static void Joblist() { ConsoleHandler.AddMessage(MessageType.REGULAR, "Current joblist:"); List <int> IDlist = Database.Instance.Data.Jobs.Keys.ToList(); IDlist.Sort(); int c = 0; foreach (int key in IDlist) { ConsoleHandler.AddMessage(MessageType.REGULAR, Database.Instance.Data.Jobs[key].ToString()); c++; } if (c == 0) { ConsoleHandler.AddMessage(MessageType.REGULAR, "(Empty)"); } }
// Main private static void Main() { Console.Title = ConsoleHandler.DNS + " console"; Console.WindowWidth = 120; Console.WindowHeight = 60; ConsoleThread = new Thread(ConsoleHandler.PrintMessages); ConsoleThread.Start(); ConsoleHandler.AddMessage(MessageType.SUCCESS, "______ ___ _ _ _____ "); ConsoleHandler.AddMessage(MessageType.SUCCESS, "| ___ \\ / _ \\ | | | | | ___|"); ConsoleHandler.AddMessage(MessageType.SUCCESS, "| |_/ / / /_\\ \\ | | | | ______ | |__ "); ConsoleHandler.AddMessage(MessageType.SUCCESS, "| __/ | _ | | | | | |______| | __| "); ConsoleHandler.AddMessage(MessageType.SUCCESS, "| | | | | | | |____ | |____ | |___ "); ConsoleHandler.AddMessage(MessageType.SUCCESS, "\\_| \\_| |_/ \\_____/ \\_____/ \\_____|"); ConsoleHandler.AddMessage(MessageType.SUCCESS, ""); // Make sure the database is populated PopulateDatabase(); // Test and set if we know where PALL-E is UpdatePositionInformation(); // Instantiate threads ConsumeBTThread = new Thread(() => ConsumeBT(new BluetoothConnection("COM3"))); // Start the threads ConsumeBTThread.Start(); // Wait until the device has bluetooth while (!HasConnection) { ; // Busy wait } CommandThread = new Thread(Commands.Execute); CommandThread.Start(); }
// Sends a package with a given type private void SendPackageBT(byte packageType, byte[] package_data) { // Calculate the size of package_data int packageDataSize = package_data.Length; // Calculate the total package size, assuming the package data size and // start, end, and type bytes. int packageTotalSize = (packageDataSize + 3); // Check whether total data size is bigger than 256 bytes. if (packageTotalSize > 256) { ConsoleHandler.AddMessage(MessageType.ERROR, "Tried to send package with over 256 bytes (package was not sent)"); return; } // Make an array that is as large as the total data size of the package. byte[] packageToSend = new byte[packageTotalSize]; // Putting the start, end, and type byte in the array. packageToSend[0] = START_BYTE; packageToSend[1] = packageType; packageToSend[packageTotalSize - 1] = END_BYTE; // Fill the package data into the array at the correct array index for (int i = 0; i < packageDataSize; i++) { packageToSend[i + 2] = (byte)package_data[i]; } // Send the request over BT this.Write(packageToSend, 0, packageTotalSize); // Wait to ensure that continous requests is possible System.Threading.Thread.Sleep(1); return; }
// Constructor public BluetoothConnection(string portName) : base(portName) { // If the connection is not established try to connect. ConsoleHandler.AddMessage(MessageType.BLUETOOTH, "Attempting to connect to " + ConsoleHandler.DNS + "..."); while (!this.IsOpen) { try { this.Open(); // Try to open the connection Program.HasConnection = true; } catch { // Catched by retrying after sleep ConsoleHandler.AddMessage(MessageType.BLUETOOTH, "Connection failed. Trying again..."); } } // Tell the user that the connection was established ConsoleHandler.AddMessage(MessageType.BLUETOOTH, "Connection established."); this.ReadTimeout = 10000; // Wait 10 sec before timeout on readbuffer this.WriteTimeout = 10000; // Wait 10 sec before timeout on writebuffer }
public static void PrintError(string s) { ConsoleHandler.AddMessage(MessageType.ERROR, "Error: " + s); Console.ResetColor(); }
public static void CurrentJobHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"currentjob\""); }
public static void HelpHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"help\""); }
public static void PositionHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"position\""); ConsoleHandler.AddMessage(MessageType.REGULAR, "\"position\" <front node> <rear node>"); }
private static void Execute(string command) { command = command.ToLower(); // No command provided, user must need help if (String.IsNullOrEmpty(command)) { PrintInvalidCommand(); return; } // Split the string into command and arguments List <String> commandAndArguments = command.Split('-').ToList(); // No command provided, user must need help if (String.IsNullOrEmpty(commandAndArguments[0])) { PrintInvalidCommand(); return; } // Split the command at every space List <string> commandSplit = commandAndArguments[0].Split(' ').ToList(); // Remove empty commands (occours when multiple spaces are entered) commandSplit.RemoveAll(s => s == ""); // Save the first part of the command string commandIdentifier = commandSplit[0]; // Remove the first part of the command commandSplit.RemoveAt(0); int arguments = commandSplit.Count; commandSplit.RemoveAll(s => s == ""); // Check if the command argument is "clear" List <String> argumentList = commandAndArguments.Skip(1).ToList(); // Trim all command arguments for (int i = 0; i < argumentList.Count; i++) { argumentList[i] = argumentList[i].Trim(); } // Check if an argument is to clear the display if (argumentList.Contains(COMMAND_ARGUMENT_CLEAR)) { argumentList.RemoveAll(x => x == COMMAND_ARGUMENT_CLEAR); Clear(); } // Check if there are unprocessed arguments if (argumentList.Count > 0) { String error = "Unknown command arguments: "; foreach (string s in argumentList) { if (s != argumentList.FirstOrDefault()) { error += ", "; } error += "'" + s + "'"; } error += "\n"; ConsoleHandler.AddMessage(MessageType.ERROR, error); return; } // Check if the command is "help" if (commandIdentifier == HELP) { if (arguments == 0) { Help(); } else { PrintIncorrectUse(); HelpHelp(); } } else if (commandIdentifier == COMMAND_STATUS) { if (arguments == 0) { Status(); } else { PrintIncorrectUse(); StatusHelp(); } } // Check if the command is "joblist" else if (commandIdentifier == COMMAND_JOBLIST) { if (arguments == 0) { Joblist(); } else if (arguments == 1) { Joblist(commandSplit[0]); } else if (arguments == 2) { Joblist(commandSplit[0], commandSplit[1]); } else { PrintIncorrectUse(); JoblistHelp(); } } else if (commandIdentifier == COMMAND_CURRENTJOB) { if (arguments == 0) { CurrentJob(); } else { PrintIncorrectUse(); CurrentJobHelp(); } } // Check if the command is "deliver", "fetch" or "navigate" else if (commandIdentifier == COMMAND_FETCH) { if (arguments == 2) { if (commandSplit[0] == "node") { FetchNode(commandSplit[1]); } else if (commandSplit[0] == "pallet") { FetchPallet(commandSplit[1]); } else { PrintIncorrectUse(); FetchNodeHelp(); FetchPalletHelp(); } } else { PrintIncorrectUse(); FetchNodeHelp(); FetchPalletHelp(); } } else if (commandIdentifier == COMMAND_DELIVER || commandIdentifier == COMMAND_NAVIGATE) { if (arguments == 1) { DeliverNavigate(commandIdentifier, commandSplit[0]); } else { PrintIncorrectUse(); DeliverNavigateHelp(); } } else if (commandIdentifier == COMMAND_DEBUG) { if (arguments == 1) { Debug(commandSplit[0]); } } else if (commandIdentifier == COMMAND_POSITION) { if (arguments == 0) { Position(); } else if (arguments == 2) { Position(commandSplit[0], commandSplit[1]); } else { PrintIncorrectUse(); PositionHelp(); } } else if (commandIdentifier == COMMAND_CLEAR) { if (arguments == 0) { Clear(); } else { PrintIncorrectUse(); ClearHelp(); } } else if (commandIdentifier == COMMAND_PALLETLIST) { if (arguments == 0) { Palletlist(); } else if (arguments == 1) { Palletlist(commandSplit[0]); } else if (arguments == 2) { Palletlist(commandSplit[0], commandSplit[1]); } else if (arguments == 3) { Palletlist(commandSplit[0], commandSplit[1], commandSplit[2]); } else { PrintIncorrectUse(); PalletlistHelp(); } } else if (commandIdentifier == COMMAND_PAYLOAD) { if (arguments == 0) { Payload(); } else if (arguments == 1) { Payload(commandSplit[0]); } else { PrintIncorrectUse(); PayloadHelp(); } } else if (commandIdentifier == COMMAND_NODE) { if (arguments == 1) { Node(commandSplit[0]); } else { NodeHelp(); } } else if (commandIdentifier == COMMAND_UNBLOCK) { if (arguments == 2) { Unblock(commandSplit[0], commandSplit[1]); } else if (arguments == 1) { Unblock(commandSplit[0]); } else { UnblockHelp(); } } else if (commandIdentifier == COMMAND_RELOAD) { if (arguments == 0) { Reload(); } else { PrintIncorrectUse(); ReloadHelp(); } } else if (commandIdentifier == COMMAND_SAVE) { if (arguments == 0) { Save(); } else { PrintIncorrectUse(); SaveHelp(); } } else if (commandIdentifier == COMMAND_EXIT) { if (arguments == 0) { Exit(); } else { PrintIncorrectUse(); ExitHelp(); } } // Some unknown command, show the help else { PrintInvalidCommand(); } ConsoleHandler.ClearCommand(); }
public static void StatusHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"status\""); }
public static void NodeHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"node\" <node>"); }
public static void FetchPalletHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"fetch\" \"pallet\" {pallet}"); }
public static void FetchNodeHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"fetch\" \"node\" {node}"); }
public static void JoblistHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"joblist\""); ConsoleHandler.AddMessage(MessageType.REGULAR, "\"joblist\" \"clear\""); ConsoleHandler.AddMessage(MessageType.REGULAR, "\"joblist\" \"remove\" {[0-9]+}"); }
private static void PrintIncorrectUse() { ConsoleHandler.AddMessage(MessageType.REGULAR, "Incorrect use:"); }
public static void ExitHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"exit\""); }
public static void PayloadHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"payload\""); ConsoleHandler.AddMessage(MessageType.REGULAR, "\"payload\" <pallet>"); }
public static void DebugHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"debug\" {[BDLMRSTU]+}"); }
public static void Status() { Forklift f = Database.Instance.Data.Forklifts.FirstOrDefault().Value; ConsoleHandler.AddMessage(MessageType.REGULAR, "Status: " + f.Status); }
public static void PrintSuccess(string s) { ConsoleHandler.AddMessage(MessageType.SUCCESS, "Success: " + s); }
// Used to check if position information is required public static void UpdatePositionInformation() { Graph g = Database.Instance.Data.Graphs.FirstOrDefault().Value; Forklift f = Database.Instance.Data.Forklifts.FirstOrDefault().Value; if (f.RearNode == null || f.FrontNode == null) { ConsoleHandler.AddMessage(MessageType.REGULAR, "Information about PALL-E position is required."); bool result; do { Node rearNode = null; Node frontNode = null; // The user must enter a faceing node while (frontNode == null) { try { ConsoleHandler.AddMessage(MessageType.REGULAR, "Enter name of front node..."); String faceing = Console.ReadLine(); ConsoleHandler.ClearCommand(); frontNode = g.getNode(faceing); } catch (NodeException e) { ConsoleHandler.AddMessage(MessageType.ERROR, "Error when finding node: '" + e.Message + "'"); } } // The user must enter a last node while (rearNode == null) { try { ConsoleHandler.AddMessage(MessageType.REGULAR, "Enter name of rear node..."); String last = Console.ReadLine(); ConsoleHandler.ClearCommand(); rearNode = g.getNode(last); } catch (NodeException e) { ConsoleHandler.AddMessage(MessageType.ERROR, "Error when finding node: '" + e.Message + "'"); } } // Update node information result = f.UpdateNodes(frontNode, rearNode); Commands.PrintSuccess("Position updated"); Commands.Position(); }while (!result); } else { ConsoleHandler.AddMessage(MessageType.REGULAR, ConsoleHandler.DNS + " position information:"); ConsoleHandler.AddMessage(MessageType.REGULAR, "Front-node: '" + f.FrontNode.Name + "'. Rear-node: '" + f.RearNode.Name + "'"); } }
public static void DeliverNavigateHelp() { ConsoleHandler.AddMessage(MessageType.REGULAR, "\"deliver\" {node}"); ConsoleHandler.AddMessage(MessageType.REGULAR, "\"navigate\" {node}"); }