public static Command ParseCommand(string Command) { Command Cmd = new Invalid(); if (isInCorrectFormat(Command)) { Cmd = GetCommandType(Command); if (Cmd.Type == CommandType.Invalid) { return(Cmd); } else { string[] SplittedCommand = Command.Split(','); if (!String.IsNullOrEmpty(SplittedCommand[1])) { Cmd.SourceID = BitConverter.ToUInt16(Encoding.GetEncoding(437).GetBytes(SplittedCommand[1]), 0); } if (!String.IsNullOrEmpty(SplittedCommand[2])) { Cmd.DestinationID = BitConverter.ToUInt16(Encoding.GetEncoding(437).GetBytes(SplittedCommand[2]), 0); } if (!String.IsNullOrEmpty(SplittedCommand[3])) { Cmd.Action_State = Encoding.GetEncoding(437).GetBytes(SplittedCommand[3])[0]; } Cmd.UserName = SplittedCommand[4]; Cmd.Password = SplittedCommand[5]; return(Cmd); } } else if (!String.IsNullOrEmpty(Command)) //LocateMe command { string[] SplittedCommand = Command.Split(','); int NumberOfAPs = Convert.ToInt32(SplittedCommand[1]); Cmd = new User_Locate(NumberOfAPs); Cmd.SourceID = ServerTools.Tools.StringToUshort(SplittedCommand[0]); User_Locate tempCmd = (User_Locate)Cmd; for (int i = 2; i < SplittedCommand.Length - 1; i += 2) { tempCmd.ReadingsList.Add(new LocationComponents.WifiReading(SplittedCommand[i], Convert.ToDouble(SplittedCommand[i + 1]))); } return(tempCmd); } else { return(Cmd); } //TODO: add case for invalid command }
static Command GetCommandType(string Command) { foreach (var Element in Dict) { if (Element.Key.IsMatch(Command)) { return(Element.Value); } } Command InvalidCommand = new Invalid(); return(InvalidCommand); }