From e15285211aaeb196fca5934aefb66f78e3d2f3af Mon Sep 17 00:00:00 2001 From: Christian Pogea Date: Mon, 14 Oct 2024 23:19:10 +0200 Subject: [PATCH] first version using SkiaSharp --- OSMMapGenerator/OSMMapGenerator.cs | 281 ++++++++++++------ OSMMapGenerator/OSMMapGeneratorNET8.csproj | 4 +- TeslaLogger/OSMMapProvider.cs | 12 +- TeslaLogger/Program.cs | 2 +- TeslaLogger/StaticMapService.cs | 5 +- TeslaLogger/Tools.cs | 6 + .../UnitTestOSMMapGenerator.cs | 23 +- 7 files changed, 233 insertions(+), 100 deletions(-) diff --git a/OSMMapGenerator/OSMMapGenerator.cs b/OSMMapGenerator/OSMMapGenerator.cs index 9e1df2b44..91c5ef25b 100644 --- a/OSMMapGenerator/OSMMapGenerator.cs +++ b/OSMMapGenerator/OSMMapGenerator.cs @@ -1,12 +1,10 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; +using SkiaSharp; using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; -using System.Drawing; -using System.Drawing.Drawing2D; -using System.Drawing.Imaging; using System.IO; using System.Net; @@ -34,7 +32,8 @@ public enum MapIcon private static int tileSize = 256; private static string MapCachePath = string.Empty; private static Random random = new Random(); - private static Font drawFont8 = new Font(FontFamily.GenericSansSerif, 8); + private static SKFont drawFont8 = new SKFont(SKTypeface.FromFamilyName("SanSerif"), 10); + /* ccc private static SolidBrush fillBrush = new SolidBrush(Color.FromArgb(51, 51, 51)); private static SolidBrush grayBrush = new SolidBrush(Color.FromArgb(226,226,226)); private static Pen bluePen = new Pen(Color.FromArgb(88,130,249), 3); @@ -44,18 +43,32 @@ public enum MapIcon private static SolidBrush blueBrush = new SolidBrush(Color.FromArgb(62, 114, 177)); private static SolidBrush redBrush = new SolidBrush(Color.FromArgb(226,77,66)); private static SolidBrush whiteBrush = new SolidBrush(Color.White); - private static Font drawFont12b = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold); - private static Pen thinWhitePen = new Pen(Color.White, 1); + */ + static SKPaint bluePen = new SKPaint {Color = new SKColor(80,130,249), Style = SKPaintStyle.Stroke, IsAntialias = true, StrokeWidth = 3}; + static SKPaint orangeBrush = new SKPaint { Color = SKColors.Orange, Style = SKPaintStyle.Fill, IsAntialias = true, StrokeWidth = 1 }; + static SKPaint greenBrush = new SKPaint { Color = new SKColor(126, 178, 109), Style = SKPaintStyle.Fill, IsAntialias = true, StrokeWidth = 1}; + static SKPaint blueBrush = new SKPaint { Color = new SKColor(62, 114, 177), Style = SKPaintStyle.Fill, IsAntialias = true, StrokeWidth = 1 }; + static SKPaint redBrush = new SKPaint { Color = new SKColor(226, 77, 66), Style = SKPaintStyle.Fill, IsAntialias = true, StrokeWidth = 1 }; + static SKPaint thinWhitePen = new SKPaint { Color = SKColors.White, Style = SKPaintStyle.Stroke, IsAntialias = true, StrokeWidth = 1 }; + + static SKPaint drawFont12b = new SKPaint(new SKFont(SKTypeface.FromFamilyName("SanSerif"), 12)){ StrokeWidth = 1, Color = SKColors.White }; + //private static SKFont drawFont12b = new SKFont(SKTypeface.FromFamilyName("SanSerif"), 12); // ccc FontStyle.Bold + // ccc private static Pen thinWhitePen = new Pen(Color.White, 1); public static void Main(string[] args) { + // args = new string[] { "-jobfile", @"c:\temp\tiles\05f1e9ff-5b20-4548-9e75-a1b6458b7d5f", "-debug" }; + if (ParseCmdLineArgs(args)) { if (debug) { Console.WriteLine("OSMMapGenerator - Job: " + jobfile); } try { string json = File.ReadAllText(jobfile); - File.Delete(jobfile); + + if (!System.Diagnostics.Debugger.IsAttached) + File.Delete(jobfile); + dynamic jsonResult = JsonConvert.DeserializeObject(json); Dictionary job = jsonResult.ToObject>(); if (job != null) @@ -66,10 +79,16 @@ public static void Main(string[] args) tileSize = Convert.ToInt32(job["tileSize"]); double x_center = Convert.ToDouble(job["x_center"]); double y_center = Convert.ToDouble(job["y_center"]); + + if (debug) { Console.WriteLine($"width: {width} - height: {height} - zoom: {zoom} - tileSize: {tileSize}"); } + MapMode mapmode = (MapMode)Enum.ToObject(typeof(MapMode), job["mapmode"]); MapCachePath = job["MapCachePath"].ToString(); + if (System.Diagnostics.Debugger.IsAttached) + MapCachePath = "map-data"; + if (debug) { Console.WriteLine($"OSMMapGenerator - DrawMap(width:{width}, height:{height}, zoom:{zoom}, x_center:{x_center}, y_center:{y_center}, mapmode:{mapmode})"); } - Bitmap map = DrawMap(width, height, zoom, x_center, y_center, mapmode); + SKBitmap map = DrawMap(width, height, zoom, x_center, y_center, mapmode); if (job.ContainsKey("latlng")) { DataTable coords = new DataTable(); @@ -130,11 +149,26 @@ public static void Main(string[] args) } } - public static void SaveImage(Bitmap image, string filename) + public static void SaveImage(SKBitmap image, string filename) { try { - image.Save(filename); + // ccc image.Save(filename); + + if (System.Diagnostics.Debugger.IsAttached) + { + int pos = filename.IndexOf("maps"); + if ( pos > 0) + filename = filename.Substring(pos); + } + + SKImage i = SKImage.FromBitmap(image); + using (SKData d = i.Encode(SKEncodedImageFormat.Png, 90)) + using (Stream sw = File.OpenWrite(filename)) + { + d.SaveTo(sw); + } + if (debug) { Console.WriteLine("OSMMapGenerator - SaveImage: " + filename); } if (File.Exists("/usr/bin/optipng")) { @@ -181,9 +215,9 @@ private static bool ParseCmdLineArgs(string[] args) return jobfilefound; } - private static Bitmap DrawMap(int width, int height, int zoom, double x_center, double y_center, MapMode mode) + private static SKBitmap DrawMap(int width, int height, int zoom, double x_center, double y_center, MapMode mode) { - Bitmap image = new Bitmap(width, height); + SKBitmap image = new SKBitmap(width, height); { DrawMapLayer(image, width, height, x_center, y_center, zoom); if (mode == MapMode.Dark) @@ -195,7 +229,7 @@ private static Bitmap DrawMap(int width, int height, int zoom, double x_center, return image; } - private static void DrawMapLayer(Bitmap image, int width, int height, double x_center, double y_center, int zoom) + private static void DrawMapLayer(SKBitmap image, int width, int height, double x_center, double y_center, int zoom) { int x_min = (int)(Math.Floor(x_center - (0.5 * width / tileSize))); int y_min = (int)(Math.Floor(y_center - (0.5 * height / tileSize))); @@ -216,40 +250,64 @@ private static void DrawMapLayer(Bitmap image, int width, int height, double x_c } foreach (Tuple tile in tiles) { - using (Bitmap tileImage = DownloadTile(tile.Item3, tile.Item4, tile.Item5)) + using (SKBitmap tileImage = DownloadTile(tile.Item3, tile.Item4, tile.Item5)) { if (tileImage != null) { - Rectangle box = new Rectangle(XtoPx(tile.Item1, x_center, width), YtoPx(tile.Item2, y_center, height), tileSize, tileSize); - CopyRegionIntoImage(tileImage, new Rectangle(0, 0, tileSize, tileSize), image, box); + + SKRectI box = new SKRectI(XtoPx(tile.Item1, x_center, width), YtoPx(tile.Item2, y_center, height), tileSize, tileSize); + CopyRegionIntoImage(tileImage, new SKRectI(0, 0, tileSize, tileSize), image, box); } } } } - private static void CopyRegionIntoImage(Bitmap srcBitmap, Rectangle srcRegion, Bitmap destBitmap, Rectangle destRegion) + static int debugCounter = 1; + private static void CopyRegionIntoImage(SKBitmap srcBitmap, SKRectI srcRegion, SKBitmap destBitmap, SKRectI destRegion) { - int srcX = destRegion.X < 0 ? Math.Abs(destRegion.X) : 0; - int srcY = destRegion.Y < 0 ? Math.Abs(destRegion.Y) : 0; - int srcW = srcRegion.Width - Math.Abs(destRegion.X); - int srcH = srcRegion.Height - Math.Abs(destRegion.Y); - int destX = destRegion.X < 0 ? 0 : destRegion.X; - int destY = destRegion.Y < 0 ? 0 : destRegion.Y; + // https://github.com/mono/SkiaSharp/issues/1934 + + if (debug) Console.WriteLine("Step1 - src: " + srcRegion.ToString() + " - dest: " + destRegion.ToString()); + + int srcX = destRegion.Left < 0 ? Math.Abs(destRegion.Left) : 0; + int srcY = destRegion.Top < 0 ? Math.Abs(destRegion.Top) : 0; + int srcW = srcRegion.Width - Math.Abs(destRegion.Left); + int srcH = srcRegion.Height - Math.Abs(destRegion.Top); + int destX = destRegion.Left < 0 ? 0 : destRegion.Left; + int destY = destRegion.Top < 0 ? 0 : destRegion.Top; int destW = srcW; int destH = srcH; - Rectangle src = new Rectangle(srcX, srcY, srcW, srcH); - Rectangle dest = new Rectangle(destX, destY, destW, destH); + SKRectI src = new SKRectI(srcX, srcY, srcX + srcW, srcY + srcH); + SKRectI dest = new SKRectI(destX, destY, destW, destH); + + if (debug) Console.WriteLine("Step2 - src: " + src.ToString() + " - dest: " + dest.ToString()); + + //cut + SKBitmap cutSrcBitmap = new SKBitmap(src.Width, src.Height); + srcBitmap.ExtractSubset(cutSrcBitmap, src); + + // if (debug) SaveImage(cutSrcBitmap, $"maps/debug-cut-{debugCounter}.png"); + + using var canvas = new SKCanvas(destBitmap); + canvas.DrawBitmap(cutSrcBitmap, (float)destX, (float)destY); + canvas.Flush(); + + // if (debug) SaveImage(destBitmap, $"maps/debug-{debugCounter}.png"); + debugCounter++; + /* using (Graphics grD = Graphics.FromImage(destBitmap)) { grD.DrawImage(srcBitmap, dest, src, GraphicsUnit.Pixel); grD.Dispose(); - } + }*/ } + - private static void ApplyDarkMode(Bitmap image) + private static void ApplyDarkMode(SKBitmap image) { + /* ccc FilterForest(image); AdjustBrightness(image, 0.6f); InvertImage(image); @@ -258,7 +316,9 @@ private static void ApplyDarkMode(Bitmap image) AdjustSaturation(image, 0.3f); AdjustBrightness(image, 0.7f); AdjustContrast(image, 1.3f); + */ } + /* // https://github.com/JimBobSquarePants/ImageProcessor/blob/release/3.0.0/src/ImageProcessor/Processing/KnownColorMatrices.cs private static void AdjustBrightness(Image image, float amount) @@ -281,27 +341,29 @@ private static void AdjustBrightness(Image image, float amount) } } } + */ - static Color cNeutral = Color.FromArgb(224, 223, 223); + static SKColor cNeutral = new SKColor(224, 223, 223); - private static void FilterForest(Bitmap image) + /* ccc + private static void FilterForest(SKBitmap image) { for (int y = 0; (y <= (image.Height - 1)); y++) { for (int x = 0; (x <= (image.Width - 1)); x++) { - Color inv = image.GetPixel(x, y); + SKColor inv = image.GetPixel(x, y); if ( - (inv.R == 233 && inv.G == 238 && inv.B == 214) || - (inv.R == 235 && inv.G == 219 && inv.B == 232) || - (inv.R == 236 && inv.G == 236 && inv.B == 228) || - (inv.R == 237 && inv.G == 242 && inv.B == 219) || - (inv.R == 238 && inv.G == 240 && inv.B == 213) || - (inv.R == 239 && inv.G == 226 && inv.B == 237) || - (inv.R == 241 && inv.G == 243 && inv.B == 221) || - (inv.R == 242 && inv.G == 238 && inv.B == 233) || - (inv.R == 242 && inv.G == 239 && inv.B == 233) + (inv.Red == 233 && inv.Green == 238 && inv.B == 214) || + (inv.Red == 235 && inv.Green == 219 && inv.B == 232) || + (inv.Red == 236 && inv.Green == 236 && inv.B == 228) || + (inv.Red == 237 && inv.Green == 242 && inv.B == 219) || + (inv.Red == 238 && inv.Green == 240 && inv.B == 213) || + (inv.Red == 239 && inv.Green == 226 && inv.B == 237) || + (inv.Red == 241 && inv.Green == 243 && inv.B == 221) || + (inv.Red == 242 && inv.Green == 238 && inv.B == 233) || + (inv.Red == 242 && inv.Green == 239 && inv.B == 233) ) image.SetPixel(x, y, cNeutral); else if ( // Dreiecke @@ -358,19 +420,22 @@ private static void FilterForest(Bitmap image) } } } + */ // https://mariusbancila.ro/blog/2009/11/13/using-colormatrix-for-creating-negative-image/ - private static void InvertImage(Bitmap image) + private static void InvertImage(SKBitmap image) { + /* ccc for (int y = 0; (y <= (image.Height - 1)); y++) { for (int x = 0; (x <= (image.Width - 1)); x++) { - Color inv = image.GetPixel(x, y); - inv = Color.FromArgb(255, (255 - inv.R), (255 - inv.G), (255 - inv.B)); + SKColor inv = image.GetPixel(x, y); + inv = SKColor.FromArgb(255, (255 - inv.Red), (255 - inv.Green), (255 - inv.Blue)); image.SetPixel(x, y, inv); } } + */ /* @@ -393,8 +458,9 @@ private static void InvertImage(Bitmap image) } // https://github.com/JimBobSquarePants/ImageProcessor/blob/release/3.0.0/src/ImageProcessor/Processing/KnownColorMatrices.cs - private static void AdjustContrast(Bitmap image, float amount) + private static void AdjustContrast(SKBitmap image, float amount) { + /* ccc using (ImageAttributes ia = new ImageAttributes()) { //create the scaling matrix @@ -417,11 +483,13 @@ private static void AdjustContrast(Bitmap image, float amount) g.Dispose(); } } + */ } // https://github.com/JimBobSquarePants/ImageProcessor/blob/release/3.0.0/src/ImageProcessor/Processing/KnownColorMatrices.cs - private static void AdjustSaturation(Bitmap image, float amount) + private static void AdjustSaturation(SKBitmap image, float amount) { + /* ccc ColorMatrix m = new ColorMatrix(); m.Matrix00 = .213F + (.787F * amount); m.Matrix10 = .715F - (.715F * amount); @@ -445,12 +513,14 @@ private static void AdjustSaturation(Bitmap image, float amount) g.Dispose(); } } + */ } // https://github.com/JimBobSquarePants/ImageProcessor/blob/release/3.0.0/src/ImageProcessor/Processing/KnownColorMatrices.cs public static float DegreeToRadian(float degree) => degree * (float)(Math.PI / 180F); - private static void HueRotate(Bitmap image, float degrees) + private static void HueRotate(SKBitmap image, float degrees) { + /* ccc degrees %= 360; while (degrees < 0) { @@ -484,11 +554,14 @@ private static void HueRotate(Bitmap image, float degrees) g.Dispose(); } } + */ } - private static Bitmap DownloadTile(int zoom, int tile_x, int tile_y) + private static SKBitmap DownloadTile(int zoom, int tile_x, int tile_y) { string localMapCacheFilePath = Path.Combine(MapCachePath, $"{zoom}_{tile_x}_{tile_y}.png"); + if (debug) Console.WriteLine("localMapCacheFilePath:" + localMapCacheFilePath); + if (MapFileExistsOrIsTooOld(localMapCacheFilePath, 8)) { // cached file too old or does not exist yet @@ -499,6 +572,7 @@ private static Bitmap DownloadTile(int zoom, int tile_x, int tile_y) int num = random.Next(0, 3); char abc = (char)('a' + num); Uri url = new Uri($"http://{abc}.tile.osm.org/{zoom}/{tile_x}/{tile_y}.png"); + if (debug) Console.WriteLine("Download:" + url); try { using (WebClient wc = new WebClient()) @@ -515,14 +589,14 @@ private static Bitmap DownloadTile(int zoom, int tile_x, int tile_y) } try { - using (Image img = Image.FromFile(localMapCacheFilePath)) + using (SKImage img = SKImage.FromEncodedData(localMapCacheFilePath)) { - return new Bitmap(img); + return SKBitmap.FromImage(img); } } catch (Exception) { - return new Bitmap(tileSize, tileSize); + return new SKBitmap(tileSize, tileSize); } } @@ -562,8 +636,9 @@ private static int XtoPx(double x, double x_center, int width) return (int)(Math.Round(px)); } - private static void DrawAttribution(Bitmap image) + private static void DrawAttribution(SKBitmap image) { + /* ccc using (Graphics g = Graphics.FromImage(image)) { g.SmoothingMode = SmoothingMode.AntiAlias; @@ -573,15 +648,18 @@ private static void DrawAttribution(Bitmap image) g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g.DrawString(attribution, drawFont8, grayBrush, image.Width - size.Width - 2, image.Height - size.Height - 2); } + */ } - private static void DrawTrip(Bitmap image, DataTable coords, int zoom, double x_center, double y_center) + private static void DrawTrip(SKBitmap image, DataTable coords, int zoom, double x_center, double y_center) { - using (Graphics graphics = Graphics.FromImage(image)) + // ccc using (Graphics graphics = Graphics.FromImage(image)) + + using var canvas = new SKCanvas(image); { - graphics.SmoothingMode = SmoothingMode.AntiAlias; + // ccc graphics.SmoothingMode = SmoothingMode.AntiAlias; // draw Trip line - /* + for (int index = 1; index < coords.Rows.Count; index++) { int x1 = XtoPx(LngToTileX(Convert.ToDouble(coords.Rows[index - 1]["lng"]), zoom), x_center, image.Width); @@ -590,22 +668,11 @@ private static void DrawTrip(Bitmap image, DataTable coords, int zoom, double x_ int y2 = YtoPx(LatToTileY(Convert.ToDouble(coords.Rows[index]["lat"]), zoom), y_center, image.Height); if (x1 != x2 || y1 != y2) { - graphics.DrawLine(whitePen, x1, y1, x2, y2); - } - } - */ - for (int index = 1; index < coords.Rows.Count; index++) - { - int x1 = XtoPx(LngToTileX(Convert.ToDouble(coords.Rows[index - 1]["lng"]), zoom), x_center, image.Width); - int y1 = YtoPx(LatToTileY(Convert.ToDouble(coords.Rows[index - 1]["lat"]), zoom), y_center, image.Height); - int x2 = XtoPx(LngToTileX(Convert.ToDouble(coords.Rows[index]["lng"]), zoom), x_center, image.Width); - int y2 = YtoPx(LatToTileY(Convert.ToDouble(coords.Rows[index]["lat"]), zoom), y_center, image.Height); - if (x1 != x2 || y1 != y2) - { - graphics.DrawLine(bluePen, x1, y1, x2, y2); + canvas.DrawLine(x1, y1, x2, y2, bluePen); } } } + canvas.Flush(); } // transform longitude to tile number @@ -620,8 +687,9 @@ private static double LatToTileY(double lat, int zoom) return (1.0 - Math.Log(Math.Tan(lat * Math.PI / 180.0) + 1 / Math.Cos(lat * Math.PI / 180.0)) / Math.PI) / 2.0 * Math.Pow(2.0, zoom); } - private static void DrawIcon(Bitmap image, double lat, double lng, MapIcon icon, int zoom, double x_center, double y_center) + private static void DrawIcon(SKBitmap image, double lat, double lng, MapIcon icon, int zoom, double x_center, double y_center) { + int scale = 1; switch (icon) { @@ -632,42 +700,79 @@ private static void DrawIcon(Bitmap image, double lat, double lng, MapIcon icon, } int x = XtoPx(LngToTileX(lng, zoom), x_center, image.Width); int y = YtoPx(LatToTileY(lat, zoom), y_center, image.Height); - Rectangle rect = new Rectangle(x - 4 * scale, y - 10 * scale, 8 * scale, 8 * scale); - Point[] triangle = new Point[] { new Point(x - 4 * scale, y - 6 * scale), new Point(x, y), new Point(x + 4 * scale, y - 6 * scale) }; - using (Graphics g = Graphics.FromImage(image)) + int left = x - 4 * scale; + int top = y - 10 * scale; + SKRectI rect = new SKRectI(left, top, left + 8 * scale, top + 8 * scale); + SKPoint[] triangle = new SKPoint[] { new SKPoint(x - 4 * scale, y - 6 * scale), new SKPoint(x, y), new SKPoint(x + 4 * scale, y - 6 * scale) }; + //using (Graphics g = Graphics.FromImage(image)) + using var g = new SKCanvas(image); { - g.SmoothingMode = SmoothingMode.AntiAlias; - g.PixelOffsetMode = PixelOffsetMode.Half; + // ccc g.SmoothingMode = SmoothingMode.AntiAlias; + // ccc g.PixelOffsetMode = PixelOffsetMode.Half; + switch (icon) { case MapIcon.Start: - g.FillPie(greenBrush, rect, 180, 180); - g.FillPolygon(greenBrush, triangle); + using (SKPath path = new SKPath()) + { + path.ArcTo(rect, -180, 180, false); + path.AddPoly(triangle); + g.DrawPath(path, greenBrush); + } + + //g.FillPie(greenBrush, rect, 180, 180); + //g.FillPolygon(greenBrush, triangle); break; case MapIcon.End: - g.FillPie(redBrush, rect, 180, 180); - g.FillPolygon(redBrush, triangle); + using (SKPath path = new SKPath()) + { + path.ArcTo(rect, -180, 180, false); + path.AddPoly(triangle); + g.DrawPath(path, redBrush); + } + + //g.FillPie(redBrush, rect, 180, 180); + //g.FillPolygon(redBrush, triangle); break; case MapIcon.Park: - g.FillPie(blueBrush, rect, 180, 180); - g.FillPolygon(blueBrush, triangle); + using (SKPath path = new SKPath()) + { + path.ArcTo(rect, -180, 180, false); + path.AddPoly(triangle); + g.DrawPath(path, blueBrush); + } + + //g.FillPie(blueBrush, rect, 180, 180); + //g.FillPolygon(blueBrush, triangle); break; case MapIcon.Charge: - g.FillPie(orangeBrush, rect, 180, 180); - g.FillPolygon(orangeBrush, triangle); + using (SKPath path = new SKPath()) + { + path.ArcTo(rect, -180, 180, false); + path.AddPoly(triangle); + g.DrawPath(path, orangeBrush); + } + + //g.FillPie(orangeBrush, rect, 180, 180); + //g.FillPolygon(orangeBrush, triangle); break; - } - g.DrawArc(thinWhitePen, rect, 180, 180); - g.DrawLine(thinWhitePen, triangle[0], triangle[1]); - g.DrawLine(thinWhitePen, triangle[1], triangle[2]); + } + + g.DrawArc(rect, 180, 180, false, thinWhitePen); + g.DrawLine( triangle[0], triangle[1], thinWhitePen); + g.DrawLine( triangle[1], triangle[2], thinWhitePen); if (icon == MapIcon.Park || icon == MapIcon.Charge) { + string text = icon == MapIcon.Park ? "P" : "\u26A1"; - SizeF size = g.MeasureString(text, drawFont12b); - g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; - g.DrawString(text, drawFont12b, whiteBrush, x - size.Width / 2, y - 6 * scale - size.Height / 2); + float size = drawFont12b.MeasureText(text); + // ccc g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; + //g.DrawText(text, x - size / 2, y - 6 * scale - size / 2, drawFont12b); + g.DrawText(text, x - size / 2, y - scale - rect.Height / 2 , drawFont12b); g.Dispose(); } + + // g.Flush(); } } } diff --git a/OSMMapGenerator/OSMMapGeneratorNET8.csproj b/OSMMapGenerator/OSMMapGeneratorNET8.csproj index 24d106b74..d67fc80d1 100644 --- a/OSMMapGenerator/OSMMapGeneratorNET8.csproj +++ b/OSMMapGenerator/OSMMapGeneratorNET8.csproj @@ -6,6 +6,7 @@ OSMMapGenerator %24{AuthorCopyright} 1.1.0 + TeslaLogger.OSMMapGenerator bin\Debug @@ -19,6 +20,7 @@ - + + \ No newline at end of file diff --git a/TeslaLogger/OSMMapProvider.cs b/TeslaLogger/OSMMapProvider.cs index ed11c5ce7..91378cb97 100644 --- a/TeslaLogger/OSMMapProvider.cs +++ b/TeslaLogger/OSMMapProvider.cs @@ -53,7 +53,9 @@ public override void CreateTripMap(DataTable coords, int width, int height, MapM File.WriteAllText(tempfile, JsonConvert.SerializeObject(job), Encoding.UTF8); GetOSMMapGeneratorFilename(out string fileName, out string arguments); - arguments += "-jobfile " + tempfile + (Program.VERBOSE ? " - debug" : ""); + arguments += "-jobfile " + tempfile + (Program.VERBOSE ? " -debug" : ""); + + Logfile.Log("cmd: " + fileName + " " + arguments); using (Process process = new Process { @@ -254,14 +256,18 @@ private static void GetOSMMapGeneratorFilename(out string fileName, out string a else { fileName = "/home/cli/dotnet"; - arguments = "/etc/teslalogger/OSMMapGenerator.exe "; + if (!File.Exists(fileName)) + fileName = "dotnet"; + arguments = "OSMMapGeneratorNET8.dll "; + + /* if (!Tools.IsMono()) { var f = new FileInfo("OSMMapGeneratorNET8.dll"); fileName = f.FullName; arguments = ""; - } + }*/ } } diff --git a/TeslaLogger/Program.cs b/TeslaLogger/Program.cs index 0b9fb9bbc..390ab4979 100644 --- a/TeslaLogger/Program.cs +++ b/TeslaLogger/Program.cs @@ -352,7 +352,7 @@ private static void InitStage2() try { - if (Environment.Version?.ToString()?.StartsWith("8.0") == true) + if (Tools.IsDotnet8()) { ExceptionlessClient.Default.Configuration.DefaultData.Add("dotnet", Environment.Version?.ToString()); ExceptionlessClient.Default.CreateFeatureUsage("USE_DOTNET8").FirstCarUserID().AddObject(Environment.Version.ToString(), "DOTNET8").Submit(); diff --git a/TeslaLogger/StaticMapService.cs b/TeslaLogger/StaticMapService.cs index 42d8e6ac0..7330dc35e 100644 --- a/TeslaLogger/StaticMapService.cs +++ b/TeslaLogger/StaticMapService.cs @@ -142,13 +142,14 @@ private void Work() int queueLength = queue.Count; if (queue.TryDequeue(out Request request)) { - //Tools.DebugLog("StaticMapService:Work() queue:" + queue.Count + " MapProvider:" + _StaticMapProvider); + Tools.DebugLog("StaticMapService:Work() queue:" + queue.Count + " MapProvider:" + _StaticMapProvider); int width = request.Width > 0 ? request.Width : 200; int height = request.Height > 0 ? request.Height : 150; if (request is TripRequest) { //Tools.DebugLog($"StaticMapService:Work() request:{request.Type} {((TripRequest)request).StartPosID}->{((TripRequest)request).EndPosID}"); string filename = System.IO.Path.Combine(GetMapDir(), GetMapFileName(((TripRequest)request).CarID, ((TripRequest)request).StartPosID, ((TripRequest)request).EndPosID)); + Tools.DebugLog("StaticMapService:filename = " + filename); if (MapFileExistsOrIsTooOld(filename)) { using (DataTable dt = TripToCoords((TripRequest)request)) @@ -384,7 +385,7 @@ public static string GetMapDir() { string mapdir = "/var/lib/grafana/plugins/teslalogger-timeline-panel/dist/maps"; - if (!Tools.IsMono()) + if (!Tools.IsMono() && !Tools.IsDotnet8()) mapdir = new DirectoryInfo("maps").FullName; try diff --git a/TeslaLogger/Tools.cs b/TeslaLogger/Tools.cs index a748e9491..4655b2177 100644 --- a/TeslaLogger/Tools.cs +++ b/TeslaLogger/Tools.cs @@ -53,6 +53,7 @@ internal static bool UseNearbySuCService() public enum UpdateType { all, stable, none }; internal static ConcurrentQueue> debugBuffer = new ConcurrentQueue>(); + internal static bool dotnet8; public static void SetThreadEnUS() { @@ -1234,6 +1235,11 @@ public static bool IsUnitTest() return false; } + public static bool IsDotnet8() + { + return Environment.Version?.ToString()?.StartsWith("8.0") == true; + } + public static bool IsDockerNET8() { try diff --git a/UnitTestsTeslalogger/UnitTestOSMMapGenerator.cs b/UnitTestsTeslalogger/UnitTestOSMMapGenerator.cs index 0816b49d0..a76acdd49 100644 --- a/UnitTestsTeslalogger/UnitTestOSMMapGenerator.cs +++ b/UnitTestsTeslalogger/UnitTestOSMMapGenerator.cs @@ -12,10 +12,23 @@ namespace UnitTestsTeslalogger [TestClass] public class UnitTestOSMMapGenerator { + [TestInitialize] + public void InitializeTests() + { + if (!Directory.Exists("maps")) + Directory.CreateDirectory("maps"); + else + { + var files = Directory.GetFiles("maps"); + foreach (var file in files) + File.Delete(file); + } + } + [TestMethod] public void ParkingP1() { - var f = new FileInfo("../../Testfile-P1.txt"); + var f = new FileInfo("../../../Testfile-P1.txt"); string tempfile = GetTempFileName(); f.CopyTo(tempfile,true); @@ -35,7 +48,7 @@ public void ParkingP1() [TestMethod] public void ParkingP2() { - var f = new FileInfo("../../Testfile-P2.txt"); + var f = new FileInfo("../../../Testfile-P2.txt"); string tempfile = GetTempFileName(); f.CopyTo(tempfile, true); @@ -52,7 +65,7 @@ public void ParkingP2() [TestMethod] public void ParkingP3() { - var f = new FileInfo("../../Testfile-P3.txt"); + var f = new FileInfo("../../../Testfile-P3.txt"); string tempfile = GetTempFileName(); f.CopyTo(tempfile, true); @@ -69,7 +82,7 @@ public void ParkingP3() [TestMethod] public void ParkingP4() { - var f = new FileInfo("../../Testfile-P4.txt"); + var f = new FileInfo("../../../Testfile-P4.txt"); string tempfile = GetTempFileName(); f.CopyTo(tempfile, true); @@ -86,7 +99,7 @@ public void ParkingP4() [TestMethod] public void TripT1() { - var f = new FileInfo("../../Testfile-T1.txt"); + var f = new FileInfo("../../../Testfile-T1.txt"); string tempfile = GetTempFileName(); f.CopyTo(tempfile, true);