From 8e6738746f4859be0de9d97c913c3cecff61485f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E6=8C=AF=E5=9A=B4?= Date: Wed, 7 Nov 2018 01:02:25 +0800 Subject: [PATCH] Prototype --- Converter.cs | 84 ++++++++++++++++++++++++++++++ Program.cs | 93 +++++++++++++++++++++++++++++++++- Properties/launchSettings.json | 8 +++ 3 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 Converter.cs create mode 100644 Properties/launchSettings.json diff --git a/Converter.cs b/Converter.cs new file mode 100644 index 0000000..821b66d --- /dev/null +++ b/Converter.cs @@ -0,0 +1,84 @@ +using System; + +namespace CoordinateUUIDConverter +{ + public static class Converter + { + public static string ToUUID(this Coordinate coordinate) + { + byte[] lonBytes = BitConverter.GetBytes(coordinate.Longitude); + byte[] latBytes = BitConverter.GetBytes(coordinate.Latitude); + byte[] floorByte = BitConverter.GetBytes(coordinate.Floor); + + string[] buffer = new string[4]; + + for (int i = 0; i < 2; i++) + { + string lonHex = Convert.ToString(lonBytes[i * 2], 16).PadLeft(2, '0') + Convert.ToString(lonBytes[i * 2 + 1], 16).PadLeft(2, '0'); + string latHex = Convert.ToString(latBytes[i * 2], 16).PadLeft(2, '0') + Convert.ToString(latBytes[i * 2 + 1], 16).PadLeft(2, '0'); + buffer[i + 2] = lonHex; + buffer[i] = latHex; + } + + string floorHex = Convert.ToString(floorByte[0], 16).PadLeft(2,'0') + + Convert.ToString(floorByte[1], 16).PadLeft(2, '0') + + Convert.ToString(floorByte[2], 16).PadLeft(2, '0') + + Convert.ToString(floorByte[3], 16).PadLeft(2, '0'); + + return floorHex + "-0000-" + buffer[0] + "-" + buffer[1] + "-0000" + buffer[2] + buffer[3]; + } + + public static Coordinate ToCoordinate(this Guid Id) + { + string[] idShards = Id.ToString().Split('-'); + string latHexStr = idShards[2] + idShards[3]; + string lonHexStr = idShards[4].Substring(4, 8); + string floorHexStr = idShards[0]; + + return new Coordinate ( + HexToFloat(latHexStr), + HexToFloat(lonHexStr), + HexToFloat(floorHexStr) + ); + } + + /// + /// Convert hex string content to a float. + /// 0xff20f342 -> 121.564445f + /// + /// + /// + private static float HexToFloat(string Hex) + { + // Hex string content to a byte array. + byte[] Bytes = new byte[4]; + Bytes[0] = Convert.ToByte(Hex.Substring(0, 2), 16); + Bytes[1] = Convert.ToByte(Hex.Substring(2, 2), 16); + Bytes[2] = Convert.ToByte(Hex.Substring(4, 2), 16); + Bytes[3] = Convert.ToByte(Hex.Substring(6, 2), 16); + + // byte array to a float. + return BitConverter.ToSingle(Bytes, 0); + } + } + + public class Coordinate + { + public float Longitude { get; } + public float Latitude { get; } + public float Floor { get; } + + public Coordinate(float latitude, float longitude, float floor) + { + if (latitude < 0 || latitude > 90) + throw new Exception("0 <= Latitude >= 90"); + + if (longitude < -180 || longitude > 180) + throw new Exception("-180 <= Longitude >= 180"); + + Longitude = longitude; + Latitude = latitude; + Floor = floor; + } + } +} diff --git a/Program.cs b/Program.cs index aa8792d..635b3ec 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Globalization; namespace CoordinateUUIDConverter { @@ -6,7 +8,96 @@ class Program { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + if(args.Length < 2) + { + var consoleColor = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Wrong number of parameters"); + Console.ForegroundColor = consoleColor; + Environment.Exit( 0 ); + } + + if (args[0].ToLower() != "-touuid" && args[0].ToLower() != "-tocoordinate") + { + var consoleColor = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Wrong of parameters"); + Console.ForegroundColor = consoleColor; + Environment.Exit( 0 ); + } + + if (args[0].ToLower() == "-touuid") + { + List coordinates = new List(); + for (int i = 1; i < args.Length; i++) + try + { + string[] buffer = args[i].Split(','); + + if (buffer.Length == 2) + coordinates.Add(new Coordinate( + float.Parse(buffer[0], CultureInfo.InvariantCulture.NumberFormat), + float.Parse(buffer[1], CultureInfo.InvariantCulture.NumberFormat), + 1)); + else + coordinates.Add(new Coordinate ( + float.Parse(buffer[0], CultureInfo.InvariantCulture.NumberFormat), + float.Parse(buffer[1], CultureInfo.InvariantCulture.NumberFormat), + float.Parse(buffer[2], CultureInfo.InvariantCulture.NumberFormat) + )); + } + catch(Exception ex) + { + var consoleColor = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("The {0} coordinate format is incorrect. {1}", i, ex.Message); + Console.ForegroundColor = consoleColor; + } + + ToUUIDProcess(coordinates); + } + + if (args[0].ToLower() == "-tocoordinate") + { + List ids = new List(); + for (int i = 1; i < args.Length; i++) + try + { + ids.Add(Guid.Parse(args[i])); + } + catch + { + var consoleColor = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("UUID format is incorrect. {0}" ,args[i]); + Console.ForegroundColor = consoleColor; + } + + ToCoordinateProcess(ids); + } + } + + private static void ToUUIDProcess(List coordinates) + { + foreach(Coordinate coordinate in coordinates) + Console.WriteLine("({0},{1},{2}): {3}", + coordinate.Latitude, + coordinate.Longitude, + coordinate.Floor, + coordinate.ToUUID()); + } + + private static void ToCoordinateProcess(List ids) + { + foreach (Guid id in ids) + { + Coordinate coordinate = id.ToCoordinate(); + Console.WriteLine("UUID: {0}, ({1},{2},{3})", + id.ToString(), + coordinate.Latitude, + coordinate.Longitude, + coordinate.Floor); + } } } } diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..74dadee --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "CoordinateUUIDConverter": { + "commandName": "Project", + "commandLineArgs": "-touuid 25.15502,121.68507,2" + } + } +} \ No newline at end of file